| Server IP : 38.190.208.107 / Your IP : 216.73.217.46 Web Server : nginx/1.28.1 System : Linux ht2026040333114 6.1.0-10-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.37-1 (2023-07-03) x86_64 User : root ( 0) PHP Version : 8.2.28 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv,eval,assert,passthru,system,exec,shell_exec,popen,proc_open MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /www/wwwroot/wp.3/wp-content/plugins/wp-statistics/src/Abstracts/ |
Upload File : |
<?php
namespace WP_Statistics\Abstracts;
use WP_REST_Request;
use WP_REST_Server;
use WP_REST_Response;
use WP_Error;
use Exception;
/**
* Abstract base class for WP Statistics REST API endpoints.
*
* Provides reusable logic for route registration, request validation,
* and signature checking. Subclasses must define the endpoint slug,
* request arguments, and handler logic.
*/
abstract class BaseRestAPI
{
/**
* REST API namespace used for all WP Statistics endpoints.
*
* @var string
*/
protected $namespace = 'wp-statistics/v2';
/**
* Endpoint slug to be defined by subclass (e.g., 'hit', 'online').
*
* @var string
*/
protected $endpoint;
/**
* HTTP method to register the route with.
*
* @var string
*/
protected $method = WP_REST_Server::CREATABLE;
/**
* BaseRestAPI constructor.
*
* Initializes database access, loads options, and hooks route registration.
*/
public function __construct()
{
add_action('rest_api_init', [$this, 'registerRoutes']);
}
/**
* Register the REST route with WordPress.
*
* Subclasses must set $endpoint before this runs.
*
* @return void
* @see https://developer.wordpress.org/reference/functions/register_rest_route/
*/
public function registerRoutes()
{
if (!$this->endpoint) {
return;
}
register_rest_route($this->namespace, '/' . $this->endpoint, [
[
'methods' => $this->method,
'callback' => [$this, 'handle'],
'args' => $this->getArgs(),
'permission_callback' => [$this, 'permissionCallback'],
],
]);
}
/**
* Permission callback for the REST API endpoint.
*
* @param WP_REST_Request $request The REST API request object.
* @return true|WP_Error Returns true if the request is authorized, or WP_Error if not.
*/
public function permissionCallback(WP_REST_Request $request)
{
return true;
}
/**
* Define expected request parameters for this endpoint.
*
* Should be overridden by child classes to validate input.
*
* @return array List of REST parameter definitions.
* @see https://developer.wordpress.org/rest-api/extending-the-rest-api/#arguments
*/
protected function getArgs()
{
return [];
}
/**
* Get the REST API endpoint slug.
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint;
}
/**
* Handle the REST API request.
*
* Subclasses must implement this to provide the actual
* endpoint behavior.
*
* @param WP_REST_Request $request The incoming REST request.
*
* @return WP_REST_Response
* @throws Exception
*/
abstract public function handle(WP_REST_Request $request);
}