Commit inicial powerservice
This commit is contained in:
+195
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
/**
|
||||
* Abstract base class for Settings abilities.
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\Abilities\Ability_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Shared infrastructure for all Settings abilities.
|
||||
*
|
||||
* Provides the constructor, check_permissions(), option-reader helpers,
|
||||
* apply_toggle_fields(), save_and_reset(), build_meta(), and write_success_schema()
|
||||
* so concrete classes only need to implement register() and execute().
|
||||
*/
|
||||
abstract class Abstract_Ability implements Ability_Interface {
|
||||
|
||||
/**
|
||||
* Allowed robots meta directive values — shared across abilities.
|
||||
*/
|
||||
const ROBOTS_DIRECTIVES = [ 'index', 'noindex', 'nofollow', 'noarchive', 'noimageindex', 'nosnippet' ];
|
||||
|
||||
/**
|
||||
* Ability category slug.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $category;
|
||||
|
||||
/**
|
||||
* Shared meta args.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $shared_meta;
|
||||
|
||||
/**
|
||||
* WordPress capability required to execute this ability.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $required_capability;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
* @param string $required_capability WP capability checked in check_permissions().
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta, string $required_capability ) {
|
||||
$this->category = $category;
|
||||
$this->shared_meta = $shared_meta;
|
||||
$this->required_capability = $required_capability;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current user has permission to execute this ability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check_permissions(): bool {
|
||||
return current_user_can( $this->required_capability );
|
||||
}
|
||||
|
||||
/**
|
||||
* Current raw general option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_general_option(): array {
|
||||
return (array) get_option( 'rank-math-options-general', [] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Current raw titles option.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_titles_option(): array {
|
||||
return (array) get_option( 'rank-math-options-titles', [] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Currently active module slugs.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected function get_active_modules(): array {
|
||||
return (array) get_option( 'rank_math_modules', [] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Accessible post type slugs.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected function get_accessible_post_types(): array {
|
||||
return array_keys( Helper::get_accessible_post_types() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply boolean-to-on/off toggle fields from input onto an option array.
|
||||
*
|
||||
* @param array $option Target option array (modified in-place).
|
||||
* @param array $input Ability input.
|
||||
* @param string[] $fields Field names to apply.
|
||||
* @return void
|
||||
*/
|
||||
protected function apply_toggle_fields( array &$option, array $input, array $fields ): void {
|
||||
foreach ( $fields as $field ) {
|
||||
if ( isset( $input[ $field ] ) ) {
|
||||
$option[ $field ] = $input[ $field ] ? 'on' : 'off';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist settings and flush the in-memory cache.
|
||||
*
|
||||
* @param array|null $general Updated general section, or null to leave untouched.
|
||||
* @param array|null $titles Updated titles section, or null to leave untouched.
|
||||
* @param array|null $sitemap Updated sitemap section, or null to leave untouched.
|
||||
* @return void
|
||||
*/
|
||||
protected function save_and_reset( ?array $general, ?array $titles, ?array $sitemap ): void {
|
||||
Helper::update_all_settings( $general, $titles, $sitemap );
|
||||
rank_math()->settings->reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the meta array for wp_register_ability(), merging in annotations.
|
||||
*
|
||||
* @param bool $read_only Whether this ability only reads data.
|
||||
* @return array
|
||||
*/
|
||||
protected function build_meta( bool $read_only ): array {
|
||||
return array_merge(
|
||||
$this->shared_meta,
|
||||
[
|
||||
'annotations' => [
|
||||
'readonly' => $read_only,
|
||||
'destructive' => false,
|
||||
'idempotent' => true,
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema fragment common to all write-ability success responses.
|
||||
*
|
||||
* @param bool $with_error Include the error property.
|
||||
* @param string $updated_description Description for the updated field.
|
||||
* @return array
|
||||
*/
|
||||
protected function write_success_schema( bool $with_error = false, string $updated_description = 'List of field names that were updated.' ): array {
|
||||
$schema = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'saved' => [
|
||||
'type' => 'boolean',
|
||||
'description' => 'Whether the update was saved.',
|
||||
],
|
||||
'updated' => [
|
||||
'type' => 'array',
|
||||
'items' => [ 'type' => 'string' ],
|
||||
'description' => $updated_description,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
if ( $with_error ) {
|
||||
$schema['properties']['error'] = [
|
||||
'type' => 'object',
|
||||
'description' => 'Present only when the update failed validation.',
|
||||
'properties' => [
|
||||
'code' => [ 'type' => 'string' ],
|
||||
'message' => [ 'type' => 'string' ],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $schema;
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-llms-txt
|
||||
*
|
||||
* @since 1.0.278
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
use RankMath\LLMS\LLMS_Txt;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-llms-txt ability.
|
||||
*
|
||||
* Delegates entirely to LLMS_Txt::output(), capturing its echoed Markdown
|
||||
* output via an output buffer rather than reimplementing the generation logic.
|
||||
*/
|
||||
class Get_Llms_Txt extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_general' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-llms-txt',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get llms.txt', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns whether the llms.txt file is enabled, its URL, and the current generated Markdown content.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$enabled = $this->is_enabled();
|
||||
|
||||
$result = [
|
||||
'enabled' => $enabled,
|
||||
'url' => home_url( '/llms.txt' ),
|
||||
'content' => $enabled ? $this->fetch_content() : '',
|
||||
];
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'LLMs Txt Fetched',
|
||||
[ 'enabled' => $enabled ],
|
||||
'rank_math_general'
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the llms.txt module is active. Extracted for testability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_enabled(): bool {
|
||||
return Helper::is_module_active( 'llms-txt' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture the LLMS_Txt::output() Markdown content via an output buffer.
|
||||
*
|
||||
* Output() also sends a couple of headers (Content-Type: text/plain,
|
||||
* X-Robots-Tag) as a side effect of reuse — harmless here since the
|
||||
* REST/MCP response sends its own headers afterward. Extracted for
|
||||
* testability.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function fetch_content(): string {
|
||||
ob_start();
|
||||
( new LLMS_Txt() )->output();
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'enabled' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Whether the llms.txt module is active.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'The URL of the llms.txt file.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'content' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'The generated llms.txt Markdown content. Empty when the module is disabled.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-robots-txt
|
||||
*
|
||||
* @since 1.0.278
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Robots_Txt;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-robots-txt ability.
|
||||
*/
|
||||
class Get_Robots_Txt extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_general' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-robots-txt',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get robots.txt', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns the current robots.txt content managed by Rank Math.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
],
|
||||
'output_schema' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'content' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'The current robots.txt content: the real file contents when a physical robots.txt exists, otherwise the content Rank Math generates (custom override or computed default).', 'seo-by-rank-math' ),
|
||||
],
|
||||
'exists' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Whether a physical robots.txt file exists in the site root.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'public' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Whether the site is set to be visible to search engines (Settings > Reading > Search engine visibility).', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
],
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$data = $this->get_robots_data();
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Robots Txt Fetched',
|
||||
[],
|
||||
'rank_math_general'
|
||||
);
|
||||
|
||||
return [
|
||||
'content' => $data['default'],
|
||||
'exists' => ! empty( $data['exists'] ),
|
||||
'public' => 0 !== absint( $data['public'] ),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch raw data from Robots_Txt. Extracted for testability.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_robots_data(): array {
|
||||
return Robots_Txt::get_robots_data();
|
||||
}
|
||||
}
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-settings
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-settings ability.
|
||||
*/
|
||||
class Get_Settings extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Keys stripped from the general section before returning to AI — contain credentials or internal state.
|
||||
*/
|
||||
const SENSITIVE_GENERAL_KEYS = [
|
||||
'facebook_secret',
|
||||
'console_profile',
|
||||
'searchConsole',
|
||||
'analyticsData',
|
||||
'analytics',
|
||||
'console_caching_control',
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-settings',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns the full normalized Rank Math settings snapshot: all four sections (general, titles, sitemap, instant_indexing), active module status, and auto-update state. Call this first in any AI configuration flow to understand what is already configured before making changes.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'sections' => [
|
||||
'type' => 'array',
|
||||
'description' => esc_html__( 'Return only these top-level sections. Omit to return the full snapshot.', 'seo-by-rank-math' ),
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'general', 'titles', 'sitemap', 'instant_indexing', 'modules', 'auto_update' ],
|
||||
],
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->output_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$settings = rank_math()->settings->all();
|
||||
|
||||
foreach ( self::SENSITIVE_GENERAL_KEYS as $key ) {
|
||||
unset( $settings['general'][ $key ] );
|
||||
}
|
||||
|
||||
$settings['modules'] = $this->get_module_status();
|
||||
$settings['auto_update'] = $this->get_auto_update_setting();
|
||||
|
||||
if ( ! empty( $input['sections'] ) ) {
|
||||
$settings = array_intersect_key( $settings, array_fill_keys( $input['sections'], true ) );
|
||||
}
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Settings Fetched',
|
||||
[ 'sections' => array_keys( $settings ) ],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a map of user-facing modules with their effective active status.
|
||||
*
|
||||
* Excludes internal modules (always loaded, not user-toggleable) and
|
||||
* skip-only entries. Modules whose dependencies are not met (disabled = true)
|
||||
* are always reported as inactive regardless of the stored option value.
|
||||
*
|
||||
* @return array<string, bool>
|
||||
*/
|
||||
private function get_module_status(): array {
|
||||
$active_modules = $this->get_active_modules();
|
||||
$status = [];
|
||||
|
||||
foreach ( rank_math()->manager->modules as $slug => $module ) {
|
||||
$only = $module->get( 'only' );
|
||||
|
||||
// Internal and skip-only modules are not user-configurable.
|
||||
if ( 'internal' === $only || 'skip' === $only ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// A module whose dependency is missing is always inactive.
|
||||
if ( $module->get( 'disabled', false ) ) {
|
||||
$status[ $slug ] = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
$status[ $slug ] = in_array( $slug, $active_modules, true );
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether automatic plugin updates are enabled. Extracted for testability.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function get_auto_update_setting(): bool {
|
||||
return Helper::get_auto_update_setting();
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON schema for the ability output.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function output_schema(): array {
|
||||
return [
|
||||
'type' => 'object',
|
||||
'description' => 'Normalized plugin settings snapshot.',
|
||||
'properties' => [
|
||||
'general' => [
|
||||
'type' => 'object',
|
||||
'description' => 'General settings (rank-math-options-general). Boolean values are normalized from on/off storage strings.',
|
||||
],
|
||||
'titles' => [
|
||||
'type' => 'object',
|
||||
'description' => 'Titles & Meta settings (rank-math-options-titles).',
|
||||
],
|
||||
'sitemap' => [
|
||||
'type' => 'object',
|
||||
'description' => 'Sitemap settings (rank-math-options-sitemap).',
|
||||
],
|
||||
'instant_indexing' => [
|
||||
'type' => 'object',
|
||||
'description' => 'Instant Indexing settings (rank-math-options-instant-indexing).',
|
||||
],
|
||||
'modules' => [
|
||||
'type' => 'object',
|
||||
'description' => 'Map of all registered module slugs to their active status.',
|
||||
'additionalProperties' => [ 'type' => 'boolean' ],
|
||||
],
|
||||
'auto_update' => [
|
||||
'type' => 'boolean',
|
||||
'description' => 'Whether automatic plugin updates are enabled.',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/get-system-status
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Status\System_Status;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/get-system-status ability.
|
||||
*
|
||||
* Delegates entirely to System_Status::get_json_data() which already
|
||||
* collects Rank Math health data and WordPress core / server info.
|
||||
*/
|
||||
class Get_System_Status extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/get-system-status',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Get system status', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Returns a structured system health snapshot identical to Status & Tools → System Status in the admin: Rank Math version, plan, DB table health, Google token status, WordPress core info, server environment, and active plugins. Call this alongside get-settings at the start of any AI configuration flow to detect environment issues before making changes.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
],
|
||||
'output_schema' => [
|
||||
'type' => 'object',
|
||||
'description' => 'System info sections keyed by section ID (e.g. rank-math, wp-core, wp-server).',
|
||||
],
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( true ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$result = $this->fetch_system_data()['systemInfo'];
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'System Status Fetched',
|
||||
[],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch raw data from System_Status. Extracted for testability.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function fetch_system_data(): array {
|
||||
return System_Status::get_json_data();
|
||||
}
|
||||
}
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-breadcrumb-settings
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-breadcrumb-settings ability.
|
||||
*/
|
||||
class Set_Breadcrumb_Settings extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_general' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-breadcrumb-settings',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set breadcrumb settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates breadcrumb display settings: enabling breadcrumbs, the separator, home link visibility/label/URL, a prefix before the trail, archive/search/404 label formats, ancestor category display, hiding the taxonomy name, removing the current post title, and showing the blog page. All fields are optional — omitted fields keep their current stored value.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'breadcrumbs' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Enable Breadcrumbs', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Enable the Breadcrumbs feature.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_separator' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Separator Character', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Character to display between breadcrumb links.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_home' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Show Home Link', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Show a link to the homepage at the start of the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_home_label' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Home Label', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Label used for the homepage link in the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_home_link' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Home Link', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'URL used for the homepage link in the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_prefix' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Prefix Breadcrumb', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Text to show before the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_archive_format' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Archive Format', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Label format used for archive pages in the breadcrumb trail. Include a placeholder token for the archive title.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_search_format' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Search Results Format', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Label format used for search results pages in the breadcrumb trail. Include a placeholder token for the search query.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_404_label' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( '404 Label', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Label used for the 404 error item in the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_remove_post_title' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Hide Post Title', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Hide the current post/page title from the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_ancestor_categories' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Show Category(s)', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'If the category is a child category, show all ancestor categories in the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_hide_taxonomy_name' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Hide Taxonomy Name', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Hide the current taxonomy term name from the breadcrumb trail.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'breadcrumbs_blog_page' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Show Blog Page', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Show the blog page in the breadcrumb trail. Only applies when a static homepage is set.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$general = $this->get_general_option();
|
||||
|
||||
$this->apply_toggle_fields(
|
||||
$general,
|
||||
$input,
|
||||
[
|
||||
'breadcrumbs',
|
||||
'breadcrumbs_home',
|
||||
'breadcrumbs_remove_post_title',
|
||||
'breadcrumbs_ancestor_categories',
|
||||
'breadcrumbs_hide_taxonomy_name',
|
||||
'breadcrumbs_blog_page',
|
||||
]
|
||||
);
|
||||
|
||||
foreach ( [ 'breadcrumbs_separator', 'breadcrumbs_home_label', 'breadcrumbs_prefix', 'breadcrumbs_archive_format', 'breadcrumbs_search_format', 'breadcrumbs_404_label' ] as $field ) {
|
||||
if ( isset( $input[ $field ] ) ) {
|
||||
$general[ $field ] = sanitize_text_field( $input[ $field ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $input['breadcrumbs_home_link'] ) ) {
|
||||
$general['breadcrumbs_home_link'] = esc_url_raw( $input['breadcrumbs_home_link'] );
|
||||
}
|
||||
|
||||
$this->save_and_reset( $general, null, null );
|
||||
|
||||
$keys = array_keys( $input );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Breadcrumb Settings Updated',
|
||||
[ 'fields_updated' => $keys ],
|
||||
'rank_math_general'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $keys,
|
||||
];
|
||||
}
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-global-seo-settings
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-global-seo-settings ability.
|
||||
*/
|
||||
class Set_Global_Seo_Settings extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Allowed values for the twitter_card_type field.
|
||||
*/
|
||||
const TWITTER_CARD_TYPES = [ 'summary_large_image', 'summary_card' ];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_titles' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-global-seo-settings',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set global SEO settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates site-wide SEO defaults: global robots meta directives, whether to noindex empty taxonomy archives, the title separator character, title capitalization, and the default Twitter card type. All fields are optional — omitted fields keep their current stored value.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'robots_global' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
'enum' => self::ROBOTS_DIRECTIVES,
|
||||
],
|
||||
'description' => esc_html__( 'Global robots meta directives applied site-wide (e.g. ["index"], ["noindex","nofollow"]).', 'seo-by-rank-math' ),
|
||||
],
|
||||
'noindex_empty_taxonomies' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Whether to noindex taxonomy archives that have no posts assigned.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'title_separator' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Character used to separate parts of the SEO title (e.g. "-", "|", "—"). A custom string is also accepted.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'capitalize_titles' => [
|
||||
'type' => 'boolean',
|
||||
'description' => esc_html__( 'Whether to capitalize the first letter of every word in SEO titles.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'twitter_card_type' => [
|
||||
'type' => 'string',
|
||||
'enum' => self::TWITTER_CARD_TYPES,
|
||||
'description' => esc_html__( 'Default Twitter card type used when sharing content.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema( true ),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
if ( isset( $input['robots_global'] ) ) {
|
||||
foreach ( (array) $input['robots_global'] as $value ) {
|
||||
if ( ! in_array( $value, self::ROBOTS_DIRECTIVES, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_robots_global',
|
||||
'message' => esc_html__( 'robots_global may only contain: index, noindex, nofollow, noarchive, noimageindex, nosnippet.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $input['twitter_card_type'] ) && ! in_array( $input['twitter_card_type'], self::TWITTER_CARD_TYPES, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_twitter_card_type',
|
||||
'message' => esc_html__( 'twitter_card_type must be either "summary_large_image" or "summary_card".', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$titles = $this->get_titles_option();
|
||||
|
||||
if ( isset( $input['robots_global'] ) ) {
|
||||
$titles['robots_global'] = array_map( 'sanitize_text_field', (array) $input['robots_global'] );
|
||||
}
|
||||
|
||||
$this->apply_toggle_fields( $titles, $input, [ 'noindex_empty_taxonomies', 'capitalize_titles' ] );
|
||||
|
||||
if ( isset( $input['title_separator'] ) ) {
|
||||
$titles['title_separator'] = sanitize_text_field( $input['title_separator'] );
|
||||
}
|
||||
|
||||
if ( isset( $input['twitter_card_type'] ) ) {
|
||||
$titles['twitter_card_type'] = sanitize_text_field( $input['twitter_card_type'] );
|
||||
}
|
||||
|
||||
$this->save_and_reset( null, $titles, null );
|
||||
|
||||
$keys = array_keys( $input );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Global SEO Settings Updated',
|
||||
[ 'fields_updated' => $keys ],
|
||||
'rank_math_titles'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $keys,
|
||||
];
|
||||
}
|
||||
}
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-homepage-seo
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-homepage-seo ability.
|
||||
*
|
||||
* Automatically detects whether the homepage is a static page or the latest-posts
|
||||
* feed and writes to the appropriate storage in each case:
|
||||
*/
|
||||
class Set_Homepage_Seo extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_titles' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-homepage-seo',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set homepage SEO', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates the SEO title, meta description, and focus keyword for the site homepage. Automatically detects whether the homepage is a static page or a latest-posts feed and writes to the correct storage in each case. Call get-settings first to see current homepage values. All fields are optional — omitted fields keep their current stored value.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'title' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'SEO Title', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'SEO title for the homepage. Supports Rank Math title variables (e.g. %sitename%, %sep%, %sitedesc%). For static-page homepages, stored as post meta; for latest-posts homepages, stored in Titles & Meta settings.', 'seo-by-rank-math' ), //phpcs:ignore WordPress.WP.I18n
|
||||
],
|
||||
'description' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Meta Description', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Meta description for the homepage. For static-page homepages, stored as post meta; for latest-posts homepages, stored in Titles & Meta settings.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'focus_keyword' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Focus Keyword', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Primary focus keyword for the homepage. Only applicable when the homepage is a static page — ignored for latest-posts homepages.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema( true ),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
if ( empty( $input ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'no_fields',
|
||||
'message' => esc_html__( 'No fields provided. Pass at least one of: title, description, focus_keyword.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
if ( 'page' === get_option( 'show_on_front' ) ) {
|
||||
return $this->update_static_page( $input );
|
||||
}
|
||||
|
||||
return $this->update_latest_posts( $input );
|
||||
}
|
||||
|
||||
/**
|
||||
* Write title/description post meta on the static front page.
|
||||
*
|
||||
* @param array $input Ability input.
|
||||
* @return array
|
||||
*/
|
||||
private function update_static_page( array $input ): array {
|
||||
$page_id = (int) get_option( 'page_on_front' );
|
||||
|
||||
if ( ! $page_id ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'no_front_page',
|
||||
'message' => esc_html__( 'show_on_front is set to "page" but no front page is assigned (page_on_front is empty). Assign a static front page under WP Dashboard > Settings > Reading.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$updated = [];
|
||||
|
||||
if ( isset( $input['title'] ) ) {
|
||||
update_post_meta( $page_id, 'rank_math_title', sanitize_text_field( $input['title'] ) );
|
||||
$updated[] = 'title';
|
||||
}
|
||||
|
||||
if ( isset( $input['description'] ) ) {
|
||||
update_post_meta( $page_id, 'rank_math_description', sanitize_textarea_field( $input['description'] ) );
|
||||
$updated[] = 'description';
|
||||
}
|
||||
|
||||
if ( isset( $input['focus_keyword'] ) ) {
|
||||
update_post_meta( $page_id, 'rank_math_focus_keyword', sanitize_text_field( $input['focus_keyword'] ) );
|
||||
$updated[] = 'focus_keyword';
|
||||
}
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Homepage SEO Updated',
|
||||
[
|
||||
'homepage_type' => 'static_page',
|
||||
'page_id' => $page_id,
|
||||
'fields_updated' => $updated,
|
||||
],
|
||||
'rank_math_titles'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $updated,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Write title/description into the Titles & Meta settings for the
|
||||
* latest-posts homepage.
|
||||
*
|
||||
* @param array $input Ability input.
|
||||
* @return array
|
||||
*/
|
||||
private function update_latest_posts( array $input ): array {
|
||||
$titles = $this->get_titles_option();
|
||||
$updated = [];
|
||||
|
||||
if ( isset( $input['title'] ) ) {
|
||||
$titles['homepage_title'] = sanitize_text_field( $input['title'] );
|
||||
$updated[] = 'title';
|
||||
}
|
||||
|
||||
if ( isset( $input['description'] ) ) {
|
||||
$titles['homepage_description'] = sanitize_textarea_field( $input['description'] );
|
||||
$updated[] = 'description';
|
||||
}
|
||||
|
||||
$this->save_and_reset( null, $titles, null );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Homepage SEO Updated',
|
||||
[
|
||||
'homepage_type' => 'latest_posts',
|
||||
'fields_updated' => $updated,
|
||||
],
|
||||
'rank_math_titles'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $updated,
|
||||
];
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-link-settings
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-link-settings ability.
|
||||
*/
|
||||
class Set_Link_Settings extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_general' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-link-settings',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set link settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates site-wide link behavior: nofollow for external and image links, opening external links in a new tab, nofollow domain allow/exclude lists, category base stripping, and attachment page redirects. All fields are optional — omitted fields keep their current stored value.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'nofollow_external_links' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Nofollow External Links', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Automatically add rel="nofollow" to external links in content.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'new_window_external_links' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Open External Links in New Tab/Window', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Automatically add target="_blank" to external links in content.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'nofollow_image_links' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Nofollow Image File Links', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Automatically add rel="nofollow" to links pointing to external image files.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'nofollow_domains' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Nofollow Domains', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Newline-separated list of domains to nofollow. If empty, nofollow applies to all external domains (when enabled). Only takes effect when nofollow_external_links or nofollow_image_links is also enabled — hidden in the UI otherwise.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'nofollow_exclude_domains' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Nofollow Exclude Domains', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Newline-separated list of domains to exclude from nofollow. Only takes effect when nofollow_external_links or nofollow_image_links is also enabled — hidden in the UI otherwise.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'strip_category_base' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Strip Category Base', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Remove /category/ from category archive URLs.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'attachment_redirect_urls' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Redirect Attachments', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Redirect attachment page URLs to the post they appear in.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$general = $this->get_general_option();
|
||||
|
||||
$this->apply_toggle_fields(
|
||||
$general,
|
||||
$input,
|
||||
[
|
||||
'nofollow_external_links',
|
||||
'new_window_external_links',
|
||||
'nofollow_image_links',
|
||||
'strip_category_base',
|
||||
'attachment_redirect_urls',
|
||||
]
|
||||
);
|
||||
|
||||
foreach ( [ 'nofollow_domains', 'nofollow_exclude_domains' ] as $field ) {
|
||||
if ( isset( $input[ $field ] ) ) {
|
||||
$general[ $field ] = sanitize_textarea_field( $input[ $field ] );
|
||||
}
|
||||
}
|
||||
|
||||
$this->save_and_reset( $general, null, null );
|
||||
|
||||
$keys = array_keys( $input );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Link Settings Updated',
|
||||
[ 'fields_updated' => $keys ],
|
||||
'rank_math_general'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $keys,
|
||||
];
|
||||
}
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-module-status
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-module-status ability.
|
||||
*/
|
||||
class Set_Module_Status extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-module-status',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set module status', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Enables or disables Rank Math modules (e.g. sitemap, redirections, 404-monitor, rich-snippet). Only provided module slugs are changed; omitted modules keep their current state. Modules with unmet dependencies (e.g. Video Sitemap requires Sitemap and Schema) cannot be enabled until their dependencies are active or enabled in the same call. Modules whose external requirements are not met (e.g. WooCommerce module without the WooCommerce plugin active) cannot be enabled at all.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'modules' ],
|
||||
'properties' => [
|
||||
'modules' => [
|
||||
'type' => 'object',
|
||||
'title' => esc_html__( 'Modules', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Map of module slug to desired active state. Call get-settings first to see the full list of registered module slugs and their current status.', 'seo-by-rank-math' ),
|
||||
'additionalProperties' => [ 'type' => 'boolean' ],
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema( true, 'List of module slugs whose status was changed.' ),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$requested = isset( $input['modules'] ) && is_array( $input['modules'] ) ? $input['modules'] : [];
|
||||
|
||||
if ( empty( $requested ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'missing_modules',
|
||||
'message' => esc_html__( 'modules must be a non-empty map of module slug to boolean.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$registered = $this->get_registered_modules();
|
||||
$active = $this->get_active_modules();
|
||||
$to_update = [];
|
||||
|
||||
foreach ( $requested as $slug => $enable ) {
|
||||
if ( ! array_key_exists( $slug, $registered ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_module',
|
||||
'message' => sprintf(
|
||||
/* translators: module slug */
|
||||
esc_html__( 'Unknown module: %s', 'seo-by-rank-math' ),
|
||||
esc_html( $slug )
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
if ( $enable ) {
|
||||
if ( $registered[ $slug ]->is_disabled() ) {
|
||||
$disabled_text = $registered[ $slug ]->get( 'disabled_text' );
|
||||
if ( empty( $disabled_text ) ) {
|
||||
$disabled_text = sprintf(
|
||||
/* translators: module slug */
|
||||
esc_html__( 'Cannot enable "%s" — its requirements are not met.', 'seo-by-rank-math' ),
|
||||
esc_html( $slug )
|
||||
);
|
||||
}
|
||||
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'module_disabled',
|
||||
'message' => $disabled_text,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
foreach ( $registered[ $slug ]->get_dependencies() as $dependency ) {
|
||||
$dependency_met = in_array( $dependency, $active, true )
|
||||
|| ( isset( $requested[ $dependency ] ) && $requested[ $dependency ] );
|
||||
|
||||
if ( ! $dependency_met ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'unmet_dependency',
|
||||
'message' => sprintf(
|
||||
/* translators: 1: module slug, 2: required module slug */
|
||||
esc_html__( 'Cannot enable "%1$s" — it requires the "%2$s" module to be active or enabled in the same call.', 'seo-by-rank-math' ),
|
||||
esc_html( $slug ),
|
||||
esc_html( $dependency )
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$to_update[ $slug ] = $enable ? 'on' : 'off';
|
||||
}
|
||||
|
||||
Helper::update_modules( $to_update );
|
||||
|
||||
foreach ( $to_update as $slug => $state ) {
|
||||
if ( 'off' === $state && in_array( $slug, [ 'sitemap', 'llms-txt' ], true ) ) {
|
||||
delete_option( 'rewrite_rules' );
|
||||
}
|
||||
|
||||
do_action( 'rank_math/module_changed', $slug, $state );
|
||||
}
|
||||
|
||||
$keys = array_keys( $requested );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Module Status Updated',
|
||||
[ 'modules_updated' => $keys ],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $keys,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Registered modules keyed by slug. Extracted for testability.
|
||||
*
|
||||
* @return array<string, \RankMath\Module\Module>
|
||||
*/
|
||||
protected function get_registered_modules(): array {
|
||||
return rank_math()->manager->modules;
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-plugin-preferences
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-plugin-preferences ability.
|
||||
*/
|
||||
class Set_Plugin_Preferences extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-plugin-preferences',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set plugin preferences', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates Rank Math plugin-level preferences that are not part of a settings section. Currently supports toggling automatic plugin updates.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'enable_auto_update' ],
|
||||
'properties' => [
|
||||
'enable_auto_update' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Enable Automatic Updates', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Whether WordPress should automatically install new Rank Math plugin updates.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema(),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$enable = ! empty( $input['enable_auto_update'] );
|
||||
|
||||
Helper::toggle_auto_update_setting( $enable ? 'on' : 'off' );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Plugin Preferences Updated',
|
||||
[ 'enable_auto_update' => $enable ],
|
||||
'manage_options'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => [ 'enable_auto_update' ],
|
||||
];
|
||||
}
|
||||
}
|
||||
+224
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-post-type-seo-settings
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-post-type-seo-settings ability.
|
||||
*/
|
||||
class Set_Post_Type_Seo_Settings extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_titles' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-post-type-seo-settings',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set post type SEO settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates per-post-type SEO defaults: default title/description templates, default Schema type, default article type, custom robots meta, and whether to show SEO controls on the editor screen. Accepts a map keyed by post type slug; only provided post types and fields are changed.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'required' => [ 'post_types' ],
|
||||
'properties' => [
|
||||
'post_types' => [
|
||||
'type' => 'object',
|
||||
'title' => esc_html__( 'Post Types', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Map of post type slug to its SEO settings. Call get-settings first to see registered post type slugs.', 'seo-by-rank-math' ),
|
||||
'additionalProperties' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'title' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Single Title', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Default title tag for single pages of this post type. Supports Rank Math title variables.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'description' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Single Description', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Default meta description for single pages of this post type. Supports Rank Math variables.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'default_rich_snippet' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Schema Type', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Default Schema type slug for new posts of this type (e.g. "off", "article", "product", "event"). Call get-settings first or see the Schema Type list in WP Admin for valid values — the list varies per post type and active modules.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'default_article_type' => [
|
||||
'type' => 'string',
|
||||
'enum' => [ 'Article', 'BlogPosting', 'NewsArticle' ],
|
||||
'title' => esc_html__( 'Article Type', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Default article schema sub-type. Only used when default_rich_snippet is "article".', 'seo-by-rank-math' ),
|
||||
],
|
||||
'custom_robots' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Custom Robots Meta', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Whether to use custom robots meta values for this post type instead of the Global Meta default.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'robots' => [
|
||||
'type' => 'array',
|
||||
'items' => [
|
||||
'type' => 'string',
|
||||
'enum' => self::ROBOTS_DIRECTIVES,
|
||||
],
|
||||
'title' => esc_html__( 'Robots Meta', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Custom robots meta values for this post type. Only takes effect when custom_robots is enabled.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'add_meta_box' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Add SEO Controls', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Whether to add SEO controls to the editor screen for posts of this type.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema( true, 'List of "post_type.field" identifiers that were updated.' ),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
$post_types = isset( $input['post_types'] ) && is_array( $input['post_types'] ) ? $input['post_types'] : [];
|
||||
|
||||
if ( empty( $post_types ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'missing_post_types',
|
||||
'message' => esc_html__( 'post_types must be a non-empty map of post type slug to settings.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$accessible = $this->get_accessible_post_types();
|
||||
|
||||
foreach ( $post_types as $post_type => $fields ) {
|
||||
if ( ! in_array( $post_type, $accessible, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_post_type',
|
||||
'message' => sprintf(
|
||||
/* translators: post type slug */
|
||||
esc_html__( 'Unknown or inaccessible post type: %s', 'seo-by-rank-math' ),
|
||||
esc_html( $post_type )
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
if ( isset( $fields['robots'] ) ) {
|
||||
foreach ( (array) $fields['robots'] as $value ) {
|
||||
if ( ! in_array( $value, self::ROBOTS_DIRECTIVES, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_robots',
|
||||
'message' => esc_html__( 'robots may only contain: index, noindex, nofollow, noarchive, noimageindex, nosnippet.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $fields['default_article_type'] ) && ! in_array( $fields['default_article_type'], [ 'Article', 'BlogPosting', 'NewsArticle' ], true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_article_type',
|
||||
'message' => esc_html__( 'default_article_type must be one of: Article, BlogPosting, NewsArticle.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$titles = $this->get_titles_option();
|
||||
$updated = [];
|
||||
|
||||
foreach ( $post_types as $post_type => $fields ) {
|
||||
$prefix = "pt_{$post_type}_";
|
||||
|
||||
foreach ( [ 'title', 'description' ] as $field ) {
|
||||
if ( isset( $fields[ $field ] ) ) {
|
||||
$titles[ $prefix . $field ] = sanitize_text_field( $fields[ $field ] );
|
||||
$updated[] = "{$post_type}.{$field}";
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $fields['default_rich_snippet'] ) ) {
|
||||
$titles[ $prefix . 'default_rich_snippet' ] = sanitize_text_field( $fields['default_rich_snippet'] );
|
||||
$updated[] = "{$post_type}.default_rich_snippet";
|
||||
}
|
||||
|
||||
if ( isset( $fields['default_article_type'] ) ) {
|
||||
$titles[ $prefix . 'default_article_type' ] = $fields['default_article_type'];
|
||||
$updated[] = "{$post_type}.default_article_type";
|
||||
}
|
||||
|
||||
if ( isset( $fields['custom_robots'] ) ) {
|
||||
$titles[ $prefix . 'custom_robots' ] = $fields['custom_robots'] ? 'on' : 'off';
|
||||
$updated[] = "{$post_type}.custom_robots";
|
||||
}
|
||||
|
||||
if ( isset( $fields['robots'] ) ) {
|
||||
$titles[ $prefix . 'robots' ] = array_map( 'sanitize_text_field', (array) $fields['robots'] );
|
||||
$updated[] = "{$post_type}.robots";
|
||||
}
|
||||
|
||||
if ( isset( $fields['add_meta_box'] ) ) {
|
||||
$titles[ $prefix . 'add_meta_box' ] = $fields['add_meta_box'] ? 'on' : 'off';
|
||||
$updated[] = "{$post_type}.add_meta_box";
|
||||
}
|
||||
}
|
||||
|
||||
$this->save_and_reset( null, $titles, null );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Post Type SEO Settings Updated',
|
||||
[
|
||||
'post_types' => array_keys( $post_types ),
|
||||
'fields_updated' => $updated,
|
||||
],
|
||||
'rank_math_titles'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $updated,
|
||||
];
|
||||
}
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-sitemap-settings
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-sitemap-settings ability.
|
||||
*/
|
||||
class Set_Sitemap_Settings extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_sitemap' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-sitemap-settings',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set sitemap settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates XML sitemap settings: links per sitemap page, image inclusion, excluded post/term IDs, author archive inclusion, and per-post-type/per-taxonomy sitemap inclusion. All fields are optional — omitted fields keep their current stored value. Requires the sitemap module to be active; enable it first with set-module-status if needed.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'items_per_page' => [
|
||||
'type' => 'integer',
|
||||
'title' => esc_html__( 'Links Per Sitemap', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Max number of links on each sitemap page.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'include_images' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Images in Sitemaps', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Include reference to images from the post content in sitemaps.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'include_featured_image' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Include Featured Images', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Include the Featured Image too, even if it does not appear directly in the post content. Only takes effect when include_images is also enabled.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'exclude_posts' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Exclude Posts', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Comma-separated post IDs to exclude from the sitemap. Applies to all post types.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'exclude_terms' => [
|
||||
'type' => 'string',
|
||||
'title' => esc_html__( 'Exclude Terms', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Comma-separated term IDs to exclude from the sitemap. Applies to all taxonomies.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'authors_sitemap' => [
|
||||
'type' => 'boolean',
|
||||
'title' => esc_html__( 'Include Authors in Sitemap', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Include author archives in the XML sitemap.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'post_types' => [
|
||||
'type' => 'object',
|
||||
'title' => esc_html__( 'Post Types', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Map of post type slug to whether it should be included in the XML sitemap. Call get-settings first to see registered post type sitemap status.', 'seo-by-rank-math' ),
|
||||
'additionalProperties' => [ 'type' => 'boolean' ],
|
||||
],
|
||||
'taxonomies' => [
|
||||
'type' => 'object',
|
||||
'title' => esc_html__( 'Taxonomies', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Map of taxonomy slug to whether it should be included in the XML sitemap.', 'seo-by-rank-math' ),
|
||||
'additionalProperties' => [ 'type' => 'boolean' ],
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema( true, 'List of top-level field names that were updated.' ),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
if ( isset( $input['post_types'] ) ) {
|
||||
$accessible_post_types = $this->get_accessible_post_types();
|
||||
foreach ( array_keys( $input['post_types'] ) as $post_type ) {
|
||||
if ( ! in_array( $post_type, $accessible_post_types, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_post_type',
|
||||
'message' => sprintf(
|
||||
/* translators: post type slug */
|
||||
esc_html__( 'Unknown or inaccessible post type: %s', 'seo-by-rank-math' ),
|
||||
esc_html( $post_type )
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $input['taxonomies'] ) ) {
|
||||
$accessible_taxonomies = $this->get_accessible_taxonomies();
|
||||
foreach ( array_keys( $input['taxonomies'] ) as $taxonomy ) {
|
||||
if ( ! in_array( $taxonomy, $accessible_taxonomies, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_taxonomy',
|
||||
'message' => sprintf(
|
||||
/* translators: taxonomy slug */
|
||||
esc_html__( 'Unknown or inaccessible taxonomy: %s', 'seo-by-rank-math' ),
|
||||
esc_html( $taxonomy )
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sitemap = $this->get_sitemap_option();
|
||||
|
||||
if ( isset( $input['items_per_page'] ) ) {
|
||||
$sitemap['items_per_page'] = (string) absint( $input['items_per_page'] );
|
||||
}
|
||||
|
||||
$this->apply_toggle_fields(
|
||||
$sitemap,
|
||||
$input,
|
||||
[ 'include_images', 'include_featured_image', 'authors_sitemap' ]
|
||||
);
|
||||
|
||||
foreach ( [ 'exclude_posts', 'exclude_terms' ] as $field ) {
|
||||
if ( isset( $input[ $field ] ) ) {
|
||||
$sitemap[ $field ] = sanitize_text_field( $input[ $field ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $input['post_types'] ) ) {
|
||||
foreach ( $input['post_types'] as $post_type => $enabled ) {
|
||||
$sitemap[ "pt_{$post_type}_sitemap" ] = $enabled ? 'on' : 'off';
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $input['taxonomies'] ) ) {
|
||||
foreach ( $input['taxonomies'] as $taxonomy => $enabled ) {
|
||||
$sitemap[ "tax_{$taxonomy}_sitemap" ] = $enabled ? 'on' : 'off';
|
||||
}
|
||||
}
|
||||
|
||||
$this->save_and_reset( null, null, $sitemap );
|
||||
|
||||
/**
|
||||
* Fires after set-sitemap-settings saves its FREE fields, so PRO can persist
|
||||
* its own sitemap fields (news_sitemap_publication_name, video_sitemap_post_type,
|
||||
* hide_video_sitemap, local_sitemap) from the same input.
|
||||
*
|
||||
* @param array $input Raw ability input.
|
||||
*/
|
||||
do_action( 'rank_math/abilities/set_sitemap_settings/after_save', $input );
|
||||
|
||||
$keys = array_keys( $input );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Sitemap Settings Updated',
|
||||
[ 'fields_updated' => $keys ],
|
||||
'rank_math_sitemap'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $keys,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Current raw sitemap option. Extracted for testability.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_sitemap_option(): array {
|
||||
return (array) get_option( 'rank-math-options-sitemap', [] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Accessible taxonomy slugs. Extracted for testability.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
protected function get_accessible_taxonomies(): array {
|
||||
return array_keys( Helper::get_accessible_taxonomies() );
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
/**
|
||||
* Ability: rank-math/set-website-identity
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers and executes the rank-math/set-website-identity ability.
|
||||
*/
|
||||
class Set_Website_Identity extends Abstract_Ability {
|
||||
|
||||
/**
|
||||
* Allowed values for the knowledgegraph_type field.
|
||||
*/
|
||||
const KNOWLEDGEGRAPH_TYPES = [ 'person', 'company' ];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $category Ability category slug.
|
||||
* @param array $shared_meta Shared meta args.
|
||||
*/
|
||||
public function __construct( string $category, array $shared_meta ) {
|
||||
parent::__construct( $category, $shared_meta, 'rank_math_titles' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the ability with the WordPress Abilities API.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void {
|
||||
\wp_register_ability(
|
||||
'rank-math/set-website-identity',
|
||||
[
|
||||
'category' => $this->category,
|
||||
'label' => esc_html__( 'Set website identity', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__(
|
||||
'Updates the site\'s Knowledge Graph identity used by Rank Math for schema and social markup: knowledge graph type (person/company), website name, alternate name, knowledge graph name, organization description, business type, and site URL override. All fields are optional — omitted fields keep their current stored value.',
|
||||
'seo-by-rank-math'
|
||||
),
|
||||
'input_schema' => [
|
||||
'type' => 'object',
|
||||
'default' => [],
|
||||
'properties' => [
|
||||
'knowledgegraph_type' => [
|
||||
'type' => 'string',
|
||||
'enum' => self::KNOWLEDGEGRAPH_TYPES,
|
||||
'description' => esc_html__( 'Whether the site represents a person or a company/organization.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'website_name' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'The website name used in schema and social markup.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'website_alternate_name' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'An alternate name for the website.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'knowledgegraph_name' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'The person or organization name used in the Knowledge Graph.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'organization_description' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Description of the organization. Only used when knowledgegraph_type is "company".', 'seo-by-rank-math' ),
|
||||
],
|
||||
'local_business_type' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Schema.org business type slug (e.g. "Restaurant", "LegalService", "Organization"), spaces removed. Only used when knowledgegraph_type is "company". Call get-settings first or see the Local SEO business type list in WP Admin for valid values.', 'seo-by-rank-math' ),
|
||||
],
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'description' => esc_html__( 'Site URL override used in Knowledge Graph markup.', 'seo-by-rank-math' ),
|
||||
],
|
||||
],
|
||||
'additionalProperties' => false,
|
||||
],
|
||||
'output_schema' => $this->write_success_schema( true ),
|
||||
'permission_callback' => [ $this, 'check_permissions' ],
|
||||
'execute_callback' => [ $this, 'execute' ],
|
||||
'meta' => $this->build_meta( false ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the ability.
|
||||
*
|
||||
* @param array $input Ability input arguments.
|
||||
* @return array
|
||||
*/
|
||||
public function execute( array $input = [] ): array {
|
||||
if ( isset( $input['knowledgegraph_type'] ) && ! in_array( $input['knowledgegraph_type'], self::KNOWLEDGEGRAPH_TYPES, true ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_knowledgegraph_type',
|
||||
'message' => esc_html__( 'knowledgegraph_type must be either "person" or "company".', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
if ( isset( $input['local_business_type'] ) && ! array_key_exists( $input['local_business_type'], $this->get_business_types() ) ) {
|
||||
return [
|
||||
'error' => [
|
||||
'code' => 'invalid_local_business_type',
|
||||
'message' => esc_html__( 'local_business_type is not a recognized Schema.org business type slug.', 'seo-by-rank-math' ),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$titles = $this->get_titles_option();
|
||||
|
||||
foreach ( [ 'knowledgegraph_type', 'website_name', 'website_alternate_name', 'knowledgegraph_name', 'local_business_type' ] as $field ) {
|
||||
if ( isset( $input[ $field ] ) ) {
|
||||
$titles[ $field ] = sanitize_text_field( $input[ $field ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $input['url'] ) ) {
|
||||
$titles['url'] = esc_url_raw( $input['url'] );
|
||||
}
|
||||
|
||||
if ( isset( $input['organization_description'] ) ) {
|
||||
$titles['organization_description'] = wp_kses_post( $input['organization_description'] );
|
||||
}
|
||||
|
||||
$this->save_and_reset( null, $titles, null );
|
||||
|
||||
$keys = array_keys( $input );
|
||||
|
||||
rank_math()->tracking->track_ability_executed(
|
||||
'Website Identity Updated',
|
||||
[ 'fields_updated' => $keys ],
|
||||
'rank_math_titles'
|
||||
);
|
||||
|
||||
return [
|
||||
'saved' => true,
|
||||
'updated' => $keys,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Valid Schema.org business type slugs. Extracted for testability.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function get_business_types(): array {
|
||||
return Helper::choices_business_types();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Subscriber for Settings abilities.
|
||||
*
|
||||
* @since 1.0.277
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Abilities\Settings
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Abilities\Settings;
|
||||
|
||||
use RankMath\Traits\Hooker;
|
||||
use RankMath\Abilities\Subscriber_Interface;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Registers the Settings ability category and its abilities.
|
||||
*/
|
||||
class Subscriber implements Subscriber_Interface {
|
||||
|
||||
use Hooker;
|
||||
|
||||
/**
|
||||
* Ability category slug for Settings abilities.
|
||||
*/
|
||||
const CATEGORY_SLUG = 'rank-math-settings';
|
||||
|
||||
/**
|
||||
* 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_abilities' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Settings ability category.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_category(): void {
|
||||
\wp_register_ability_category(
|
||||
self::CATEGORY_SLUG,
|
||||
[
|
||||
'label' => esc_html__( 'Settings', 'seo-by-rank-math' ),
|
||||
'description' => esc_html__( 'Abilities to read and configure Rank Math plugin settings.', 'seo-by-rank-math' ),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register all Settings abilities.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_abilities(): void {
|
||||
$classes = [
|
||||
Get_Settings::class,
|
||||
Get_System_Status::class,
|
||||
Get_Llms_Txt::class,
|
||||
Get_Robots_Txt::class,
|
||||
Set_Website_Identity::class,
|
||||
Set_Global_Seo_Settings::class,
|
||||
Set_Homepage_Seo::class,
|
||||
Set_Link_Settings::class,
|
||||
Set_Module_Status::class,
|
||||
Set_Sitemap_Settings::class,
|
||||
Set_Plugin_Preferences::class,
|
||||
Set_Post_Type_Seo_Settings::class,
|
||||
Set_Breadcrumb_Settings::class,
|
||||
];
|
||||
|
||||
foreach ( $classes as $class ) {
|
||||
( new $class( self::CATEGORY_SLUG, $this->shared_meta ) )->register();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden.
|
||||
Reference in New Issue
Block a user