first commit

This commit is contained in:
2024-07-31 13:12:38 +07:00
commit b4e8cbe182
10213 changed files with 3125839 additions and 0 deletions

View File

@@ -0,0 +1,346 @@
<?php
namespace Elementor\Data\Base;
use Elementor\Data\Manager;
use Elementor\Plugin;
use WP_REST_Controller;
use WP_REST_Server;
abstract class Controller extends WP_REST_Controller {
/**
* Loaded endpoint(s).
*
* @var \Elementor\Data\Base\Endpoint[]
*/
public $endpoints = [];
/**
* Loaded processor(s).
*
* @var \Elementor\Data\Base\Processor[][]
*/
public $processors = [];
/**
* Controller constructor.
*
* Register endpoints on 'rest_api_init'.
*
*/
public function __construct() {
// TODO: Controllers and endpoints can have common interface.
// TODO: Uncomment when native 3rd plugins uses V2.
//$this->deprecated();
$this->namespace = Manager::ROOT_NAMESPACE . '/v' . Manager::VERSION;
$this->rest_base = Manager::REST_BASE . $this->get_name();
add_action( 'rest_api_init', function () {
$this->register(); // Because 'register' is protected.
} );
/**
* Since all actions were removed for custom internal REST server.
* Re-add the actions.
*/
add_action( 'elementor_rest_api_before_init', function () {
add_action( 'rest_api_init', function() {
$this->register();
} );
} );
}
/**
* Get controller name.
*
* @return string
*/
abstract public function get_name();
/**
* Get controller namespace.
*
* @return string
*/
public function get_namespace() {
return $this->namespace;
}
/**
* Get controller reset base.
*
* @return string
*/
public function get_rest_base() {
return $this->rest_base;
}
/**
* Get controller route.
*
* @return string
*/
public function get_controller_route() {
return $this->get_namespace() . '/' . $this->get_rest_base();
}
/**
* Retrieves the index for a controller.
*
* @return \WP_REST_Response|\WP_Error
*/
public function get_controller_index() {
$server = rest_get_server();
$routes = $server->get_routes();
$endpoints = array_intersect_key( $server->get_routes(), $routes );
$controller_route = $this->get_controller_route();
array_walk( $endpoints, function ( &$item, $endpoint ) use ( &$endpoints, $controller_route ) {
if ( ! strstr( $endpoint, $controller_route ) ) {
unset( $endpoints[ $endpoint ] );
}
} );
$data = [
'namespace' => $this->get_namespace(),
'controller' => $controller_route,
'routes' => $server->get_data_for_routes( $endpoints ),
];
$response = rest_ensure_response( $data );
// Link to the root index.
$response->add_link( 'up', rest_url( '/' ) );
return $response;
}
/**
* Get processors.
*
* @param string $command
*
* @return \Elementor\Data\Base\Processor[]
*/
public function get_processors( $command ) {
$result = [];
if ( isset( $this->processors[ $command ] ) ) {
$result = $this->processors[ $command ];
}
return $result;
}
public function get_items( $request ) {
return $this->get_controller_index();
}
/**
* Creates multiple items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function create_items( $request ) {
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] );
}
/**
* Updates multiple items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function update_items( $request ) {
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] );
}
/**
* Delete multiple items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function delete_items( $request ) {
return new \WP_Error( 'invalid-method', sprintf( "Method '%s' not implemented. Must be overridden in subclass.", __METHOD__ ), [ 'status' => 405 ] );
}
/**
* Register endpoints.
*/
abstract public function register_endpoints();
/**
* Register processors.
*/
public function register_processors() {
}
/**
* Register internal endpoints.
*/
protected function register_internal_endpoints() {
register_rest_route( $this->get_namespace(), '/' . $this->get_rest_base(), [
[
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'args' => [],
'permission_callback' => function ( $request ) {
return $this->get_permission_callback( $request );
},
],
] );
}
/**
* Register endpoint.
*
* @param string $endpoint_class
*
* @return \Elementor\Data\Base\Endpoint
*/
protected function register_endpoint( $endpoint_class ) {
$endpoint_instance = new $endpoint_class( $this );
// TODO: Validate instance like in register_sub_endpoint().
$endpoint_route = $this->get_name() . '/' . $endpoint_instance->get_name();
$this->endpoints[ $endpoint_route ] = $endpoint_instance;
$command = $endpoint_route;
$format = $endpoint_instance::get_format();
if ( $command ) {
$format = $command . '/' . $format;
} else {
$format = $format . $command;
}
// `$e.data.registerFormat()`.
Manager::instance()->register_endpoint_format( $command, $format );
return $endpoint_instance;
}
/**
* Register a processor.
*
* That will be later attached to the endpoint class.
*
* @param string $processor_class
*
* @return \Elementor\Data\Base\Processor $processor_instance
*/
protected function register_processor( $processor_class ) {
$processor_instance = new $processor_class( $this );
// TODO: Validate processor instance.
$command = $processor_instance->get_command();
if ( ! isset( $this->processors[ $command ] ) ) {
$this->processors[ $command ] = [];
}
$this->processors[ $command ] [] = $processor_instance;
return $processor_instance;
}
/**
* Register.
*
* Endpoints & processors.
*/
protected function register() {
$this->register_internal_endpoints();
$this->register_endpoints();
// Aka hooks.
$this->register_processors();
}
/**
* Retrieves a recursive collection of all endpoint(s), items.
*
* Get items recursive, will run overall endpoints of the current controller.
* For each endpoint it will run `$endpoint->getItems( $request ) // the $request passed in get_items_recursive`.
* Will skip $skip_endpoints endpoint(s).
*
* Example, scenario:
* Controller 'test-controller'.
* Controller endpoints: 'endpoint1', 'endpoint2'.
* Endpoint2 get_items method: `get_items() { return 'test' }`.
* Call `Controller.get_items_recursive( ['endpoint1'] )`, result: [ 'endpoint2' => 'test' ];
*
* @param array $skip_endpoints
*
* @return array
*/
public function get_items_recursive( $skip_endpoints = [] ) {
$response = [];
foreach ( $this->endpoints as $endpoint ) {
// Skip self.
if ( in_array( $endpoint, $skip_endpoints, true ) ) {
continue;
}
$response[ $endpoint->get_name() ] = $endpoint->get_items( null );
}
return $response;
}
/**
* Get permission callback.
*
* Default controller permission callback.
* By default endpoint will inherit the permission callback from the controller.
* By default permission is `current_user_can( 'administrator' );`.
*
* @param \WP_REST_Request $request
*
* @return bool
*/
public function get_permission_callback( $request ) {
// The function is public since endpoint need to access it.
switch ( $request->get_method() ) {
case 'GET':
case 'POST':
case 'UPDATE':
case 'PUT':
case 'DELETE':
case 'PATCH':
return current_user_can( 'administrator' );
}
return false;
}
private static $notify_deprecated = true;
private function deprecated() {
add_action( 'elementor/init', function () {
if ( ! self::$notify_deprecated ) {
return;
}
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function(
'Elementor\Data\Manager',
'3.5.0',
'Elementor\Data\V2\Manager'
);
self::$notify_deprecated = false;
} );
}
}

View File

@@ -0,0 +1,353 @@
<?php
namespace Elementor\Data\Base;
use Elementor\Data\Manager;
use WP_REST_Server;
abstract class Endpoint {
const AVAILABLE_METHODS = [
WP_REST_Server::READABLE,
WP_REST_Server::CREATABLE,
WP_REST_Server::EDITABLE,
WP_REST_Server::DELETABLE,
WP_REST_Server::ALLMETHODS,
];
/**
* Controller of current endpoint.
*
* @var \Elementor\Data\Base\Controller
*/
protected $controller;
/**
* Loaded sub endpoint(s).
*
* @var \Elementor\Data\Base\SubEndpoint[]
*/
private $sub_endpoints = [];
/**
* Get format suffix.
*
* Examples:
* '{one_parameter_name}'.
* '{one_parameter_name}/{two_parameter_name}/'.
* '{one_parameter_name}/whatever/anything/{two_parameter_name}/' and so on for each endpoint or sub-endpoint.
*
* @return string current location will later be added automatically.
*/
public static function get_format() {
return '';
}
/**
* Endpoint constructor.
*
* run `$this->>register()`.
*
* @param \Elementor\Data\Base\Controller $controller
*
* @throws \Exception
*/
public function __construct( $controller ) {
if ( ! ( $controller instanceof Controller ) ) {
throw new \Exception( 'Invalid controller.' );
}
$this->controller = $controller;
$this->register();
}
/**
* Get endpoint name.
*
* @return string
*/
abstract public function get_name();
/**
* Get base route.
*
* Removing 'index' from endpoint.
*
* @return string
*/
public function get_base_route() {
$endpoint_name = $this->get_name();
// TODO: Allow this only for internal routes.
// TODO: Make difference between internal and external endpoints.
if ( 'index' === $endpoint_name ) {
$endpoint_name = '';
}
return '/' . $this->controller->get_rest_base() . '/' . $endpoint_name;
}
/**
* Register the endpoint.
*
* By default: register get items route.
*
* @throws \Exception
*/
protected function register() {
$this->register_items_route();
}
/**
* Register sub endpoint.
*
* @param string $route
* @param string $endpoint_class
*
* @return \Elementor\Data\Base\SubEndpoint
* @throws \Exception
*/
protected function register_sub_endpoint( $route, $endpoint_class ) {
$endpoint_instance = new $endpoint_class( $route, $this );
if ( ! ( $endpoint_instance instanceof SubEndpoint ) ) {
throw new \Exception( 'Invalid endpoint instance.' );
}
$endpoint_route = $route . '/' . $endpoint_instance->get_name();
$this->sub_endpoints[ $endpoint_route ] = $endpoint_instance;
$component_name = $endpoint_instance->controller->get_rest_base();
$parent_instance = $endpoint_instance->get_parent();
$parent_name = $endpoint_instance->get_name();
$parent_format_suffix = $parent_instance::get_format();
$current_format_suffix = $endpoint_instance::get_format();
$command = $component_name . '/' . $parent_name;
$format = $component_name . '/' . $parent_format_suffix . '/' . $parent_name . '/' . $current_format_suffix;
Manager::instance()->register_endpoint_format( $command, $format );
return $endpoint_instance;
}
/**
* Base callback.
*
* All reset requests from the client should pass this function.
*
* @param string $methods
* @param \WP_REST_Request $request
* @param bool $is_multi
*
* @return mixed|\WP_Error|\WP_HTTP_Response|\WP_REST_Response
* @throws \Exception
*/
public function base_callback( $methods, $request, $is_multi = false ) {
// TODO: Find better solution.
$json_params = $request->get_json_params();
if ( $json_params ) {
$request->set_body_params( $json_params );
}
// TODO: Handle permission callback.
switch ( $methods ) {
case WP_REST_Server::READABLE:
$result = $is_multi ? $this->get_items( $request ) : $this->get_item( $request->get_param( 'id' ), $request );
break;
case WP_REST_Server::CREATABLE:
$result = $is_multi ? $this->create_items( $request ) : $this->create_item( $request->get_param( 'id' ), $request );
break;
case WP_REST_Server::EDITABLE:
$result = $is_multi ? $this->update_items( $request ) : $this->update_item( $request->get_param( 'id' ), $request );
break;
case WP_REST_Server::DELETABLE:
$result = $is_multi ? $this->delete_items( $request ) : $this->delete_item( $request->get_param( 'id' ), $request );
break;
default:
throw new \Exception( 'Invalid method.' );
}
return rest_ensure_response( $result );
}
/**
* Retrieves a collection of items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
return $this->controller->get_items( $request );
}
/**
* Retrieves one item from the collection.
*
* @param string $id
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function get_item( $id, $request ) {
return $this->controller->get_item( $request );
}
/**
* Get permission callback.
*
* By default get permission callback from the controller.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return boolean
*/
public function get_permission_callback( $request ) {
return $this->controller->get_permission_callback( $request );
}
/**
* Creates one item.
*
* @param string $id id of request item.
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function create_item( $id, $request ) {
return $this->controller->create_item( $request );
}
/**
* Creates multiple items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function create_items( $request ) {
return $this->controller->create_items( $request );
}
/**
* Updates one item.
*
* @param string $id id of request item.
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function update_item( $id, $request ) {
return $this->controller->update_item( $request );
}
/**
* Updates multiple items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function update_items( $request ) {
return $this->controller->update_items( $request );
}
/**
* Delete one item.
*
* @param string $id id of request item.
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function delete_item( $id, $request ) {
return $this->controller->delete_item( $request );
}
/**
* Delete multiple items.
*
* @param \WP_REST_Request $request Full data about the request.
*
* @return \WP_Error|\WP_REST_Response Response object on success, or WP_Error object on failure.
*/
public function delete_items( $request ) {
return $this->controller->delete_items( $request );
}
/**
* Register item route.
*
* @param array $args
* @param string $route
* @param string $methods
*
* @throws \Exception
*/
public function register_item_route( $methods = WP_REST_Server::READABLE, $args = [], $route = '/' ) {
$args = array_merge( [
'id' => [
'description' => 'Unique identifier for the object.',
'type' => 'string',
],
], $args );
if ( isset( $args['id'] ) && $args['id'] ) {
$route .= '(?P<id>[\w]+)/';
}
$this->register_route( $route, $methods, function ( $request ) use ( $methods ) {
return $this->base_callback( $methods, $request );
}, $args );
}
/**
* Register items route.
*
* @param string $methods
*
* @throws \Exception
*/
public function register_items_route( $methods = WP_REST_Server::READABLE ) {
$this->register_route( '', $methods, function ( $request ) use ( $methods ) {
return $this->base_callback( $methods, $request, true );
} );
}
/**
* Register route.
*
* @param string $route
* @param string $methods
* @param null $callback
* @param array $args
*
* @return bool
* @throws \Exception
*/
public function register_route( $route = '', $methods = WP_REST_Server::READABLE, $callback = null, $args = [] ) {
if ( ! in_array( $methods, self::AVAILABLE_METHODS, true ) ) {
throw new \Exception( 'Invalid method.' );
}
$route = $this->get_base_route() . $route;
return register_rest_route( $this->controller->get_namespace(), $route, [
[
'args' => $args,
'methods' => $methods,
'callback' => $callback,
'permission_callback' => function ( $request ) {
return $this->get_permission_callback( $request );
},
],
] );
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Elementor\Data\Base;
abstract class Processor {
/**
* Controller.
*
* @var \Elementor\Data\Base\Controller
*/
private $controller;
/**
* Processor constructor.
*
* @param \Elementor\Data\Base\Controller $controller
*/
public function __construct( $controller ) {
$this->controller = $controller;
}
/**
* Get processor command.
*
* @return string
*/
abstract public function get_command();
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Elementor\Data\Base\Processor;
use Elementor\Data\Base\Processor;
abstract class After extends Processor {
/**
* Get conditions for running processor.
*
* @param array $args
* @param mixed $result
*
* @return bool
*/
public function get_conditions( $args, $result ) {
return true;
}
/**
* Apply processor.
*
* @param $args
* @param $result
*
* @return mixed
*/
abstract public function apply( $args, $result );
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Elementor\Data\Base\Processor;
use Elementor\Data\Base\Processor;
abstract class Before extends Processor {
/**
* Get conditions for running processor.
* @param array $args
*
* @return bool
*/
public function get_conditions( $args ) {
return true;
}
/**
* Apply processor.
*
* @param array $args
*
* @return mixed
*/
abstract public function apply( $args );
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Elementor\Data\Base;
// TODO: Add test.
abstract class SubEndpoint extends Endpoint {
/**
* @var Endpoint
*/
protected $parent_endpoint;
/**
* @var string
*/
protected $parent_route = '';
public function __construct( $parent_route, $parent_endpoint ) {
$this->parent_endpoint = $parent_endpoint;
$this->parent_route = $parent_route;
parent::__construct( $this->parent_endpoint->controller );
}
/**
* Get parent route.
*
* @return \Elementor\Data\Base\Endpoint
*/
public function get_parent() {
return $this->parent_endpoint;
}
public function get_base_route() {
$controller_name = $this->controller->get_name();
return $controller_name . '/' . $this->parent_route . $this->get_name();
}
}