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:
19
modules/loop-filter/data/controller.php
Normal file
19
modules/loop-filter/data/controller.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\LoopFilter\Data;
|
||||
|
||||
use ElementorPro\Core\Data\Controller as Controller_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
class Controller extends Controller_Base {
|
||||
public function get_name() {
|
||||
return 'loop-filter';
|
||||
}
|
||||
|
||||
protected function register_endpoints() {
|
||||
$this->register_endpoint( Endpoints\Get_Post_Type_Taxonomies::class );
|
||||
$this->register_endpoint( Endpoints\Refresh_Loop::class );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\LoopFilter\Data\Endpoints;
|
||||
|
||||
use ElementorPro\Modules\LoopFilter\Traits\Taxonomy_Filter_Trait;
|
||||
use ElementorPro\Core\Data\Endpoints\Refresh_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
// Create a class that extends the Base Endpoint class.
|
||||
// This class should handle fetching taxonomies from the database, it registers an endpoint that can be accessed via the REST API.
|
||||
// The endpoint accepts a string argument of 'post_type' and returns an array of taxonomies for that post type.
|
||||
|
||||
class Get_Post_Type_Taxonomies extends Refresh_Base {
|
||||
|
||||
use Taxonomy_Filter_Trait;
|
||||
|
||||
public function get_name() : string {
|
||||
return 'get_post_type_taxonomies';
|
||||
}
|
||||
|
||||
public function get_route() : string {
|
||||
return 'get-post-type-taxonomies';
|
||||
}
|
||||
|
||||
protected function permission_callback( $request, $widget_name = '' ) : bool {
|
||||
return current_user_can( 'edit_posts' );
|
||||
}
|
||||
|
||||
public function get_items( \WP_REST_Request $request ): array {
|
||||
$data = $request->get_params();
|
||||
|
||||
return $this->get_taxonomy_options( [ $data['post_type'] ] );
|
||||
}
|
||||
|
||||
protected function register() {
|
||||
register_rest_route( $this->controller->get_namespace(), $this->get_route(), [
|
||||
[
|
||||
'args' => [
|
||||
'post_type' => [
|
||||
'description' => 'The post type for which to fetch the list of taxonomies.',
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param ) {
|
||||
return ! empty( $param ) && is_string( $param );
|
||||
},
|
||||
],
|
||||
],
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => [ $this, 'get_items' ],
|
||||
'permission_callback' => function ( $request ) {
|
||||
return $this->permission_callback( $request );
|
||||
},
|
||||
],
|
||||
] );
|
||||
}
|
||||
}
|
||||
119
modules/loop-filter/data/endpoints/refresh-loop.php
Normal file
119
modules/loop-filter/data/endpoints/refresh-loop.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\LoopFilter\Data\Endpoints;
|
||||
|
||||
use Elementor\Widget_Base;
|
||||
use Elementor\Utils;
|
||||
use ElementorPro\Modules\LoopFilter\Module;
|
||||
use ElementorPro\Plugin;
|
||||
use ElementorPro\Core\Data\Endpoints\Refresh_Base;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Refresh_Loop extends Refresh_Base {
|
||||
public function get_name() : string {
|
||||
return 'refresh-loop';
|
||||
}
|
||||
|
||||
public function get_route() : string {
|
||||
return 'refresh-loop';
|
||||
}
|
||||
|
||||
public function get_updated_loop_widget_markup( \WP_REST_Request $request ): array {
|
||||
$data = $request->get_params();
|
||||
|
||||
/** @var Module $loop_filter */
|
||||
$loop_filter = Plugin::instance()->modules_manager->get_modules( 'loop-filter' );
|
||||
|
||||
$loop_filter->register_widget_filter( $data['widget_id'], $data['widget_filters'] );
|
||||
|
||||
if ( $this->is_edit_mode( $data['post_id'] ) ) {
|
||||
$widget_instance = Plugin::elementor()->elements_manager->create_element_instance( $data['widget_model'] );
|
||||
} else {
|
||||
$widget_instance = $this->create_widget_instance_from_db( $data['post_id'], $data['widget_id'] );
|
||||
}
|
||||
|
||||
set_query_var( 'pagination_base_url', $data['pagination_base_url'] );
|
||||
|
||||
ob_start();
|
||||
|
||||
/** @var Module $loop_filter */
|
||||
$loop_filter = Plugin::instance()->modules_manager->get_modules( 'loop-filter' );
|
||||
|
||||
add_filter( 'elementor/query/query_args', [ $loop_filter, 'filter_loop_query' ], 10, 2 );
|
||||
|
||||
/** @var Widget_Base $widget_instance */
|
||||
$widget_instance->render_content();
|
||||
|
||||
remove_filter( 'elementor/query/query_args', [ $loop_filter, 'filter_loop_query' ] );
|
||||
|
||||
$markup = ob_get_clean();
|
||||
|
||||
set_query_var( 'pagination_base_url', null );
|
||||
|
||||
return [
|
||||
'data' => $markup,
|
||||
];
|
||||
}
|
||||
|
||||
protected function register() {
|
||||
register_rest_route( $this->controller->get_namespace(), $this->get_route(), [
|
||||
[
|
||||
'args' => [
|
||||
'post_id' => [
|
||||
'description' => 'The post ID of the page containing the loop.',
|
||||
'type' => 'integer',
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param ) {
|
||||
return ! empty( $param ) && is_numeric( $param );
|
||||
},
|
||||
],
|
||||
'widget_id' => [
|
||||
'description' => 'The ID of the loop widget.',
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'validate_callback' => function ( $param ) {
|
||||
return $this->is_widget_id_valid( $param );
|
||||
},
|
||||
],
|
||||
'widget_filters' => [
|
||||
'description' => 'The filters for the loop widget.',
|
||||
'type' => 'object',
|
||||
'required' => true,
|
||||
'validate_callback' => function( $param ) {
|
||||
// TODO: Build a validator for this that iterates over all passed filters and validates them.
|
||||
return is_array( $param );
|
||||
},
|
||||
],
|
||||
'widget_model' => [
|
||||
'description' => 'The model of the loop widget. In Editor mode only.',
|
||||
'type' => 'object',
|
||||
'required' => false,
|
||||
'validate_callback' => function ( $param, $request ) {
|
||||
$params = $request->get_params();
|
||||
|
||||
if ( ! $this->is_edit_mode( $params['post_id'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->is_widget_model_valid( $param );
|
||||
},
|
||||
],
|
||||
'pagination_base_url' => [
|
||||
'required' => false,
|
||||
'validate_callback' => function ( $param ) {
|
||||
return filter_var( $param, FILTER_VALIDATE_URL );
|
||||
},
|
||||
'sanitize_callback' => 'esc_url_raw',
|
||||
],
|
||||
],
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => [ $this, 'get_updated_loop_widget_markup' ],
|
||||
'permission_callback' => function ( $request ) {
|
||||
return $this->permission_callback( $request, 'loop-grid' );
|
||||
},
|
||||
],
|
||||
] );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user