mirror of
https://github.com/proelements/proelements.git
synced 2026-04-05 20:13:47 +00:00
v3.33.1
This commit is contained in:
33
core/data/controller.php
Normal file
33
core/data/controller.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\Data;
|
||||
|
||||
use ElementorPro\Modules\LoopFilter\Data\Endpoints\Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
abstract class Controller {
|
||||
|
||||
private $endpoints = [];
|
||||
|
||||
abstract public function get_name();
|
||||
|
||||
public function get_namespace() {
|
||||
return 'elementor-pro/v1';
|
||||
}
|
||||
|
||||
abstract protected function register_endpoints();
|
||||
|
||||
protected function register_endpoint( $endpoint ) {
|
||||
$endpoint_instance = new $endpoint( $this );
|
||||
|
||||
$this->endpoints[ $endpoint_instance->get_name() ] = $endpoint_instance;
|
||||
}
|
||||
|
||||
public function __construct() {
|
||||
add_action( 'rest_api_init', function () {
|
||||
$this->register_endpoints(); // Because 'register_endpoints' is protected.
|
||||
} );
|
||||
}
|
||||
}
|
||||
28
core/data/endpoints/base.php
Normal file
28
core/data/endpoints/base.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\Data\Endpoints;
|
||||
|
||||
use ElementorPro\Core\Data\Controller;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Base {
|
||||
|
||||
protected $controller;
|
||||
|
||||
protected function register() {}
|
||||
|
||||
/**
|
||||
* Endpoint constructor.
|
||||
*
|
||||
* runs `$this->register()`.
|
||||
*
|
||||
* @param Controller $controller
|
||||
*/
|
||||
public function __construct( Controller $controller ) {
|
||||
$this->controller = $controller;
|
||||
|
||||
$this->register();
|
||||
}
|
||||
}
|
||||
87
core/data/endpoints/refresh-base.php
Normal file
87
core/data/endpoints/refresh-base.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\Data\Endpoints;
|
||||
|
||||
use Elementor\Utils;
|
||||
use ElementorPro\Core\Utils as Pro_Utils;
|
||||
use ElementorPro\Plugin;
|
||||
use ElementorPro\Core\Data\Interfaces\Endpoint;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
abstract class Refresh_Base extends Base implements Endpoint {
|
||||
protected $is_edit_mode;
|
||||
|
||||
abstract public function get_name() : string;
|
||||
|
||||
abstract public function get_route() : string;
|
||||
|
||||
protected function permission_callback( $request, $widget_name = '' ): bool {
|
||||
$data = $request->get_params();
|
||||
|
||||
if ( $this->is_edit_mode( $data['post_id'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$post = get_post( $data['post_id'] );
|
||||
|
||||
if ( ! $post || 'publish' !== $post->post_status ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$document = Plugin::elementor()->documents->get( $data['post_id'] );
|
||||
|
||||
if ( ! $document ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$element_data = $document->get_elements_data();
|
||||
$widget = Utils::find_element_recursive( $element_data, $data['widget_id'] );
|
||||
|
||||
if ( empty( $widget ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( 'widget' !== $widget['elType'] || $widget_name !== $widget['widgetType'] ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function is_widget_model_valid( $widget_model ) {
|
||||
return is_array( $widget_model )
|
||||
&& isset( $widget_model['id'] )
|
||||
&& is_string( $widget_model['id'] )
|
||||
&& isset( $widget_model['settings'] )
|
||||
&& is_array( $widget_model['settings'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* The widget ID can only be 7 characters long, and contain only letters and numbers.
|
||||
*
|
||||
* @param $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function is_widget_id_valid( $widget_id ) {
|
||||
return preg_match( '/^[a-zA-Z0-9]+$/', $widget_id )
|
||||
&& strlen( $widget_id ) === 7;
|
||||
}
|
||||
|
||||
protected function create_widget_instance_from_db( $post_id, $widget_id ) {
|
||||
return Pro_Utils::create_widget_instance_from_db( $post_id, $widget_id );
|
||||
}
|
||||
|
||||
protected function is_edit_mode( $post_id ) {
|
||||
if ( isset( $this->is_edit_mode ) ) {
|
||||
return $this->is_edit_mode;
|
||||
}
|
||||
|
||||
$document = Plugin::elementor()->documents->get( $post_id );
|
||||
|
||||
$this->is_edit_mode = ! empty( $document ) && $document->is_editable_by_current_user();
|
||||
|
||||
return $this->is_edit_mode;
|
||||
}
|
||||
}
|
||||
18
core/data/interfaces/endpoint.php
Normal file
18
core/data/interfaces/endpoint.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace ElementorPro\Core\Data\Interfaces;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
interface Endpoint {
|
||||
/**
|
||||
* @return string The interface ID
|
||||
*/
|
||||
public function get_name(): string;
|
||||
|
||||
/**
|
||||
* @return string The route slug which will be used to access the endpoint by URL.
|
||||
*/
|
||||
public function get_route(): string;
|
||||
}
|
||||
Reference in New Issue
Block a user