| 1 |
<?php |
| 2 |
namespace Elementor\Modules\DefaultValues\Data; |
| 3 |
|
| 4 |
use Elementor\Data\V2\Base\Exceptions\Error_404; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\Core\Kits\Exceptions\Kit_Not_Exists; |
| 7 |
use Elementor\Data\V2\Base\Controller as Base_Controller; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Controller extends Base_Controller { |
| 14 |
public function get_name() { |
| 15 |
return 'default-values'; |
| 16 |
} |
| 17 |
|
| 18 |
public function register_endpoints() { |
| 19 |
$type_validate_callback = function ( $param ) { |
| 20 |
$types = array_keys( Plugin::$instance->widgets_manager->get_widget_types() ); |
| 21 |
|
| 22 |
return in_array( $param, $types, true ); |
| 23 |
}; |
| 24 |
|
| 25 |
$this->index_endpoint->register_item_route( |
| 26 |
\WP_REST_Server::CREATABLE, |
| 27 |
[ |
| 28 |
'id_arg_name' => 'type', |
| 29 |
'id_arg_type_regex' => '[\w]+', |
| 30 |
'type' => [ |
| 31 |
'description' => 'Unique identifier for the object.', |
| 32 |
'required' => true, |
| 33 |
'type' => 'string', |
| 34 |
'validate_callback' => $type_validate_callback, |
| 35 |
], |
| 36 |
'settings' => [ |
| 37 |
'description' => 'All the default values for the requested type', |
| 38 |
'required' => true, |
| 39 |
'type' => 'object', |
| 40 |
], |
| 41 |
] |
| 42 |
); |
| 43 |
|
| 44 |
$this->index_endpoint->register_item_route( |
| 45 |
\WP_REST_Server::DELETABLE, |
| 46 |
[ |
| 47 |
'id_arg_name' => 'type', |
| 48 |
'id_arg_type_regex' => '[\w]+', |
| 49 |
'type' => [ |
| 50 |
'description' => 'Unique identifier for the object.', |
| 51 |
'required' => true, |
| 52 |
'type' => 'string', |
| 53 |
'validate_callback' => $type_validate_callback, |
| 54 |
], |
| 55 |
] |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
public function create_item( $request ) { |
| 60 |
try { |
| 61 |
$response = Repository::instance()->store( |
| 62 |
$request->get_param( 'type' ), |
| 63 |
$request->get_param( 'settings' ) |
| 64 |
); |
| 65 |
|
| 66 |
Plugin::$instance->files_manager->clear_cache(); |
| 67 |
|
| 68 |
return (object) $response; |
| 69 |
} catch ( Kit_Not_Exists $exception ) { |
| 70 |
return new Error_404( __( 'Kit not exists.', 'elementor' ), 'kit_not_exists' ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public function delete_item( $request ) { |
| 75 |
try { |
| 76 |
$response = Repository::instance()->delete( |
| 77 |
$request->get_param( 'type' ) |
| 78 |
); |
| 79 |
|
| 80 |
Plugin::$instance->files_manager->clear_cache(); |
| 81 |
|
| 82 |
return (object) $response; |
| 83 |
} catch ( Kit_Not_Exists $exception ) { |
| 84 |
return new Error_404( __( 'Kit not exists.', 'elementor' ), 'kit_not_exists' ); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|