PluginProbe
Search Atlas SEO – OTTO AI SEO Automation for WordPress / 2.6.5
Search Atlas SEO – OTTO AI SEO Automation for WordPress v2.6.5
2.7.1 2.7.2 2.7.0 2.6.26 2.6.25 2.6.24 2.6.23 2.6.22 2.6.21 2.6.20 2.6.19 2.6.18 2.6.17 2.6.16 2.6.15 2.6.14 2.6.13 2.6.12 2.6.11 2.6.10 2.6.9 2.6.8 2.6.7 2.6.6 2.6.5 All 141 releases
metasync / wp-mcp-server / tools / class-mcp-tool-redirects.php

class-mcp-tool-redirects.php in Search Atlas SEO – OTTO AI SEO Automation for WordPress 2.6.5, at wp-mcp-server/tools/class-mcp-tool-redirects.php

355 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MCP Tool: Redirect Management
4 *
5 * Provides tools for managing WordPress redirects.
6 *
7 * @package MetaSync
8 * @subpackage MCP_Server/Tools
9 */
10
11 if (!defined('ABSPATH')) {
12 exit;
13 }
14
15 /**
16 * Create Redirect Tool
17 */
18 class MCP_Tool_Create_Redirect extends MCP_Tool_Base {
19
20 public function get_name() {
21 return 'wordpress_create_redirect';
22 }
23
24 public function get_description() {
25 return 'Create a 301 or 302 redirect from one URL to another';
26 }
27
28 public function get_input_schema() {
29 return [
30 'type' => 'object',
31 'properties' => [
32 'source' => [
33 'type' => 'string',
34 'description' => 'Source URL path (e.g., /old-page or https://example.com/old-page)'
35 ],
36 'destination' => [
37 'type' => 'string',
38 'description' => 'Destination URL (full URL or path)'
39 ],
40 'type' => [
41 'type' => 'integer',
42 'description' => 'Redirect HTTP code',
43 'enum' => [301, 302],
44 'default' => 301
45 ],
46 'description' => [
47 'type' => 'string',
48 'description' => 'Optional description of the redirect',
49 'default' => ''
50 ]
51 ],
52 'required' => ['source', 'destination']
53 ];
54 }
55
56 public function execute($params) {
57 $this->validate_params($params);
58 $this->require_capability('manage_options');
59
60 // Load database class
61 require_once plugin_dir_path(dirname(dirname(__FILE__))) . 'redirections/class-metasync-redirection-database.php';
62 $db = new Metasync_Redirection_Database();
63
64 $source = $this->sanitize_url($params['source']);
65 $destination = $this->sanitize_url($params['destination']);
66 $http_code = isset($params['type']) ? $this->sanitize_integer($params['type']) : 301;
67 $description = isset($params['description']) ? $this->sanitize_textarea($params['description']) : '';
68
69 // Parse source to get path
70 $source_path = parse_url($source, PHP_URL_PATH) ?: $source;
71
72 // Loop detection — refuse to create a redirect whose chain resolves back to the source
73 require_once plugin_dir_path(dirname(dirname(__FILE__))) . 'redirections/class-metasync-redirection.php';
74 $db_ref = $db;
75 $redirection_helper = new Metasync_Redirection($db_ref);
76 $loop_chain = [];
77 if ($redirection_helper->would_create_loop($source_path, $destination, $loop_chain)) {
78 return [
79 'error' => 'loop_detected',
80 'chain' => $loop_chain,
81 ];
82 }
83
84 // Create redirect
85 $result = $db->add([
86 'sources_from' => [$source_path],
87 'url_redirect_to' => $destination,
88 'http_code' => $http_code,
89 'description' => $description,
90 'status' => 'active',
91 'pattern_type' => 'exact'
92 ]);
93
94 if ($result === false) {
95 throw new Exception('Failed to create redirect');
96 }
97
98 return $this->success([
99 'redirect_id' => $result,
100 'source' => $source_path,
101 'destination' => $destination,
102 'type' => $http_code,
103 'status' => 'active'
104 ], 'Redirect created successfully');
105 }
106 }
107
108 /**
109 * List Redirects Tool
110 */
111 class MCP_Tool_List_Redirects extends MCP_Tool_Base {
112
113 public function get_name() {
114 return 'wordpress_list_redirects';
115 }
116
117 public function get_description() {
118 return 'List all redirects with optional status filter';
119 }
120
121 public function get_input_schema() {
122 return [
123 'type' => 'object',
124 'properties' => [
125 'status' => [
126 'type' => 'string',
127 'description' => 'Filter by status',
128 'enum' => ['active', 'inactive', 'all'],
129 'default' => 'active'
130 ],
131 'limit' => [
132 'type' => 'integer',
133 'description' => 'Maximum number of results',
134 'default' => 50,
135 'minimum' => 1,
136 'maximum' => 200
137 ]
138 ],
139 'required' => []
140 ];
141 }
142
143 public function execute($params) {
144 $this->validate_params($params);
145 $this->require_capability('manage_options');
146
147 require_once plugin_dir_path(dirname(dirname(__FILE__))) . 'redirections/class-metasync-redirection-database.php';
148 $db = new Metasync_Redirection_Database();
149
150 $status = isset($params['status']) ? $params['status'] : 'active';
151
152 // Get redirects
153 if ($status === 'active') {
154 $redirects = $db->getAllActiveRecords();
155 } else {
156 $redirects = $db->getAllRecords();
157 }
158
159 // Filter by status if needed
160 if ($status !== 'all' && $status !== 'active') {
161 $redirects = array_filter($redirects, function($r) use ($status) {
162 return $r->status === $status;
163 });
164 }
165
166 // Limit results
167 $limit = isset($params['limit']) ? $this->sanitize_integer($params['limit']) : 50;
168 $redirects = array_slice($redirects, 0, $limit);
169
170 // Format results (sources_from is stored serialized in DB)
171 $result = array_map(function($redirect) {
172 $sources_raw = $redirect->sources_from;
173 $source = null;
174 if ( ! empty( $sources_raw ) ) {
175 $sources = unserialize( $sources_raw, ['allowed_classes' => false] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
176 if ( is_array( $sources ) && ! empty( $sources ) ) {
177 $keys = array_keys( $sources );
178 $source = is_int( $keys[0] ) ? $sources[ $keys[0] ] : $keys[0];
179 }
180 }
181 return [
182 'id' => $redirect->id,
183 'source' => $source,
184 'destination' => $redirect->url_redirect_to,
185 'type' => $redirect->http_code,
186 'hits' => $redirect->hits_count,
187 'status' => $redirect->status,
188 'pattern_type' => $redirect->pattern_type ?? 'exact',
189 'description' => $redirect->description ?? '',
190 'created_at' => $redirect->created_at ?? null
191 ];
192 }, $redirects);
193
194 return $this->success([
195 'redirects' => $result,
196 'total' => count($result)
197 ]);
198 }
199 }
200
201 /**
202 * Delete Redirect Tool
203 */
204 class MCP_Tool_Delete_Redirect extends MCP_Tool_Base {
205
206 public function get_name() {
207 return 'wordpress_delete_redirect';
208 }
209
210 public function get_description() {
211 return 'Delete a redirect by ID';
212 }
213
214 public function get_input_schema() {
215 return [
216 'type' => 'object',
217 'properties' => [
218 'redirect_id' => [
219 'type' => 'integer',
220 'description' => 'Redirect ID to delete',
221 'minimum' => 1
222 ]
223 ],
224 'required' => ['redirect_id']
225 ];
226 }
227
228 public function execute($params) {
229 $this->validate_params($params);
230 $this->require_capability('manage_options');
231
232 require_once plugin_dir_path(dirname(dirname(__FILE__))) . 'redirections/class-metasync-redirection-database.php';
233 $db = new Metasync_Redirection_Database();
234
235 $redirect_id = $this->sanitize_integer($params['redirect_id']);
236
237 // Verify redirect exists
238 $redirect = $db->find($redirect_id);
239 if (!$redirect) {
240 throw new Exception(sprintf("Redirect not found: %d", absint($redirect_id)));
241 }
242
243 // Delete redirect
244 $result = $db->delete([$redirect_id]);
245
246 if ($result === false) {
247 throw new Exception('Failed to delete redirect');
248 }
249
250 return $this->success([
251 'redirect_id' => $redirect_id,
252 'deleted' => true
253 ], 'Redirect deleted successfully');
254 }
255 }
256
257 /**
258 * Update Redirect Tool
259 */
260 class MCP_Tool_Update_Redirect extends MCP_Tool_Base {
261
262 public function get_name() {
263 return 'wordpress_update_redirect';
264 }
265
266 public function get_description() {
267 return 'Update an existing redirect';
268 }
269
270 public function get_input_schema() {
271 return [
272 'type' => 'object',
273 'properties' => [
274 'redirect_id' => [
275 'type' => 'integer',
276 'description' => 'Redirect ID to update',
277 'minimum' => 1
278 ],
279 'destination' => [
280 'type' => 'string',
281 'description' => 'New destination URL (optional)'
282 ],
283 'type' => [
284 'type' => 'integer',
285 'description' => 'New redirect HTTP code (optional)',
286 'enum' => [301, 302]
287 ],
288 'status' => [
289 'type' => 'string',
290 'description' => 'New status (optional)',
291 'enum' => ['active', 'inactive']
292 ]
293 ],
294 'required' => ['redirect_id']
295 ];
296 }
297
298 public function execute($params) {
299 $this->validate_params($params);
300 $this->require_capability('manage_options');
301
302 require_once plugin_dir_path(dirname(dirname(__FILE__))) . 'redirections/class-metasync-redirection-database.php';
303 $db = new Metasync_Redirection_Database();
304
305 $redirect_id = $this->sanitize_integer($params['redirect_id']);
306
307 // Verify redirect exists
308 $redirect = $db->find($redirect_id);
309 if (!$redirect) {
310 throw new Exception(sprintf("Redirect not found: %d", absint($redirect_id)));
311 }
312
313 // Build update args
314 $update_args = ['updated_at' => current_time('mysql')];
315
316 if (isset($params['destination'])) {
317 $update_args['url_redirect_to'] = $this->sanitize_url($params['destination']);
318 }
319
320 if (isset($params['type'])) {
321 $update_args['http_code'] = $this->sanitize_integer($params['type']);
322 }
323
324 if (isset($params['status'])) {
325 $update_args['status'] = $this->sanitize_string($params['status']);
326 }
327
328 // Update redirect
329 $result = $db->update($update_args, $redirect_id);
330
331 if ($result === false) {
332 throw new Exception('Failed to update redirect');
333 }
334
335 // Get updated redirect (sources_from is stored serialized in DB)
336 $updated = $db->find($redirect_id);
337 $source = null;
338 if ( ! empty( $updated->sources_from ) ) {
339 $sources = unserialize( $updated->sources_from, ['allowed_classes' => false] ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
340 if ( is_array( $sources ) && ! empty( $sources ) ) {
341 $keys = array_keys( $sources );
342 $source = is_int( $keys[0] ) ? $sources[ $keys[0] ] : $keys[0];
343 }
344 }
345
346 return $this->success([
347 'redirect_id' => $redirect_id,
348 'source' => $source,
349 'destination' => $updated->url_redirect_to,
350 'type' => $updated->http_code,
351 'status' => $updated->status
352 ], 'Redirect updated successfully');
353 }
354 }
355