| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update instant indexing 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 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Updates ThinkRank instant indexing (IndexNow) settings. |
| 20 |
* |
| 21 |
* Only the enabled flag and the auto-submit post types are writable here. The |
| 22 |
* IndexNow API key is auto-generated and is never modified by this ability. |
| 23 |
*/ |
| 24 |
class Update_Instant_Indexing_Settings extends Ability_Base { |
| 25 |
/** |
| 26 |
* Option name that stores the instant indexing settings. |
| 27 |
*/ |
| 28 |
private const OPTION = 'thinkrank_instant_indexing_settings'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Default instant indexing settings. |
| 32 |
*/ |
| 33 |
private const DEFAULTS = [ |
| 34 |
'enabled' => false, |
| 35 |
'auto_submit_post_types' => [ 'post', 'page' ], |
| 36 |
'api_key' => '', |
| 37 |
]; |
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
$this->id = 'thinkrank/update-instant-indexing-settings'; |
| 44 |
$this->label = __( 'Update ThinkRank Instant Indexing Settings', 'thinkrank' ); |
| 45 |
$this->description = __( 'Update ThinkRank instant indexing (IndexNow) settings: the enabled flag and the auto-submit post types. The API key is auto-generated and cannot be set here.', 'thinkrank' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* {@inheritDoc} |
| 50 |
* |
| 51 |
* @return array<string, bool|float|string> |
| 52 |
*/ |
| 53 |
public function get_annotations() { |
| 54 |
return [ |
| 55 |
'readonly' => false, |
| 56 |
'destructive' => true, |
| 57 |
'idempotent' => true, |
| 58 |
'priority' => 2.0, |
| 59 |
'openWorldHint' => false, |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* {@inheritDoc} |
| 65 |
* |
| 66 |
* @return array<string, mixed> |
| 67 |
*/ |
| 68 |
public function get_input_schema() { |
| 69 |
return [ |
| 70 |
'type' => 'object', |
| 71 |
'additionalProperties' => false, |
| 72 |
'properties' => [ |
| 73 |
'settings' => [ |
| 74 |
'type' => 'object', |
| 75 |
'description' => __( 'Instant indexing settings to update.', 'thinkrank' ), |
| 76 |
'properties' => [ |
| 77 |
'enabled' => [ 'type' => 'boolean' ], |
| 78 |
'auto_submit_post_types' => [ |
| 79 |
'type' => 'array', |
| 80 |
'items' => [ 'type' => 'string' ], |
| 81 |
], |
| 82 |
], |
| 83 |
], |
| 84 |
], |
| 85 |
'required' => [ 'settings' ], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* {@inheritDoc} |
| 91 |
* |
| 92 |
* @return array<string, mixed> |
| 93 |
*/ |
| 94 |
public function get_output_schema() { |
| 95 |
return [ |
| 96 |
'type' => 'object', |
| 97 |
'properties' => [ |
| 98 |
'success' => [ 'type' => 'boolean' ], |
| 99 |
'message' => [ 'type' => 'string' ], |
| 100 |
], |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Execute ability. |
| 106 |
* |
| 107 |
* @param array<string, mixed> $input Ability input payload. |
| 108 |
* @return array<string, mixed>|\WP_Error |
| 109 |
*/ |
| 110 |
public function execute( $input ) { |
| 111 |
$settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : []; |
| 112 |
|
| 113 |
if ( empty( $settings ) ) { |
| 114 |
return new \WP_Error( |
| 115 |
'thinkrank_missing_instant_indexing_settings_payload', |
| 116 |
__( 'An instant indexing settings payload is required.', 'thinkrank' ), |
| 117 |
[ 'status' => 400 ] |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
$merged = wp_parse_args( (array) get_option( self::OPTION, [] ), self::DEFAULTS ); |
| 122 |
$found = false; |
| 123 |
|
| 124 |
if ( array_key_exists( 'enabled', $settings ) ) { |
| 125 |
$merged['enabled'] = (bool) $settings['enabled']; |
| 126 |
$found = true; |
| 127 |
} |
| 128 |
|
| 129 |
if ( array_key_exists( 'auto_submit_post_types', $settings ) && is_array( $settings['auto_submit_post_types'] ) ) { |
| 130 |
$merged['auto_submit_post_types'] = array_values( array_map( 'sanitize_key', $settings['auto_submit_post_types'] ) ); |
| 131 |
$found = true; |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! $found ) { |
| 135 |
return new \WP_Error( |
| 136 |
'thinkrank_no_valid_instant_indexing_setting_keys', |
| 137 |
__( 'No valid instant indexing setting keys were provided.', 'thinkrank' ), |
| 138 |
[ 'status' => 400 ] |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
$result = update_option( self::OPTION, $merged ); |
| 143 |
|
| 144 |
return [ |
| 145 |
'success' => (bool) $result, |
| 146 |
'message' => (bool) $result |
| 147 |
? __( 'Instant indexing settings updated.', 'thinkrank' ) |
| 148 |
: __( 'Instant indexing settings were unchanged.', 'thinkrank' ), |
| 149 |
]; |
| 150 |
} |
| 151 |
} |
| 152 |
|