PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.14.1
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.14.1
2.14.1 2.14.0 2.13.0 2.12.0 2.11.0 2.10.0 2.9.0 2.8.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 All 56 releases
thinkrank / includes / api / class-ai-insights-endpoint.php

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

103 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Core\Capability_Manager;
24 use ThinkRank\SEO\Ai_Traffic_Tracker;
25 use WP_REST_Controller;
26 use WP_REST_Request;
27 use WP_REST_Response;
28
29 // Prevent direct access
30 if (!defined('ABSPATH')) {
31 exit;
32 }
33
34 /**
35 * REST controller for AI traffic.
36 */
37 class Ai_Insights_Endpoint extends WP_REST_Controller {
38
39 /**
40 * API namespace
41 *
42 * @var string
43 */
44 protected $namespace = 'thinkrank/v1';
45
46 /**
47 * API resource base
48 *
49 * @var string
50 */
51 protected $rest_base = 'ai-insights';
52
53 /**
54 * Register routes.
55 *
56 * @return void
57 */
58 public function register_routes(): void {
59 register_rest_route($this->namespace, '/' . $this->rest_base . '/traffic', [
60 'methods' => 'GET',
61 'callback' => [$this, 'get_traffic'],
62 'permission_callback' => [$this, 'check_admin_permissions'],
63 'args' => [
64 'days' => [
65 'required' => false,
66 'type' => 'integer',
67 'default' => 30,
68 'minimum' => 1,
69 'maximum' => 180,
70 ],
71 ],
72 ]);
73 }
74
75 /**
76 * Admin permission gate (matches the other admin-only endpoints).
77 *
78 * @return bool
79 */
80 public function check_admin_permissions(): bool {
81 // The capability the Role Manager actually writes against, not
82 // manage_options: `route_map()` maps this prefix to
83 // thinkrank_ai_insights and the matrix offers an "AI Insights" row, so
84 // demanding manage_options here made that grant do nothing (#844).
85 return Capability_Manager::current_user_can('thinkrank_ai_insights');
86 }
87
88 /**
89 * AI traffic dashboard summary.
90 *
91 * @param WP_REST_Request $request Request.
92 * @return WP_REST_Response
93 */
94 public function get_traffic(WP_REST_Request $request): WP_REST_Response {
95 $tracker = new Ai_Traffic_Tracker();
96
97 return new WP_REST_Response([
98 'success' => true,
99 'data' => $tracker->summary((int) $request->get_param('days')),
100 ], 200);
101 }
102 }
103