| 1 |
<?php |
| 2 |
namespace Elementor\TemplateLibrary; |
| 3 |
|
| 4 |
use Elementor\Api; |
| 5 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 6 |
use Elementor\Core\Settings\Manager as SettingsManager; |
| 7 |
use Elementor\Includes\TemplateLibrary\Data\Controller; |
| 8 |
use Elementor\TemplateLibrary\Classes\Import_Images; |
| 9 |
use Elementor\Plugin; |
| 10 |
use Elementor\User; |
| 11 |
use Elementor\Utils; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Elementor template library manager. |
| 19 |
* |
| 20 |
* Elementor template library manager handler class is responsible for |
| 21 |
* initializing the template library. |
| 22 |
* |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class Manager { |
| 26 |
|
| 27 |
/** |
| 28 |
* Registered template sources. |
| 29 |
* |
| 30 |
* Holds a list of all the supported sources with their instances. |
| 31 |
* |
| 32 |
* @access protected |
| 33 |
* |
| 34 |
* @var Source_Base[] |
| 35 |
*/ |
| 36 |
protected $_registered_sources = []; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 37 |
|
| 38 |
/** |
| 39 |
* Imported template images. |
| 40 |
* |
| 41 |
* Holds an instance of `Import_Images` class. |
| 42 |
* |
| 43 |
* @access private |
| 44 |
* |
| 45 |
* @var Import_Images |
| 46 |
*/ |
| 47 |
private $_import_images = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore |
| 48 |
|
| 49 |
/** |
| 50 |
* Template library manager constructor. |
| 51 |
* |
| 52 |
* Initializing the template library manager by registering default template |
| 53 |
* sources and initializing ajax calls. |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* @access public |
| 57 |
*/ |
| 58 |
public function __construct() { |
| 59 |
Plugin::$instance->data_manager_v2->register_controller( new Controller() ); |
| 60 |
|
| 61 |
$this->register_default_sources(); |
| 62 |
|
| 63 |
$this->add_actions(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @since 2.3.0 |
| 68 |
* @access public |
| 69 |
*/ |
| 70 |
public function add_actions() { |
| 71 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 72 |
add_action( 'wp_ajax_elementor_library_direct_actions', [ $this, 'handle_direct_actions' ] ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get `Import_Images` instance. |
| 77 |
* |
| 78 |
* Retrieve the instance of the `Import_Images` class. |
| 79 |
* |
| 80 |
* @since 1.0.0 |
| 81 |
* @access public |
| 82 |
* |
| 83 |
* @return Import_Images Imported images instance. |
| 84 |
*/ |
| 85 |
public function get_import_images_instance() { |
| 86 |
if ( null === $this->_import_images ) { |
| 87 |
$this->_import_images = new Import_Images(); |
| 88 |
} |
| 89 |
|
| 90 |
return $this->_import_images; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Register template source. |
| 95 |
* |
| 96 |
* Used to register new template sources displayed in the template library. |
| 97 |
* |
| 98 |
* @since 1.0.0 |
| 99 |
* @access public |
| 100 |
* |
| 101 |
* @param string $source_class The name of source class. |
| 102 |
* @param array $args Optional. Class arguments. Default is an |
| 103 |
* empty array. |
| 104 |
* |
| 105 |
* @return \WP_Error|true True if the source was registered, `WP_Error` |
| 106 |
* otherwise. |
| 107 |
*/ |
| 108 |
public function register_source( $source_class, $args = [] ) { |
| 109 |
if ( ! class_exists( $source_class ) ) { |
| 110 |
return new \WP_Error( 'source_class_name_not_exists' ); |
| 111 |
} |
| 112 |
|
| 113 |
$source_instance = new $source_class( $args ); |
| 114 |
|
| 115 |
if ( ! $source_instance instanceof Source_Base ) { |
| 116 |
return new \WP_Error( 'wrong_instance_source' ); |
| 117 |
} |
| 118 |
|
| 119 |
$source_id = $source_instance->get_id(); |
| 120 |
|
| 121 |
if ( isset( $this->_registered_sources[ $source_id ] ) ) { |
| 122 |
return new \WP_Error( 'source_exists' ); |
| 123 |
} |
| 124 |
|
| 125 |
$this->_registered_sources[ $source_id ] = $source_instance; |
| 126 |
|
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Unregister template source. |
| 132 |
* |
| 133 |
* Remove an existing template sources from the list of registered template |
| 134 |
* sources. |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
* @deprecated 2.7.0 |
| 138 |
* @access public |
| 139 |
* |
| 140 |
* @param string $id The source ID. |
| 141 |
* |
| 142 |
* @return bool Whether the source was unregistered. |
| 143 |
*/ |
| 144 |
public function unregister_source( $id ) { |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get registered template sources. |
| 150 |
* |
| 151 |
* Retrieve registered template sources. |
| 152 |
* |
| 153 |
* @since 1.0.0 |
| 154 |
* @access public |
| 155 |
* |
| 156 |
* @return Source_Base[] Registered template sources. |
| 157 |
*/ |
| 158 |
public function get_registered_sources() { |
| 159 |
return $this->_registered_sources; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get template source. |
| 164 |
* |
| 165 |
* Retrieve single template sources for a given template ID. |
| 166 |
* |
| 167 |
* @since 1.0.0 |
| 168 |
* @access public |
| 169 |
* |
| 170 |
* @param string $id The source ID. |
| 171 |
* |
| 172 |
* @return false|Source_Base Template sources if one exist, False otherwise. |
| 173 |
*/ |
| 174 |
public function get_source( $id ) { |
| 175 |
$sources = $this->get_registered_sources(); |
| 176 |
|
| 177 |
if ( ! isset( $sources[ $id ] ) ) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
return $sources[ $id ]; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get templates. |
| 186 |
* |
| 187 |
* Retrieve all the templates from all the registered sources. |
| 188 |
* |
| 189 |
* @param array $filter_sources |
| 190 |
* @param bool $force_update |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public function get_templates( array $filter_sources = [], bool $force_update = false ): array { |
| 194 |
$templates = []; |
| 195 |
|
| 196 |
foreach ( $this->get_registered_sources() as $source ) { |
| 197 |
if ( ! empty( $filter_sources ) && ! in_array( $source->get_id(), $filter_sources, true ) ) { |
| 198 |
continue; |
| 199 |
} |
| 200 |
|
| 201 |
$templates = array_merge( $templates, $source->get_items( [ 'force_update' => $force_update ] ) ); |
| 202 |
} |
| 203 |
|
| 204 |
return $templates; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get library data. |
| 209 |
* |
| 210 |
* Retrieve the library data. |
| 211 |
* |
| 212 |
* @since 1.9.0 |
| 213 |
* @access public |
| 214 |
* |
| 215 |
* @param array $args Library arguments. |
| 216 |
* |
| 217 |
* @return array Library data. |
| 218 |
*/ |
| 219 |
public function get_library_data( array $args ) { |
| 220 |
$library_data = Api::get_library_data( ! empty( $args['sync'] ) ); |
| 221 |
|
| 222 |
if ( empty( $library_data ) ) { |
| 223 |
return $library_data; |
| 224 |
} |
| 225 |
|
| 226 |
// Ensure all document are registered. |
| 227 |
Plugin::$instance->documents->get_document_types(); |
| 228 |
|
| 229 |
$filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : []; |
| 230 |
$force_update = ! empty( $args['sync'] ); |
| 231 |
|
| 232 |
return [ |
| 233 |
'templates' => $this->get_templates( $filter_sources, $force_update ), |
| 234 |
'config' => $library_data['types_data'], |
| 235 |
]; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Save template. |
| 240 |
* |
| 241 |
* Save new or update existing template on the database. |
| 242 |
* |
| 243 |
* @since 1.0.0 |
| 244 |
* @access public |
| 245 |
* |
| 246 |
* @param array $args Template arguments. |
| 247 |
* |
| 248 |
* @return \WP_Error|int The ID of the saved/updated template. |
| 249 |
*/ |
| 250 |
public function save_template( array $args ) { |
| 251 |
$validate_args = $this->ensure_args( [ 'post_id', 'source', 'content', 'type' ], $args ); |
| 252 |
|
| 253 |
if ( is_wp_error( $validate_args ) ) { |
| 254 |
return $validate_args; |
| 255 |
} |
| 256 |
|
| 257 |
$source = $this->get_source( $args['source'] ); |
| 258 |
|
| 259 |
if ( ! $source ) { |
| 260 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 261 |
} |
| 262 |
|
| 263 |
$args['content'] = json_decode( $args['content'], true ); |
| 264 |
|
| 265 |
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] ); |
| 266 |
|
| 267 |
$args['page_settings'] = $page->get_data( 'settings' ); |
| 268 |
|
| 269 |
$template_id = $source->save_item( $args ); |
| 270 |
|
| 271 |
if ( is_wp_error( $template_id ) ) { |
| 272 |
return $template_id; |
| 273 |
} |
| 274 |
|
| 275 |
return $source->get_item( $template_id ); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Update template. |
| 280 |
* |
| 281 |
* Update template on the database. |
| 282 |
* |
| 283 |
* @since 1.0.0 |
| 284 |
* @access public |
| 285 |
* |
| 286 |
* @param array $template_data New template data. |
| 287 |
* |
| 288 |
* @return \WP_Error|Source_Base Template sources instance if the templates |
| 289 |
* was updated, `WP_Error` otherwise. |
| 290 |
*/ |
| 291 |
public function update_template( array $template_data ) { |
| 292 |
$validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data ); |
| 293 |
|
| 294 |
if ( is_wp_error( $validate_args ) ) { |
| 295 |
return $validate_args; |
| 296 |
} |
| 297 |
|
| 298 |
$source = $this->get_source( $template_data['source'] ); |
| 299 |
|
| 300 |
if ( ! $source ) { |
| 301 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 302 |
} |
| 303 |
|
| 304 |
$template_data['content'] = json_decode( $template_data['content'], true ); |
| 305 |
|
| 306 |
$update = $source->update_item( $template_data ); |
| 307 |
|
| 308 |
if ( is_wp_error( $update ) ) { |
| 309 |
return $update; |
| 310 |
} |
| 311 |
|
| 312 |
return $source->get_item( $template_data['id'] ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Update templates. |
| 317 |
* |
| 318 |
* Update template on the database. |
| 319 |
* |
| 320 |
* @since 1.0.0 |
| 321 |
* @access public |
| 322 |
* |
| 323 |
* @param array $args Template arguments. |
| 324 |
* |
| 325 |
* @return \WP_Error|true True if templates updated, `WP_Error` otherwise. |
| 326 |
*/ |
| 327 |
public function update_templates( array $args ) { |
| 328 |
foreach ( $args['templates'] as $template_data ) { |
| 329 |
$result = $this->update_template( $template_data ); |
| 330 |
|
| 331 |
if ( is_wp_error( $result ) ) { |
| 332 |
return $result; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return true; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Get template data. |
| 341 |
* |
| 342 |
* Retrieve the template data. |
| 343 |
* |
| 344 |
* @since 1.5.0 |
| 345 |
* @access public |
| 346 |
* |
| 347 |
* @param array $args Template arguments. |
| 348 |
* |
| 349 |
* @return \WP_Error|bool|array ?? |
| 350 |
*/ |
| 351 |
public function get_template_data( array $args ) { |
| 352 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 353 |
|
| 354 |
if ( is_wp_error( $validate_args ) ) { |
| 355 |
return $validate_args; |
| 356 |
} |
| 357 |
|
| 358 |
if ( isset( $args['edit_mode'] ) ) { |
| 359 |
Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] ); |
| 360 |
} |
| 361 |
|
| 362 |
$source = $this->get_source( $args['source'] ); |
| 363 |
|
| 364 |
if ( ! $source ) { |
| 365 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 366 |
} |
| 367 |
|
| 368 |
do_action( 'elementor/template-library/before_get_source_data', $args, $source ); |
| 369 |
|
| 370 |
$data = $source->get_data( $args ); |
| 371 |
|
| 372 |
do_action( 'elementor/template-library/after_get_source_data', $args, $source ); |
| 373 |
|
| 374 |
return $data; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Delete template. |
| 379 |
* |
| 380 |
* Delete template from the database. |
| 381 |
* |
| 382 |
* @since 1.0.0 |
| 383 |
* @access public |
| 384 |
* |
| 385 |
* @param array $args Template arguments. |
| 386 |
* |
| 387 |
* @return \WP_Post|\WP_Error|false|null Post data on success, false or null |
| 388 |
* or 'WP_Error' on failure. |
| 389 |
*/ |
| 390 |
public function delete_template( array $args ) { |
| 391 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 392 |
|
| 393 |
if ( is_wp_error( $validate_args ) ) { |
| 394 |
return $validate_args; |
| 395 |
} |
| 396 |
|
| 397 |
$source = $this->get_source( $args['source'] ); |
| 398 |
|
| 399 |
if ( ! $source ) { |
| 400 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 401 |
} |
| 402 |
|
| 403 |
return $source->delete_template( $args['template_id'] ); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Export template. |
| 408 |
* |
| 409 |
* Export template to a file after ensuring it is a valid Elementor template |
| 410 |
* and checking user permissions for private posts. |
| 411 |
* |
| 412 |
* @since 1.0.0 |
| 413 |
* @access public |
| 414 |
* |
| 415 |
* @param array $args Template arguments. |
| 416 |
* |
| 417 |
* @return mixed Whether the export succeeded or failed. |
| 418 |
*/ |
| 419 |
public function export_template( array $args ) { |
| 420 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 421 |
|
| 422 |
if ( is_wp_error( $validate_args ) ) { |
| 423 |
return $validate_args; |
| 424 |
} |
| 425 |
|
| 426 |
$post_id = intval( $args['template_id'] ); |
| 427 |
$post_status = get_post_status( $post_id ); |
| 428 |
|
| 429 |
if ( get_post_type( $post_id ) !== Source_Local::CPT ) { |
| 430 |
return new \WP_Error( 'template_error', esc_html__( 'Invalid template type or template does not exist.', 'elementor' ) ); |
| 431 |
} |
| 432 |
|
| 433 |
if ( 'private' === $post_status && ! current_user_can( 'read_private_posts', $post_id ) ) { |
| 434 |
return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to access this template.', 'elementor' ) ); |
| 435 |
} |
| 436 |
|
| 437 |
if ( 'publish' !== $post_status && ! current_user_can( 'edit_post', $post_id ) ) { |
| 438 |
return new \WP_Error( 'template_error', esc_html__( 'You do not have permission to export this template.', 'elementor' ) ); |
| 439 |
} |
| 440 |
|
| 441 |
$source = $this->get_source( $args['source'] ); |
| 442 |
|
| 443 |
if ( ! $source ) { |
| 444 |
return new \WP_Error( 'template_error', 'Template source not found' ); |
| 445 |
} |
| 446 |
|
| 447 |
return $source->export_template( $args['template_id'] ); |
| 448 |
} |
| 449 |
|
| 450 |
/** |
| 451 |
* @since 2.3.0 |
| 452 |
* @access public |
| 453 |
*/ |
| 454 |
public function direct_import_template() { |
| 455 |
/** @var Source_Local $source */ |
| 456 |
$source = $this->get_source( 'local' ); |
| 457 |
$file = Utils::get_super_global_value( $_FILES, 'file' ); |
| 458 |
return $source->import_template( $file['name'], $file['tmp_name'] ); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Import template. |
| 463 |
* |
| 464 |
* Import template from a file. |
| 465 |
* |
| 466 |
* @since 1.0.0 |
| 467 |
* @access public |
| 468 |
* |
| 469 |
* @param array $data |
| 470 |
* |
| 471 |
* @return mixed Whether the export succeeded or failed. |
| 472 |
*/ |
| 473 |
public function import_template( array $data ) { |
| 474 |
// If the template is a JSON file, allow uploading it. |
| 475 |
add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 476 |
add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 477 |
|
| 478 |
// Imported templates can be either JSON files, or Zip files containing multiple JSON files |
| 479 |
$upload_result = Plugin::$instance->uploads_manager->handle_elementor_upload( $data, [ 'zip', 'json' ] ); |
| 480 |
|
| 481 |
remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 482 |
remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 483 |
|
| 484 |
if ( is_wp_error( $upload_result ) ) { |
| 485 |
Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); |
| 486 |
|
| 487 |
return $upload_result; |
| 488 |
} |
| 489 |
|
| 490 |
/** @var Source_Local $source_local */ |
| 491 |
$source_local = $this->get_source( 'local' ); |
| 492 |
|
| 493 |
$import_result = $source_local->import_template( $upload_result['name'], $upload_result['tmp_name'] ); |
| 494 |
|
| 495 |
// Remove the temporary directory generated for the stream-uploaded file. |
| 496 |
Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); |
| 497 |
|
| 498 |
return $import_result; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Enable JSON Template Upload |
| 503 |
* |
| 504 |
* Runs on the 'elementor/files/allow-file-type/json' Uploads Manager filter. |
| 505 |
* |
| 506 |
* @since 3.5.0 |
| 507 |
* @access public |
| 508 |
* |
| 509 |
* return bool |
| 510 |
*/ |
| 511 |
public function enable_json_template_upload() { |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Mark template as favorite. |
| 517 |
* |
| 518 |
* Add the template to the user favorite templates. |
| 519 |
* |
| 520 |
* @since 1.9.0 |
| 521 |
* @access public |
| 522 |
* |
| 523 |
* @param array $args Template arguments. |
| 524 |
* |
| 525 |
* @return mixed Whether the template marked as favorite. |
| 526 |
*/ |
| 527 |
public function mark_template_as_favorite( $args ) { |
| 528 |
$validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args ); |
| 529 |
|
| 530 |
if ( is_wp_error( $validate_args ) ) { |
| 531 |
return $validate_args; |
| 532 |
} |
| 533 |
|
| 534 |
$source = $this->get_source( $args['source'] ); |
| 535 |
|
| 536 |
return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) ); |
| 537 |
} |
| 538 |
|
| 539 |
public function import_from_json( array $args ) { |
| 540 |
$validate_args = $this->ensure_args( [ 'editor_post_id', 'elements' ], $args ); |
| 541 |
|
| 542 |
if ( is_wp_error( $validate_args ) ) { |
| 543 |
return $validate_args; |
| 544 |
} |
| 545 |
|
| 546 |
$elements = json_decode( $args['elements'], true ); |
| 547 |
|
| 548 |
$document = Plugin::$instance->documents->get( $args['editor_post_id'] ); |
| 549 |
if ( ! $document ) { |
| 550 |
return new \WP_Error( 'template_error', 'Document not found.' ); |
| 551 |
} |
| 552 |
|
| 553 |
$import_data = $document->get_import_data( [ 'content' => $elements ] ); |
| 554 |
|
| 555 |
return $import_data['content']; |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Register default template sources. |
| 560 |
* |
| 561 |
* Register the 'local' and 'remote' template sources that Elementor use by |
| 562 |
* default. |
| 563 |
* |
| 564 |
* @since 1.0.0 |
| 565 |
* @access private |
| 566 |
*/ |
| 567 |
private function register_default_sources() { |
| 568 |
$sources = [ |
| 569 |
'local', |
| 570 |
'remote', |
| 571 |
]; |
| 572 |
|
| 573 |
foreach ( $sources as $source_filename ) { |
| 574 |
$class_name = ucwords( $source_filename ); |
| 575 |
$class_name = str_replace( '-', '_', $class_name ); |
| 576 |
|
| 577 |
$this->register_source( __NAMESPACE__ . '\Source_' . $class_name ); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Handle ajax request. |
| 583 |
* |
| 584 |
* Fire authenticated ajax actions for any given ajax request. |
| 585 |
* |
| 586 |
* @since 1.0.0 |
| 587 |
* @access private |
| 588 |
* |
| 589 |
* @param string $ajax_request Ajax request. |
| 590 |
* |
| 591 |
* @param array $data |
| 592 |
* |
| 593 |
* @return mixed |
| 594 |
* @throws \Exception |
| 595 |
*/ |
| 596 |
private function handle_ajax_request( $ajax_request, array $data ) { |
| 597 |
if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 598 |
throw new \Exception( 'Access denied.' ); |
| 599 |
} |
| 600 |
|
| 601 |
if ( ! empty( $data['editor_post_id'] ) ) { |
| 602 |
$editor_post_id = absint( $data['editor_post_id'] ); |
| 603 |
|
| 604 |
if ( ! get_post( $editor_post_id ) ) { |
| 605 |
throw new \Exception( 'Post not found.' ); |
| 606 |
} |
| 607 |
|
| 608 |
Plugin::$instance->db->switch_to_post( $editor_post_id ); |
| 609 |
} |
| 610 |
|
| 611 |
$result = call_user_func( [ $this, $ajax_request ], $data ); |
| 612 |
|
| 613 |
if ( is_wp_error( $result ) ) { |
| 614 |
throw new \Exception( $result->get_error_message() ); |
| 615 |
} |
| 616 |
|
| 617 |
return $result; |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Init ajax calls. |
| 622 |
* |
| 623 |
* Initialize template library ajax calls for allowed ajax requests. |
| 624 |
* |
| 625 |
* @since 2.3.0 |
| 626 |
* @access public |
| 627 |
* |
| 628 |
* @param Ajax $ajax |
| 629 |
*/ |
| 630 |
public function register_ajax_actions( Ajax $ajax ) { |
| 631 |
$library_ajax_requests = [ |
| 632 |
'get_library_data', |
| 633 |
'get_template_data', |
| 634 |
'save_template', |
| 635 |
'update_templates', |
| 636 |
'delete_template', |
| 637 |
'import_template', |
| 638 |
'mark_template_as_favorite', |
| 639 |
'import_from_json', |
| 640 |
]; |
| 641 |
|
| 642 |
foreach ( $library_ajax_requests as $ajax_request ) { |
| 643 |
$ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) { |
| 644 |
return $this->handle_ajax_request( $ajax_request, $data ); |
| 645 |
} ); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* @since 2.3.0 |
| 651 |
* @access public |
| 652 |
*/ |
| 653 |
public function handle_direct_actions() { |
| 654 |
if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 655 |
return; |
| 656 |
} |
| 657 |
|
| 658 |
/** @var Ajax $ajax */ |
| 659 |
$ajax = Plugin::$instance->common->get_component( 'ajax' ); |
| 660 |
|
| 661 |
if ( ! $ajax->verify_request_nonce() ) { |
| 662 |
$this->handle_direct_action_error( 'Access Denied' ); |
| 663 |
} |
| 664 |
|
| 665 |
$action = Utils::get_super_global_value( $_REQUEST, 'library_action' ); // phpcs:ignore -- Nonce already verified. |
| 666 |
|
| 667 |
$whitelist_methods = [ |
| 668 |
'export_template', |
| 669 |
'direct_import_template', |
| 670 |
]; |
| 671 |
|
| 672 |
if ( in_array( $action, $whitelist_methods, true ) ) { |
| 673 |
$result = $this->$action( $_REQUEST ); // phpcs:ignore -- Nonce already verified. |
| 674 |
} else { |
| 675 |
$result = new \WP_Error( 'method_not_exists', 'Method Not exists' ); |
| 676 |
} |
| 677 |
|
| 678 |
if ( is_wp_error( $result ) ) { |
| 679 |
/** @var \WP_Error $result */ |
| 680 |
$this->handle_direct_action_error( $result->get_error_message() . '.' ); |
| 681 |
} |
| 682 |
|
| 683 |
$callback = "on_{$action}_success"; |
| 684 |
|
| 685 |
if ( method_exists( $this, $callback ) ) { |
| 686 |
$this->$callback( $result ); |
| 687 |
} |
| 688 |
|
| 689 |
die; |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
* On successful template import. |
| 694 |
* |
| 695 |
* Redirect the user to the template library after template import was |
| 696 |
* successful finished. |
| 697 |
* |
| 698 |
* @since 2.3.0 |
| 699 |
* @access private |
| 700 |
*/ |
| 701 |
private function on_direct_import_template_success() { |
| 702 |
wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) ); |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* @since 2.3.0 |
| 707 |
* @access private |
| 708 |
*/ |
| 709 |
private function handle_direct_action_error( $message ) { |
| 710 |
_default_wp_die_handler( $message, 'Elementor Library' ); |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Ensure arguments exist. |
| 715 |
* |
| 716 |
* Checks whether the required arguments exist in the specified arguments. |
| 717 |
* |
| 718 |
* @since 1.0.0 |
| 719 |
* @access private |
| 720 |
* |
| 721 |
* @param array $required_args Required arguments to check whether they |
| 722 |
* exist. |
| 723 |
* @param array $specified_args The list of all the specified arguments to |
| 724 |
* check against. |
| 725 |
* |
| 726 |
* @return \WP_Error|true True on success, 'WP_Error' otherwise. |
| 727 |
*/ |
| 728 |
private function ensure_args( array $required_args, array $specified_args ) { |
| 729 |
$not_specified_args = array_diff( $required_args, array_keys( $specified_args ) ); |
| 730 |
|
| 731 |
if ( $not_specified_args ) { |
| 732 |
return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) ); |
| 733 |
} |
| 734 |
|
| 735 |
return true; |
| 736 |
} |
| 737 |
} |
| 738 |
|