atarim-wpal-integration-guide.txt
4 days ago
class-avcf-wpal-abilities.php
4 days ago
class-avcf-wpal-detector.php
4 days ago
class-avcf-wpal-rest.php
4 days ago
class-avcf-wpal-stats.php
4 days ago
class-avcf-wpal-rest.php
90 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST endpoints for WP Activity Log statistics. |
| 4 | * |
| 5 | * Exposes WPAL yesterday counts and (premium) drill-down links to Atarim's |
| 6 | * dashboard backend over the existing atarim/v1 REST namespace. The MCP |
| 7 | * surface registers its own abilities in doit/class-avcf-mcp.php and does |
| 8 | * not depend on these endpoints. |
| 9 | * |
| 10 | * Authentication is server-to-server via the X-Atarim-Token header, validated |
| 11 | * against the secret token saved during site connection. WordPress user |
| 12 | * sessions are not used because the Atarim app calls these endpoints from |
| 13 | * its own backend. |
| 14 | * |
| 15 | * Endpoints: |
| 16 | * GET /wp-json/atarim/v1/wpal/stats Yesterday's counts plus availability. |
| 17 | * GET /wp-json/atarim/v1/wpal/links Premium drill-down links (or fallback). |
| 18 | * |
| 19 | * @package atarim-visual-collaboration |
| 20 | */ |
| 21 | |
| 22 | if ( ! defined('ABSPATH') ) { |
| 23 | exit; // Exit if accessed directly |
| 24 | } |
| 25 | |
| 26 | class AVCF_WPAL_REST { |
| 27 | |
| 28 | /** |
| 29 | * @var AVCF_WPAL_Stats |
| 30 | */ |
| 31 | private $stats; |
| 32 | |
| 33 | /** |
| 34 | * @var AVCF_MCP_Auth |
| 35 | */ |
| 36 | private $auth; |
| 37 | |
| 38 | public function __construct() { |
| 39 | $this->stats = new AVCF_WPAL_Stats(); |
| 40 | $this->auth = new AVCF_MCP_Auth(); |
| 41 | add_action( 'rest_api_init', array( $this, 'avcf_wpal_register_routes' ) ); |
| 42 | } |
| 43 | |
| 44 | public function avcf_wpal_register_routes() { |
| 45 | $permission_callback = array( $this, 'avcf_wpal_permission_check' ); |
| 46 | |
| 47 | register_rest_route( 'atarim/v1', '/wpal/stats', array( |
| 48 | 'methods' => 'GET', |
| 49 | 'callback' => array( $this, 'avcf_wpal_handle_stats' ), |
| 50 | 'permission_callback' => $permission_callback, |
| 51 | ) ); |
| 52 | |
| 53 | register_rest_route( 'atarim/v1', '/wpal/links', array( |
| 54 | 'methods' => 'GET', |
| 55 | 'callback' => array( $this, 'avcf_wpal_handle_links' ), |
| 56 | 'permission_callback' => $permission_callback, |
| 57 | ) ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Authenticate the request against the Atarim secret token. |
| 62 | * |
| 63 | * These endpoints are called server-to-server by Atarim's backend, so |
| 64 | * no WordPress user session is available. Auth is by shared secret in |
| 65 | * the X-Atarim-Token header, validated with hash_equals against the |
| 66 | * token saved during site connection (avc_atarim_secret_token). |
| 67 | */ |
| 68 | public function avcf_wpal_permission_check( WP_REST_Request $request ) { |
| 69 | if ( $this->auth->avcf_mcp_validate_request() ) { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | return new WP_Error( |
| 74 | 'avcf_wpal_invalid_token', |
| 75 | __( 'Invalid or missing Atarim authentication token.', 'atarim-visual-collaboration' ), |
| 76 | array( 'status' => 401 ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | public function avcf_wpal_handle_stats( WP_REST_Request $request ) { |
| 81 | return new WP_REST_Response( $this->stats->avcf_wpal_get_yesterday_counts(), 200 ); |
| 82 | } |
| 83 | |
| 84 | public function avcf_wpal_handle_links( WP_REST_Request $request ) { |
| 85 | return new WP_REST_Response( $this->stats->avcf_wpal_get_yesterday_links(), 200 ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | new AVCF_WPAL_REST(); |
| 90 |