| 1 |
<?php |
| 2 |
namespace Elementor\Includes\TemplateLibrary\Data\Endpoints; |
| 3 |
|
| 4 |
use Elementor\Data\V2\Base\Endpoint; |
| 5 |
use Elementor\Plugin; |
| 6 |
use Elementor\TemplateLibrary\Source_Local; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Templates extends Endpoint { |
| 13 |
|
| 14 |
protected function register() { |
| 15 |
parent::register(); |
| 16 |
|
| 17 |
$document_types = Plugin::$instance->documents->get_document_types( [ |
| 18 |
'show_in_library' => true, |
| 19 |
] ); |
| 20 |
|
| 21 |
$this->register_route( '', \WP_REST_Server::CREATABLE, [ |
| 22 |
'is_multi' => true, |
| 23 |
'title' => [ |
| 24 |
'required' => false, |
| 25 |
'type' => 'string', |
| 26 |
'description' => 'The title of the document', |
| 27 |
], |
| 28 |
'type' => [ |
| 29 |
'required' => true, |
| 30 |
'description' => 'The document type.', |
| 31 |
'type' => 'string', |
| 32 |
'enum' => array_keys( $document_types ), |
| 33 |
], |
| 34 |
'content' => [ |
| 35 |
'required' => false, |
| 36 |
'description' => 'Elementor data object', |
| 37 |
'type' => 'object', |
| 38 |
], |
| 39 |
] ); |
| 40 |
} |
| 41 |
|
| 42 |
public function get_name() { |
| 43 |
return 'templates'; |
| 44 |
} |
| 45 |
|
| 46 |
public function get_format() { |
| 47 |
return 'template-library/templates'; |
| 48 |
} |
| 49 |
|
| 50 |
public function get_items( $request ) { |
| 51 |
return Plugin::$instance->templates_manager->get_library_data( [ 'filter_sources' => [ $request->get_param( 'source' ) ] ] ); |
| 52 |
} |
| 53 |
|
| 54 |
public function create_items( $request ) { |
| 55 |
/** @var Source_Local $source */ |
| 56 |
$source = Plugin::$instance->templates_manager->get_source( 'local' ); |
| 57 |
|
| 58 |
$result = $source->save_item( [ |
| 59 |
'title' => $request->get_param( 'title' ), |
| 60 |
'type' => $request->get_param( 'type' ), |
| 61 |
'content' => $request->get_param( 'content' ), |
| 62 |
'page_settings' => $request->get_param( 'page_settings' ), |
| 63 |
] ); |
| 64 |
|
| 65 |
if ( is_wp_error( $result ) ) { |
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
return $source->get_item( $result ); |
| 70 |
} |
| 71 |
} |
| 72 |
|