PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.10.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.10.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 / api / class-global-robot-meta-endpoint.php

class-global-robot-meta-endpoint.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.10.0, at includes/api/class-global-robot-meta-endpoint.php

182 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Global Robot Meta API Endpoints Class
4 *
5 * REST API endpoints for global robots meta tags settings.
6 *
7 * @package ThinkRank
8 * @subpackage API
9 * @since 1.0.0
10 */
11
12 declare(strict_types=1);
13
14 namespace ThinkRank\API;
15
16 use WP_REST_Controller;
17 use WP_REST_Request;
18 use WP_REST_Response;
19 use WP_Error;
20
21 /**
22 * Global Robot Meta API Endpoints Class
23 *
24 * @since 1.0.0
25 */
26 class Global_Robot_Meta_Endpoint extends WP_REST_Controller {
27
28 /**
29 * API namespace
30 *
31 * @since 1.0.0
32 * @var string
33 */
34 protected $namespace = 'thinkrank/v1';
35
36 /**
37 * API resource base
38 *
39 * @since 1.0.0
40 * @var string
41 */
42 protected $rest_base = 'global-robot-meta';
43
44 /**
45 * Option name
46 */
47 private const OPTION_NAME = 'thinkrank_global_robot_meta_settings';
48
49 /**
50 * Register API routes
51 *
52 * @since 1.0.0
53 */
54 public function register_routes(): void {
55 register_rest_route(
56 $this->namespace,
57 '/' . $this->rest_base . '/settings',
58 [
59 [
60 'methods' => 'GET',
61 'callback' => [$this, 'get_settings'],
62 'permission_callback' => [$this, 'check_read_permissions']
63 ],
64 [
65 'methods' => 'POST',
66 'callback' => [$this, 'update_settings'],
67 'permission_callback' => [$this, 'check_manage_permissions'],
68 'args' => $this->get_settings_args()
69 ]
70 ]
71 );
72 }
73
74 /**
75 * Get settings
76 *
77 * @param WP_REST_Request $request Request object
78 * @return WP_REST_Response Response object
79 */
80 public function get_settings(WP_REST_Request $request): WP_REST_Response {
81 $settings = get_option(self::OPTION_NAME, $this->get_default_settings());
82
83 return new WP_REST_Response([
84 'success' => true,
85 'settings' => $settings,
86 'message' => 'Settings retrieved successfully'
87 ], 200);
88 }
89
90 /**
91 * Update settings
92 *
93 * @param WP_REST_Request $request Request object
94 * @return WP_REST_Response|WP_Error Response object or error
95 */
96 public function update_settings(WP_REST_Request $request) {
97 $settings = $request->get_param('settings');
98
99 if (empty($settings) || !is_array($settings)) {
100 return new WP_Error(
101 'invalid_settings',
102 'Settings must be provided as an array',
103 ['status' => 400]
104 );
105 }
106
107 // Sanitize settings
108 $sanitized_settings = [
109 'index' => isset($settings['index']) ? (bool) $settings['index'] : true,
110 'noindex' => isset($settings['noindex']) ? (bool) $settings['noindex'] : false,
111 'nofollow' => isset($settings['nofollow']) ? (bool) $settings['nofollow'] : false,
112 'noarchive' => isset($settings['noarchive']) ? (bool) $settings['noarchive'] : false,
113 'noimageindex' => isset($settings['noimageindex']) ? (bool) $settings['noimageindex'] : false,
114 'nosnippet' => isset($settings['nosnippet']) ? (bool) $settings['nosnippet'] : false,
115 ];
116
117 update_option(self::OPTION_NAME, $sanitized_settings);
118
119 return new WP_REST_Response([
120 'success' => true,
121 'settings' => $sanitized_settings,
122 'message' => 'Settings saved successfully'
123 ], 200);
124 }
125
126 /**
127 * Get default settings
128 *
129 * @return array
130 */
131 private function get_default_settings(): array {
132 return [
133 'index' => true,
134 'noindex' => false,
135 'nofollow' => false,
136 'noarchive' => false,
137 'noimageindex' => false,
138 'nosnippet' => false,
139 ];
140 }
141
142 /**
143 * Get settings args for validation
144 *
145 * @return array
146 */
147 private function get_settings_args(): array {
148 return [
149 'settings' => [
150 'required' => true,
151 'type' => 'object',
152 'properties' => [
153 'index' => ['type' => 'boolean'],
154 'noindex' => ['type' => 'boolean'],
155 'nofollow' => ['type' => 'boolean'],
156 'noarchive' => ['type' => 'boolean'],
157 'noimageindex' => ['type' => 'boolean'],
158 'nosnippet' => ['type' => 'boolean'],
159 ]
160 ]
161 ];
162 }
163
164 /**
165 * Check read permissions
166 *
167 * @return bool
168 */
169 public function check_read_permissions(): bool {
170 return current_user_can('edit_posts');
171 }
172
173 /**
174 * Check manage permissions
175 *
176 * @return bool
177 */
178 public function check_manage_permissions(): bool {
179 return current_user_can('manage_options');
180 }
181 }
182