| 1 |
<?php |
| 2 |
namespace Elementor\Core; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Document; |
| 5 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 6 |
use Elementor\Core\DocumentTypes\Page; |
| 7 |
use Elementor\Core\DocumentTypes\Post; |
| 8 |
use Elementor\Plugin; |
| 9 |
use Elementor\TemplateLibrary\Source_Local; |
| 10 |
use Elementor\Utils; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Elementor documents manager. |
| 18 |
* |
| 19 |
* Elementor documents manager handler class is responsible for registering and |
| 20 |
* managing Elementor documents. |
| 21 |
* |
| 22 |
* @since 2.0.0 |
| 23 |
*/ |
| 24 |
class Documents_Manager { |
| 25 |
|
| 26 |
/** |
| 27 |
* Registered types. |
| 28 |
* |
| 29 |
* Holds the list of all the registered types. |
| 30 |
* |
| 31 |
* @since 2.0.0 |
| 32 |
* @access protected |
| 33 |
* |
| 34 |
* @var Document[] |
| 35 |
*/ |
| 36 |
protected $types = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* Registered documents. |
| 40 |
* |
| 41 |
* Holds the list of all the registered documents. |
| 42 |
* |
| 43 |
* @since 2.0.0 |
| 44 |
* @access protected |
| 45 |
* |
| 46 |
* @var Document[] |
| 47 |
*/ |
| 48 |
protected $documents = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* Current document. |
| 52 |
* |
| 53 |
* Holds the current document. |
| 54 |
* |
| 55 |
* @since 2.0.0 |
| 56 |
* @access protected |
| 57 |
* |
| 58 |
* @var Document |
| 59 |
*/ |
| 60 |
protected $current_doc; |
| 61 |
|
| 62 |
/** |
| 63 |
* Switched data. |
| 64 |
* |
| 65 |
* Holds the current document when changing to the requested post. |
| 66 |
* |
| 67 |
* @since 2.0.0 |
| 68 |
* @access protected |
| 69 |
* |
| 70 |
* @var array |
| 71 |
*/ |
| 72 |
protected $switched_data = []; |
| 73 |
|
| 74 |
protected $cpt = []; |
| 75 |
|
| 76 |
/** |
| 77 |
* Documents manager constructor. |
| 78 |
* |
| 79 |
* Initializing the Elementor documents manager. |
| 80 |
* |
| 81 |
* @since 2.0.0 |
| 82 |
* @access public |
| 83 |
*/ |
| 84 |
public function __construct() { |
| 85 |
add_action( 'elementor/documents/register', [ $this, 'register_default_types' ], 0 ); |
| 86 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 87 |
add_filter( 'post_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 ); |
| 88 |
add_filter( 'page_row_actions', [ $this, 'filter_post_row_actions' ], 11, 2 ); |
| 89 |
add_filter( 'user_has_cap', [ $this, 'remove_user_edit_cap' ], 10, 3 ); |
| 90 |
add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Register ajax actions. |
| 95 |
* |
| 96 |
* Process ajax action handles when saving data and discarding changes. |
| 97 |
* |
| 98 |
* Fired by `elementor/ajax/register_actions` action. |
| 99 |
* |
| 100 |
* @since 2.0.0 |
| 101 |
* @access public |
| 102 |
* |
| 103 |
* @param Ajax $ajax_manager An instance of the ajax manager. |
| 104 |
*/ |
| 105 |
public function register_ajax_actions( $ajax_manager ) { |
| 106 |
$ajax_manager->register_ajax_action( 'save_builder', [ $this, 'ajax_save' ] ); |
| 107 |
$ajax_manager->register_ajax_action( 'discard_changes', [ $this, 'ajax_discard_changes' ] ); |
| 108 |
$ajax_manager->register_ajax_action( 'get_document_config', [ $this, 'ajax_get_document_config' ] ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Register default types. |
| 113 |
* |
| 114 |
* Registers the default document types. |
| 115 |
* |
| 116 |
* @since 2.0.0 |
| 117 |
* @access public |
| 118 |
*/ |
| 119 |
public function register_default_types() { |
| 120 |
$default_types = [ |
| 121 |
'post' => Post::get_class_full_name(), // BC. |
| 122 |
'wp-post' => Post::get_class_full_name(), |
| 123 |
'wp-page' => Page::get_class_full_name(), |
| 124 |
]; |
| 125 |
|
| 126 |
foreach ( $default_types as $type => $class ) { |
| 127 |
$this->register_document_type( $type, $class ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Register document type. |
| 133 |
* |
| 134 |
* Registers a single document. |
| 135 |
* |
| 136 |
* @since 2.0.0 |
| 137 |
* @access public |
| 138 |
* |
| 139 |
* @param string $type Document type name. |
| 140 |
* @param string $class The name of the class that registers the document type. |
| 141 |
* Full name with the namespace. |
| 142 |
* |
| 143 |
* @return Documents_Manager The updated document manager instance. |
| 144 |
*/ |
| 145 |
public function register_document_type( $type, $class ) { |
| 146 |
$this->types[ $type ] = $class; |
| 147 |
|
| 148 |
$cpt = $class::get_property( 'cpt' ); |
| 149 |
|
| 150 |
if ( $cpt ) { |
| 151 |
foreach ( $cpt as $post_type ) { |
| 152 |
$this->cpt[ $post_type ] = $type; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
if ( $class::get_property( 'register_type' ) ) { |
| 157 |
Source_Local::add_template_type( $type ); |
| 158 |
} |
| 159 |
|
| 160 |
return $this; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get document. |
| 165 |
* |
| 166 |
* Retrieve the document data based on a post ID. |
| 167 |
* |
| 168 |
* @since 2.0.0 |
| 169 |
* @access public |
| 170 |
* |
| 171 |
* @param int $post_id Post ID. |
| 172 |
* @param bool $from_cache Optional. Whether to retrieve cached data. Default is true. |
| 173 |
* |
| 174 |
* @return false|Document Document data or false if post ID was not entered. |
| 175 |
*/ |
| 176 |
public function get( $post_id, $from_cache = true ) { |
| 177 |
$this->register_types(); |
| 178 |
|
| 179 |
$post_id = absint( $post_id ); |
| 180 |
|
| 181 |
if ( ! $post_id || ! get_post( $post_id ) ) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Retrieve document post ID. |
| 187 |
* |
| 188 |
* Filters the document post ID. |
| 189 |
* |
| 190 |
* @since 2.0.7 |
| 191 |
* |
| 192 |
* @param int $post_id The post ID of the document. |
| 193 |
*/ |
| 194 |
$post_id = apply_filters( 'elementor/documents/get/post_id', $post_id ); |
| 195 |
|
| 196 |
if ( ! $from_cache || ! isset( $this->documents[ $post_id ] ) ) { |
| 197 |
$doc_type = $this->get_doc_type_by_id( $post_id ); |
| 198 |
$doc_type_class = $this->get_document_type( $doc_type ); |
| 199 |
|
| 200 |
$this->documents[ $post_id ] = new $doc_type_class( [ |
| 201 |
'post_id' => $post_id, |
| 202 |
] ); |
| 203 |
} |
| 204 |
|
| 205 |
return $this->documents[ $post_id ]; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Get document or autosave. |
| 210 |
* |
| 211 |
* Retrieve either the document or the autosave. |
| 212 |
* |
| 213 |
* @since 2.0.0 |
| 214 |
* @access public |
| 215 |
* |
| 216 |
* @param int $id Optional. Post ID. Default is `0`. |
| 217 |
* @param int $user_id Optional. User ID. Default is `0`. |
| 218 |
* |
| 219 |
* @return false|Document The document if it exist, False otherwise. |
| 220 |
*/ |
| 221 |
public function get_doc_or_auto_save( $id, $user_id = 0 ) { |
| 222 |
$document = $this->get( $id ); |
| 223 |
if ( $document && $document->get_autosave_id( $user_id ) ) { |
| 224 |
$document = $document->get_autosave( $user_id ); |
| 225 |
} |
| 226 |
|
| 227 |
return $document; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get document for frontend. |
| 232 |
* |
| 233 |
* Retrieve the document for frontend use. |
| 234 |
* |
| 235 |
* @since 2.0.0 |
| 236 |
* @access public |
| 237 |
* |
| 238 |
* @param int $post_id Optional. Post ID. Default is `0`. |
| 239 |
* |
| 240 |
* @return false|Document The document if it exist, False otherwise. |
| 241 |
*/ |
| 242 |
public function get_doc_for_frontend( $post_id ) { |
| 243 |
if ( is_preview() || Plugin::$instance->preview->is_preview_mode() ) { |
| 244 |
$document = $this->get_doc_or_auto_save( $post_id, get_current_user_id() ); |
| 245 |
} else { |
| 246 |
$document = $this->get( $post_id ); |
| 247 |
} |
| 248 |
|
| 249 |
return $document; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Get document type. |
| 254 |
* |
| 255 |
* Retrieve the type of any given document. |
| 256 |
* |
| 257 |
* @since 2.0.0 |
| 258 |
* @access public |
| 259 |
* |
| 260 |
* @param string $type |
| 261 |
* |
| 262 |
* @param string $fallback |
| 263 |
* |
| 264 |
* @return Document|bool The type of the document. |
| 265 |
*/ |
| 266 |
public function get_document_type( $type, $fallback = 'post' ) { |
| 267 |
$types = $this->get_document_types(); |
| 268 |
|
| 269 |
if ( isset( $types[ $type ] ) ) { |
| 270 |
return $types[ $type ]; |
| 271 |
} |
| 272 |
|
| 273 |
if ( isset( $types[ $fallback ] ) ) { |
| 274 |
return $types[ $fallback ]; |
| 275 |
} |
| 276 |
|
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Get document types. |
| 282 |
* |
| 283 |
* Retrieve the all the registered document types. |
| 284 |
* |
| 285 |
* @since 2.0.0 |
| 286 |
* @access public |
| 287 |
* |
| 288 |
* @param array $args Optional. An array of key => value arguments to match against |
| 289 |
* the properties. Default is empty array. |
| 290 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one |
| 291 |
* element from the array needs to match; 'and' means all elements |
| 292 |
* must match; 'not' means no elements may match. Default 'and'. |
| 293 |
* |
| 294 |
* @return Document[] All the registered document types. |
| 295 |
*/ |
| 296 |
public function get_document_types( $args = [], $operator = 'and' ) { |
| 297 |
$this->register_types(); |
| 298 |
|
| 299 |
if ( ! empty( $args ) ) { |
| 300 |
$types_properties = $this->get_types_properties(); |
| 301 |
|
| 302 |
$filtered = wp_filter_object_list( $types_properties, $args, $operator ); |
| 303 |
|
| 304 |
return array_intersect_key( $this->types, $filtered ); |
| 305 |
} |
| 306 |
|
| 307 |
return $this->types; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Get document types with their properties. |
| 312 |
* |
| 313 |
* @return array A list of properties arrays indexed by the type. |
| 314 |
*/ |
| 315 |
public function get_types_properties() { |
| 316 |
$types_properties = []; |
| 317 |
|
| 318 |
foreach ( $this->get_document_types() as $type => $class ) { |
| 319 |
$types_properties[ $type ] = $class::get_properties(); |
| 320 |
} |
| 321 |
return $types_properties; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Create a document. |
| 326 |
* |
| 327 |
* Create a new document using any given parameters. |
| 328 |
* |
| 329 |
* @since 2.0.0 |
| 330 |
* @access public |
| 331 |
* |
| 332 |
* @param string $type Document type. |
| 333 |
* @param array $post_data An array containing the post data. |
| 334 |
* @param array $meta_data An array containing the post meta data. |
| 335 |
* |
| 336 |
* @return Document The type of the document. |
| 337 |
*/ |
| 338 |
public function create( $type, $post_data = [], $meta_data = [] ) { |
| 339 |
$class = $this->get_document_type( $type, false ); |
| 340 |
|
| 341 |
if ( ! $class ) { |
| 342 |
return new \WP_Error( 500, sprintf( 'Type %s does not exist.', $type ) ); |
| 343 |
} |
| 344 |
|
| 345 |
if ( empty( $post_data['post_title'] ) ) { |
| 346 |
$post_data['post_title'] = esc_html__( 'Elementor', 'elementor' ); |
| 347 |
if ( 'post' !== $type ) { |
| 348 |
$post_data['post_title'] = sprintf( |
| 349 |
/* translators: %s: Document title. */ |
| 350 |
__( 'Elementor %s', 'elementor' ), |
| 351 |
call_user_func( [ $class, 'get_title' ] ) |
| 352 |
); |
| 353 |
} |
| 354 |
$update_title = true; |
| 355 |
} |
| 356 |
|
| 357 |
$meta_data['_elementor_edit_mode'] = 'builder'; |
| 358 |
|
| 359 |
// Save the type as-is for plugins that hooked at `wp_insert_post`. |
| 360 |
$meta_data[ Document::TYPE_META_KEY ] = $type; |
| 361 |
|
| 362 |
$post_data['meta_input'] = $meta_data; |
| 363 |
|
| 364 |
$post_types = $class::get_property( 'cpt' ); |
| 365 |
|
| 366 |
if ( ! empty( $post_types[0] ) && empty( $post_data['post_type'] ) ) { |
| 367 |
$post_data['post_type'] = $post_types[0]; |
| 368 |
} |
| 369 |
|
| 370 |
$post_id = wp_insert_post( $post_data ); |
| 371 |
|
| 372 |
if ( ! empty( $update_title ) ) { |
| 373 |
$post_data['ID'] = $post_id; |
| 374 |
$post_data['post_title'] .= ' #' . $post_id; |
| 375 |
|
| 376 |
// The meta doesn't need update. |
| 377 |
unset( $post_data['meta_input'] ); |
| 378 |
|
| 379 |
wp_update_post( $post_data ); |
| 380 |
} |
| 381 |
|
| 382 |
/** @var Document $document */ |
| 383 |
$document = new $class( [ |
| 384 |
'post_id' => $post_id, |
| 385 |
] ); |
| 386 |
|
| 387 |
// Let the $document to re-save the template type by his way + version. |
| 388 |
$document->save( [] ); |
| 389 |
|
| 390 |
return $document; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Remove user edit capabilities if document is not editable. |
| 395 |
* |
| 396 |
* Filters the user capabilities to disable editing in admin. |
| 397 |
* |
| 398 |
* @param array $allcaps An array of all the user's capabilities. |
| 399 |
* @param array $caps Actual capabilities for meta capability. |
| 400 |
* @param array $args Optional parameters passed to has_cap(), typically object ID. |
| 401 |
* |
| 402 |
* @return array |
| 403 |
*/ |
| 404 |
public function remove_user_edit_cap( $allcaps, $caps, $args ) { |
| 405 |
global $pagenow; |
| 406 |
|
| 407 |
if ( ! in_array( $pagenow, [ 'post.php', 'edit.php' ], true ) ) { |
| 408 |
return $allcaps; |
| 409 |
} |
| 410 |
|
| 411 |
// Don't touch not existing or not allowed caps. |
| 412 |
if ( empty( $caps[0] ) || empty( $allcaps[ $caps[0] ] ) ) { |
| 413 |
return $allcaps; |
| 414 |
} |
| 415 |
|
| 416 |
$capability = $args[0]; |
| 417 |
|
| 418 |
if ( 'edit_post' !== $capability ) { |
| 419 |
return $allcaps; |
| 420 |
} |
| 421 |
|
| 422 |
if ( empty( $args[2] ) ) { |
| 423 |
return $allcaps; |
| 424 |
} |
| 425 |
|
| 426 |
$post_id = $args[2]; |
| 427 |
|
| 428 |
$document = Plugin::$instance->documents->get( $post_id ); |
| 429 |
|
| 430 |
if ( ! $document ) { |
| 431 |
return $allcaps; |
| 432 |
} |
| 433 |
|
| 434 |
$allcaps[ $caps[0] ] = $document::get_property( 'is_editable' ); |
| 435 |
|
| 436 |
return $allcaps; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Filter Post Row Actions. |
| 441 |
* |
| 442 |
* Let the Document to filter the array of row action links on the Posts list table. |
| 443 |
* |
| 444 |
* @param array $actions |
| 445 |
* @param \WP_Post $post |
| 446 |
* |
| 447 |
* @return array |
| 448 |
*/ |
| 449 |
public function filter_post_row_actions( $actions, $post ) { |
| 450 |
$document = $this->get( $post->ID ); |
| 451 |
|
| 452 |
if ( $document ) { |
| 453 |
$actions = $document->filter_admin_row_actions( $actions ); |
| 454 |
} |
| 455 |
|
| 456 |
return $actions; |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* Save document data using ajax. |
| 461 |
* |
| 462 |
* Save the document on the builder using ajax, when saving the changes, and refresh the editor. |
| 463 |
* |
| 464 |
* @since 2.0.0 |
| 465 |
* @access public |
| 466 |
* |
| 467 |
* @param $request Post ID. |
| 468 |
* |
| 469 |
* @throws \Exception If current user don't have permissions to edit the post or the post is not using Elementor. |
| 470 |
* |
| 471 |
* @return array The document data after saving. |
| 472 |
*/ |
| 473 |
public function ajax_save( $request ) { |
| 474 |
$document = $this->get( $request['editor_post_id'] ); |
| 475 |
|
| 476 |
if ( ! $document->is_built_with_elementor() || ! $document->is_editable_by_current_user() ) { |
| 477 |
throw new \Exception( 'Access denied.' ); |
| 478 |
} |
| 479 |
|
| 480 |
$this->switch_to_document( $document ); |
| 481 |
|
| 482 |
// Set the post as global post. |
| 483 |
Plugin::$instance->db->switch_to_post( $document->get_post()->ID ); |
| 484 |
|
| 485 |
$status = Document::STATUS_DRAFT; |
| 486 |
|
| 487 |
if ( isset( $request['status'] ) && in_array( $request['status'], [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE, Document::STATUS_PENDING, Document::STATUS_AUTOSAVE ], true ) ) { |
| 488 |
$status = $request['status']; |
| 489 |
} |
| 490 |
|
| 491 |
if ( Document::STATUS_AUTOSAVE === $status ) { |
| 492 |
// If the post is a draft - save the `autosave` to the original draft. |
| 493 |
// Allow a revision only if the original post is already published. |
| 494 |
if ( in_array( $document->get_post()->post_status, [ Document::STATUS_PUBLISH, Document::STATUS_PRIVATE ], true ) ) { |
| 495 |
$document = $document->get_autosave( 0, true ); |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
// Set default page template because the footer-saver doesn't send default values, |
| 500 |
// But if the template was changed from canvas to default - it needed to save. |
| 501 |
if ( Utils::is_cpt_custom_templates_supported() && ! isset( $request['settings']['template'] ) ) { |
| 502 |
$request['settings']['template'] = 'default'; |
| 503 |
} |
| 504 |
|
| 505 |
$data = [ |
| 506 |
'elements' => $request['elements'], |
| 507 |
'settings' => $request['settings'], |
| 508 |
]; |
| 509 |
|
| 510 |
$document->save( $data ); |
| 511 |
|
| 512 |
// Refresh after save. |
| 513 |
$document = $this->get( $document->get_post()->ID, false ); |
| 514 |
|
| 515 |
$return_data = [ |
| 516 |
'status' => $document->get_post()->post_status, |
| 517 |
'config' => [ |
| 518 |
'document' => [ |
| 519 |
'last_edited' => $document->get_last_edited(), |
| 520 |
'urls' => [ |
| 521 |
'wp_preview' => $document->get_wp_preview_url(), |
| 522 |
], |
| 523 |
], |
| 524 |
], |
| 525 |
]; |
| 526 |
|
| 527 |
/** |
| 528 |
* Returned documents ajax saved data. |
| 529 |
* |
| 530 |
* Filters the ajax data returned when saving the post on the builder. |
| 531 |
* |
| 532 |
* @since 2.0.0 |
| 533 |
* |
| 534 |
* @param array $return_data The returned data. |
| 535 |
* @param Document $document The document instance. |
| 536 |
*/ |
| 537 |
$return_data = apply_filters( 'elementor/documents/ajax_save/return_data', $return_data, $document ); |
| 538 |
|
| 539 |
return $return_data; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Ajax discard changes. |
| 544 |
* |
| 545 |
* Load the document data from an autosave, deleting unsaved changes. |
| 546 |
* |
| 547 |
* @since 2.0.0 |
| 548 |
* @access public |
| 549 |
* |
| 550 |
* @param $request |
| 551 |
* |
| 552 |
* @return bool True if changes discarded, False otherwise. |
| 553 |
*/ |
| 554 |
public function ajax_discard_changes( $request ) { |
| 555 |
$document = $this->get( $request['editor_post_id'] ); |
| 556 |
|
| 557 |
$autosave = $document->get_autosave(); |
| 558 |
|
| 559 |
if ( $autosave ) { |
| 560 |
$success = $autosave->delete(); |
| 561 |
} else { |
| 562 |
$success = true; |
| 563 |
} |
| 564 |
|
| 565 |
return $success; |
| 566 |
} |
| 567 |
|
| 568 |
public function ajax_get_document_config( $request ) { |
| 569 |
$post_id = absint( $request['id'] ); |
| 570 |
|
| 571 |
Plugin::$instance->editor->set_post_id( $post_id ); |
| 572 |
|
| 573 |
$document = $this->get_doc_or_auto_save( $post_id ); |
| 574 |
|
| 575 |
if ( ! $document ) { |
| 576 |
throw new \Exception( 'Not found.' ); |
| 577 |
} |
| 578 |
|
| 579 |
if ( ! $document->is_editable_by_current_user() ) { |
| 580 |
throw new \Exception( 'Access denied.' ); |
| 581 |
} |
| 582 |
|
| 583 |
// Set the global data like $post, $authordata and etc |
| 584 |
Plugin::$instance->db->switch_to_post( $post_id ); |
| 585 |
|
| 586 |
$this->switch_to_document( $document ); |
| 587 |
|
| 588 |
// Change mode to Builder |
| 589 |
$document->set_is_built_with_elementor( true ); |
| 590 |
|
| 591 |
$doc_config = $document->get_config(); |
| 592 |
|
| 593 |
return $doc_config; |
| 594 |
} |
| 595 |
|
| 596 |
/** |
| 597 |
* Switch to document. |
| 598 |
* |
| 599 |
* Change the document to any new given document type. |
| 600 |
* |
| 601 |
* @since 2.0.0 |
| 602 |
* @access public |
| 603 |
* |
| 604 |
* @param Document $document The document to switch to. |
| 605 |
*/ |
| 606 |
public function switch_to_document( $document ) { |
| 607 |
// If is already switched, or is the same post, return. |
| 608 |
if ( $this->current_doc === $document ) { |
| 609 |
$this->switched_data[] = false; |
| 610 |
return; |
| 611 |
} |
| 612 |
|
| 613 |
$this->switched_data[] = [ |
| 614 |
'switched_doc' => $document, |
| 615 |
'original_doc' => $this->current_doc, // Note, it can be null if the global isn't set |
| 616 |
]; |
| 617 |
|
| 618 |
$this->current_doc = $document; |
| 619 |
} |
| 620 |
|
| 621 |
/** |
| 622 |
* Restore document. |
| 623 |
* |
| 624 |
* Rollback to the original document. |
| 625 |
* |
| 626 |
* @since 2.0.0 |
| 627 |
* @access public |
| 628 |
*/ |
| 629 |
public function restore_document() { |
| 630 |
$data = array_pop( $this->switched_data ); |
| 631 |
|
| 632 |
// If not switched, return. |
| 633 |
if ( ! $data ) { |
| 634 |
return; |
| 635 |
} |
| 636 |
|
| 637 |
$this->current_doc = $data['original_doc']; |
| 638 |
} |
| 639 |
|
| 640 |
/** |
| 641 |
* Get current document. |
| 642 |
* |
| 643 |
* Retrieve the current document. |
| 644 |
* |
| 645 |
* @since 2.0.0 |
| 646 |
* @access public |
| 647 |
* |
| 648 |
* @return Document The current document. |
| 649 |
*/ |
| 650 |
public function get_current() { |
| 651 |
return $this->current_doc; |
| 652 |
} |
| 653 |
|
| 654 |
public function localize_settings( $settings ) { |
| 655 |
$translations = []; |
| 656 |
|
| 657 |
foreach ( $this->get_document_types() as $type => $class ) { |
| 658 |
$translations[ $type ] = $class::get_title(); |
| 659 |
} |
| 660 |
|
| 661 |
return array_replace_recursive( $settings, [ |
| 662 |
'i18n' => $translations, |
| 663 |
] ); |
| 664 |
} |
| 665 |
|
| 666 |
private function register_types() { |
| 667 |
if ( ! did_action( 'elementor/documents/register' ) ) { |
| 668 |
/** |
| 669 |
* Register Elementor documents. |
| 670 |
* |
| 671 |
* @since 2.0.0 |
| 672 |
* |
| 673 |
* @param Documents_Manager $this The document manager instance. |
| 674 |
*/ |
| 675 |
do_action( 'elementor/documents/register', $this ); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Get create new post URL. |
| 681 |
* |
| 682 |
* Retrieve a custom URL for creating a new post/page using Elementor. |
| 683 |
* |
| 684 |
* @param string $post_type Optional. Post type slug. Default is 'page'. |
| 685 |
* @param string|null $template_type Optional. Query arg 'template_type'. Default is null. |
| 686 |
* |
| 687 |
* @return string A URL for creating new post using Elementor. |
| 688 |
*/ |
| 689 |
public static function get_create_new_post_url( $post_type = 'page', $template_type = null ) { |
| 690 |
$query_args = [ |
| 691 |
'action' => 'elementor_new_post', |
| 692 |
'post_type' => $post_type, |
| 693 |
]; |
| 694 |
|
| 695 |
if ( $template_type ) { |
| 696 |
$query_args['template_type'] = $template_type; |
| 697 |
} |
| 698 |
|
| 699 |
$new_post_url = add_query_arg( $query_args, admin_url( 'edit.php' ) ); |
| 700 |
|
| 701 |
$new_post_url = add_query_arg( '_wpnonce', wp_create_nonce( 'elementor_action_new_post' ), $new_post_url ); |
| 702 |
|
| 703 |
return $new_post_url; |
| 704 |
} |
| 705 |
|
| 706 |
private function get_doc_type_by_id( $post_id ) { |
| 707 |
// Auto-save inherits from the original post. |
| 708 |
if ( wp_is_post_autosave( $post_id ) ) { |
| 709 |
$post_id = wp_get_post_parent_id( $post_id ); |
| 710 |
} |
| 711 |
|
| 712 |
// Content built with Elementor. |
| 713 |
$template_type = get_post_meta( $post_id, Document::TYPE_META_KEY, true ); |
| 714 |
|
| 715 |
if ( $template_type && isset( $this->types[ $template_type ] ) ) { |
| 716 |
return $template_type; |
| 717 |
} |
| 718 |
|
| 719 |
// Elementor installation on a site with existing content (which doesn't contain Elementor's meta). |
| 720 |
$post_type = get_post_type( $post_id ); |
| 721 |
|
| 722 |
return $this->cpt[ $post_type ] ?? 'post'; |
| 723 |
} |
| 724 |
} |
| 725 |
|