| @@ -67,8 +67,57 @@ | ||
| 67 | 67 | 'methods' => 'POST', |
| 68 | 68 | 'callback' => [$this, 'run_analysis'], |
| 69 | 69 | 'permission_callback' => [$this, 'check_permissions'], |
| 70 | 70 | ]); |
| 71 | + | |
| 72 | + register_rest_route(self::NAMESPACE, '/seo-analyzer/fix', [ | |
| 73 | + 'methods' => 'POST', | |
| 74 | + 'callback' => [$this, 'apply_fix'], | |
| 75 | + 'permission_callback' => [$this, 'check_permissions'], | |
| 76 | + 'args' => [ | |
| 77 | + 'check_id' => [ | |
| 78 | + 'required' => true, | |
| 79 | + 'type' => 'string', | |
| 80 | + 'sanitize_callback' => 'sanitize_key', | |
| 81 | + ], | |
| 82 | + ], | |
| 83 | + ]); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Apply a one-click fix, then re-run the analysis so the caller gets the | |
| 88 | + * updated score in the same round trip. | |
| 89 | + * | |
| 90 | + * Re-running is the point: a fix the user cannot see land is | |
| 91 | + * indistinguishable from one that silently failed. | |
| 92 | + * | |
| 93 | + * @since 1.28.0 | |
| 94 | + * | |
| 95 | + * @param WP_REST_Request $request Request object. | |
| 96 | + * @return WP_REST_Response | |
| 97 | + */ | |
| 98 | + public function apply_fix(WP_REST_Request $request): WP_REST_Response { | |
| 99 | + $check_id = (string) $request->get_param('check_id'); | |
| 100 | + | |
| 101 | + try { | |
| 102 | + $outcome = (new \ThinkRank\SEO\SEO_Analyzer_Fixer())->fix($check_id); | |
| 103 | + } catch (\Throwable $e) { | |
| 104 | + return new WP_REST_Response([ | |
| 105 | + 'success' => false, | |
| 106 | + 'message' => $e->getMessage(), | |
| 107 | + ], 400); | |
| 108 | + } | |
| 109 | + | |
| 110 | + return new WP_REST_Response([ | |
| 111 | + 'success' => true, | |
| 112 | + 'message' => $outcome['message'], | |
| 113 | + 'data' => [ | |
| 114 | + 'check_id' => $check_id, | |
| 115 | + 'fixed' => (bool) $outcome['fixed'], | |
| 116 | + 'result' => $outcome['data'], | |
| 117 | + 'analysis' => $this->analyzer->run(true), | |
| 118 | + ], | |
| 119 | + ], 200); | |
| 71 | 120 | } |
| 72 | 121 | |
| 73 | 122 | /** |
| 74 | 123 | * Return the last cached analysis (computing it if none is cached). |