| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update sitemap settings ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Settings |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Settings; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Sitemap_Generator; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Updates ThinkRank XML sitemap settings. |
| 21 |
* |
| 22 |
* Sitemap settings are persisted through the Sitemap_Generator SEO manager. |
| 23 |
*/ |
| 24 |
class Update_Sitemap_Settings extends Ability_Base { |
| 25 |
/** |
| 26 |
* Boolean-typed sitemap keys. |
| 27 |
*/ |
| 28 |
private const BOOL_KEYS = [ |
| 29 |
'enabled', |
| 30 |
'include_images', |
| 31 |
'include_posts', |
| 32 |
'include_pages', |
| 33 |
'include_categories', |
| 34 |
'include_tags', |
| 35 |
]; |
| 36 |
|
| 37 |
/** |
| 38 |
* String-typed sitemap keys. |
| 39 |
*/ |
| 40 |
private const STRING_KEYS = [ |
| 41 |
'exclude_posts', |
| 42 |
'exclude_terms', |
| 43 |
]; |
| 44 |
|
| 45 |
/** |
| 46 |
* Constructor. |
| 47 |
*/ |
| 48 |
public function __construct() { |
| 49 |
$this->id = 'thinkrank/update-sitemap-settings'; |
| 50 |
$this->label = __( 'Update ThinkRank Sitemap Settings', 'thinkrank' ); |
| 51 |
$this->description = __( 'Update ThinkRank XML sitemap settings, including per-type inclusion toggles and exclusion lists.', 'thinkrank' ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* {@inheritDoc} |
| 56 |
* |
| 57 |
* @return array<string, bool|float|string> |
| 58 |
*/ |
| 59 |
public function get_annotations() { |
| 60 |
return [ |
| 61 |
'readonly' => false, |
| 62 |
'destructive' => true, |
| 63 |
'idempotent' => true, |
| 64 |
'priority' => 2.0, |
| 65 |
'openWorldHint' => false, |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* {@inheritDoc} |
| 71 |
* |
| 72 |
* @return array<string, mixed> |
| 73 |
*/ |
| 74 |
public function get_input_schema() { |
| 75 |
return [ |
| 76 |
'type' => 'object', |
| 77 |
'additionalProperties' => false, |
| 78 |
'properties' => [ |
| 79 |
'settings' => [ |
| 80 |
'type' => 'object', |
| 81 |
'description' => __( 'Sitemap settings to update.', 'thinkrank' ), |
| 82 |
'properties' => [ |
| 83 |
'enabled' => [ 'type' => 'boolean' ], |
| 84 |
'include_images' => [ 'type' => 'boolean' ], |
| 85 |
'include_posts' => [ 'type' => 'boolean' ], |
| 86 |
'include_pages' => [ 'type' => 'boolean' ], |
| 87 |
'include_categories' => [ 'type' => 'boolean' ], |
| 88 |
'include_tags' => [ 'type' => 'boolean' ], |
| 89 |
'exclude_posts' => [ 'type' => 'string' ], |
| 90 |
'exclude_terms' => [ 'type' => 'string' ], |
| 91 |
], |
| 92 |
], |
| 93 |
], |
| 94 |
'required' => [ 'settings' ], |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* {@inheritDoc} |
| 100 |
* |
| 101 |
* @return array<string, mixed> |
| 102 |
*/ |
| 103 |
public function get_output_schema() { |
| 104 |
return [ |
| 105 |
'type' => 'object', |
| 106 |
'properties' => [ |
| 107 |
'success' => [ 'type' => 'boolean' ], |
| 108 |
'message' => [ 'type' => 'string' ], |
| 109 |
], |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Execute ability. |
| 115 |
* |
| 116 |
* @param array<string, mixed> $input Ability input payload. |
| 117 |
* @return array<string, mixed>|\WP_Error |
| 118 |
*/ |
| 119 |
public function execute( $input ) { |
| 120 |
$settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : []; |
| 121 |
|
| 122 |
if ( empty( $settings ) ) { |
| 123 |
return new \WP_Error( |
| 124 |
'thinkrank_missing_sitemap_settings_payload', |
| 125 |
__( 'A sitemap settings payload is required.', 'thinkrank' ), |
| 126 |
[ 'status' => 400 ] |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
$gen = new Sitemap_Generator(); |
| 131 |
$merged = $gen->get_settings( 'site', null ); |
| 132 |
$found = false; |
| 133 |
|
| 134 |
foreach ( self::BOOL_KEYS as $key ) { |
| 135 |
if ( array_key_exists( $key, $settings ) ) { |
| 136 |
$merged[ $key ] = (bool) $settings[ $key ]; |
| 137 |
$found = true; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
foreach ( self::STRING_KEYS as $key ) { |
| 142 |
if ( array_key_exists( $key, $settings ) ) { |
| 143 |
$merged[ $key ] = sanitize_text_field( (string) $settings[ $key ] ); |
| 144 |
$found = true; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! $found ) { |
| 149 |
return new \WP_Error( |
| 150 |
'thinkrank_no_valid_sitemap_setting_keys', |
| 151 |
__( 'No valid sitemap setting keys were provided.', 'thinkrank' ), |
| 152 |
[ 'status' => 400 ] |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
$result = $gen->save_settings( 'site', null, $merged ); |
| 157 |
|
| 158 |
// Rebuild the served sitemap so the change takes effect instead of going |
| 159 |
// stale until an unrelated content edit (debounced). |
| 160 |
if ( $result ) { |
| 161 |
$gen->schedule_regeneration(); |
| 162 |
} |
| 163 |
|
| 164 |
return [ |
| 165 |
'success' => (bool) $result, |
| 166 |
'message' => (bool) $result |
| 167 |
? __( 'Sitemap settings updated.', 'thinkrank' ) |
| 168 |
: __( 'Failed to update sitemap settings.', 'thinkrank' ), |
| 169 |
]; |
| 170 |
} |
| 171 |
} |
| 172 |
|