PluginProbe
Parse.ly / 3.17.0
Parse.ly v3.17.0
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / rest-api / class-rest-api-controller.php

class-rest-api-controller.php in Parse.ly 3.17.0, at src/rest-api/class-rest-api-controller.php

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