| 1 |
<?php |
| 2 |
/** |
| 3 |
* Endpoints: Base API proxy endpoint class for all API proxy endpoints |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.4.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Endpoints; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
use Parsely\RemoteAPI\Proxy; |
| 15 |
use stdClass; |
| 16 |
use WP_Error; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Server; |
| 19 |
|
| 20 |
/** |
| 21 |
* Configures a REST API endpoint for use. |
| 22 |
*/ |
| 23 |
abstract class Base_API_Proxy { |
| 24 |
/** |
| 25 |
* Parsely object instance. |
| 26 |
* |
| 27 |
* @var Parsely |
| 28 |
*/ |
| 29 |
protected $parsely; |
| 30 |
|
| 31 |
/** |
| 32 |
* Proxy object which does the actual calls to the Parse.ly API. |
| 33 |
* |
| 34 |
* @var Proxy |
| 35 |
*/ |
| 36 |
private $proxy; |
| 37 |
|
| 38 |
/** |
| 39 |
* Registers the endpoint's WP REST route. |
| 40 |
*/ |
| 41 |
abstract public function run(): void; |
| 42 |
|
| 43 |
/** |
| 44 |
* Generates the final data from the passed response. |
| 45 |
* |
| 46 |
* @param array<string, mixed> $response The response received by the proxy. |
| 47 |
* @return array<stdClass> The generated data. |
| 48 |
*/ |
| 49 |
abstract protected function generate_data( array $response ): array; |
| 50 |
|
| 51 |
/** |
| 52 |
* Cached "proxy" to the Parse.ly API endpoint. |
| 53 |
* |
| 54 |
* @param WP_REST_Request $request The request object. |
| 55 |
* @return stdClass|WPError stdClass containing the data or a WP_Error |
| 56 |
* object on failure. |
| 57 |
*/ |
| 58 |
abstract public function get_items( WP_REST_Request $request ); |
| 59 |
|
| 60 |
/** |
| 61 |
* Determines if there are enough permissions to call the endpoint. |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
abstract public function permission_callback(): bool; |
| 66 |
|
| 67 |
/** |
| 68 |
* Constructor. |
| 69 |
* |
| 70 |
* @param Parsely $parsely Instance of Parsely class. |
| 71 |
* @param Proxy $proxy Proxy object which does the actual calls to the |
| 72 |
* Parse.ly API. |
| 73 |
*/ |
| 74 |
public function __construct( Parsely $parsely, Proxy $proxy ) { |
| 75 |
$this->parsely = $parsely; |
| 76 |
$this->proxy = $proxy; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Registers the endpoint's WP REST route. |
| 81 |
* |
| 82 |
* @param string $endpoint The endpoint's route (e.g. /stats/posts). |
| 83 |
*/ |
| 84 |
protected function register_endpoint( string $endpoint ): void { |
| 85 |
$filter_key = trim( str_replace( '/', '_', $endpoint ), '_' ); |
| 86 |
if ( ! apply_filters( 'wp_parsely_enable_' . $filter_key . '_api_proxy', true ) ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$get_items_args = array( |
| 91 |
'query' => array( |
| 92 |
'default' => array(), |
| 93 |
'sanitize_callback' => function ( array $query ) { |
| 94 |
$sanitized_query = array(); |
| 95 |
foreach ( $query as $key => $value ) { |
| 96 |
$sanitized_query[ sanitize_key( $key ) ] = sanitize_text_field( $value ); |
| 97 |
} |
| 98 |
|
| 99 |
return $sanitized_query; |
| 100 |
}, |
| 101 |
), |
| 102 |
); |
| 103 |
|
| 104 |
$rest_route_args = array( |
| 105 |
array( |
| 106 |
'methods' => WP_REST_Server::READABLE, |
| 107 |
'callback' => array( $this, 'get_items' ), |
| 108 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 109 |
'args' => $get_items_args, |
| 110 |
), |
| 111 |
); |
| 112 |
|
| 113 |
register_rest_route( 'wp-parsely/v1', $endpoint, $rest_route_args ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Cached "proxy" to the endpoint. |
| 118 |
* |
| 119 |
* @param WP_REST_Request $request The request object. |
| 120 |
* @param bool $require_api_secret Specifies if the API Secret is |
| 121 |
* required. |
| 122 |
* @param string $param_item The param element to use to |
| 123 |
* get the items. |
| 124 |
* @return stdClass|WPError stdClass containing the data or a WP_Error |
| 125 |
* object on failure. |
| 126 |
*/ |
| 127 |
protected function get_data( WP_REST_Request $request, bool $require_api_secret = true, string $param_item = null ) { |
| 128 |
if ( false === $this->parsely->api_key_is_set() ) { |
| 129 |
return new WP_Error( |
| 130 |
'parsely_site_id_not_set', |
| 131 |
__( 'A Parse.ly API Key must be set in site options to use this endpoint', 'wp-parsely' ), |
| 132 |
array( 'status' => 403 ) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
if ( true === $require_api_secret && false === $this->parsely->api_secret_is_set() ) { |
| 137 |
return new WP_Error( |
| 138 |
'parsely_api_secret_not_set', |
| 139 |
__( 'A Parse.ly API Secret must be set in site options to use this endpoint', 'wp-parsely' ), |
| 140 |
array( 'status' => 403 ) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
if ( null !== $param_item ) { |
| 145 |
$params = $request->get_param( $param_item ); |
| 146 |
} else { |
| 147 |
$params = $request->get_params(); |
| 148 |
} |
| 149 |
|
| 150 |
// A proxy with caching behavior is used here. |
| 151 |
$response = $this->proxy->get_items( $params ); |
| 152 |
|
| 153 |
if ( is_wp_error( $response ) ) { |
| 154 |
return $response; |
| 155 |
} |
| 156 |
|
| 157 |
return (object) array( 'data' => $this->generate_data( $response ) ); |
| 158 |
} |
| 159 |
} |
| 160 |
|