| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Endpoint: Check Auth |
| 4 |
* Parse.ly Content Intelligence `/check-auth` API endpoint class |
| 5 |
* |
| 6 |
* @package Parsely |
| 7 |
* @since 3.19.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace Parsely\REST_API\Content_Helper; |
| 13 |
|
| 14 |
use Parsely\REST_API\Base_Endpoint; |
| 15 |
use Parsely\Services\Suggestions_API\Suggestions_API_Service; |
| 16 |
use WP_Error; |
| 17 |
use WP_REST_Request; |
| 18 |
use WP_REST_Response; |
| 19 |
|
| 20 |
/** |
| 21 |
* The Check Auth API. |
| 22 |
* |
| 23 |
* Provides an endpoint for checking a Site ID's authorization to use the |
| 24 |
* Suggestions API and its features. |
| 25 |
* |
| 26 |
* @since 3.19.0 |
| 27 |
*/ |
| 28 |
class Endpoint_Check_Auth extends Base_Endpoint { |
| 29 |
/** |
| 30 |
* The Suggestions API service. |
| 31 |
* |
| 32 |
* @since 3.19.0 |
| 33 |
* |
| 34 |
* @var Suggestions_API_Service $suggestions_api |
| 35 |
*/ |
| 36 |
protected $suggestions_api; |
| 37 |
|
| 38 |
/** |
| 39 |
* Initializes the class. |
| 40 |
* |
| 41 |
* @since 3.19.0 |
| 42 |
* |
| 43 |
* @param Content_Helper_Controller $controller The Content Intelligence controller. |
| 44 |
*/ |
| 45 |
public function __construct( Content_Helper_Controller $controller ) { |
| 46 |
parent::__construct( $controller ); |
| 47 |
$this->suggestions_api = $controller->get_parsely()->get_suggestions_api(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the name of the endpoint. |
| 52 |
* |
| 53 |
* @since 3.19.0 |
| 54 |
* |
| 55 |
* @return string The endpoint's name. |
| 56 |
*/ |
| 57 |
public static function get_endpoint_name(): string { |
| 58 |
return 'check-auth'; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns the name of the feature associated with the current endpoint. |
| 63 |
* |
| 64 |
* @since 3.19.0 |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
public function get_pch_feature_name(): string { |
| 69 |
return 'check_auth'; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Registers the routes for the endpoint. |
| 74 |
* |
| 75 |
* @since 3.19.0 |
| 76 |
*/ |
| 77 |
public function register_routes(): void { |
| 78 |
/** |
| 79 |
* POST /check-auth |
| 80 |
* Returns whether the Site ID has the authorization to use the |
| 81 |
* Suggestions API or Suggestions API feature. |
| 82 |
*/ |
| 83 |
$this->register_rest_route( |
| 84 |
'/', |
| 85 |
array( 'POST' ), |
| 86 |
array( $this, 'get_check_auth' ), |
| 87 |
array( |
| 88 |
'auth_scope' => array( |
| 89 |
'description' => __( 'The scope for which to authorize.', 'wp-parsely' ), |
| 90 |
'type' => 'string', |
| 91 |
'required' => true, |
| 92 |
), |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* API Endpoint: POST /check-auth |
| 99 |
* |
| 100 |
* Returns whether the Site ID has the authorization to use the Suggestions |
| 101 |
* API or Suggestions API feature. |
| 102 |
* |
| 103 |
* @since 3.19.0 |
| 104 |
* |
| 105 |
* @param WP_REST_Request $request The request object. |
| 106 |
* @return WP_REST_Response|WP_Error The response object or a WP_Error object on failure. |
| 107 |
*/ |
| 108 |
public function get_check_auth( WP_REST_Request $request ) { |
| 109 |
$auth_scope = $request->get_param( 'auth_scope' ); |
| 110 |
|
| 111 |
$response = $this->suggestions_api->get_check_auth( |
| 112 |
array( 'auth_scope' => $auth_scope ) |
| 113 |
); |
| 114 |
|
| 115 |
if ( is_wp_error( $response ) ) { |
| 116 |
return $response; |
| 117 |
} |
| 118 |
|
| 119 |
return new WP_REST_Response( array( 'data' => $response ), 200 ); |
| 120 |
} |
| 121 |
} |
| 122 |
|