| 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 |
* Constructor. |
| 27 |
*/ |
| 28 |
public function __construct() { |
| 29 |
$this->id = 'thinkrank/update-sitemap-settings'; |
| 30 |
$this->label = __( 'Update ThinkRank Sitemap Settings', 'thinkrank' ); |
| 31 |
$this->description = __( 'Update ThinkRank XML sitemap settings: per-type inclusion toggles, exclusion lists, and the index/splitting, styling, filename and search-engine ping options. Read the current values with get-sitemap-settings first; only the keys you pass are changed.', 'thinkrank' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* {@inheritDoc} |
| 36 |
* |
| 37 |
* @return array<string, bool|float|string> |
| 38 |
*/ |
| 39 |
public function get_annotations() { |
| 40 |
return [ |
| 41 |
'readonly' => false, |
| 42 |
// Settings writes are recoverable: the matching get-* ability reads |
| 43 |
// the previous value, so nothing is lost that cannot be put back. |
| 44 |
// `destructive` is reserved for calls that lose data or reach |
| 45 |
// outside the site, and marking routine configuration with it made |
| 46 |
// MCP clients demand a human approval for every save — which users |
| 47 |
// reported as a permission bug, because the client's refusal reads |
| 48 |
// as "No approval received" (#675). |
| 49 |
'destructive' => false, |
| 50 |
'idempotent' => true, |
| 51 |
'priority' => 2.0, |
| 52 |
'openWorldHint' => false, |
| 53 |
]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritDoc} |
| 58 |
* |
| 59 |
* @return array<string, mixed> |
| 60 |
*/ |
| 61 |
public function get_input_schema() { |
| 62 |
return [ |
| 63 |
'type' => 'object', |
| 64 |
'additionalProperties' => false, |
| 65 |
'properties' => [ |
| 66 |
'settings' => [ |
| 67 |
'type' => 'object', |
| 68 |
'description' => __( 'Sitemap settings to update.', 'thinkrank' ), |
| 69 |
'properties' => Settings_Key_Map::sitemap(), |
| 70 |
], |
| 71 |
], |
| 72 |
'required' => [ 'settings' ], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* {@inheritDoc} |
| 78 |
* |
| 79 |
* @return array<string, mixed> |
| 80 |
*/ |
| 81 |
public function get_output_schema() { |
| 82 |
return [ |
| 83 |
'type' => 'object', |
| 84 |
'properties' => [ |
| 85 |
'success' => [ 'type' => 'boolean' ], |
| 86 |
'message' => [ 'type' => 'string' ], |
| 87 |
], |
| 88 |
]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Execute ability. |
| 93 |
* |
| 94 |
* @param array<string, mixed> $input Ability input payload. |
| 95 |
* @return array<string, mixed>|\WP_Error |
| 96 |
*/ |
| 97 |
public function execute( $input ) { |
| 98 |
$settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : []; |
| 99 |
|
| 100 |
if ( empty( $settings ) ) { |
| 101 |
return new \WP_Error( |
| 102 |
'thinkrank_missing_sitemap_settings_payload', |
| 103 |
__( 'A sitemap settings payload is required.', 'thinkrank' ), |
| 104 |
[ 'status' => 400 ] |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
$gen = new Sitemap_Generator(); |
| 109 |
$merged = $gen->get_settings( 'site', null ); |
| 110 |
$patch = Settings_Key_Map::coerce( Settings_Key_Map::sitemap(), $settings ); |
| 111 |
$merged = array_merge( $merged, $patch ); |
| 112 |
|
| 113 |
if ( empty( $patch ) ) { |
| 114 |
return new \WP_Error( |
| 115 |
'thinkrank_no_valid_sitemap_setting_keys', |
| 116 |
__( 'No valid sitemap setting keys were provided.', 'thinkrank' ), |
| 117 |
[ 'status' => 400 ] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
$result = $gen->save_settings( 'site', null, $merged ); |
| 122 |
|
| 123 |
// Rebuild the served sitemap so the change takes effect instead of going |
| 124 |
// stale until an unrelated content edit (debounced). |
| 125 |
if ( $result ) { |
| 126 |
$gen->schedule_regeneration(); |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
'success' => (bool) $result, |
| 131 |
'message' => (bool) $result |
| 132 |
? __( 'Sitemap settings updated.', 'thinkrank' ) |
| 133 |
: __( 'Failed to update sitemap settings.', 'thinkrank' ), |
| 134 |
]; |
| 135 |
} |
| 136 |
} |
| 137 |
|