| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get performance data ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Analysis |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Analysis; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Performance_Monitoring_Manager; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Retrieves ThinkRank's Core Web Vitals / performance snapshot for the site. |
| 21 |
* |
| 22 |
* Mirrors `GET /thinkrank/v1/performance/monitor`: returns the collected Core |
| 23 |
* Web Vitals (LCP, INP, CLS, FCP), a 0-100 performance score and grade, and the |
| 24 |
* SEO correlation. Requires a connected Google account for real data; when not |
| 25 |
* connected the payload still returns with a `core_web_vitals.error` marker and |
| 26 |
* zeroed metrics. |
| 27 |
*/ |
| 28 |
class Get_Performance_Data extends Ability_Base { |
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
$this->id = 'thinkrank/get-performance-data'; |
| 34 |
$this->label = __( 'Get Performance Data', 'thinkrank' ); |
| 35 |
$this->description = __( 'Retrieve the site Core Web Vitals / performance snapshot (LCP, INP, CLS, FCP), performance score and grade, and SEO correlation. Requires a connected Google account for real data; otherwise returns an empty snapshot with a not-connected marker. get-integrations-status reports whether that account is connected.', 'thinkrank' ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* {@inheritDoc} |
| 40 |
* |
| 41 |
* @return array<string, bool|float|string> |
| 42 |
*/ |
| 43 |
public function get_annotations() { |
| 44 |
return [ |
| 45 |
'readonly' => true, |
| 46 |
'destructive' => false, |
| 47 |
'idempotent' => true, |
| 48 |
'priority' => 1.0, |
| 49 |
'openWorldHint' => false, |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* {@inheritDoc} |
| 55 |
* |
| 56 |
* @return array<string, mixed> |
| 57 |
*/ |
| 58 |
public function get_input_schema() { |
| 59 |
return [ |
| 60 |
'type' => 'object', |
| 61 |
'additionalProperties' => false, |
| 62 |
'properties' => [ |
| 63 |
'device_type' => [ |
| 64 |
'type' => 'string', |
| 65 |
'description' => __( 'The device profile to report.', 'thinkrank' ), |
| 66 |
'enum' => [ 'mobile', 'desktop' ], |
| 67 |
'default' => 'mobile', |
| 68 |
], |
| 69 |
], |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* {@inheritDoc} |
| 75 |
* |
| 76 |
* @return array<string, mixed> |
| 77 |
*/ |
| 78 |
public function get_output_schema() { |
| 79 |
return [ |
| 80 |
'type' => 'object', |
| 81 |
'additionalProperties' => true, |
| 82 |
'properties' => [ |
| 83 |
'success' => [ 'type' => 'boolean' ], |
| 84 |
'data' => [ |
| 85 |
'type' => 'object', |
| 86 |
'additionalProperties' => true, |
| 87 |
], |
| 88 |
'message' => [ 'type' => 'string' ], |
| 89 |
], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Execute ability. |
| 95 |
* |
| 96 |
* @param array<string, mixed> $input Ability input payload. |
| 97 |
* @return array<string, mixed>|\WP_Error |
| 98 |
*/ |
| 99 |
public function execute( $input ) { |
| 100 |
$device_type = isset( $input['device_type'] ) ? sanitize_key( (string) $input['device_type'] ) : 'mobile'; |
| 101 |
|
| 102 |
if ( ! in_array( $device_type, [ 'mobile', 'desktop' ], true ) ) { |
| 103 |
$device_type = 'mobile'; |
| 104 |
} |
| 105 |
|
| 106 |
try { |
| 107 |
$manager = new Performance_Monitoring_Manager(); |
| 108 |
|
| 109 |
return $manager->get_performance_snapshot( $device_type ); |
| 110 |
} catch ( \Throwable $e ) { |
| 111 |
return new \WP_Error( |
| 112 |
'thinkrank_performance_data_failed', |
| 113 |
$e->getMessage(), |
| 114 |
[ 'status' => 500 ] |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|