| 1 |
<?php |
| 2 |
namespace Elementor\Core\Editor\Data\WidgetsConfig; |
| 3 |
|
| 4 |
use Elementor\Core\Utils\Collection; |
| 5 |
use Elementor\Data\V2\Base\Controller as Controller_Base; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
class Controller extends Controller_Base { |
| 9 |
public function get_name() { |
| 10 |
return 'widgets-config'; |
| 11 |
} |
| 12 |
|
| 13 |
public function register_endpoints() { |
| 14 |
$this->index_endpoint->register_item_route( |
| 15 |
\WP_REST_Server::READABLE, |
| 16 |
[ |
| 17 |
'id_arg_type_regex' => '[\w]+', |
| 18 |
'exclude' => [ |
| 19 |
'description' => 'ids of widgets that should be excluded', |
| 20 |
'type' => 'array', |
| 21 |
'default' => [], |
| 22 |
'items' => [ |
| 23 |
'type' => 'string', |
| 24 |
], |
| 25 |
], |
| 26 |
] |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_items_args( $methods ) { |
| 31 |
return [ |
| 32 |
'exclude' => [ |
| 33 |
'description' => 'ids of widgets that should be excluded', |
| 34 |
'type' => 'array', |
| 35 |
'default' => [], |
| 36 |
'items' => [ |
| 37 |
'type' => 'string', |
| 38 |
], |
| 39 |
], |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
public function get_items( $request ) { |
| 44 |
$config = ( new Collection( Plugin::$instance->widgets_manager->get_widget_types() ) ); |
| 45 |
|
| 46 |
$exclude = $request->get_param( 'exclude' ); |
| 47 |
|
| 48 |
if ( ! empty( $exclude ) ) { |
| 49 |
$config = $config->filter( function ( $widget, $widget_key ) use ( $exclude ) { |
| 50 |
return ! in_array( $widget_key, $exclude, true ); |
| 51 |
} ); |
| 52 |
} |
| 53 |
|
| 54 |
return (object) $config |
| 55 |
->map( function ( $widget ) { |
| 56 |
return $this->prepare_for_response( $widget ); |
| 57 |
} ) |
| 58 |
->all(); |
| 59 |
} |
| 60 |
|
| 61 |
public function get_item( $request ) { |
| 62 |
$widget = Plugin::$instance->widgets_manager->get_widget_types( $request->get_param( 'id' ) ); |
| 63 |
|
| 64 |
return (object) $this->prepare_for_response( $widget ); |
| 65 |
} |
| 66 |
|
| 67 |
public function get_permission_callback( $request ) { |
| 68 |
return current_user_can( 'edit_posts' ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @param $widget |
| 73 |
* |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
private function prepare_for_response( $widget ) { |
| 77 |
return [ |
| 78 |
'controls' => $widget->get_stack( false )['controls'], |
| 79 |
'tabs_controls' => $widget->get_tabs_controls(), |
| 80 |
]; |
| 81 |
} |
| 82 |
} |
| 83 |
|