| 1 |
<?php |
| 2 |
/** |
| 3 |
* AI Insights API endpoints. |
| 4 |
* |
| 5 |
* Admin REST surface for the AI Insights screen (#248 P1): |
| 6 |
* |
| 7 |
* GET /ai-insights/traffic — AI referral/crawler dashboard summary |
| 8 |
* |
| 9 |
* Automatic AI metadata on publish, the other tab on this screen, is a |
| 10 |
* ThinkRank Pro feature and registers its own routes there (#673). Brand |
| 11 |
* Visibility used to live in this section too; its v1 routes here were removed |
| 12 |
* in 1.30.0 (#301), and the feature itself was removed later. |
| 13 |
* |
| 14 |
* @package ThinkRank |
| 15 |
* @subpackage API |
| 16 |
* @since 1.27.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
declare(strict_types=1); |
| 20 |
|
| 21 |
namespace ThinkRank\API; |
| 22 |
|
| 23 |
use ThinkRank\SEO\Ai_Traffic_Tracker; |
| 24 |
use WP_REST_Controller; |
| 25 |
use WP_REST_Request; |
| 26 |
use WP_REST_Response; |
| 27 |
|
| 28 |
// Prevent direct access |
| 29 |
if (!defined('ABSPATH')) { |
| 30 |
exit; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* REST controller for AI traffic. |
| 35 |
*/ |
| 36 |
class Ai_Insights_Endpoint extends WP_REST_Controller { |
| 37 |
|
| 38 |
/** |
| 39 |
* API namespace |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
protected $namespace = 'thinkrank/v1'; |
| 44 |
|
| 45 |
/** |
| 46 |
* API resource base |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
protected $rest_base = 'ai-insights'; |
| 51 |
|
| 52 |
/** |
| 53 |
* Register routes. |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function register_routes(): void { |
| 58 |
register_rest_route($this->namespace, '/' . $this->rest_base . '/traffic', [ |
| 59 |
'methods' => 'GET', |
| 60 |
'callback' => [$this, 'get_traffic'], |
| 61 |
'permission_callback' => [$this, 'check_admin_permissions'], |
| 62 |
'args' => [ |
| 63 |
'days' => [ |
| 64 |
'required' => false, |
| 65 |
'type' => 'integer', |
| 66 |
'default' => 30, |
| 67 |
'minimum' => 1, |
| 68 |
'maximum' => 180, |
| 69 |
], |
| 70 |
], |
| 71 |
]); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Admin permission gate (matches the other admin-only endpoints). |
| 76 |
* |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function check_admin_permissions(): bool { |
| 80 |
return current_user_can('manage_options'); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* AI traffic dashboard summary. |
| 85 |
* |
| 86 |
* @param WP_REST_Request $request Request. |
| 87 |
* @return WP_REST_Response |
| 88 |
*/ |
| 89 |
public function get_traffic(WP_REST_Request $request): WP_REST_Response { |
| 90 |
$tracker = new Ai_Traffic_Tracker(); |
| 91 |
|
| 92 |
return new WP_REST_Response([ |
| 93 |
'success' => true, |
| 94 |
'data' => $tracker->summary((int) $request->get_param('days')), |
| 95 |
], 200); |
| 96 |
} |
| 97 |
} |
| 98 |
|