Commit inicial powerservice
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
/**
|
||||
* Audit result value object for the rank-math/audit-site-seo ability.
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Site SEO audit result, agent-consumable shape.
|
||||
*/
|
||||
class Audit_Result {
|
||||
|
||||
/**
|
||||
* Analyzed URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $url;
|
||||
|
||||
/**
|
||||
* Score 0–100.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $score;
|
||||
|
||||
/**
|
||||
* Grade: good|average|bad.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $grade;
|
||||
|
||||
/**
|
||||
* Outcome counts: ok, fail, warning, info.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $statuses;
|
||||
|
||||
/**
|
||||
* Total tests counted.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $total_tests;
|
||||
|
||||
/**
|
||||
* Unix timestamp of last run, 0 if never.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $last_run_at;
|
||||
|
||||
/**
|
||||
* Remote API status: ok|unavailable|skipped.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $remote_api_status;
|
||||
|
||||
/**
|
||||
* Per-test findings.
|
||||
*
|
||||
* @var Finding[]
|
||||
*/
|
||||
public $findings;
|
||||
|
||||
/**
|
||||
* Error details when the audit could not run, or null on success.
|
||||
* Shape: { code: string, message: string }
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
public $error;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $args See properties.
|
||||
*/
|
||||
public function __construct( array $args ) {
|
||||
foreach ( $args as $key => $value ) {
|
||||
if ( property_exists( $this, $key ) ) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize to array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() {
|
||||
return [
|
||||
'url' => $this->url,
|
||||
'score' => (int) $this->score,
|
||||
'grade' => $this->grade,
|
||||
'statuses' => (array) $this->statuses,
|
||||
'total_tests' => (int) $this->total_tests,
|
||||
'last_run_at' => (int) $this->last_run_at,
|
||||
'remote_api_status' => $this->remote_api_status,
|
||||
'findings' => array_map(
|
||||
function ( Finding $f ) {
|
||||
return $f->to_array();
|
||||
},
|
||||
(array) $this->findings
|
||||
),
|
||||
'error' => $this->error,
|
||||
];
|
||||
}
|
||||
}
|
||||
+226
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* Audit runner service for the rank-math/audit-site-seo ability.
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
use RankMath\KB;
|
||||
use RankMath\Helpers\Url;
|
||||
use RankMath\SEO_Analysis\SEO_Analyzer;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Runs the Rank Math site SEO audit and shapes the result for ability/MCP consumers.
|
||||
*/
|
||||
class Audit_Runner {
|
||||
|
||||
/**
|
||||
* SEO Analyzer instance.
|
||||
*
|
||||
* @var SEO_Analyzer
|
||||
*/
|
||||
private $analyzer;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param SEO_Analyzer|null $analyzer SEO Analyzer instance.
|
||||
*/
|
||||
public function __construct( ?SEO_Analyzer $analyzer = null ) {
|
||||
$this->analyzer = $analyzer ?? new SEO_Analyzer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the audit and return a structured result.
|
||||
*
|
||||
* External URL auditing requires Rank Math PRO. FREE installs receive
|
||||
* an Audit_Result with error.code 'pro_required' instead of findings.
|
||||
*
|
||||
* @param bool $refresh Re-run tests instead of returning cached results.
|
||||
* @param string|null $url Optional URL to audit. Defaults to this site's home URL.
|
||||
* @return Audit_Result
|
||||
*/
|
||||
public function run( $refresh = true, $url = null ) {
|
||||
if ( ! empty( $url ) ) {
|
||||
$clean = wp_http_validate_url( $url );
|
||||
if ( false === $clean ) {
|
||||
return $this->error_result(
|
||||
'invalid_url',
|
||||
__( 'The provided URL is not valid.', 'seo-by-rank-math' )
|
||||
);
|
||||
}
|
||||
|
||||
if ( Url::is_external( $clean ) && ! apply_filters( 'rank_math/analysis/is_allowed_url', false, $clean ) ) {
|
||||
return $this->error_result(
|
||||
'pro_required',
|
||||
sprintf(
|
||||
/* translators: %s: URL to the Rank Math PRO upgrade page */
|
||||
__( 'Competitor URL analysis requires Rank Math PRO. Upgrade at %s', 'seo-by-rank-math' ),
|
||||
KB::get( 'pro', 'Audit Site SEO Ability' )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// The tests file is normally loaded only in the admin context. Include it here
|
||||
// so local tests are available when the ability is invoked via REST/MCP.
|
||||
$tests_file = RANK_MATH_PATH . 'includes/modules/seo-analysis/seo-analysis-tests.php';
|
||||
if ( file_exists( $tests_file ) ) {
|
||||
include_once $tests_file;
|
||||
}
|
||||
|
||||
if ( ! empty( $url ) ) {
|
||||
$this->analyzer->analyse_url = esc_url_raw( $url );
|
||||
$this->analyzer->analyse_subpage = true;
|
||||
}
|
||||
|
||||
$this->analyzer->set_url();
|
||||
|
||||
if ( $refresh || ! $this->has_cached_results() ) {
|
||||
$this->analyzer->run_tests( $refresh );
|
||||
} else {
|
||||
$this->analyzer->get_results_from_storage();
|
||||
}
|
||||
|
||||
return $this->shape( $this->analyzer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a minimal Audit_Result carrying only an error (no findings).
|
||||
*
|
||||
* @param string $code Machine-readable error code.
|
||||
* @param string $message Human-readable message for the AI/user.
|
||||
* @return Audit_Result
|
||||
*/
|
||||
private function error_result( $code, $message ) {
|
||||
return new Audit_Result(
|
||||
[
|
||||
'url' => '',
|
||||
'score' => 0,
|
||||
'grade' => 'bad',
|
||||
'statuses' => [
|
||||
'ok' => 0,
|
||||
'fail' => 0,
|
||||
'warning' => 0,
|
||||
'info' => 0,
|
||||
],
|
||||
'total_tests' => 0,
|
||||
'last_run_at' => 0,
|
||||
'remote_api_status' => 'skipped',
|
||||
'findings' => [],
|
||||
'error' => [
|
||||
'code' => $code,
|
||||
'message' => $message,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a cached audit result exists.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function has_cached_results() {
|
||||
return (bool) get_option( 'rank_math_seo_analysis_results' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an Audit_Result from a populated SEO_Analyzer.
|
||||
*
|
||||
* @param SEO_Analyzer $analyzer Populated analyzer instance.
|
||||
* @return Audit_Result
|
||||
*/
|
||||
private function shape( SEO_Analyzer $analyzer ) {
|
||||
$findings = [];
|
||||
$statuses = [
|
||||
'ok' => 0,
|
||||
'fail' => 0,
|
||||
'warning' => 0,
|
||||
'info' => 0,
|
||||
];
|
||||
$total = 0;
|
||||
$score_sum = 0;
|
||||
$score_pass = 0;
|
||||
|
||||
if ( is_array( $analyzer->results ) ) {
|
||||
foreach ( $analyzer->results as $id => $result ) {
|
||||
if ( ! is_object( $result ) || $result->is_hidden() || $result->is_excluded() ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data = $result->get_result();
|
||||
$status = $result->get_status();
|
||||
$score = (int) $result->get_score();
|
||||
|
||||
$statuses[ $status ] = isset( $statuses[ $status ] ) ? $statuses[ $status ] + 1 : 1;
|
||||
++$total;
|
||||
$score_sum += $score;
|
||||
if ( 'ok' === $status ) {
|
||||
$score_pass += $score;
|
||||
}
|
||||
|
||||
// Prefer the per-run `message` (contains actual counts/links) over the
|
||||
// static `description` (generic text defined in the test registration).
|
||||
$description = isset( $data['message'] ) ? $data['message'] : ( isset( $data['description'] ) ? $data['description'] : '' );
|
||||
|
||||
$fix_html = isset( $data['fix'] ) ? $data['fix'] : '';
|
||||
|
||||
$findings[] = new Finding(
|
||||
[
|
||||
'test_id' => $id,
|
||||
'category' => $result->get_category(),
|
||||
'status' => $status,
|
||||
'score' => $score,
|
||||
'title' => isset( $data['title'] ) ? $data['title'] : '',
|
||||
'description' => wp_strip_all_tags( $description ),
|
||||
'fix_text' => html_entity_decode( wp_strip_all_tags( $fix_html ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ),
|
||||
'fix_html' => $fix_html,
|
||||
'fix_hint' => Fix_Hint_Map::get( $id ),
|
||||
'kb_link' => isset( $data['kb_link'] ) ? $data['kb_link'] : '',
|
||||
'data' => isset( $data['data'] ) ? $data['data'] : null,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$percent = $score_sum > 0 ? (int) round( ( $score_pass / $score_sum ) * 100 ) : 0;
|
||||
|
||||
return new Audit_Result(
|
||||
[
|
||||
'url' => $analyzer->analyse_url,
|
||||
'score' => $percent,
|
||||
'grade' => $this->grade_for( $percent ),
|
||||
'statuses' => $statuses,
|
||||
'total_tests' => $total,
|
||||
'last_run_at' => (int) get_option( 'rank_math_seo_analysis_date', 0 ),
|
||||
'remote_api_status' => $analyzer->remote_api_succeeded() ? 'ok' : 'unavailable',
|
||||
'findings' => $findings,
|
||||
'error' => null,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Score → grade bucket.
|
||||
*
|
||||
* @param int $percent Score percentage 0–100.
|
||||
* @return string good|average|bad
|
||||
*/
|
||||
private function grade_for( $percent ) {
|
||||
if ( $percent < 50 ) {
|
||||
return 'bad';
|
||||
}
|
||||
if ( $percent < 70 ) {
|
||||
return 'average';
|
||||
}
|
||||
return 'good';
|
||||
}
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/audit-site-seo
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/audit-site-seo ability.
|
||||
*/
|
||||
class Audit_Site_SEO implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Audit runner instance.
|
||||
*
|
||||
* @var Audit_Runner
|
||||
*/
|
||||
private $runner;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
* @param Audit_Runner|null $runner Audit runner instance.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta, ?Audit_Runner $runner = null ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
$this->runner = $runner ?? new Audit_Runner();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/audit-site-seo',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Audit site SEO', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
"Runs Rank Math's site-wide SEO audit and returns a structured score plus per-test findings with fix hints. Combines local checks with the remote rankmath.com API tests.",
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'refresh' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Re-run the audit. Defaults to true (always fresh when called by an AI host).', 'seo-by-rank-math' ),
|
||||
'default' => true,
|
||||
],
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'format' => 'uri',
|
||||
'description' => esc_html__( "Optional URL to audit. Defaults to this site's home URL. When provided, only remote API tests run (local checks are site-wide).", 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => true,
|
||||
'destructive' => false,
|
||||
'idempotent' => true,
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( 'rank_math_site_analysis' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$refresh = isset( $input['refresh'] ) ? (bool) $input['refresh'] : true;
|
||||
$url = isset( $input['url'] ) ? $input['url'] : null;
|
||||
$result = $this->runner->run( $refresh, $url )->to_array();
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'SEO Audit Run',
|
||||
[
|
||||
'score' => isset( $result['score'] ) ? $result['score'] : 0,
|
||||
'grade' => isset( $result['grade'] ) ? $result['grade'] : '',
|
||||
'has_url' => ! empty( $url ),
|
||||
'has_error' => ! empty( $result['error'] ),
|
||||
],
|
||||
'rank_math_site_analysis'
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'url' => [ 'type' => 'string' ],
|
||||
'score' => [
|
||||
'type' => 'integer',
|
||||
'minimum' => 0,
|
||||
'maximum' => 100,
|
||||
],
|
||||
'grade' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'good', 'average', 'bad' ],
|
||||
],
|
||||
'statuses' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'ok' => [ 'type' => 'integer' ],
|
||||
'fail' => [ 'type' => 'integer' ],
|
||||
'warning' => [ 'type' => 'integer' ],
|
||||
'info' => [ 'type' => 'integer' ],
|
||||
],
|
||||
],
|
||||
'total_tests' => [ 'type' => 'integer' ],
|
||||
'last_run_at' => [ 'type' => 'integer' ],
|
||||
'remote_api_status' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'ok', 'unavailable', 'skipped' ],
|
||||
],
|
||||
'findings' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'test_id' => [ 'type' => 'string' ],
|
||||
'category' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'priority', 'basic', 'advanced', 'performance', 'security' ],
|
||||
],
|
||||
'status' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'ok', 'fail', 'warning', 'info' ],
|
||||
],
|
||||
'score' => [ 'type' => 'integer' ],
|
||||
'title' => [ 'type' => 'string' ],
|
||||
'description' => [ 'type' => 'string' ],
|
||||
'fix_text' => [ 'type' => 'string' ],
|
||||
'fix_html' => [ 'type' => 'string' ],
|
||||
'fix_hint' => [ 'type' => [ 'object', 'null' ] ],
|
||||
'kb_link' => [ 'type' => 'string' ],
|
||||
'data' => [ 'type' => [ 'object', 'array', 'null' ] ],
|
||||
],
|
||||
],
|
||||
],
|
||||
'error' => [
|
||||
'type' => [ 'object', 'null' ],
|
||||
'properties' => [
|
||||
'code' => [ 'type' => 'string' ],
|
||||
'message' => [ 'type' => 'string' ],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
/**
|
||||
* Finding value object for the rank-math/audit-site-seo ability.
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* One SEO-audit test result, agent-consumable shape.
|
||||
*/
|
||||
class Finding {
|
||||
|
||||
/**
|
||||
* Test identifier.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $test_id;
|
||||
|
||||
/**
|
||||
* Test category: priority|basic|advanced|performance|security.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category;
|
||||
|
||||
/**
|
||||
* Test outcome: ok|fail|warning|info.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $status;
|
||||
|
||||
/**
|
||||
* Score contribution for this test.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $score;
|
||||
|
||||
/**
|
||||
* Human-readable test title.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $title;
|
||||
|
||||
/**
|
||||
* Plain-text description of the current result (actual counts/state, not static guidance).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
|
||||
/**
|
||||
* Plain-text how-to-fix — HTML tags and entities stripped, ready for AI consumption.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $fix_text;
|
||||
|
||||
/**
|
||||
* Original how-to-fix HTML, preserved for admin-UI consumers.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $fix_html;
|
||||
|
||||
/**
|
||||
* Structured fix hint an agent can act on, or null if none mapped.
|
||||
*
|
||||
* @var array|null
|
||||
*/
|
||||
public $fix_hint;
|
||||
|
||||
/**
|
||||
* Knowledge-base article URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $kb_link;
|
||||
|
||||
/**
|
||||
* Free-form per-test supplemental data.
|
||||
*
|
||||
* @var mixed
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $args See properties.
|
||||
*/
|
||||
public function __construct( array $args ) {
|
||||
foreach ( $args as $key => $value ) {
|
||||
if ( property_exists( $this, $key ) ) {
|
||||
$this->{$key} = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize to array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() {
|
||||
return [
|
||||
'test_id' => $this->test_id,
|
||||
'category' => $this->category,
|
||||
'status' => $this->status,
|
||||
'score' => (int) $this->score,
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'fix_text' => $this->fix_text,
|
||||
'fix_html' => $this->fix_html,
|
||||
'fix_hint' => $this->fix_hint,
|
||||
'kb_link' => $this->kb_link,
|
||||
'data' => $this->data,
|
||||
];
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Fix hint map for the rank-math/audit-site-seo ability.
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Maps SEO-audit test IDs to structured fix hints an agent can act on.
|
||||
*
|
||||
* Kind:
|
||||
* - 'wp_admin' → user/agent should open this WP admin page
|
||||
* - 'setting' → fix is a Rank Math setting (`rank-math-options-{section}`)
|
||||
* - 'ability' → another Rank Math ability can fix it (only entries for shipped abilities)
|
||||
* - 'manual' → content edit per post; no single setting flip will resolve it
|
||||
*
|
||||
* Tests not in this map return null for fix_hint; the agent falls back to the
|
||||
* `fix_html` text. Entries are added here as new abilities ship.
|
||||
*/
|
||||
class Fix_Hint_Map {
|
||||
|
||||
/**
|
||||
* Retrieves the fix hint for a given test ID.
|
||||
*
|
||||
* @param string $test_id The ID of the test for which to retrieve a fix hint.
|
||||
* @return array|null The fix hint array or null if not found.
|
||||
*/
|
||||
public static function get( $test_id ) {
|
||||
$map = [
|
||||
// Single WP setting fix.
|
||||
'site_description' => [
|
||||
'kind' => 'wp_admin',
|
||||
'target' => 'options-general.php',
|
||||
],
|
||||
'permalink_structure' => [
|
||||
'kind' => 'wp_admin',
|
||||
'target' => 'options-permalink.php',
|
||||
],
|
||||
'blog_public' => [
|
||||
'kind' => 'wp_admin',
|
||||
'target' => 'options-reading.php',
|
||||
],
|
||||
|
||||
// Rank Math settings panels.
|
||||
'sitemaps' => [
|
||||
'kind' => 'setting',
|
||||
'target' => 'rank-math-options-sitemap',
|
||||
],
|
||||
'noindex' => [
|
||||
'kind' => 'setting',
|
||||
'target' => 'rank-math-options-titles',
|
||||
],
|
||||
'opengraph' => [
|
||||
'kind' => 'setting',
|
||||
'target' => 'rank-math-options-titles',
|
||||
],
|
||||
'schema' => [
|
||||
'kind' => 'setting',
|
||||
'target' => 'rank-math-options-titles',
|
||||
],
|
||||
'robots_txt' => [
|
||||
'kind' => 'setting',
|
||||
'target' => 'rank-math-options-general',
|
||||
],
|
||||
|
||||
// Per-post content edits — no single flip; agent must walk posts.
|
||||
'h1_heading' => [
|
||||
'kind' => 'manual',
|
||||
'target' => 'edit_post_content',
|
||||
],
|
||||
'h2_headings' => [
|
||||
'kind' => 'manual',
|
||||
'target' => 'edit_post_content',
|
||||
],
|
||||
'keywords_meta' => [
|
||||
'kind' => 'manual',
|
||||
'target' => 'edit_post_keywords',
|
||||
],
|
||||
'title_length' => [
|
||||
'kind' => 'manual',
|
||||
'target' => 'edit_post_titles',
|
||||
],
|
||||
];
|
||||
|
||||
return isset( $map[ $test_id ] ) ? $map[ $test_id ] : null;
|
||||
}
|
||||
}
|
||||
+430
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
/**
|
||||
* Fix runner service for the rank-math/fix-site-seo ability.
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Applies automatic fixes for failing SEO analysis tests.
|
||||
*/
|
||||
class Fix_Runner {
|
||||
|
||||
/**
|
||||
* Maximum posts to process in a single synchronous fix call.
|
||||
*/
|
||||
const POST_LIMIT = 100;
|
||||
|
||||
/**
|
||||
* Apply the fix for a given test and return a result summary.
|
||||
*
|
||||
* @param string $test_id SEO analysis test identifier.
|
||||
* @param array $input Raw ability input (may include 'value', 'post_limit').
|
||||
* @return array { fixed: bool, summary: string, details: array }
|
||||
*/
|
||||
public function fix( $test_id, array $input = [] ) {
|
||||
$handler = 'fix_' . $test_id;
|
||||
if ( ! method_exists( $this, $handler ) ) {
|
||||
return $this->cannot_fix(
|
||||
sprintf(
|
||||
/* translators: %s: test ID */
|
||||
__( 'No automatic fix is available for test "%s". Use the fix_text guidance to resolve it manually.', 'seo-by-rank-math' ),
|
||||
$test_id
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $this->{$handler}( $input );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: make the site visible to search engines.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_blog_public( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
update_option( 'blog_public', 1 );
|
||||
return $this->ok( __( 'Site visibility set to public. Search engines can now index your site.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: set permalink structure to /%postname%/.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_permalink_structure( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
global $wp_rewrite;
|
||||
|
||||
if ( '/%postname%/' === get_option( 'permalink_structure' ) ) {
|
||||
return $this->ok( __( 'Permalink structure is already set to /%postname%/.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
update_option( 'permalink_structure', '/%postname%/' );
|
||||
$wp_rewrite->set_permalink_structure( '/%postname%/' );
|
||||
$wp_rewrite->flush_rules( true );
|
||||
|
||||
return $this->ok( __( 'Permalink structure set to /%postname%/ and rewrite rules flushed.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: set the site tagline to a custom value.
|
||||
*
|
||||
* Requires `value` in input — refuses if missing or looks like the default.
|
||||
*
|
||||
* @param array $input Must contain 'value'.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_site_description( array $input ) {
|
||||
$value = isset( $input['value'] ) ? sanitize_text_field( $input['value'] ) : '';
|
||||
|
||||
if ( empty( $value ) ) {
|
||||
return $this->cannot_fix(
|
||||
__( 'A "value" input is required to set the site tagline. Provide a short, unique description of your site.', 'seo-by-rank-math' )
|
||||
);
|
||||
}
|
||||
|
||||
if ( 'just another wordpress site' === strtolower( $value ) ) { // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
|
||||
return $this->cannot_fix(
|
||||
__( 'The provided tagline is the default WordPress value. Please supply a unique tagline.', 'seo-by-rank-math' )
|
||||
);
|
||||
}
|
||||
|
||||
update_option( 'blogdescription', $value );
|
||||
return $this->ok(
|
||||
sprintf(
|
||||
/* translators: %s: tagline value */
|
||||
__( 'Site tagline updated to: "%s".', 'seo-by-rank-math' ),
|
||||
$value
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Rank Math module fixes
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Fix: enable the Rank Math sitemap module.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_sitemaps( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
Helper::update_modules( [ 'sitemap' => 'on' ] );
|
||||
return $this->ok( __( 'Sitemap module enabled. Your XML sitemap is now active.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: enable the Rank Math schema (rich snippet) module.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_schema( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
Helper::update_modules( [ 'rich-snippet' => 'on' ] );
|
||||
return $this->ok( __( 'Schema / rich snippet module enabled.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Rank Math settings fixes
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Fix: ensure the global robots setting does not include noindex.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_noindex( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
$titles = (array) get_option( 'rank-math-options-titles', [] );
|
||||
|
||||
$robots = isset( $titles['robots_global'] ) ? (array) $titles['robots_global'] : [ 'index', 'follow' ];
|
||||
$robots = array_diff( $robots, [ 'noindex' ] );
|
||||
if ( ! in_array( 'index', $robots, true ) ) {
|
||||
$robots[] = 'index';
|
||||
}
|
||||
$titles['robots_global'] = array_values( $robots );
|
||||
|
||||
update_option( 'rank-math-options-titles', $titles );
|
||||
return $this->ok( __( 'Global robots setting updated to allow indexing. Run a fresh audit to confirm the remote API test passes.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: OpenGraph is always enabled in Rank Math — explain to the AI.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_opengraph( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
return $this->cannot_fix(
|
||||
__( 'OpenGraph output is always enabled in Rank Math. If this test is failing, the remote audit API may need a fallback Open Graph image. Set one at Rank Math → Titles & Meta → Global Meta → Open Graph Thumbnail.', 'seo-by-rank-math' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: write a safe default robots.txt content into Rank Math settings.
|
||||
*
|
||||
* @param array $input Unused.
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_robots_txt( array $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
$general = (array) get_option( 'rank-math-options-general', [] );
|
||||
|
||||
if ( isset( $general['robots_txt_content'] ) && '' !== trim( $general['robots_txt_content'] ) ) {
|
||||
return $this->ok( __( 'Robots.txt content is already configured in Rank Math settings.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
$general['robots_txt_content'] = "User-agent: *\nDisallow: /wp-admin/\nAllow: /wp-admin/admin-ajax.php";
|
||||
update_option( 'rank-math-options-general', $general );
|
||||
|
||||
return $this->ok( __( 'Default robots.txt content written to Rank Math settings.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Per-post bulk fixes
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Fix: set a focus keyword on every published post that is missing one.
|
||||
*
|
||||
* The keyword is derived from the first 3 significant words of the post title.
|
||||
* Accepts optional 'post_limit' input (default: POST_LIMIT).
|
||||
*
|
||||
* @param array $input Optional: post_limit (int).
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_focus_keywords( array $input ) {
|
||||
$limit = isset( $input['post_limit'] ) ? absint( $input['post_limit'] ) : self::POST_LIMIT;
|
||||
$limit = max( 1, min( $limit, 500 ) );
|
||||
$types = Helper::get_allowed_post_types();
|
||||
|
||||
if ( empty( $types ) ) {
|
||||
return $this->cannot_fix( __( 'No indexable post types are configured in Rank Math.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
$posts = get_posts(
|
||||
[
|
||||
'post_type' => $types,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $limit,
|
||||
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery
|
||||
[
|
||||
'key' => 'rank_math_focus_keyword',
|
||||
'compare' => 'NOT EXISTS',
|
||||
],
|
||||
],
|
||||
'fields' => 'ids',
|
||||
]
|
||||
);
|
||||
|
||||
if ( empty( $posts ) ) {
|
||||
return $this->ok( __( 'All published posts already have a focus keyword set.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
$updated = 0;
|
||||
$truncated = count( $posts ) >= $limit;
|
||||
|
||||
foreach ( $posts as $post_id ) {
|
||||
$title = get_the_title( $post_id );
|
||||
$keyword = $this->keyword_from_title( $title );
|
||||
if ( empty( $keyword ) ) {
|
||||
continue;
|
||||
}
|
||||
update_post_meta( $post_id, 'rank_math_focus_keyword', $keyword );
|
||||
++$updated;
|
||||
}
|
||||
|
||||
$summary = sprintf(
|
||||
/* translators: 1: count updated, 2: total found */
|
||||
__( 'Set focus keywords on %1$d posts (derived from post titles).', 'seo-by-rank-math' ),
|
||||
$updated
|
||||
);
|
||||
|
||||
if ( $truncated ) {
|
||||
$summary .= ' ' . sprintf(
|
||||
/* translators: %d: post_limit */
|
||||
__( 'Results were limited to %d posts — run again to continue.', 'seo-by-rank-math' ),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
|
||||
return $this->ok(
|
||||
$summary,
|
||||
[
|
||||
'updated' => $updated,
|
||||
'truncated' => $truncated,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix: update the focus keyword to match the existing post title for posts where it doesn't.
|
||||
*
|
||||
* Changing the focus keyword (not the title) is safe: it doesn't affect URLs or published content.
|
||||
* Accepts optional 'post_limit' input (default: POST_LIMIT).
|
||||
*
|
||||
* @param array $input Optional: post_limit (int).
|
||||
* @return array
|
||||
*/
|
||||
protected function fix_post_titles( array $input ) {
|
||||
global $wpdb;
|
||||
|
||||
$limit = isset( $input['post_limit'] ) ? absint( $input['post_limit'] ) : self::POST_LIMIT;
|
||||
$limit = max( 1, min( $limit, 500 ) );
|
||||
$types = Helper::get_allowed_post_types();
|
||||
|
||||
if ( empty( $types ) ) {
|
||||
return $this->cannot_fix( __( 'No indexable post types are configured in Rank Math.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
$in_post_types = "'" . implode( "','", array_map( 'esc_sql', $types ) ) . "'";
|
||||
|
||||
// Reuse the same query as the test — posts with focus keyword set but keyword not in title.
|
||||
$meta_query = new \WP_Meta_Query(
|
||||
[
|
||||
'relation' => 'AND',
|
||||
[
|
||||
'key' => 'rank_math_focus_keyword',
|
||||
'compare' => 'EXISTS',
|
||||
],
|
||||
[
|
||||
'relation' => 'OR',
|
||||
[
|
||||
'key' => 'rank_math_robots',
|
||||
'value' => 'noindex',
|
||||
'compare' => 'NOT LIKE',
|
||||
],
|
||||
[
|
||||
'key' => 'rank_math_robots',
|
||||
'compare' => 'NOT EXISTS',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
$mq_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
|
||||
// phpcs:disable WordPress.DB.DirectDatabaseQuery
|
||||
$rows = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder
|
||||
"SELECT {$wpdb->posts}.ID, {$wpdb->posts}.post_title
|
||||
FROM {$wpdb->posts} {$mq_sql['join']}
|
||||
WHERE 1=1 {$mq_sql['where']}
|
||||
AND {$wpdb->posts}.post_type IN ({$in_post_types})
|
||||
AND {$wpdb->posts}.post_status = 'publish'
|
||||
AND REPLACE( {$wpdb->posts}.post_title, %s, %s ) NOT LIKE
|
||||
CONCAT( %s, SUBSTRING_INDEX( {$wpdb->postmeta}.meta_value, %s, 1 ), %s )
|
||||
GROUP BY {$wpdb->posts}.ID
|
||||
LIMIT %d",
|
||||
'&',
|
||||
'&',
|
||||
'%',
|
||||
',',
|
||||
'%',
|
||||
$limit
|
||||
)
|
||||
);
|
||||
// phpcs:enable WordPress.DB.DirectDatabaseQuery
|
||||
|
||||
if ( empty( $rows ) ) {
|
||||
return $this->ok( __( 'All posts with focus keywords already include them in their titles.', 'seo-by-rank-math' ) );
|
||||
}
|
||||
|
||||
$updated = 0;
|
||||
$truncated = count( $rows ) >= $limit;
|
||||
|
||||
foreach ( $rows as $row ) {
|
||||
$keyword = $this->keyword_from_title( $row->post_title );
|
||||
if ( empty( $keyword ) ) {
|
||||
continue;
|
||||
}
|
||||
update_post_meta( (int) $row->ID, 'rank_math_focus_keyword', $keyword );
|
||||
++$updated;
|
||||
}
|
||||
|
||||
$summary = sprintf(
|
||||
/* translators: %d: count updated */
|
||||
__( 'Updated focus keywords on %d posts to match their titles.', 'seo-by-rank-math' ),
|
||||
$updated
|
||||
);
|
||||
|
||||
if ( $truncated ) {
|
||||
$summary .= ' ' . sprintf(
|
||||
/* translators: %d: post_limit */
|
||||
__( 'Results were limited to %d posts — run again to continue.', 'seo-by-rank-math' ),
|
||||
$limit
|
||||
);
|
||||
}
|
||||
|
||||
return $this->ok(
|
||||
$summary,
|
||||
[
|
||||
'updated' => $updated,
|
||||
'truncated' => $truncated,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Derive a focus keyword from a post title.
|
||||
*
|
||||
* Returns the first 3 words, lower-cased, with punctuation stripped.
|
||||
* Returns empty string if nothing usable remains.
|
||||
*
|
||||
* @param string $title Post title.
|
||||
* @return string
|
||||
*/
|
||||
private function keyword_from_title( $title ) {
|
||||
$words = preg_split( '/\s+/', strtolower( html_entity_decode( $title, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ) );
|
||||
$words = array_values(
|
||||
array_filter(
|
||||
$words,
|
||||
function ( $w ) {
|
||||
return ! empty( preg_replace( '/[^a-z0-9]/i', '', $w ) );
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
return implode( ' ', array_slice( $words, 0, 3 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a successful fix result.
|
||||
*
|
||||
* @param string $summary Human-readable summary.
|
||||
* @param array $details Optional extra data.
|
||||
* @return array
|
||||
*/
|
||||
private function ok( $summary, array $details = [] ) {
|
||||
return [
|
||||
'fixed' => true,
|
||||
'summary' => $summary,
|
||||
'details' => $details,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a result indicating the fix could not be applied.
|
||||
*
|
||||
* @param string $summary Reason why the fix could not be applied.
|
||||
* @return array
|
||||
*/
|
||||
private function cannot_fix( $summary ) {
|
||||
return [
|
||||
'fixed' => false,
|
||||
'summary' => $summary,
|
||||
'details' => [],
|
||||
];
|
||||
}
|
||||
}
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/fix-site-seo
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/fix-site-seo ability.
|
||||
*/
|
||||
class Fix_Site_SEO implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Fixable test IDs — kept in sync with Fix_Runner handler methods.
|
||||
*/
|
||||
const FIXABLE_TEST_IDS = [
|
||||
'blog_public',
|
||||
'permalink_structure',
|
||||
'site_description',
|
||||
'sitemaps',
|
||||
'schema',
|
||||
'noindex',
|
||||
'opengraph',
|
||||
'robots_txt',
|
||||
'focus_keywords',
|
||||
'post_titles',
|
||||
];
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Fix runner instance.
|
||||
*
|
||||
* @var Fix_Runner
|
||||
*/
|
||||
private $runner;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
* @param Fix_Runner|null $runner Fix runner instance.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta, ?Fix_Runner $runner = null ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
$this->runner = $runner ?? new Fix_Runner();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/fix-site-seo',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Fix site SEO', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Applies an automatic fix for a single failing Rank Math SEO test. Pass the test_id from an audit-site-seo finding. Some fixes require additional input (e.g. value for site_description). Bulk fixes accept an optional post_limit.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'test_id' ],
|
||||
'properties' => [
|
||||
'test_id' => [
|
||||
'type' => 'string',
|
||||
'enum' => self::FIXABLE_TEST_IDS,
|
||||
'description' => esc_html__( 'The SEO test identifier to fix (from audit-site-seo findings).', 'seo-by-rank-math' ),
|
||||
],
|
||||
'value' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Required for site_description: the new tagline to set.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'post_limit' => [
|
||||
'type' => 'integer',
|
||||
'minimum' => 1,
|
||||
'maximum' => 500,
|
||||
'description' => esc_html__( 'For bulk post fixes (focus_keywords, post_titles): max posts to update in one call. Default 100.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => false,
|
||||
'destructive' => false,
|
||||
'idempotent' => false,
|
||||
],
|
||||
]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( 'rank_math_site_analysis' ) && current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$test_id = (string) $input['test_id'];
|
||||
$result = $this->runner->fix( $test_id, (array) $input );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'SEO Audit Test Fixed',
|
||||
[
|
||||
'test_id' => $test_id,
|
||||
'fixed' => ! empty( $result['fixed'] ),
|
||||
],
|
||||
'rank_math_site_analysis'
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'fixed' => [ 'type' => 'boolean' ],
|
||||
'summary' => [ 'type' => 'string' ],
|
||||
'details' => [ 'type' => 'object' ],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Subscriber for SEO Analysis abilities.
|
||||
*
|
||||
* @since 1.0.271
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\SEO_Analysis
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\SEO_Analysis;
|
||||
|
||||
use RankMath\Traits\Hooker;
|
||||
use RankMath\Abilities\Subscriber_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers the SEO Analysis ability category and its abilities.
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
|
||||
use Hooker;
|
||||
|
||||
/**
|
||||
* Ability category slug for SEO Analysis abilities.
|
||||
*/
|
||||
const CATEGORY_SLUG = 'rank-math-seo-analysis';
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $shared_meta;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $shared_meta Shared meta args from the top-level Abilities class.
|
||||
*/
|
||||
public function __construct( array $shared_meta ) {
|
||||
$this->shared_meta = $shared_meta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wire hooks.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
$this->action( 'wp_abilities_api_categories_init', 'register_category' );
|
||||
$this->action( 'wp_abilities_api_init', 'register_audit_site_seo' );
|
||||
$this->action( 'wp_abilities_api_init', 'register_fix_site_seo' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the SEO Analysis ability category.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_category(): void {
|
||||
\wp_register_ability_category(
|
||||
self::CATEGORY_SLUG,
|
||||
[
|
||||
'label' => esc_html__( 'SEO Analysis', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Abilities to audit and fix site-wide SEO issues.', 'seo-by-rank-math' ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the rank-math/audit-site-seo ability.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_audit_site_seo(): void {
|
||||
( new Audit_Site_SEO( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the rank-math/fix-site-seo ability.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_fix_site_seo(): void {
|
||||
( new Fix_Site_SEO( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden.
|
||||
Reference in New Issue
Block a user