PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.1.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.1.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.1.0, at includes/api/class-performance-endpoint.php

456 lines 14.1 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 'args' => [
98 'device_type' => [
99 'default' => 'mobile',
100 'type' => 'string',
101 'enum' => ['mobile', 'desktop'],
102 'sanitize_callback' => 'sanitize_key'
103 ]
104 ]
105 ]
106 ]
107 );
108
109
110
111 // Recommendations endpoint
112 register_rest_route(
113 $this->namespace,
114 '/' . $this->rest_base . '/recommendations',
115 [
116 [
117 'methods' => WP_REST_Server::READABLE,
118 'callback' => [$this, 'get_recommendations'],
119 'permission_callback' => [$this, 'check_read_permissions']
120 ]
121 ]
122 );
123
124 // Historical data endpoint
125 register_rest_route(
126 $this->namespace,
127 '/' . $this->rest_base . '/history',
128 [
129 [
130 'methods' => WP_REST_Server::READABLE,
131 'callback' => [$this, 'get_historical_data'],
132 'permission_callback' => [$this, 'check_read_permissions'],
133 'args' => $this->get_historical_data_args()
134 ]
135 ]
136 );
137
138 // Opportunities endpoint
139 register_rest_route(
140 $this->namespace,
141 '/' . $this->rest_base . '/opportunities',
142 [
143 [
144 'methods' => WP_REST_Server::READABLE,
145 'callback' => [$this, 'get_opportunities'],
146 'permission_callback' => [$this, 'check_read_permissions'],
147 'args' => [
148 'device_type' => [
149 'default' => 'mobile',
150 'type' => 'string',
151 'enum' => ['mobile', 'desktop'],
152 'sanitize_callback' => 'sanitize_key'
153 ]
154 ]
155 ]
156 ]
157 );
158
159 // Diagnostics endpoint
160 register_rest_route(
161 $this->namespace,
162 '/' . $this->rest_base . '/diagnostics',
163 [
164 [
165 'methods' => WP_REST_Server::READABLE,
166 'callback' => [$this, 'get_diagnostics'],
167 'permission_callback' => [$this, 'check_read_permissions'],
168 'args' => [
169 'device_type' => [
170 'default' => 'mobile',
171 'type' => 'string',
172 'enum' => ['mobile', 'desktop'],
173 'sanitize_callback' => 'sanitize_key'
174 ]
175 ]
176 ]
177 ]
178 );
179
180 // Data collection endpoint
181 register_rest_route(
182 $this->namespace,
183 '/' . $this->rest_base . '/collect',
184 [
185 [
186 'methods' => WP_REST_Server::CREATABLE,
187 'callback' => [$this, 'collect_performance_data'],
188 'permission_callback' => [$this, 'check_manage_permissions']
189 ]
190 ]
191 );
192
193
194 }
195
196 /**
197 * Get comprehensive performance data
198 *
199 * @since 1.0.0
200 *
201 * @param WP_REST_Request $request Request object
202 * @return WP_REST_Response|WP_Error Response object or error
203 */
204 public function get_performance_data(WP_REST_Request $request) {
205 try {
206 // Get device type from request
207 $device_type = $request->get_param('device_type') ?? 'mobile';
208
209 // Use cached response wrapper for performance - include device type in cache key
210 $response_data = $this->cached_response(
211 'performance_data',
212 function() use ($device_type) {
213 // Get Core Web Vitals with device type
214 $core_web_vitals = $this->performance_manager->get_core_web_vitals('', false, $device_type);
215
216 // Get performance score based on the device-specific Core Web Vitals
217 $performance_score = $this->performance_manager->get_performance_score($core_web_vitals);
218
219 // Get performance grade based on the device-specific performance score
220 $performance_grade = $this->performance_manager->get_performance_grade($performance_score);
221
222 // Get SEO performance correlation based on device-specific data
223 $seo_correlation = $this->performance_manager->get_seo_performance_correlation($core_web_vitals, $performance_score);
224
225 return [
226 'success' => true,
227 'data' => [
228 'core_web_vitals' => $core_web_vitals,
229 'performance_score' => $performance_score,
230 'performance_grade' => $performance_grade,
231 'seo_correlation' => $seo_correlation,
232 'device_type' => $device_type,
233 'last_updated' => current_time('mysql'),
234 'status' => 'success'
235 ],
236 'message' => __('Performance data retrieved successfully', 'thinkrank')
237 ];
238 },
239 ['device_type' => $device_type], // Include device type in cache key
240 null, // Use default cache duration
241 get_current_user_id()
242 );
243
244 return new WP_REST_Response($response_data, 200);
245
246 } catch (\Exception $e) {
247 return new WP_Error(
248 'performance_data_failed',
249 'Failed to retrieve performance data: ' . $e->getMessage(),
250 ['status' => 500]
251 );
252 }
253 }
254
255
256
257
258
259 /**
260 * Get performance recommendations
261 *
262 * @since 1.0.0
263 *
264 * @param WP_REST_Request $request Request object
265 * @return WP_REST_Response|WP_Error Response object or error
266 */
267 public function get_recommendations(WP_REST_Request $request) {
268 try {
269 $recommendations = $this->performance_manager->get_performance_recommendations();
270
271 return new WP_REST_Response([
272 'success' => true,
273 'data' => $recommendations,
274 'message' => __('Performance recommendations retrieved successfully', 'thinkrank')
275 ], 200);
276
277 } catch (\Exception $e) {
278 return new WP_Error(
279 'recommendations_failed',
280 'Failed to retrieve recommendations: ' . $e->getMessage(),
281 ['status' => 500]
282 );
283 }
284 }
285
286 /**
287 * Get historical performance data
288 *
289 * @since 1.0.0
290 *
291 * @param WP_REST_Request $request Request object
292 * @return WP_REST_Response|WP_Error Response object or error
293 */
294 public function get_historical_data(WP_REST_Request $request) {
295 try {
296 $days = $request->get_param('days') ?? 30;
297 $metric = $request->get_param('metric') ?? 'all';
298
299 $historical_data = $this->performance_manager->get_historical_data($days, $metric);
300
301 return new WP_REST_Response([
302 'success' => true,
303 'data' => $historical_data,
304 'message' => __('Historical data retrieved successfully', 'thinkrank')
305 ], 200);
306
307 } catch (\Exception $e) {
308 return new WP_Error(
309 'historical_data_failed',
310 'Failed to retrieve historical data: ' . $e->getMessage(),
311 ['status' => 500]
312 );
313 }
314 }
315
316 /**
317 * Get performance opportunities
318 *
319 * @since 1.0.0
320 *
321 * @param WP_REST_Request $request Request object
322 * @return WP_REST_Response|WP_Error Response object or error
323 */
324 public function get_opportunities(WP_REST_Request $request) {
325 try {
326 // Get device type from request
327 $device_type = $request->get_param('device_type') ?? 'mobile';
328
329 $opportunities = $this->performance_manager->get_performance_opportunities('', $device_type);
330
331 return new WP_REST_Response([
332 'success' => true,
333 'data' => $opportunities,
334 'device_type' => $device_type,
335 'message' => __('Performance opportunities retrieved successfully', 'thinkrank')
336 ], 200);
337
338 } catch (\Exception $e) {
339 return new WP_Error(
340 'opportunities_failed',
341 'Failed to retrieve performance opportunities: ' . $e->getMessage(),
342 ['status' => 500]
343 );
344 }
345 }
346
347 /**
348 * Get performance diagnostics
349 *
350 * @since 1.0.0
351 *
352 * @param WP_REST_Request $request Request object
353 * @return WP_REST_Response|WP_Error Response object or error
354 */
355 public function get_diagnostics(WP_REST_Request $request) {
356 try {
357 // Get device type from request
358 $device_type = $request->get_param('device_type') ?? 'mobile';
359
360 $diagnostics = $this->performance_manager->get_performance_diagnostics('', $device_type);
361
362 return new WP_REST_Response([
363 'success' => true,
364 'data' => $diagnostics,
365 'device_type' => $device_type,
366 'message' => __('Performance diagnostics retrieved successfully', 'thinkrank')
367 ], 200);
368
369 } catch (\Exception $e) {
370 return new WP_Error(
371 'diagnostics_failed',
372 'Failed to retrieve performance diagnostics: ' . $e->getMessage(),
373 ['status' => 500]
374 );
375 }
376 }
377
378 /**
379 * Manually collect performance data
380 *
381 * @since 1.0.0
382 *
383 * @param WP_REST_Request $request Request object
384 * @return WP_REST_Response|WP_Error Response object or error
385 */
386 public function collect_performance_data(WP_REST_Request $request) {
387 try {
388 $results = $this->data_collector->manual_collect();
389
390 return new WP_REST_Response([
391 'success' => $results['success'],
392 'data' => $results,
393 'message' => $results['message']
394 ], $results['success'] ? 200 : 500);
395
396 } catch (\Exception $e) {
397 return new WP_Error(
398 'data_collection_failed',
399 'Failed to collect performance data: ' . $e->getMessage(),
400 ['status' => 500]
401 );
402 }
403 }
404
405
406
407 /**
408 * Check read permissions
409 *
410 * @since 1.0.0
411 *
412 * @return bool True if user can read
413 */
414 public function check_read_permissions(): bool {
415 return current_user_can('read');
416 }
417
418 /**
419 * Check manage permissions
420 *
421 * @since 1.0.0
422 *
423 * @return bool True if user can manage options
424 */
425 public function check_manage_permissions(): bool {
426 return current_user_can('manage_options');
427 }
428
429 /**
430 * Get arguments for historical data endpoint
431 *
432 * @since 1.0.0
433 *
434 * @return array Arguments array
435 */
436 private function get_historical_data_args(): array {
437 return [
438 'days' => [
439 'required' => false,
440 'type' => 'integer',
441 'default' => 30,
442 'minimum' => 1,
443 'maximum' => 365,
444 'description' => 'Number of days of historical data to retrieve'
445 ],
446 'metric' => [
447 'required' => false,
448 'type' => 'string',
449 'default' => 'all',
450 'enum' => ['all', 'lcp', 'fid', 'cls', 'inp', 'score'],
451 'description' => 'Specific metric to retrieve'
452 ]
453 ];
454 }
455 }
456