| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API Controller |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.17.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\REST_API; |
| 12 |
|
| 13 |
use Parsely\REST_API\Content_Helper\Content_Helper_Controller; |
| 14 |
use Parsely\REST_API\Settings\Settings_Controller; |
| 15 |
use Parsely\REST_API\Stats\Stats_Controller; |
| 16 |
use Parsely\REST_API\Utils\Utils_Controller; |
| 17 |
|
| 18 |
/** |
| 19 |
* The REST API Controller. |
| 20 |
* |
| 21 |
* Used to define the namespace, version, and controllers for the REST API. |
| 22 |
* |
| 23 |
* @since 3.17.0 |
| 24 |
*/ |
| 25 |
class REST_API_Controller extends Base_API_Controller { |
| 26 |
/** |
| 27 |
* The controllers for each namespace. |
| 28 |
* |
| 29 |
* @since 3.17.0 |
| 30 |
* |
| 31 |
* @var Base_API_Controller[] |
| 32 |
*/ |
| 33 |
public $controllers = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Gets the namespace for the API. |
| 37 |
* |
| 38 |
* @since 3.17.0 |
| 39 |
* |
| 40 |
* @return string The namespace. |
| 41 |
*/ |
| 42 |
protected function get_namespace(): string { |
| 43 |
return 'wp-parsely'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Gets the version for the API. |
| 48 |
* |
| 49 |
* @since 3.17.0 |
| 50 |
* |
| 51 |
* @return string The version. |
| 52 |
*/ |
| 53 |
protected function get_version(): string { |
| 54 |
return 'v2'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Initializes the REST API controller. |
| 59 |
* |
| 60 |
* @since 3.17.0 |
| 61 |
*/ |
| 62 |
public function init(): void { |
| 63 |
// Register the controllers for each namespace. |
| 64 |
$controllers = array( |
| 65 |
new Content_Helper_Controller( $this->get_parsely() ), |
| 66 |
new Stats_Controller( $this->get_parsely() ), |
| 67 |
new Settings_Controller( $this->get_parsely() ), |
| 68 |
new Utils_Controller( $this->get_parsely() ), |
| 69 |
); |
| 70 |
|
| 71 |
// Initialize the controllers. |
| 72 |
foreach ( $controllers as $controller ) { |
| 73 |
$controller->init(); |
| 74 |
} |
| 75 |
|
| 76 |
$this->controllers = $controllers; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Determines if the specified endpoint is available to the current user. |
| 81 |
* |
| 82 |
* @since 3.17.0 |
| 83 |
* |
| 84 |
* @param string $endpoint The endpoint to check. |
| 85 |
* @return bool True if the endpoint is available to the current user, false otherwise. |
| 86 |
*/ |
| 87 |
public function is_available_to_current_user( string $endpoint ): bool { |
| 88 |
// Remove any forward or trailing slashes. |
| 89 |
$endpoint = trim( $endpoint, '/' ); |
| 90 |
|
| 91 |
// Get the controller for the endpoint. |
| 92 |
$controller = $this->get_controller_for_endpoint( $endpoint ); |
| 93 |
if ( null === $controller ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// Get the endpoint object. |
| 98 |
$endpoint_obj = $controller->get_endpoint( $endpoint ); |
| 99 |
if ( null === $endpoint_obj ) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
// Check if the endpoint is available to the current user. |
| 104 |
$is_available = $endpoint_obj->is_available_to_current_user(); |
| 105 |
if ( is_wp_error( $is_available ) ) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
return $is_available; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Gets the controller for the specified endpoint. |
| 114 |
* |
| 115 |
* @since 3.17.0 |
| 116 |
* |
| 117 |
* @param string $endpoint The endpoint to get the controller for. |
| 118 |
* @return Base_API_Controller|null The controller for the specified endpoint. |
| 119 |
*/ |
| 120 |
private function get_controller_for_endpoint( string $endpoint ): ?Base_API_Controller { |
| 121 |
foreach ( $this->controllers as $controller ) { |
| 122 |
if ( null !== $controller->get_endpoint( $endpoint ) ) { |
| 123 |
return $controller; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return null; |
| 128 |
} |
| 129 |
} |
| 130 |
|