PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.27.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.27.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-seo-analyzer-endpoint.php

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

125 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Site SEO Analyzer REST API Endpoint
4 *
5 * Exposes the site-wide SEO Analyzer:
6 * - GET /thinkrank/v1/seo-analyzer → last cached analysis (computes if empty)
7 * - POST /thinkrank/v1/seo-analyzer/run → force a fresh analysis (busts the cache)
8 *
9 * @package ThinkRank\API
10 * @since 1.18.0
11 */
12
13 declare(strict_types=1);
14
15 namespace ThinkRank\API;
16
17 use ThinkRank\SEO\SEO_Analyzer;
18 use WP_REST_Request;
19 use WP_REST_Response;
20
21 // Prevent direct access
22 if (!defined('ABSPATH')) {
23 exit;
24 }
25
26 /**
27 * SEO Analyzer Endpoint Class
28 *
29 * @since 1.18.0
30 */
31 class SEO_Analyzer_Endpoint {
32
33 /**
34 * API namespace.
35 */
36 private const NAMESPACE = 'thinkrank/v1';
37
38 /**
39 * Analyzer engine.
40 *
41 * @var SEO_Analyzer
42 */
43 private SEO_Analyzer $analyzer;
44
45 /**
46 * Constructor.
47 *
48 * @param SEO_Analyzer|null $analyzer Optional analyzer (injected in tests).
49 */
50 public function __construct(?SEO_Analyzer $analyzer = null) {
51 $this->analyzer = $analyzer ?? new SEO_Analyzer();
52 }
53
54 /**
55 * Register REST API routes.
56 *
57 * @return void
58 */
59 public function register_routes(): void {
60 register_rest_route(self::NAMESPACE, '/seo-analyzer', [
61 'methods' => 'GET',
62 'callback' => [$this, 'get_analysis'],
63 'permission_callback' => [$this, 'check_permissions'],
64 ]);
65
66 register_rest_route(self::NAMESPACE, '/seo-analyzer/run', [
67 'methods' => 'POST',
68 'callback' => [$this, 'run_analysis'],
69 'permission_callback' => [$this, 'check_permissions'],
70 ]);
71 }
72
73 /**
74 * Return the last cached analysis (computing it if none is cached).
75 *
76 * @param WP_REST_Request $request Request object.
77 * @return WP_REST_Response
78 */
79 public function get_analysis(WP_REST_Request $request): WP_REST_Response {
80 return new WP_REST_Response([
81 'success' => true,
82 'data' => $this->analyzer->run(false),
83 ], 200);
84 }
85
86 /**
87 * Force a fresh analysis, busting the cache.
88 *
89 * @param WP_REST_Request $request Request object.
90 * @return WP_REST_Response
91 */
92 public function run_analysis(WP_REST_Request $request): WP_REST_Response {
93 return new WP_REST_Response([
94 'success' => true,
95 'data' => $this->analyzer->run(true),
96 ], 200);
97 }
98
99 /**
100 * Only administrators may run the site-wide analyzer.
101 *
102 * @param WP_REST_Request $request Request object.
103 * @return bool|\WP_Error
104 */
105 public function check_permissions(WP_REST_Request $request) {
106 if (!is_user_logged_in()) {
107 return new \WP_Error(
108 'rest_forbidden',
109 __('You must be logged in to access this endpoint.', 'thinkrank'),
110 ['status' => 401]
111 );
112 }
113
114 if (!current_user_can('manage_options')) {
115 return new \WP_Error(
116 'rest_forbidden',
117 __('You do not have permission to run the site SEO analyzer.', 'thinkrank'),
118 ['status' => 403]
119 );
120 }
121
122 return true;
123 }
124 }
125