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.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 1.10.0 All 48 releases
thinkrank / includes / abilities / settings / class-update-robots-meta-settings.php

class-update-robots-meta-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-robots-meta-settings.php

160 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update robots meta 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 site-wide default robots meta settings.
20 *
21 * These flags form the base robots meta directives applied to the whole site,
22 * before any post-type or per-post overrides.
23 */
24 class Update_Robots_Meta_Settings extends Ability_Base {
25 /**
26 * Option name that stores the global robots meta settings.
27 */
28 private const OPTION = 'thinkrank_global_robot_meta_settings';
29
30 /**
31 * Default robots meta directives.
32 */
33 private const DEFAULTS = [
34 'index' => true,
35 'noindex' => false,
36 'nofollow' => false,
37 'noarchive' => false,
38 'noimageindex' => false,
39 'nosnippet' => false,
40 ];
41
42 /**
43 * Constructor.
44 */
45 public function __construct() {
46 $this->id = 'thinkrank/update-robots-meta-settings';
47 $this->label = __( 'Update ThinkRank Robots Meta Settings', 'thinkrank' );
48 $this->description = __( 'Update ThinkRank site-wide default robots meta directives (index, noindex, nofollow, noarchive, noimageindex, nosnippet). Read the current values with get-robots-meta-settings first.', 'thinkrank' );
49 }
50
51 /**
52 * {@inheritDoc}
53 *
54 * @return array<string, bool|float|string>
55 */
56 public function get_annotations() {
57 return [
58 'readonly' => false,
59 // Settings writes are recoverable: the matching get-* ability reads
60 // the previous value, so nothing is lost that cannot be put back.
61 // `destructive` is reserved for calls that lose data or reach
62 // outside the site, and marking routine configuration with it made
63 // MCP clients demand a human approval for every save — which users
64 // reported as a permission bug, because the client's refusal reads
65 // as "No approval received" (#675).
66 'destructive' => false,
67 'idempotent' => true,
68 'priority' => 2.0,
69 'openWorldHint' => false,
70 ];
71 }
72
73 /**
74 * {@inheritDoc}
75 *
76 * @return array<string, mixed>
77 */
78 public function get_input_schema() {
79 return [
80 'type' => 'object',
81 'additionalProperties' => false,
82 'properties' => [
83 'settings' => [
84 'type' => 'object',
85 'description' => __( 'Robots meta directives to update.', 'thinkrank' ),
86 'properties' => [
87 'index' => [ 'type' => 'boolean' ],
88 'noindex' => [ 'type' => 'boolean' ],
89 'nofollow' => [ 'type' => 'boolean' ],
90 'noarchive' => [ 'type' => 'boolean' ],
91 'noimageindex' => [ 'type' => 'boolean' ],
92 'nosnippet' => [ 'type' => 'boolean' ],
93 ],
94 ],
95 ],
96 'required' => [ 'settings' ],
97 ];
98 }
99
100 /**
101 * {@inheritDoc}
102 *
103 * @return array<string, mixed>
104 */
105 public function get_output_schema() {
106 return [
107 'type' => 'object',
108 'properties' => [
109 'success' => [ 'type' => 'boolean' ],
110 'message' => [ 'type' => 'string' ],
111 ],
112 ];
113 }
114
115 /**
116 * Execute ability.
117 *
118 * @param array<string, mixed> $input Ability input payload.
119 * @return array<string, mixed>|\WP_Error
120 */
121 public function execute( $input ) {
122 $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : [];
123
124 if ( empty( $settings ) ) {
125 return new \WP_Error(
126 'thinkrank_missing_robots_meta_settings_payload',
127 __( 'A robots meta settings payload is required.', 'thinkrank' ),
128 [ 'status' => 400 ]
129 );
130 }
131
132 $merged = wp_parse_args( (array) get_option( self::OPTION, [] ), self::DEFAULTS );
133 $found = false;
134
135 foreach ( array_keys( self::DEFAULTS ) as $key ) {
136 if ( array_key_exists( $key, $settings ) ) {
137 $merged[ $key ] = (bool) $settings[ $key ];
138 $found = true;
139 }
140 }
141
142 if ( ! $found ) {
143 return new \WP_Error(
144 'thinkrank_no_valid_robots_meta_setting_keys',
145 __( 'No valid robots meta setting keys were provided.', 'thinkrank' ),
146 [ 'status' => 400 ]
147 );
148 }
149
150 $result = update_option( self::OPTION, $merged );
151
152 return [
153 'success' => (bool) $result,
154 'message' => (bool) $result
155 ? __( 'Robots meta settings updated.', 'thinkrank' )
156 : __( 'Robots meta settings were unchanged.', 'thinkrank' ),
157 ];
158 }
159 }
160