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