| 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 |
$force_update = ! empty( $args['sync'] ); |
| 243 |
|
| 244 |
$library_data = Api::get_library_data( $force_update ); |
| 245 |
|
| 246 |
if ( empty( $library_data ) ) { |
| 247 |
return $library_data; |
| 248 |
} |
| 249 |
|
| 250 |
// Ensure all document are registered. |
| 251 |
Plugin::$instance->documents->get_document_types(); |
| 252 |
|
| 253 |
$filter_sources = ! empty( $args['filter_sources'] ) ? $args['filter_sources'] : []; |
| 254 |
|
| 255 |
$full_library_data = [ |
| 256 |
'templates' => $this->get_templates( $filter_sources, $force_update ), |
| 257 |
'config' => $library_data['types_data'], |
| 258 |
]; |
| 259 |
|
| 260 |
/** |
| 261 |
* Filter the full library data. |
| 262 |
* |
| 263 |
* @since 3.32.2 |
| 264 |
* @param-out $full_library_data - 'templates' and 'config' data ('config' holds the list of categories). |
| 265 |
*/ |
| 266 |
return apply_filters( 'elementor/library/full-data', $full_library_data ); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Save template. |
| 271 |
* |
| 272 |
* Save new or update existing template on the database. |
| 273 |
* |
| 274 |
* @since 1.0.0 |
| 275 |
* @access public |
| 276 |
* |
| 277 |
* @param array $args Template arguments. |
| 278 |
* |
| 279 |
* @return \WP_Error|int The ID of the saved/updated template. |
| 280 |
*/ |
| 281 |
public function save_template( array $args ) { |
| 282 |
$validate_args = $this->ensure_args( [ 'post_id', 'source', 'content', 'type' ], $args ); |
| 283 |
|
| 284 |
if ( is_wp_error( $validate_args ) ) { |
| 285 |
return $validate_args; |
| 286 |
} |
| 287 |
|
| 288 |
$sources = (array) $args['source']; // BC |
| 289 |
$results = []; |
| 290 |
|
| 291 |
foreach ( $sources as $source ) { |
| 292 |
$args_copy = $args; |
| 293 |
$args_copy['source'] = $source; |
| 294 |
$results[] = $this->save_template_item( $args_copy ); |
| 295 |
} |
| 296 |
|
| 297 |
return 1 === count( $results ) ? $results[0] : $results; |
| 298 |
} |
| 299 |
|
| 300 |
private function save_template_item( array $args ) { |
| 301 |
$source = $this->get_source( $args['source'] ); |
| 302 |
|
| 303 |
if ( ! $source ) { |
| 304 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 305 |
} |
| 306 |
|
| 307 |
$args['content'] = json_decode( $args['content'], true ); |
| 308 |
|
| 309 |
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['post_id'] ); |
| 310 |
|
| 311 |
$args['page_settings'] = $page->get_data( 'settings' ); |
| 312 |
|
| 313 |
$template_id = $source->save_item( $args ); |
| 314 |
|
| 315 |
if ( is_wp_error( $template_id ) ) { |
| 316 |
return $template_id; |
| 317 |
} |
| 318 |
|
| 319 |
return $source->get_item( $template_id ); |
| 320 |
} |
| 321 |
|
| 322 |
public function move_template( array $args ) { |
| 323 |
$validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 324 |
|
| 325 |
if ( is_wp_error( $validate_args ) ) { |
| 326 |
return $validate_args; |
| 327 |
} |
| 328 |
|
| 329 |
$args['source'] = $args['source'][0]; |
| 330 |
|
| 331 |
$result = $this->move_template_item( $args ); |
| 332 |
|
| 333 |
if ( ! $this->is_action_to_same_source( $args ) ) { |
| 334 |
$this->delete_template( [ |
| 335 |
'source' => $args['from_source'], |
| 336 |
'template_id' => $args['from_template_id'], |
| 337 |
] ); |
| 338 |
} |
| 339 |
|
| 340 |
return $result; |
| 341 |
} |
| 342 |
|
| 343 |
private function move_template_item( array $args ) { |
| 344 |
$validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 345 |
|
| 346 |
if ( is_wp_error( $validate_args ) ) { |
| 347 |
return $validate_args; |
| 348 |
} |
| 349 |
|
| 350 |
$source = $this->get_source( $args['source'] ); |
| 351 |
|
| 352 |
if ( ! $source ) { |
| 353 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 354 |
} |
| 355 |
|
| 356 |
if ( $this->is_action_to_same_source( $args ) ) { |
| 357 |
return $source->move_template_to_folder( $args ); |
| 358 |
} |
| 359 |
|
| 360 |
if ( 'local' === $args['from_source'] ) { |
| 361 |
$args = $this->format_args_for_single_action_from_local_to_cloud( $args ); |
| 362 |
} |
| 363 |
|
| 364 |
if ( 'cloud' === $args['from_source'] ) { |
| 365 |
$args = $this->format_args_for_single_action_from_cloud_to_local( $args ); |
| 366 |
} |
| 367 |
|
| 368 |
$template_id = $source->save_item( $args ); |
| 369 |
|
| 370 |
if ( is_wp_error( $template_id ) ) { |
| 371 |
return $template_id; |
| 372 |
} |
| 373 |
|
| 374 |
return $source->get_item( $template_id ); |
| 375 |
} |
| 376 |
|
| 377 |
public function copy_template( array $args ) { |
| 378 |
$validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 379 |
|
| 380 |
if ( is_wp_error( $validate_args ) ) { |
| 381 |
return $validate_args; |
| 382 |
} |
| 383 |
|
| 384 |
$source = $this->get_source( $args['source'][0] ); |
| 385 |
|
| 386 |
if ( ! $source ) { |
| 387 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 388 |
} |
| 389 |
|
| 390 |
if ( 'local' === $args['from_source'] ) { |
| 391 |
$args = $this->format_args_for_single_action_from_local_to_cloud( $args ); |
| 392 |
} |
| 393 |
|
| 394 |
if ( 'cloud' === $args['from_source'] ) { |
| 395 |
$args = $this->format_args_for_single_action_from_cloud_to_local( $args ); |
| 396 |
} |
| 397 |
|
| 398 |
$template_id = $source->save_item( $args ); |
| 399 |
|
| 400 |
if ( is_wp_error( $template_id ) ) { |
| 401 |
return $template_id; |
| 402 |
} |
| 403 |
|
| 404 |
return $source->get_item( $template_id ); |
| 405 |
} |
| 406 |
|
| 407 |
private function is_action_to_same_source( $args ) { |
| 408 |
return $args['source'] === $args['from_source']; |
| 409 |
} |
| 410 |
|
| 411 |
private function format_args_for_single_action_from_local_to_cloud( $args ) { |
| 412 |
if ( ! $this->is_allowed_to_read_template( [ |
| 413 |
'source' => $args['from_source'], |
| 414 |
'template_id' => $args['from_template_id'], |
| 415 |
] ) ) { |
| 416 |
return new \WP_Error( |
| 417 |
'template_error', |
| 418 |
esc_html__( 'You do not have permission to access this template.', 'elementor' ) |
| 419 |
); |
| 420 |
} |
| 421 |
|
| 422 |
$document = Plugin::$instance->documents->get( $args['from_template_id'] ); |
| 423 |
|
| 424 |
if ( ! $document ) { |
| 425 |
return new \WP_Error( 'template_error', 'Document not found.' ); |
| 426 |
} |
| 427 |
|
| 428 |
$args['content'] = $document->get_elements_data(); |
| 429 |
|
| 430 |
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $args['from_template_id'] ); |
| 431 |
$args['page_settings'] = $page->get_data( 'settings' ); |
| 432 |
|
| 433 |
return $args; |
| 434 |
} |
| 435 |
|
| 436 |
private function format_args_for_single_action_from_cloud_to_local( $args ) { |
| 437 |
$from_source = $this->get_source( $args['from_source'] ); |
| 438 |
|
| 439 |
if ( ! $from_source ) { |
| 440 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 441 |
} |
| 442 |
|
| 443 |
$data = $from_source->get_item( $args['from_template_id'] ); |
| 444 |
|
| 445 |
if ( is_wp_error( $data ) || empty( $data['content'] ) ) { |
| 446 |
return new \WP_Error( 'template_error', 'Unable to format template args.' ); |
| 447 |
} |
| 448 |
|
| 449 |
$decoded_data = json_decode( $data['content'], true ); |
| 450 |
$args['content'] = $decoded_data['content']; |
| 451 |
$args['page_settings'] = $decoded_data['page_settings']; |
| 452 |
|
| 453 |
return $args; |
| 454 |
} |
| 455 |
|
| 456 |
/** |
| 457 |
* Update template. |
| 458 |
* |
| 459 |
* Update template on the database. |
| 460 |
* |
| 461 |
* @since 1.0.0 |
| 462 |
* @access public |
| 463 |
* |
| 464 |
* @param array $template_data New template data. |
| 465 |
* |
| 466 |
* @return \WP_Error|Source_Base Template sources instance if the templates |
| 467 |
* was updated, `WP_Error` otherwise. |
| 468 |
*/ |
| 469 |
public function update_template( array $template_data ) { |
| 470 |
$validate_args = $this->ensure_args( [ 'source', 'content', 'type' ], $template_data ); |
| 471 |
|
| 472 |
if ( is_wp_error( $validate_args ) ) { |
| 473 |
return $validate_args; |
| 474 |
} |
| 475 |
|
| 476 |
$source = $this->get_source( $template_data['source'] ); |
| 477 |
|
| 478 |
if ( ! $source ) { |
| 479 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 480 |
} |
| 481 |
|
| 482 |
$template_data['content'] = json_decode( $template_data['content'], true ); |
| 483 |
|
| 484 |
$update = $source->update_item( $template_data ); |
| 485 |
|
| 486 |
if ( is_wp_error( $update ) ) { |
| 487 |
return $update; |
| 488 |
} |
| 489 |
|
| 490 |
return $source->get_item( $template_data['id'] ); |
| 491 |
} |
| 492 |
|
| 493 |
public function rename_template( array $template_data ) { |
| 494 |
$validate_args = $this->ensure_args( [ 'source', 'title', 'id' ], $template_data ); |
| 495 |
|
| 496 |
if ( is_wp_error( $validate_args ) ) { |
| 497 |
return $validate_args; |
| 498 |
} |
| 499 |
|
| 500 |
$source = $this->get_source( $template_data['source'] ); |
| 501 |
|
| 502 |
if ( ! $source ) { |
| 503 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 504 |
} |
| 505 |
|
| 506 |
$update = $source->update_item( $template_data ); |
| 507 |
|
| 508 |
if ( is_wp_error( $update ) ) { |
| 509 |
return $update; |
| 510 |
} |
| 511 |
|
| 512 |
return $source->get_item( $template_data['id'] ); |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Update templates. |
| 517 |
* |
| 518 |
* Update template on the database. |
| 519 |
* |
| 520 |
* @since 1.0.0 |
| 521 |
* @access public |
| 522 |
* |
| 523 |
* @param array $args Template arguments. |
| 524 |
* |
| 525 |
* @return \WP_Error|true True if templates updated, `WP_Error` otherwise. |
| 526 |
*/ |
| 527 |
public function update_templates( array $args ) { |
| 528 |
foreach ( $args['templates'] as $template_data ) { |
| 529 |
$result = $this->update_template( $template_data ); |
| 530 |
|
| 531 |
if ( is_wp_error( $result ) ) { |
| 532 |
return $result; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
return true; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Get template data. |
| 541 |
* |
| 542 |
* Retrieve the template data. |
| 543 |
* |
| 544 |
* @since 1.5.0 |
| 545 |
* @access public |
| 546 |
* |
| 547 |
* @param array $args Template arguments. |
| 548 |
* |
| 549 |
* @return \WP_Error|bool|array ?? |
| 550 |
*/ |
| 551 |
public function get_template_data( array $args ) { |
| 552 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 553 |
|
| 554 |
if ( is_wp_error( $validate_args ) ) { |
| 555 |
return $validate_args; |
| 556 |
} |
| 557 |
|
| 558 |
if ( ! $this->is_allowed_to_read_template( $args ) ) { |
| 559 |
return new \WP_Error( |
| 560 |
'template_error', |
| 561 |
esc_html__( 'You do not have permission to access this template.', 'elementor' ) |
| 562 |
); |
| 563 |
} |
| 564 |
|
| 565 |
if ( isset( $args['edit_mode'] ) ) { |
| 566 |
Plugin::$instance->editor->set_edit_mode( $args['edit_mode'] ); |
| 567 |
} |
| 568 |
|
| 569 |
$source = $this->get_source( $args['source'] ); |
| 570 |
|
| 571 |
if ( ! $source ) { |
| 572 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 573 |
} |
| 574 |
|
| 575 |
do_action( 'elementor/template-library/before_get_source_data', $args, $source ); |
| 576 |
|
| 577 |
$data = $source->get_data( $args ); |
| 578 |
|
| 579 |
do_action( 'elementor/template-library/after_get_source_data', $args, $source ); |
| 580 |
|
| 581 |
return $data; |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* Delete template. |
| 586 |
* |
| 587 |
* Delete template from the database. |
| 588 |
* |
| 589 |
* @since 1.0.0 |
| 590 |
* @access public |
| 591 |
* |
| 592 |
* @param array $args Template arguments. |
| 593 |
* |
| 594 |
* @return \WP_Post|\WP_Error|false|null Post data on success, false or null |
| 595 |
* or 'WP_Error' on failure. |
| 596 |
*/ |
| 597 |
public function delete_template( array $args ) { |
| 598 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 599 |
|
| 600 |
if ( is_wp_error( $validate_args ) ) { |
| 601 |
return $validate_args; |
| 602 |
} |
| 603 |
|
| 604 |
$source = $this->get_source( $args['source'] ); |
| 605 |
|
| 606 |
if ( ! $source ) { |
| 607 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 608 |
} |
| 609 |
|
| 610 |
return $source->delete_template( $args['template_id'] ); |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Export template. |
| 615 |
* |
| 616 |
* Export template to a file after ensuring it is a valid Elementor template |
| 617 |
* and checking user permissions for private posts. |
| 618 |
* |
| 619 |
* @since 1.0.0 |
| 620 |
* @access public |
| 621 |
* |
| 622 |
* @param array $args Template arguments. |
| 623 |
* |
| 624 |
* @return mixed Whether the export succeeded or failed. |
| 625 |
*/ |
| 626 |
public function export_template( array $args ) { |
| 627 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $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->export_template( $args['template_id'] ); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* @since 2.3.0 |
| 644 |
* @access public |
| 645 |
*/ |
| 646 |
public function direct_import_template() { |
| 647 |
/** @var Source_Local $source */ |
| 648 |
$source = $this->get_source( 'local' ); |
| 649 |
$file = Utils::get_super_global_value( $_FILES, 'file' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 650 |
return $source->import_template( $file['name'], $file['tmp_name'] ); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Import template. |
| 655 |
* |
| 656 |
* Import template from a file. |
| 657 |
* |
| 658 |
* @since 1.0.0 |
| 659 |
* @access public |
| 660 |
* |
| 661 |
* @param array $data |
| 662 |
* |
| 663 |
* @return mixed Whether the export succeeded or failed. |
| 664 |
*/ |
| 665 |
public function import_template( array $data ) { |
| 666 |
// If the template is a JSON file, allow uploading it. |
| 667 |
add_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 668 |
add_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 669 |
|
| 670 |
// Imported templates can be either JSON files, or Zip files containing multiple JSON files |
| 671 |
$upload_result = Plugin::$instance->uploads_manager->handle_elementor_upload( $data, [ 'zip', 'json' ] ); |
| 672 |
|
| 673 |
remove_filter( 'elementor/files/allow-file-type/json', [ $this, 'enable_json_template_upload' ] ); |
| 674 |
remove_filter( 'elementor/files/allow_unfiltered_upload', [ $this, 'enable_json_template_upload' ] ); |
| 675 |
|
| 676 |
if ( is_wp_error( $upload_result ) ) { |
| 677 |
Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); |
| 678 |
|
| 679 |
return $upload_result; |
| 680 |
} |
| 681 |
|
| 682 |
$source = $this->get_source( $data['source'] ?? 'local' ); |
| 683 |
|
| 684 |
$import_result = $source->import_template( $upload_result['name'], $upload_result['tmp_name'] ); |
| 685 |
|
| 686 |
// Remove the temporary directory generated for the stream-uploaded file. |
| 687 |
Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $upload_result['tmp_name'] ) ); |
| 688 |
|
| 689 |
return $import_result; |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
* Enable JSON Template Upload |
| 694 |
* |
| 695 |
* Runs on the 'elementor/files/allow-file-type/json' Uploads Manager filter. |
| 696 |
* |
| 697 |
* @since 3.5.0 |
| 698 |
* @access public |
| 699 |
* |
| 700 |
* return bool |
| 701 |
*/ |
| 702 |
public function enable_json_template_upload() { |
| 703 |
return true; |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* Mark template as favorite. |
| 708 |
* |
| 709 |
* Add the template to the user favorite templates. |
| 710 |
* |
| 711 |
* @since 1.9.0 |
| 712 |
* @access public |
| 713 |
* |
| 714 |
* @param array $args Template arguments. |
| 715 |
* |
| 716 |
* @return mixed Whether the template marked as favorite. |
| 717 |
*/ |
| 718 |
public function mark_template_as_favorite( $args ) { |
| 719 |
$validate_args = $this->ensure_args( [ 'source', 'template_id', 'favorite' ], $args ); |
| 720 |
|
| 721 |
if ( is_wp_error( $validate_args ) ) { |
| 722 |
return $validate_args; |
| 723 |
} |
| 724 |
|
| 725 |
$source = $this->get_source( $args['source'] ); |
| 726 |
|
| 727 |
return $source->mark_as_favorite( $args['template_id'], filter_var( $args['favorite'], FILTER_VALIDATE_BOOLEAN ) ); |
| 728 |
} |
| 729 |
|
| 730 |
public function import_from_json( array $args ) { |
| 731 |
$validate_args = $this->ensure_args( [ 'editor_post_id', 'elements' ], $args ); |
| 732 |
|
| 733 |
if ( is_wp_error( $validate_args ) ) { |
| 734 |
return $validate_args; |
| 735 |
} |
| 736 |
|
| 737 |
$elements = json_decode( $args['elements'], true ); |
| 738 |
|
| 739 |
$document = Plugin::$instance->documents->get( $args['editor_post_id'] ); |
| 740 |
if ( ! $document ) { |
| 741 |
return new \WP_Error( 'template_error', 'Document not found.' ); |
| 742 |
} |
| 743 |
|
| 744 |
$import_data = $document->get_import_data( [ 'content' => $elements ] ); |
| 745 |
|
| 746 |
return $import_data['content']; |
| 747 |
} |
| 748 |
|
| 749 |
public function get_item_children( array $args ) { |
| 750 |
$validate_args = $this->ensure_args( [ 'source', 'template_id' ], $args ); |
| 751 |
|
| 752 |
if ( is_wp_error( $validate_args ) ) { |
| 753 |
return $validate_args; |
| 754 |
} |
| 755 |
|
| 756 |
$source = $this->get_source( $args['source'] ); |
| 757 |
|
| 758 |
if ( ! $source ) { |
| 759 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 760 |
} |
| 761 |
|
| 762 |
return $source->get_item_children( $args ); |
| 763 |
} |
| 764 |
|
| 765 |
public function search_templates( array $args ) { |
| 766 |
$validate_args = $this->ensure_args( [ 'source', 'search' ], $args ); |
| 767 |
|
| 768 |
if ( is_wp_error( $validate_args ) ) { |
| 769 |
return $validate_args; |
| 770 |
} |
| 771 |
|
| 772 |
$source = $this->get_source( $args['source'] ); |
| 773 |
|
| 774 |
if ( ! $source ) { |
| 775 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 776 |
} |
| 777 |
|
| 778 |
return $source->search_templates( $args ); |
| 779 |
} |
| 780 |
|
| 781 |
public function load_more_templates( array $args ) { |
| 782 |
$validate_args = $this->ensure_args( [ 'source', 'offset' ], $args ); |
| 783 |
|
| 784 |
if ( is_wp_error( $validate_args ) ) { |
| 785 |
return $validate_args; |
| 786 |
} |
| 787 |
|
| 788 |
$source = $this->get_source( $args['source'] ); |
| 789 |
|
| 790 |
if ( ! $source ) { |
| 791 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 792 |
} |
| 793 |
|
| 794 |
return $source->get_items( $args ); |
| 795 |
} |
| 796 |
|
| 797 |
public function create_folder( array $args ) { |
| 798 |
$validate_args = $this->ensure_args( [ 'source', 'title' ], $args ); |
| 799 |
|
| 800 |
if ( is_wp_error( $validate_args ) ) { |
| 801 |
return $validate_args; |
| 802 |
} |
| 803 |
|
| 804 |
$source = $this->get_source( $args['source'] ); |
| 805 |
|
| 806 |
if ( ! $source ) { |
| 807 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 808 |
} |
| 809 |
|
| 810 |
return $source->save_folder( $args ); |
| 811 |
} |
| 812 |
|
| 813 |
public function get_folders( array $args ) { |
| 814 |
$validate_args = $this->ensure_args( [ 'source', 'offset' ], $args ); |
| 815 |
|
| 816 |
if ( is_wp_error( $validate_args ) ) { |
| 817 |
return $validate_args; |
| 818 |
} |
| 819 |
|
| 820 |
$source = $this->get_source( $args['source'] ); |
| 821 |
|
| 822 |
if ( ! $source ) { |
| 823 |
return new \WP_Error( 'template_error', 'Folder source not found.' ); |
| 824 |
} |
| 825 |
|
| 826 |
$args['templateType'] = 'folder'; |
| 827 |
|
| 828 |
return $source->get_items( $args ); |
| 829 |
} |
| 830 |
|
| 831 |
/** |
| 832 |
* Register default template sources. |
| 833 |
* |
| 834 |
* Register the 'local' and 'remote' template sources that Elementor use by |
| 835 |
* default. |
| 836 |
* |
| 837 |
* @since 1.0.0 |
| 838 |
* @access private |
| 839 |
*/ |
| 840 |
private function register_default_sources() { |
| 841 |
$sources = [ |
| 842 |
'local', |
| 843 |
'remote', |
| 844 |
'cloud', |
| 845 |
]; |
| 846 |
|
| 847 |
foreach ( $sources as $source_filename ) { |
| 848 |
$class_name = ucwords( $source_filename ); |
| 849 |
$class_name = str_replace( '-', '_', $class_name ); |
| 850 |
|
| 851 |
$this->register_source( __NAMESPACE__ . '\Source_' . $class_name ); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* Handle ajax request. |
| 857 |
* |
| 858 |
* Fire authenticated ajax actions for any given ajax request. |
| 859 |
* |
| 860 |
* @since 1.0.0 |
| 861 |
* @access private |
| 862 |
* |
| 863 |
* @param string $ajax_request Ajax request. |
| 864 |
* |
| 865 |
* @param array $data |
| 866 |
* |
| 867 |
* @return mixed |
| 868 |
* @throws \Exception If the user has no permission or the post is not found. |
| 869 |
*/ |
| 870 |
private function handle_ajax_request( $ajax_request, array $data ) { |
| 871 |
if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 872 |
throw new \Exception( 'Access denied.' ); |
| 873 |
} |
| 874 |
|
| 875 |
if ( ! empty( $data['editor_post_id'] ) ) { |
| 876 |
$editor_post_id = absint( $data['editor_post_id'] ); |
| 877 |
|
| 878 |
if ( ! get_post( $editor_post_id ) ) { |
| 879 |
throw new \Exception( 'Post not found.' ); |
| 880 |
} |
| 881 |
|
| 882 |
Plugin::$instance->db->switch_to_post( $editor_post_id ); |
| 883 |
} |
| 884 |
|
| 885 |
$result = call_user_func( [ $this, $ajax_request ], $data ); |
| 886 |
|
| 887 |
if ( is_wp_error( $result ) ) { |
| 888 |
throw new \Exception( esc_html( $result->get_error_message() ) ); |
| 889 |
} |
| 890 |
|
| 891 |
return $result; // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
* @throws \Exception |
| 896 |
*/ |
| 897 |
public function save_template_screenshot( $data ): string { |
| 898 |
$validate_args = $this->ensure_args( [ 'template_id', 'screenshot' ], $data ); |
| 899 |
|
| 900 |
if ( is_wp_error( $validate_args ) ) { |
| 901 |
return $validate_args; |
| 902 |
} |
| 903 |
|
| 904 |
$raw_binary = base64_decode( substr( $data['screenshot'], strlen( 'data:image/png;base64,' ) ) ); |
| 905 |
|
| 906 |
return $this->get_source( 'cloud' )->save_item_preview( $data['template_id'], $raw_binary ); |
| 907 |
} |
| 908 |
|
| 909 |
/** |
| 910 |
* @throws \Exception |
| 911 |
*/ |
| 912 |
public function template_screenshot_failed( $data ): string { |
| 913 |
$validate_args = $this->ensure_args( [ 'template_id' ], $data ); |
| 914 |
|
| 915 |
if ( is_wp_error( $validate_args ) ) { |
| 916 |
return $validate_args; |
| 917 |
} |
| 918 |
|
| 919 |
return $this->get_source( 'cloud' )->mark_preview_as_failed( $data['template_id'], $data['error'] ); |
| 920 |
} |
| 921 |
|
| 922 |
public function bulk_delete_templates( $data ) { |
| 923 |
$validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data ); |
| 924 |
|
| 925 |
if ( is_wp_error( $validate_args ) ) { |
| 926 |
return $validate_args; |
| 927 |
} |
| 928 |
|
| 929 |
$source = $this->get_source( $data['source'] ); |
| 930 |
|
| 931 |
if ( ! $source ) { |
| 932 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 933 |
} |
| 934 |
|
| 935 |
if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) { |
| 936 |
return new \WP_Error( 'template_error', 'Template IDs are missing.' ); |
| 937 |
} |
| 938 |
|
| 939 |
return $source->bulk_delete_items( $data['template_ids'] ); |
| 940 |
} |
| 941 |
|
| 942 |
public function bulk_undo_delete_items( $data ) { |
| 943 |
$validate_args = $this->ensure_args( [ 'template_ids', 'source' ], $data ); |
| 944 |
|
| 945 |
if ( is_wp_error( $validate_args ) ) { |
| 946 |
return $validate_args; |
| 947 |
} |
| 948 |
|
| 949 |
$source = $this->get_source( $data['source'] ); |
| 950 |
|
| 951 |
if ( ! $source ) { |
| 952 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 953 |
} |
| 954 |
|
| 955 |
if ( empty( $data['template_ids'] ) || ! is_array( $data['template_ids'] ) ) { |
| 956 |
return new \WP_Error( 'template_error', 'Template IDs are missing.' ); |
| 957 |
} |
| 958 |
|
| 959 |
return $source->bulk_undo_delete_items( $data['template_ids'] ); |
| 960 |
} |
| 961 |
|
| 962 |
/** |
| 963 |
* Init ajax calls. |
| 964 |
* |
| 965 |
* Initialize template library ajax calls for allowed ajax requests. |
| 966 |
* |
| 967 |
* @since 2.3.0 |
| 968 |
* @access public |
| 969 |
* |
| 970 |
* @param Ajax $ajax |
| 971 |
*/ |
| 972 |
public function register_ajax_actions( Ajax $ajax ) { |
| 973 |
$library_ajax_requests = [ |
| 974 |
'get_library_data', |
| 975 |
'get_template_data', |
| 976 |
'save_template', |
| 977 |
'update_templates', |
| 978 |
'delete_template', |
| 979 |
'import_template', |
| 980 |
'mark_template_as_favorite', |
| 981 |
'import_from_json', |
| 982 |
'get_item_children', |
| 983 |
'search_templates', |
| 984 |
'rename_template', |
| 985 |
'load_more_templates', |
| 986 |
'create_folder', |
| 987 |
'get_folders', |
| 988 |
'save_template_screenshot', |
| 989 |
'move_template', |
| 990 |
'copy_template', |
| 991 |
'bulk_move_templates', |
| 992 |
'bulk_delete_templates', |
| 993 |
'bulk_copy_templates', |
| 994 |
'bulk_undo_delete_items', |
| 995 |
'get_templates_quota', |
| 996 |
'template_screenshot_failed', |
| 997 |
]; |
| 998 |
|
| 999 |
foreach ( $library_ajax_requests as $ajax_request ) { |
| 1000 |
$ajax->register_ajax_action( $ajax_request, function( $data ) use ( $ajax_request ) { |
| 1001 |
return $this->handle_ajax_request( $ajax_request, $data ); |
| 1002 |
} ); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* @since 2.3.0 |
| 1008 |
* @access public |
| 1009 |
*/ |
| 1010 |
public function handle_direct_actions() { |
| 1011 |
if ( ! User::is_current_user_can_edit_post_type( Source_Local::CPT ) ) { |
| 1012 |
return; |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** @var Ajax $ajax */ |
| 1016 |
$ajax = Plugin::$instance->common->get_component( 'ajax' ); |
| 1017 |
|
| 1018 |
if ( ! $ajax->verify_request_nonce() ) { |
| 1019 |
$this->handle_direct_action_error( 'Access Denied' ); |
| 1020 |
} |
| 1021 |
|
| 1022 |
$action = Utils::get_super_global_value( $_REQUEST, 'library_action' ); // phpcs:ignore -- Nonce already verified. |
| 1023 |
|
| 1024 |
$whitelist_methods = [ |
| 1025 |
'export_template', |
| 1026 |
'direct_import_template', |
| 1027 |
]; |
| 1028 |
|
| 1029 |
if ( 'direct_import_template' === $action && ! User::is_current_user_can_upload_json() ) { |
| 1030 |
return; |
| 1031 |
} |
| 1032 |
|
| 1033 |
if ( in_array( $action, $whitelist_methods, true ) ) { |
| 1034 |
$result = $this->$action( $_REQUEST ); // phpcs:ignore -- Nonce already verified. |
| 1035 |
} else { |
| 1036 |
$result = new \WP_Error( 'method_not_exists', 'Method Not exists' ); |
| 1037 |
} |
| 1038 |
|
| 1039 |
if ( is_wp_error( $result ) ) { |
| 1040 |
/** @var \WP_Error $result */ |
| 1041 |
$this->handle_direct_action_error( $result->get_error_message() . '.' ); |
| 1042 |
} |
| 1043 |
|
| 1044 |
$callback = "on_{$action}_success"; |
| 1045 |
|
| 1046 |
if ( method_exists( $this, $callback ) ) { |
| 1047 |
$this->$callback( $result ); |
| 1048 |
} |
| 1049 |
|
| 1050 |
die; |
| 1051 |
} |
| 1052 |
|
| 1053 |
/** |
| 1054 |
* On successful template import. |
| 1055 |
* |
| 1056 |
* Redirect the user to the template library after template import was |
| 1057 |
* successful finished. |
| 1058 |
* |
| 1059 |
* @since 2.3.0 |
| 1060 |
* @access private |
| 1061 |
*/ |
| 1062 |
private function on_direct_import_template_success() { |
| 1063 |
wp_safe_redirect( admin_url( Source_Local::ADMIN_MENU_SLUG ) ); |
| 1064 |
} |
| 1065 |
|
| 1066 |
/** |
| 1067 |
* @since 2.3.0 |
| 1068 |
* @access private |
| 1069 |
*/ |
| 1070 |
private function handle_direct_action_error( $message ) { |
| 1071 |
_default_wp_die_handler( $message, 'Elementor Library' ); |
| 1072 |
} |
| 1073 |
|
| 1074 |
/** |
| 1075 |
* Ensure arguments exist. |
| 1076 |
* |
| 1077 |
* Checks whether the required arguments exist in the specified arguments. |
| 1078 |
* |
| 1079 |
* @since 1.0.0 |
| 1080 |
* @access private |
| 1081 |
* |
| 1082 |
* @param array $required_args Required arguments to check whether they |
| 1083 |
* exist. |
| 1084 |
* @param array $specified_args The list of all the specified arguments to |
| 1085 |
* check against. |
| 1086 |
* |
| 1087 |
* @return \WP_Error|true True on success, 'WP_Error' otherwise. |
| 1088 |
*/ |
| 1089 |
private function ensure_args( array $required_args, array $specified_args ) { |
| 1090 |
$not_specified_args = array_diff( $required_args, array_keys( $specified_args ) ); |
| 1091 |
|
| 1092 |
if ( $not_specified_args ) { |
| 1093 |
return new \WP_Error( 'arguments_not_specified', sprintf( 'The required argument(s) "%s" not specified.', implode( ', ', $not_specified_args ) ) ); |
| 1094 |
} |
| 1095 |
|
| 1096 |
return true; |
| 1097 |
} |
| 1098 |
|
| 1099 |
private function is_allowed_to_read_template( array $args ): bool { |
| 1100 |
if ( 'remote' === $args['source'] || 'cloud' === $args['source'] ) { |
| 1101 |
return true; |
| 1102 |
} |
| 1103 |
|
| 1104 |
if ( null === $this->wordpress_adapter ) { |
| 1105 |
$this->set_wordpress_adapter( new WordPress_Adapter() ); |
| 1106 |
} |
| 1107 |
|
| 1108 |
if ( ! $this->should_check_permissions( $args ) ) { |
| 1109 |
return true; |
| 1110 |
} |
| 1111 |
|
| 1112 |
$post_id = intval( $args['template_id'] ); |
| 1113 |
$post_status = $this->wordpress_adapter->get_post_status( $post_id ); |
| 1114 |
$is_private_or_non_published = ( 'private' === $post_status && ! $this->wordpress_adapter->current_user_can( 'read_private_posts', $post_id ) ) || ( 'publish' !== $post_status ); |
| 1115 |
|
| 1116 |
$can_read_template = $is_private_or_non_published || $this->wordpress_adapter->current_user_can( 'edit_post', $post_id ); |
| 1117 |
|
| 1118 |
return apply_filters( 'elementor/template-library/is_allowed_to_read_template', $can_read_template, $args ); |
| 1119 |
} |
| 1120 |
|
| 1121 |
private function should_check_permissions( array $args ): bool { |
| 1122 |
if ( null === $this->elementor_adapter ) { |
| 1123 |
$this->set_elementor_adapter( new Elementor_Adapter() ); |
| 1124 |
} |
| 1125 |
|
| 1126 |
// TODO: Remove $isWidgetTemplate in 3.28.0 as there is a Pro dependency |
| 1127 |
$check_permissions = isset( $args['check_permissions'] ) && false === $args['check_permissions']; |
| 1128 |
$is_widget_template = 'widget' === $this->elementor_adapter->get_template_type( $args['template_id'] ); |
| 1129 |
|
| 1130 |
if ( $check_permissions || $is_widget_template ) { |
| 1131 |
return false; |
| 1132 |
} |
| 1133 |
|
| 1134 |
return true; |
| 1135 |
} |
| 1136 |
|
| 1137 |
public function bulk_move_templates( array $args ) { |
| 1138 |
$validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 1139 |
|
| 1140 |
if ( is_wp_error( $validate_args ) ) { |
| 1141 |
return $validate_args; |
| 1142 |
} |
| 1143 |
|
| 1144 |
$args['source'] = $args['source'][0]; |
| 1145 |
|
| 1146 |
return $this->bulk_move_template_items( $args ); |
| 1147 |
} |
| 1148 |
|
| 1149 |
private function bulk_move_template_items( array $args ) { |
| 1150 |
$source = $this->get_source( $args['source'] ); |
| 1151 |
|
| 1152 |
if ( ! $source ) { |
| 1153 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 1154 |
} |
| 1155 |
|
| 1156 |
if ( $this->is_action_to_same_source( $args ) ) { |
| 1157 |
return $source->move_bulk_templates_to_folder( $args ); |
| 1158 |
} |
| 1159 |
|
| 1160 |
$bulk_args = 'local' === $args['from_source'] |
| 1161 |
? $this->format_args_for_bulk_action_from_local( $args ) |
| 1162 |
: $this->format_args_for_bulk_action_from_cloud( $args ); |
| 1163 |
|
| 1164 |
if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) { |
| 1165 |
$is_quota_valid = $source->validate_quota( $bulk_args ); |
| 1166 |
|
| 1167 |
if ( is_wp_error( $is_quota_valid ) ) { |
| 1168 |
return $is_quota_valid; |
| 1169 |
} |
| 1170 |
|
| 1171 |
if ( ! $is_quota_valid ) { |
| 1172 |
return new \WP_Error( 'quota_error', 'The moving failed because it will pass the maximum templates you can save.' ); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
$bulk_save = $source->save_bulk_items( $bulk_args ); |
| 1177 |
|
| 1178 |
if ( ! empty( $bulk_save ) ) { |
| 1179 |
$this->bulk_delete_templates( [ |
| 1180 |
'template_ids' => $args['from_template_id'], |
| 1181 |
'source' => $args['from_source'], |
| 1182 |
] ); |
| 1183 |
} |
| 1184 |
|
| 1185 |
return $bulk_save; |
| 1186 |
} |
| 1187 |
|
| 1188 |
private function format_args_for_bulk_action_from_local( $args ) { |
| 1189 |
$bulk_args = []; |
| 1190 |
|
| 1191 |
foreach ( $args['from_template_id'] as $from_template_id ) { |
| 1192 |
if ( ! $this->is_allowed_to_read_template( [ |
| 1193 |
'source' => $args['from_source'], |
| 1194 |
'template_id' => $from_template_id, |
| 1195 |
] ) ) { |
| 1196 |
continue; |
| 1197 |
} |
| 1198 |
|
| 1199 |
$document = Plugin::$instance->documents->get( $from_template_id ); |
| 1200 |
|
| 1201 |
if ( ! $document ) { |
| 1202 |
continue; |
| 1203 |
} |
| 1204 |
|
| 1205 |
$page = SettingsManager::get_settings_managers( 'page' )->get_model( $from_template_id ); |
| 1206 |
|
| 1207 |
$bulk_args[] = array_merge( |
| 1208 |
$args, |
| 1209 |
[ |
| 1210 |
'title' => $document->get_post()->post_title, |
| 1211 |
'type' => $document::get_type(), |
| 1212 |
'content' => $document->get_elements_data(), |
| 1213 |
'page_settings' => $page->get_data( 'settings' ), |
| 1214 |
] |
| 1215 |
); |
| 1216 |
} |
| 1217 |
|
| 1218 |
return $bulk_args; |
| 1219 |
} |
| 1220 |
|
| 1221 |
private function format_args_for_bulk_action_from_cloud( $args ) { |
| 1222 |
$from_source = $this->get_source( $args['from_source'] ); |
| 1223 |
|
| 1224 |
if ( ! $from_source ) { |
| 1225 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 1226 |
} |
| 1227 |
|
| 1228 |
$templates = $from_source->get_bulk_items( $args ); |
| 1229 |
$bulk_args = []; |
| 1230 |
|
| 1231 |
foreach ( $templates as $template ) { |
| 1232 |
$content = json_decode( $template['content'], true ); |
| 1233 |
|
| 1234 |
$bulk_args[] = array_merge( |
| 1235 |
$args, |
| 1236 |
[ |
| 1237 |
'title' => $template['title'], |
| 1238 |
'type' => $template['type'], |
| 1239 |
'content' => $content['content'], |
| 1240 |
'page_settings' => $content['page_settings'], |
| 1241 |
] |
| 1242 |
); |
| 1243 |
} |
| 1244 |
|
| 1245 |
return $bulk_args; |
| 1246 |
} |
| 1247 |
|
| 1248 |
public function bulk_copy_templates( array $args ) { |
| 1249 |
$validate_args = $this->ensure_args( [ 'source', 'from_source', 'from_template_id' ], $args ); |
| 1250 |
|
| 1251 |
if ( is_wp_error( $validate_args ) ) { |
| 1252 |
return $validate_args; |
| 1253 |
} |
| 1254 |
|
| 1255 |
$args['source'] = $args['source'][0]; |
| 1256 |
|
| 1257 |
return $this->bulk_copy_template_items( $args ); |
| 1258 |
} |
| 1259 |
|
| 1260 |
private function bulk_copy_template_items( array $args ) { |
| 1261 |
$source = $this->get_source( $args['source'] ); |
| 1262 |
|
| 1263 |
if ( ! $source ) { |
| 1264 |
return new \WP_Error( 'template_error', 'Template source not found.' ); |
| 1265 |
} |
| 1266 |
|
| 1267 |
$bulk_args = 'local' === $args['from_source'] |
| 1268 |
? $this->format_args_for_bulk_action_from_local( $args ) |
| 1269 |
: $this->format_args_for_bulk_action_from_cloud( $args ); |
| 1270 |
|
| 1271 |
if ( $source->supports_quota() && ! $this->is_action_to_same_source( $args ) ) { |
| 1272 |
$is_quota_valid = $source->validate_quota( $bulk_args ); |
| 1273 |
|
| 1274 |
if ( is_wp_error( $is_quota_valid ) ) { |
| 1275 |
return $is_quota_valid; |
| 1276 |
} |
| 1277 |
|
| 1278 |
if ( ! $is_quota_valid ) { |
| 1279 |
return new \WP_Error( 'quota_error', 'The copying failed because it will pass the maximum templates you can save.' ); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
return $source->save_bulk_items( $bulk_args ); |
| 1284 |
} |
| 1285 |
|
| 1286 |
public function get_templates_quota( array $args ) { |
| 1287 |
$validate_args = $this->ensure_args( [ 'source' ], $args ); |
| 1288 |
|
| 1289 |
if ( is_wp_error( $validate_args ) ) { |
| 1290 |
return $validate_args; |
| 1291 |
} |
| 1292 |
|
| 1293 |
$source = $this->get_source( $args['source'] ); |
| 1294 |
|
| 1295 |
if ( ! $source ) { |
| 1296 |
return new \WP_Error( 'template_error', 'Source not found.' ); |
| 1297 |
} |
| 1298 |
|
| 1299 |
return $source->get_quota(); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|