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-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.10.0, at includes/api/class-performance-endpoint.php

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