| 1 |
<?php |
| 2 |
namespace Elementor\Data\Base; |
| 3 |
|
| 4 |
use Elementor\Data\Manager; |
| 5 |
use WP_REST_Controller; |
| 6 |
use WP_REST_Server; |
| 7 |
|
| 8 |
abstract class Controller extends WP_REST_Controller { |
| 9 |
/** |
| 10 |
* Loaded endpoint(s). |
| 11 |
* |
| 12 |
* @var \Elementor\Data\Base\Endpoint[] |
| 13 |
*/ |
| 14 |
public $endpoints = []; |
| 15 |
|
| 16 |
/** |
| 17 |
* Loaded processor(s). |
| 18 |
* |
| 19 |
* @var \Elementor\Data\Base\Processor[][] |
| 20 |
*/ |
| 21 |
public $processors = []; |
| 22 |
|
| 23 |
/** |
| 24 |
* Controller constructor. |
| 25 |
* |
| 26 |
* Register endpoints on 'rest_api_init'. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
// TODO: Controllers and endpoints can have common interface. |
| 31 |
|
| 32 |
$this->namespace = Manager::ROOT_NAMESPACE . '/v' . Manager::VERSION; |
| 33 |
$this->rest_base = Manager::REST_BASE . $this->get_name(); |
| 34 |
|
| 35 |
add_action( 'rest_api_init', function () { |
| 36 |
$this->register(); // Because 'register' is protected. |
| 37 |
} ); |
| 38 |
|
| 39 |
/** |
| 40 |
* Since all actions were removed for custom internal REST server. |
| 41 |
* Re-add the actions. |
| 42 |
*/ |
| 43 |
add_action( 'elementor_rest_api_before_init', function () { |
| 44 |
add_action( 'rest_api_init', function() { |
| 45 |
$this->register(); |
| 46 |
} ); |
| 47 |
} ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get controller name. |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
abstract public function get_name(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Get controller namespace. |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public function get_namespace() { |
| 63 |
return $this->namespace; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get controller reset base. |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public function get_rest_base() { |
| 72 |
return $this->rest_base; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get controller route. |
| 77 |
* |
| 78 |
* @return string |
| 79 |
*/ |
| 80 |
public function get_controller_route() { |
| 81 |
return $this->get_namespace() . '/' . $this->get_rest_base(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Retrieves the index for a controller. |
| 86 |
* |
| 87 |
* @return \WP_REST_Response|\WP_Error |
| 88 |
*/ |
| 89 |
public function get_controller_index() { |
| 90 |
$server = rest_get_server(); |
| 91 |
$routes = $server->get_routes(); |
| 92 |
|
| 93 |
$endpoints = array_intersect_key( $server->get_routes(), $routes ); |
| 94 |
|
| 95 |
$controller_route = $this->get_controller_route(); |
| 96 |
|
| 97 |
array_walk( $endpoints, function ( &$item, $endpoint ) use ( &$endpoints, $controller_route ) { |
| 98 |
if ( ! strstr( $endpoint, $controller_route ) ) { |
| 99 |
unset( $endpoints[ $endpoint ] ); |
| 100 |
} |
| 101 |
} ); |
| 102 |
|
| 103 |
$data = [ |
| 104 |
'namespace' => $this->get_namespace(), |
| 105 |
'controller' => $controller_route, |
| 106 |
'routes' => $server->get_data_for_routes( $endpoints ), |
| 107 |
]; |
| 108 |
|
| 109 |
$response = rest_ensure_response( $data ); |
| 110 |
|
| 111 |
// Link to the root index. |
| 112 |
$response->add_link( 'up', rest_url( '/' ) ); |
| 113 |
|
| 114 |
return $response; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get processors. |
| 119 |
* |
| 120 |
* @param string $command |
| 121 |
* |
| 122 |
* @return \Elementor\Data\Base\Processor[] |
| 123 |
*/ |
| 124 |
public function get_processors( $command ) { |
| 125 |
$result = []; |
| 126 |
|
| 127 |
if ( isset( $this->processors[ $command ] ) ) { |
| 128 |
$result = $this->processors[ $command ]; |
| 129 |
} |
| 130 |
|
| 131 |
return $result; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_items( $request ) { |
| 135 |
return $this->get_controller_index(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Register endpoints. |
| 140 |
*/ |
| 141 |
abstract public function register_endpoints(); |
| 142 |
|
| 143 |
/** |
| 144 |
* Register processors. |
| 145 |
*/ |
| 146 |
public function register_processors() { |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Register internal endpoints. |
| 151 |
*/ |
| 152 |
protected function register_internal_endpoints() { |
| 153 |
register_rest_route( $this->get_namespace(), '/' . $this->get_rest_base(), [ |
| 154 |
[ |
| 155 |
'methods' => WP_REST_Server::READABLE, |
| 156 |
'callback' => array( $this, 'get_items' ), |
| 157 |
'args' => [], |
| 158 |
'permission_callback' => function ( $request ) { |
| 159 |
return $this->get_permission_callback( $request ); |
| 160 |
}, |
| 161 |
], |
| 162 |
] ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Register endpoint. |
| 167 |
* |
| 168 |
* @param string $endpoint_class |
| 169 |
* |
| 170 |
* @return \Elementor\Data\Base\Endpoint |
| 171 |
*/ |
| 172 |
protected function register_endpoint( $endpoint_class ) { |
| 173 |
$endpoint_instance = new $endpoint_class( $this ); |
| 174 |
|
| 175 |
// TODO: Validate instance like in register_sub_endpoint(). |
| 176 |
|
| 177 |
$endpoint_route = $this->get_name() . '/' . $endpoint_instance->get_name(); |
| 178 |
|
| 179 |
$this->endpoints[ $endpoint_route ] = $endpoint_instance; |
| 180 |
|
| 181 |
$command = $endpoint_route; |
| 182 |
$format = $endpoint_instance::get_format(); |
| 183 |
|
| 184 |
if ( $command ) { |
| 185 |
$format = $command . '/' . $format; |
| 186 |
} else { |
| 187 |
$format = $format . $command; |
| 188 |
} |
| 189 |
|
| 190 |
// `$e.data.registerFormat()`. |
| 191 |
Manager::instance()->register_endpoint_format( $command, $format ); |
| 192 |
|
| 193 |
return $endpoint_instance; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Register a processor. |
| 198 |
* |
| 199 |
* That will be later attached to the endpoint class. |
| 200 |
* |
| 201 |
* @param string $processor_class |
| 202 |
* |
| 203 |
* @return \Elementor\Data\Base\Processor $processor_instance |
| 204 |
*/ |
| 205 |
protected function register_processor( $processor_class ) { |
| 206 |
$processor_instance = new $processor_class( $this ); |
| 207 |
|
| 208 |
// TODO: Validate processor instance. |
| 209 |
|
| 210 |
$command = $processor_instance->get_command(); |
| 211 |
|
| 212 |
if ( ! isset( $this->processors[ $command ] ) ) { |
| 213 |
$this->processors[ $command ] = []; |
| 214 |
} |
| 215 |
|
| 216 |
$this->processors[ $command ] [] = $processor_instance; |
| 217 |
|
| 218 |
return $processor_instance; |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Register. |
| 223 |
* |
| 224 |
* Endpoints & processors. |
| 225 |
*/ |
| 226 |
protected function register() { |
| 227 |
$this->register_internal_endpoints(); |
| 228 |
$this->register_endpoints(); |
| 229 |
|
| 230 |
// Aka hooks. |
| 231 |
$this->register_processors(); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Retrieves a recursive collection of all endpoint(s), items. |
| 236 |
* |
| 237 |
* Get items recursive, will run overall endpoints of the current controller. |
| 238 |
* For each endpoint it will run `$endpoint->getItems( $request ) // the $request passed in get_items_recursive`. |
| 239 |
* Will skip $skip_endpoints endpoint(s). |
| 240 |
* |
| 241 |
* Example, scenario: |
| 242 |
* Controller 'test-controller'. |
| 243 |
* Controller endpoints: 'endpoint1', 'endpoint2'. |
| 244 |
* Endpoint2 get_items method: `get_items() { return 'test' }`. |
| 245 |
* Call `Controller.get_items_recursive( ['endpoint1'] )`, result: [ 'endpoint2' => 'test' ]; |
| 246 |
* |
| 247 |
* @param array $skip_endpoints |
| 248 |
* |
| 249 |
* @return array |
| 250 |
*/ |
| 251 |
public function get_items_recursive( $skip_endpoints = [] ) { |
| 252 |
$response = []; |
| 253 |
|
| 254 |
foreach ( $this->endpoints as $endpoint ) { |
| 255 |
// Skip self. |
| 256 |
if ( in_array( $endpoint, $skip_endpoints, true ) ) { |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
$response[ $endpoint->get_name() ] = $endpoint->get_items( null ); |
| 261 |
} |
| 262 |
|
| 263 |
return $response; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get permission callback. |
| 268 |
* |
| 269 |
* Default controller permission callback. |
| 270 |
* By default endpoint will inherit the permission callback from the controller. |
| 271 |
* By default permission is `current_user_can( 'administrator' );`. |
| 272 |
* |
| 273 |
* @param \WP_REST_Request $request |
| 274 |
* |
| 275 |
* @return bool |
| 276 |
*/ |
| 277 |
public function get_permission_callback( $request ) { |
| 278 |
// The function is public since endpoint need to access it. |
| 279 |
switch ( $request->get_method() ) { |
| 280 |
case 'GET': |
| 281 |
case 'POST': |
| 282 |
case 'UPDATE': |
| 283 |
case 'PUT': |
| 284 |
case 'DELETE': |
| 285 |
case 'PATCH': |
| 286 |
return current_user_can( 'administrator' ); |
| 287 |
} |
| 288 |
|
| 289 |
return false; |
| 290 |
} |
| 291 |
} |
| 292 |
|