PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.7.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.7.0
2.8.0 2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 All 49 releases
thinkrank / includes / abilities / settings / class-update-instant-indexing-settings.php

class-update-instant-indexing-settings.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.7.0, at includes/abilities/settings/class-update-instant-indexing-settings.php

159 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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. Read the current values with get-instant-indexing-settings first.', '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 // Settings writes are recoverable: the matching get-* ability reads
57 // the previous value, so nothing is lost that cannot be put back.
58 // `destructive` is reserved for calls that lose data or reach
59 // outside the site, and marking routine configuration with it made
60 // MCP clients demand a human approval for every save — which users
61 // reported as a permission bug, because the client's refusal reads
62 // as "No approval received" (#675).
63 'destructive' => false,
64 'idempotent' => true,
65 'priority' => 2.0,
66 'openWorldHint' => false,
67 ];
68 }
69
70 /**
71 * {@inheritDoc}
72 *
73 * @return array<string, mixed>
74 */
75 public function get_input_schema() {
76 return [
77 'type' => 'object',
78 'additionalProperties' => false,
79 'properties' => [
80 'settings' => [
81 'type' => 'object',
82 'description' => __( 'Instant indexing settings to update.', 'thinkrank' ),
83 'properties' => [
84 'enabled' => [ 'type' => 'boolean' ],
85 'auto_submit_post_types' => [
86 'type' => 'array',
87 'items' => [ 'type' => 'string' ],
88 ],
89 ],
90 ],
91 ],
92 'required' => [ 'settings' ],
93 ];
94 }
95
96 /**
97 * {@inheritDoc}
98 *
99 * @return array<string, mixed>
100 */
101 public function get_output_schema() {
102 return [
103 'type' => 'object',
104 'properties' => [
105 'success' => [ 'type' => 'boolean' ],
106 'message' => [ 'type' => 'string' ],
107 ],
108 ];
109 }
110
111 /**
112 * Execute ability.
113 *
114 * @param array<string, mixed> $input Ability input payload.
115 * @return array<string, mixed>|\WP_Error
116 */
117 public function execute( $input ) {
118 $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : [];
119
120 if ( empty( $settings ) ) {
121 return new \WP_Error(
122 'thinkrank_missing_instant_indexing_settings_payload',
123 __( 'An instant indexing settings payload is required.', 'thinkrank' ),
124 [ 'status' => 400 ]
125 );
126 }
127
128 $merged = wp_parse_args( (array) get_option( self::OPTION, [] ), self::DEFAULTS );
129 $found = false;
130
131 if ( array_key_exists( 'enabled', $settings ) ) {
132 $merged['enabled'] = (bool) $settings['enabled'];
133 $found = true;
134 }
135
136 if ( array_key_exists( 'auto_submit_post_types', $settings ) && is_array( $settings['auto_submit_post_types'] ) ) {
137 $merged['auto_submit_post_types'] = array_values( array_map( 'sanitize_key', $settings['auto_submit_post_types'] ) );
138 $found = true;
139 }
140
141 if ( ! $found ) {
142 return new \WP_Error(
143 'thinkrank_no_valid_instant_indexing_setting_keys',
144 __( 'No valid instant indexing setting keys were provided.', 'thinkrank' ),
145 [ 'status' => 400 ]
146 );
147 }
148
149 $result = update_option( self::OPTION, $merged );
150
151 return [
152 'success' => (bool) $result,
153 'message' => (bool) $result
154 ? __( 'Instant indexing settings updated.', 'thinkrank' )
155 : __( 'Instant indexing settings were unchanged.', 'thinkrank' ),
156 ];
157 }
158 }
159