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

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

420 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Performance API Endpoint
4 *
5 * REST API endpoints for Core Web Vitals monitoring and performance insights.
6 * Connects the frontend Performance tab to the existing Performance Monitoring Manager.
7 *
8 * @package ThinkRank
9 * @subpackage API
10 * @since 1.0.0
11 */
12
13 namespace ThinkRank\API;
14
15 use ThinkRank\SEO\Performance_Monitoring_Manager;
16 use ThinkRank\SEO\Performance_Data_Collector;
17 use ThinkRank\API\Traits\API_Cache;
18 use WP_REST_Controller;
19 use WP_REST_Server;
20 use WP_REST_Request;
21 use WP_REST_Response;
22 use WP_Error;
23
24 // Load API Cache trait
25 require_once THINKRANK_PLUGIN_DIR . 'includes/api/traits/trait-api-cache.php';
26
27 /**
28 * Performance API Endpoint Class
29 *
30 * @since 1.0.0
31 */
32 class Performance_Endpoint extends WP_REST_Controller {
33
34 use API_Cache;
35
36 /**
37 * REST API namespace
38 *
39 * @since 1.0.0
40 * @var string
41 */
42 protected $namespace = 'thinkrank/v1';
43
44 /**
45 * REST API base
46 *
47 * @since 1.0.0
48 * @var string
49 */
50 protected $rest_base = 'performance';
51
52 /**
53 * Performance Monitoring Manager instance
54 *
55 * @since 1.0.0
56 * @var Performance_Monitoring_Manager
57 */
58 private Performance_Monitoring_Manager $performance_manager;
59
60 /**
61 * Performance Data Collector instance
62 *
63 * @since 1.0.0
64 * @var Performance_Data_Collector
65 */
66 private Performance_Data_Collector $data_collector;
67
68 /**
69 * Constructor
70 *
71 * @since 1.0.0
72 */
73 public function __construct() {
74 $this->performance_manager = new Performance_Monitoring_Manager();
75 $this->data_collector = new Performance_Data_Collector();
76
77 // Configure caching for performance endpoints
78 $this->set_cache_prefix('thinkrank_performance_');
79 $this->set_cache_duration(300); // 5 minutes for performance data
80 }
81
82 /**
83 * Register REST API routes
84 *
85 * @since 1.0.0
86 */
87 public function register_routes() {
88 // Monitor endpoint - Get current performance data
89 register_rest_route(
90 $this->namespace,
91 '/' . $this->rest_base . '/monitor',
92 [
93 [
94 'methods' => WP_REST_Server::READABLE,
95 'callback' => [$this, 'get_performance_data'],
96 'permission_callback' => [$this, 'check_read_permissions']
97 ]
98 ]
99 );
100
101
102
103 // Recommendations endpoint
104 register_rest_route(
105 $this->namespace,
106 '/' . $this->rest_base . '/recommendations',
107 [
108 [
109 'methods' => WP_REST_Server::READABLE,
110 'callback' => [$this, 'get_recommendations'],
111 'permission_callback' => [$this, 'check_read_permissions']
112 ]
113 ]
114 );
115
116 // Historical data endpoint
117 register_rest_route(
118 $this->namespace,
119 '/' . $this->rest_base . '/history',
120 [
121 [
122 'methods' => WP_REST_Server::READABLE,
123 'callback' => [$this, 'get_historical_data'],
124 'permission_callback' => [$this, 'check_read_permissions'],
125 'args' => $this->get_historical_data_args()
126 ]
127 ]
128 );
129
130 // Opportunities endpoint
131 register_rest_route(
132 $this->namespace,
133 '/' . $this->rest_base . '/opportunities',
134 [
135 [
136 'methods' => WP_REST_Server::READABLE,
137 'callback' => [$this, 'get_opportunities'],
138 'permission_callback' => [$this, 'check_read_permissions']
139 ]
140 ]
141 );
142
143 // Diagnostics endpoint
144 register_rest_route(
145 $this->namespace,
146 '/' . $this->rest_base . '/diagnostics',
147 [
148 [
149 'methods' => WP_REST_Server::READABLE,
150 'callback' => [$this, 'get_diagnostics'],
151 'permission_callback' => [$this, 'check_read_permissions']
152 ]
153 ]
154 );
155
156 // Data collection endpoint
157 register_rest_route(
158 $this->namespace,
159 '/' . $this->rest_base . '/collect',
160 [
161 [
162 'methods' => WP_REST_Server::CREATABLE,
163 'callback' => [$this, 'collect_performance_data'],
164 'permission_callback' => [$this, 'check_manage_permissions']
165 ]
166 ]
167 );
168
169
170 }
171
172 /**
173 * Get comprehensive performance data
174 *
175 * @since 1.0.0
176 *
177 * @param WP_REST_Request $request Request object
178 * @return WP_REST_Response|WP_Error Response object or error
179 */
180 public function get_performance_data(WP_REST_Request $request) {
181 try {
182 // Use cached response wrapper for performance
183 $response_data = $this->cached_response(
184 'performance_data',
185 function() {
186 // Get Core Web Vitals
187 $core_web_vitals = $this->performance_manager->get_core_web_vitals();
188
189 // Get performance score
190 $performance_score = $this->performance_manager->get_performance_score();
191
192 // Get performance grade
193 $performance_grade = $this->performance_manager->get_performance_grade($performance_score);
194
195 // Get SEO performance correlation
196 $seo_correlation = $this->performance_manager->get_seo_performance_correlation();
197
198 return [
199 'success' => true,
200 'data' => [
201 'core_web_vitals' => $core_web_vitals,
202 'performance_score' => $performance_score,
203 'performance_grade' => $performance_grade,
204 'seo_correlation' => $seo_correlation,
205 'last_updated' => current_time('mysql'),
206 'status' => 'success'
207 ],
208 'message' => __('Performance data retrieved successfully', 'thinkrank')
209 ];
210 },
211 [], // No specific parameters for cache key
212 null, // Use default cache duration
213 get_current_user_id()
214 );
215
216 return new WP_REST_Response($response_data, 200);
217
218 } catch (\Exception $e) {
219 return new WP_Error(
220 'performance_data_failed',
221 'Failed to retrieve performance data: ' . $e->getMessage(),
222 ['status' => 500]
223 );
224 }
225 }
226
227
228
229
230
231 /**
232 * Get performance recommendations
233 *
234 * @since 1.0.0
235 *
236 * @param WP_REST_Request $request Request object
237 * @return WP_REST_Response|WP_Error Response object or error
238 */
239 public function get_recommendations(WP_REST_Request $request) {
240 try {
241 $recommendations = $this->performance_manager->get_performance_recommendations();
242
243 return new WP_REST_Response([
244 'success' => true,
245 'data' => $recommendations,
246 'message' => __('Performance recommendations retrieved successfully', 'thinkrank')
247 ], 200);
248
249 } catch (\Exception $e) {
250 return new WP_Error(
251 'recommendations_failed',
252 'Failed to retrieve recommendations: ' . $e->getMessage(),
253 ['status' => 500]
254 );
255 }
256 }
257
258 /**
259 * Get historical performance data
260 *
261 * @since 1.0.0
262 *
263 * @param WP_REST_Request $request Request object
264 * @return WP_REST_Response|WP_Error Response object or error
265 */
266 public function get_historical_data(WP_REST_Request $request) {
267 try {
268 $days = $request->get_param('days') ?? 30;
269 $metric = $request->get_param('metric') ?? 'all';
270
271 $historical_data = $this->performance_manager->get_historical_data($days, $metric);
272
273 return new WP_REST_Response([
274 'success' => true,
275 'data' => $historical_data,
276 'message' => __('Historical data retrieved successfully', 'thinkrank')
277 ], 200);
278
279 } catch (\Exception $e) {
280 return new WP_Error(
281 'historical_data_failed',
282 'Failed to retrieve historical data: ' . $e->getMessage(),
283 ['status' => 500]
284 );
285 }
286 }
287
288 /**
289 * Get performance opportunities
290 *
291 * @since 1.0.0
292 *
293 * @param WP_REST_Request $request Request object
294 * @return WP_REST_Response|WP_Error Response object or error
295 */
296 public function get_opportunities(WP_REST_Request $request) {
297 try {
298 $opportunities = $this->performance_manager->get_performance_opportunities();
299
300 return new WP_REST_Response([
301 'success' => true,
302 'data' => $opportunities,
303 'message' => __('Performance opportunities retrieved successfully', 'thinkrank')
304 ], 200);
305
306 } catch (\Exception $e) {
307 return new WP_Error(
308 'opportunities_failed',
309 'Failed to retrieve performance opportunities: ' . $e->getMessage(),
310 ['status' => 500]
311 );
312 }
313 }
314
315 /**
316 * Get performance diagnostics
317 *
318 * @since 1.0.0
319 *
320 * @param WP_REST_Request $request Request object
321 * @return WP_REST_Response|WP_Error Response object or error
322 */
323 public function get_diagnostics(WP_REST_Request $request) {
324 try {
325 $diagnostics = $this->performance_manager->get_performance_diagnostics();
326
327 return new WP_REST_Response([
328 'success' => true,
329 'data' => $diagnostics,
330 'message' => __('Performance diagnostics retrieved successfully', 'thinkrank')
331 ], 200);
332
333 } catch (\Exception $e) {
334 return new WP_Error(
335 'diagnostics_failed',
336 'Failed to retrieve performance diagnostics: ' . $e->getMessage(),
337 ['status' => 500]
338 );
339 }
340 }
341
342 /**
343 * Manually collect performance data
344 *
345 * @since 1.0.0
346 *
347 * @param WP_REST_Request $request Request object
348 * @return WP_REST_Response|WP_Error Response object or error
349 */
350 public function collect_performance_data(WP_REST_Request $request) {
351 try {
352 $results = $this->data_collector->manual_collect();
353
354 return new WP_REST_Response([
355 'success' => $results['success'],
356 'data' => $results,
357 'message' => $results['message']
358 ], $results['success'] ? 200 : 500);
359
360 } catch (\Exception $e) {
361 return new WP_Error(
362 'data_collection_failed',
363 'Failed to collect performance data: ' . $e->getMessage(),
364 ['status' => 500]
365 );
366 }
367 }
368
369
370
371 /**
372 * Check read permissions
373 *
374 * @since 1.0.0
375 *
376 * @return bool True if user can read
377 */
378 public function check_read_permissions(): bool {
379 return current_user_can('read');
380 }
381
382 /**
383 * Check manage permissions
384 *
385 * @since 1.0.0
386 *
387 * @return bool True if user can manage options
388 */
389 public function check_manage_permissions(): bool {
390 return current_user_can('manage_options');
391 }
392
393 /**
394 * Get arguments for historical data endpoint
395 *
396 * @since 1.0.0
397 *
398 * @return array Arguments array
399 */
400 private function get_historical_data_args(): array {
401 return [
402 'days' => [
403 'required' => false,
404 'type' => 'integer',
405 'default' => 30,
406 'minimum' => 1,
407 'maximum' => 365,
408 'description' => 'Number of days of historical data to retrieve'
409 ],
410 'metric' => [
411 'required' => false,
412 'type' => 'string',
413 'default' => 'all',
414 'enum' => ['all', 'lcp', 'fid', 'cls', 'inp', 'score'],
415 'description' => 'Specific metric to retrieve'
416 ]
417 ];
418 }
419 }
420