| 1 |
<?php |
| 2 |
namespace Elementor\Core\Base; |
| 3 |
|
| 4 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 5 |
use Elementor\Core\Settings\Page\Model as Page_Model; |
| 6 |
use Elementor\Core\Utils\Exceptions; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Controls_Manager; |
| 9 |
use Elementor\Controls_Stack; |
| 10 |
use Elementor\User; |
| 11 |
use Elementor\Core\Settings\Manager as SettingsManager; |
| 12 |
use Elementor\Utils; |
| 13 |
use Elementor\Widget_Base; |
| 14 |
use Elementor\Core\Settings\Page\Manager as PageManager; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; // Exit if accessed directly |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Elementor document. |
| 22 |
* |
| 23 |
* An abstract class that provides the needed properties and methods to |
| 24 |
* manage and handle documents in inheriting classes. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @abstract |
| 28 |
*/ |
| 29 |
abstract class Document extends Controls_Stack { |
| 30 |
|
| 31 |
/** |
| 32 |
* Document type meta key. |
| 33 |
*/ |
| 34 |
const TYPE_META_KEY = '_elementor_template_type'; |
| 35 |
const PAGE_META_KEY = '_elementor_page_settings'; |
| 36 |
|
| 37 |
const BUILT_WITH_ELEMENTOR_META_KEY = '_elementor_edit_mode'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Document publish status. |
| 41 |
*/ |
| 42 |
const STATUS_PUBLISH = 'publish'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Document draft status. |
| 46 |
*/ |
| 47 |
const STATUS_DRAFT = 'draft'; |
| 48 |
|
| 49 |
/** |
| 50 |
* Document private status. |
| 51 |
*/ |
| 52 |
const STATUS_PRIVATE = 'private'; |
| 53 |
|
| 54 |
/** |
| 55 |
* Document autosave status. |
| 56 |
*/ |
| 57 |
const STATUS_AUTOSAVE = 'autosave'; |
| 58 |
|
| 59 |
/** |
| 60 |
* Document pending status. |
| 61 |
*/ |
| 62 |
const STATUS_PENDING = 'pending'; |
| 63 |
|
| 64 |
|
| 65 |
private $main_id; |
| 66 |
|
| 67 |
/** |
| 68 |
* @var bool |
| 69 |
*/ |
| 70 |
private $is_saving = false; |
| 71 |
|
| 72 |
private static $properties = []; |
| 73 |
|
| 74 |
/** |
| 75 |
* Document post data. |
| 76 |
* |
| 77 |
* Holds the document post data. |
| 78 |
* |
| 79 |
* @since 2.0.0 |
| 80 |
* @access protected |
| 81 |
* |
| 82 |
* @var \WP_Post WordPress post data. |
| 83 |
*/ |
| 84 |
protected $post; |
| 85 |
|
| 86 |
/** |
| 87 |
* @since 2.1.0 |
| 88 |
* @access protected |
| 89 |
* @static |
| 90 |
*/ |
| 91 |
protected static function get_editor_panel_categories() { |
| 92 |
return Plugin::$instance->elements_manager->get_categories(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get properties. |
| 97 |
* |
| 98 |
* Retrieve the document properties. |
| 99 |
* |
| 100 |
* @since 2.0.0 |
| 101 |
* @access public |
| 102 |
* @static |
| 103 |
* |
| 104 |
* @return array Document properties. |
| 105 |
*/ |
| 106 |
public static function get_properties() { |
| 107 |
return [ |
| 108 |
'has_elements' => true, |
| 109 |
'is_editable' => true, |
| 110 |
'edit_capability' => '', |
| 111 |
'show_in_finder' => true, |
| 112 |
'show_on_admin_bar' => true, |
| 113 |
'support_kit' => false, |
| 114 |
]; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @since 2.1.0 |
| 119 |
* @access public |
| 120 |
* @static |
| 121 |
*/ |
| 122 |
public static function get_editor_panel_config() { |
| 123 |
$default_route = 'panel/elements/categories'; |
| 124 |
|
| 125 |
if ( ! Plugin::instance()->role_manager->user_can( 'design' ) ) { |
| 126 |
$default_route = 'panel/page-settings/settings'; |
| 127 |
} |
| 128 |
|
| 129 |
return [ |
| 130 |
'title' => static::get_title(), // JS Container title. |
| 131 |
'widgets_settings' => [], |
| 132 |
'elements_categories' => static::get_editor_panel_categories(), |
| 133 |
'default_route' => $default_route, |
| 134 |
'has_elements' => static::get_property( 'has_elements' ), |
| 135 |
'support_kit' => static::get_property( 'support_kit' ), |
| 136 |
'messages' => [ |
| 137 |
/* translators: %s: the document title. */ |
| 138 |
'publish_notification' => sprintf( __( 'Hurray! Your %s is live.', 'elementor' ), static::get_title() ), |
| 139 |
], |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get element title. |
| 145 |
* |
| 146 |
* Retrieve the element title. |
| 147 |
* |
| 148 |
* @since 2.0.0 |
| 149 |
* @access public |
| 150 |
* @static |
| 151 |
* |
| 152 |
* @return string Element title. |
| 153 |
*/ |
| 154 |
public static function get_title() { |
| 155 |
return __( 'Document', 'elementor' ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get property. |
| 160 |
* |
| 161 |
* Retrieve the document property. |
| 162 |
* |
| 163 |
* @since 2.0.0 |
| 164 |
* @access public |
| 165 |
* @static |
| 166 |
* |
| 167 |
* @param string $key The property key. |
| 168 |
* |
| 169 |
* @return mixed The property value. |
| 170 |
*/ |
| 171 |
public static function get_property( $key ) { |
| 172 |
$id = static::get_class_full_name(); |
| 173 |
|
| 174 |
if ( ! isset( self::$properties[ $id ] ) ) { |
| 175 |
self::$properties[ $id ] = static::get_properties(); |
| 176 |
} |
| 177 |
|
| 178 |
return self::get_items( self::$properties[ $id ], $key ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @since 2.0.0 |
| 183 |
* @access public |
| 184 |
* @static |
| 185 |
*/ |
| 186 |
public static function get_class_full_name() { |
| 187 |
return get_called_class(); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* @since 2.0.0 |
| 192 |
* @access public |
| 193 |
*/ |
| 194 |
public function get_unique_name() { |
| 195 |
return $this->get_name() . '-' . $this->post->ID; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* @since 2.3.0 |
| 200 |
* @access public |
| 201 |
*/ |
| 202 |
public function get_post_type_title() { |
| 203 |
$post_type_object = get_post_type_object( $this->post->post_type ); |
| 204 |
|
| 205 |
return $post_type_object->labels->singular_name; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* @since 2.0.12 |
| 210 |
* @deprecated 2.4.0 Use `Document::get_remote_library_config()` instead |
| 211 |
* @access public |
| 212 |
*/ |
| 213 |
public function get_remote_library_type() { |
| 214 |
_deprecated_function( __METHOD__, '2.4.0', __CLASS__ . '::get_remote_library_config()' ); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @since 2.0.0 |
| 219 |
* @access public |
| 220 |
*/ |
| 221 |
public function get_main_id() { |
| 222 |
if ( ! $this->main_id ) { |
| 223 |
$post_id = $this->post->ID; |
| 224 |
|
| 225 |
$parent_post_id = wp_is_post_revision( $post_id ); |
| 226 |
|
| 227 |
if ( $parent_post_id ) { |
| 228 |
$post_id = $parent_post_id; |
| 229 |
} |
| 230 |
|
| 231 |
$this->main_id = $post_id; |
| 232 |
} |
| 233 |
|
| 234 |
return $this->main_id; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* @since 2.0.0 |
| 239 |
* @access public |
| 240 |
* |
| 241 |
* @param $data |
| 242 |
* |
| 243 |
* @throws \Exception If the widget was not found. |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
public function render_element( $data ) { |
| 248 |
// Start buffering |
| 249 |
ob_start(); |
| 250 |
|
| 251 |
/** @var Widget_Base $widget */ |
| 252 |
$widget = Plugin::$instance->elements_manager->create_element_instance( $data ); |
| 253 |
|
| 254 |
if ( ! $widget ) { |
| 255 |
throw new \Exception( 'Widget not found.' ); |
| 256 |
} |
| 257 |
|
| 258 |
$widget->render_content(); |
| 259 |
|
| 260 |
$render_html = ob_get_clean(); |
| 261 |
|
| 262 |
return $render_html; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* @since 2.0.0 |
| 267 |
* @access public |
| 268 |
*/ |
| 269 |
public function get_main_post() { |
| 270 |
return get_post( $this->get_main_id() ); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* @since 2.0.6 |
| 275 |
* @deprecated 2.4.0 Use `Document::get_container_attributes()` instead |
| 276 |
* @access public |
| 277 |
*/ |
| 278 |
public function get_container_classes() { |
| 279 |
_deprecated_function( __METHOD__, '2.4.0', __CLASS__ . '::get_container_attributes()' ); |
| 280 |
|
| 281 |
return ''; |
| 282 |
} |
| 283 |
|
| 284 |
public function get_container_attributes() { |
| 285 |
$id = $this->get_main_id(); |
| 286 |
|
| 287 |
$attributes = [ |
| 288 |
'data-elementor-type' => $this->get_name(), |
| 289 |
'data-elementor-id' => $id, |
| 290 |
'class' => 'elementor elementor-' . $id, |
| 291 |
]; |
| 292 |
|
| 293 |
$version_meta = $this->get_main_meta( '_elementor_version' ); |
| 294 |
|
| 295 |
if ( version_compare( $version_meta, '2.5.0', '<' ) ) { |
| 296 |
$attributes['class'] .= ' elementor-bc-flex-widget'; |
| 297 |
} |
| 298 |
|
| 299 |
if ( Plugin::$instance->preview->is_preview() ) { |
| 300 |
$attributes['data-elementor-title'] = static::get_title(); |
| 301 |
} else { |
| 302 |
$attributes['data-elementor-settings'] = wp_json_encode( $this->get_frontend_settings() ); |
| 303 |
} |
| 304 |
|
| 305 |
return $attributes; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @since 2.0.0 |
| 310 |
* @access public |
| 311 |
*/ |
| 312 |
public function get_wp_preview_url() { |
| 313 |
$main_post_id = $this->get_main_id(); |
| 314 |
$document = $this; |
| 315 |
|
| 316 |
// Ajax request from editor. |
| 317 |
if ( ! empty( $_POST['initial_document_id'] ) ) { |
| 318 |
$document = Plugin::$instance->documents->get( $_POST['initial_document_id'] ); |
| 319 |
} |
| 320 |
|
| 321 |
$url = get_preview_post_link( |
| 322 |
$document->get_main_id(), |
| 323 |
[ |
| 324 |
'preview_id' => $main_post_id, |
| 325 |
'preview_nonce' => wp_create_nonce( 'post_preview_' . $main_post_id ), |
| 326 |
] |
| 327 |
); |
| 328 |
|
| 329 |
/** |
| 330 |
* Document "WordPress preview" URL. |
| 331 |
* |
| 332 |
* Filters the WordPress preview URL. |
| 333 |
* |
| 334 |
* @since 2.0.0 |
| 335 |
* |
| 336 |
* @param string $url WordPress preview URL. |
| 337 |
* @param Document $this The document instance. |
| 338 |
*/ |
| 339 |
$url = apply_filters( 'elementor/document/urls/wp_preview', $url, $this ); |
| 340 |
|
| 341 |
return $url; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* @since 2.0.0 |
| 346 |
* @access public |
| 347 |
*/ |
| 348 |
public function get_exit_to_dashboard_url() { |
| 349 |
$url = get_edit_post_link( $this->get_main_id(), 'raw' ); |
| 350 |
|
| 351 |
/** |
| 352 |
* Document "exit to dashboard" URL. |
| 353 |
* |
| 354 |
* Filters the "Exit To Dashboard" URL. |
| 355 |
* |
| 356 |
* @since 2.0.0 |
| 357 |
* |
| 358 |
* @param string $url The exit URL |
| 359 |
* @param Document $this The document instance. |
| 360 |
*/ |
| 361 |
$url = apply_filters( 'elementor/document/urls/exit_to_dashboard', $url, $this ); |
| 362 |
|
| 363 |
return $url; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Get auto-saved post revision. |
| 368 |
* |
| 369 |
* Retrieve the auto-saved post revision that is newer than current post. |
| 370 |
* |
| 371 |
* @since 2.0.0 |
| 372 |
* @access public |
| 373 |
* |
| 374 |
* |
| 375 |
* @return bool|Document |
| 376 |
*/ |
| 377 |
|
| 378 |
public function get_newer_autosave() { |
| 379 |
$autosave = $this->get_autosave(); |
| 380 |
|
| 381 |
// Detect if there exists an autosave newer than the post. |
| 382 |
if ( $autosave && mysql2date( 'U', $autosave->get_post()->post_modified_gmt, false ) > mysql2date( 'U', $this->post->post_modified_gmt, false ) ) { |
| 383 |
return $autosave; |
| 384 |
} |
| 385 |
|
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* @since 2.0.0 |
| 391 |
* @access public |
| 392 |
*/ |
| 393 |
public function is_autosave() { |
| 394 |
return wp_is_post_autosave( $this->post->ID ); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Check if the current document is a 'revision' |
| 399 |
* |
| 400 |
* @return bool |
| 401 |
*/ |
| 402 |
public function is_revision() { |
| 403 |
return 'revision' === $this->post->post_type; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Checks if the current document status is 'trash'. |
| 408 |
* |
| 409 |
* @return bool |
| 410 |
*/ |
| 411 |
public function is_trash() { |
| 412 |
return 'trash' === $this->post->post_status; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* @since 2.0.0 |
| 417 |
* @access public |
| 418 |
* |
| 419 |
* @param int $user_id |
| 420 |
* @param bool $create |
| 421 |
* |
| 422 |
* @return bool|Document |
| 423 |
*/ |
| 424 |
public function get_autosave( $user_id = 0, $create = false ) { |
| 425 |
if ( ! $user_id ) { |
| 426 |
$user_id = get_current_user_id(); |
| 427 |
} |
| 428 |
|
| 429 |
$autosave_id = $this->get_autosave_id( $user_id ); |
| 430 |
|
| 431 |
if ( $autosave_id ) { |
| 432 |
$document = Plugin::$instance->documents->get( $autosave_id ); |
| 433 |
} elseif ( $create ) { |
| 434 |
$autosave_id = wp_create_post_autosave( [ |
| 435 |
'post_ID' => $this->post->ID, |
| 436 |
'post_type' => $this->post->post_type, |
| 437 |
'post_title' => $this->post->post_title, |
| 438 |
'post_excerpt' => $this->post->post_excerpt, |
| 439 |
// Hack to cause $autosave_is_different=true in `wp_create_post_autosave`. |
| 440 |
'post_content' => '<!-- Created With Elementor -->', |
| 441 |
'post_modified' => current_time( 'mysql' ), |
| 442 |
] ); |
| 443 |
|
| 444 |
Plugin::$instance->db->copy_elementor_meta( $this->post->ID, $autosave_id ); |
| 445 |
|
| 446 |
$document = Plugin::$instance->documents->get( $autosave_id ); |
| 447 |
$document->save_template_type(); |
| 448 |
} else { |
| 449 |
$document = false; |
| 450 |
} |
| 451 |
|
| 452 |
return $document; |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Add/Remove edit link in dashboard. |
| 457 |
* |
| 458 |
* Add or remove an edit link to the post/page action links on the post/pages list table. |
| 459 |
* |
| 460 |
* Fired by `post_row_actions` and `page_row_actions` filters. |
| 461 |
* |
| 462 |
* @access public |
| 463 |
* |
| 464 |
* @param array $actions An array of row action links. |
| 465 |
* |
| 466 |
* @return array An updated array of row action links. |
| 467 |
*/ |
| 468 |
public function filter_admin_row_actions( $actions ) { |
| 469 |
if ( $this->is_built_with_elementor() && $this->is_editable_by_current_user() ) { |
| 470 |
$actions['edit_with_elementor'] = sprintf( |
| 471 |
'<a href="%1$s">%2$s</a>', |
| 472 |
$this->get_edit_url(), |
| 473 |
__( 'Edit with Elementor', 'elementor' ) |
| 474 |
); |
| 475 |
} |
| 476 |
|
| 477 |
return $actions; |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* @since 2.0.0 |
| 482 |
* @access public |
| 483 |
*/ |
| 484 |
public function is_editable_by_current_user() { |
| 485 |
$edit_capability = static::get_property( 'edit_capability' ); |
| 486 |
if ( $edit_capability && ! current_user_can( $edit_capability ) ) { |
| 487 |
return false; |
| 488 |
} |
| 489 |
|
| 490 |
return self::get_property( 'is_editable' ) && User::is_current_user_can_edit( $this->get_main_id() ); |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* @since 2.9.0 |
| 495 |
* @access protected |
| 496 |
*/ |
| 497 |
protected function get_initial_config() { |
| 498 |
// Get document data *after* the scripts hook - so plugins can run compatibility before get data, but *before* enqueue the editor script - so elements can enqueue their own scripts that depended in editor script. |
| 499 |
|
| 500 |
$locked_user = Plugin::$instance->editor->get_locked_user( $this->get_main_id() ); |
| 501 |
|
| 502 |
if ( $locked_user ) { |
| 503 |
$locked_user = $locked_user->display_name; |
| 504 |
} |
| 505 |
|
| 506 |
$post_type_object = get_post_type_object( $this->get_main_post()->post_type ); |
| 507 |
|
| 508 |
$settings = SettingsManager::get_settings_managers_config(); |
| 509 |
|
| 510 |
$config = [ |
| 511 |
'id' => $this->get_main_id(), |
| 512 |
'type' => $this->get_name(), |
| 513 |
'version' => $this->get_main_meta( '_elementor_version' ), |
| 514 |
'settings' => $settings['page'], |
| 515 |
'remoteLibrary' => $this->get_remote_library_config(), |
| 516 |
'last_edited' => $this->get_last_edited(), |
| 517 |
'panel' => static::get_editor_panel_config(), |
| 518 |
'container' => 'body', |
| 519 |
'post_type_title' => $this->get_post_type_title(), |
| 520 |
'user' => [ |
| 521 |
'can_publish' => current_user_can( $post_type_object->cap->publish_posts ), |
| 522 |
|
| 523 |
// Deprecated config since 2.9.0. |
| 524 |
'locked' => $locked_user, |
| 525 |
], |
| 526 |
'urls' => [ |
| 527 |
'exit_to_dashboard' => $this->get_exit_to_dashboard_url(), |
| 528 |
'preview' => $this->get_preview_url(), |
| 529 |
'wp_preview' => $this->get_wp_preview_url(), |
| 530 |
'permalink' => $this->get_permalink(), |
| 531 |
'have_a_look' => $this->get_have_a_look_url(), |
| 532 |
], |
| 533 |
]; |
| 534 |
|
| 535 |
if ( static::get_property( 'has_elements' ) ) { |
| 536 |
$config['elements'] = $this->get_elements_raw_data( null, true ); |
| 537 |
$config['widgets'] = Plugin::$instance->widgets_manager->get_widget_types_config(); |
| 538 |
} |
| 539 |
|
| 540 |
$additional_config = apply_filters( 'elementor/document/config', [], $this->get_main_id() ); |
| 541 |
|
| 542 |
if ( ! empty( $additional_config ) ) { |
| 543 |
$config = array_replace_recursive( $config, $additional_config ); |
| 544 |
} |
| 545 |
|
| 546 |
return $config; |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* @since 3.1.0 |
| 551 |
* @access protected |
| 552 |
*/ |
| 553 |
protected function register_controls() { |
| 554 |
$this->register_document_controls(); |
| 555 |
/** |
| 556 |
* Register document controls. |
| 557 |
* |
| 558 |
* Fires after Elementor registers the document controls. |
| 559 |
* |
| 560 |
* @since 2.0.0 |
| 561 |
* |
| 562 |
* @param Document $this The document instance. |
| 563 |
*/ |
| 564 |
do_action( 'elementor/documents/register_controls', $this ); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* @since 2.0.0 |
| 569 |
* @access public |
| 570 |
* |
| 571 |
* @param $data |
| 572 |
* |
| 573 |
* @return bool |
| 574 |
*/ |
| 575 |
public function save( $data ) { |
| 576 |
$this->add_handle_revisions_changed_filter(); |
| 577 |
|
| 578 |
if ( ! $this->is_editable_by_current_user() ) { |
| 579 |
return false; |
| 580 |
} |
| 581 |
|
| 582 |
$this->set_is_saving( true ); |
| 583 |
|
| 584 |
/** |
| 585 |
* Before document save. |
| 586 |
* |
| 587 |
* Fires when document save starts on Elementor. |
| 588 |
* |
| 589 |
* @since 2.5.12 |
| 590 |
* |
| 591 |
* @param \Elementor\Core\Base\Document $this The current document. |
| 592 |
* @param $data. |
| 593 |
*/ |
| 594 |
do_action( 'elementor/document/before_save', $this, $data ); |
| 595 |
|
| 596 |
if ( ! current_user_can( 'unfiltered_html' ) ) { |
| 597 |
$data = wp_kses_post_deep( $data ); |
| 598 |
} |
| 599 |
|
| 600 |
if ( ! empty( $data['settings'] ) ) { |
| 601 |
if ( isset( $data['settings']['post_status'] ) && self::STATUS_AUTOSAVE === $data['settings']['post_status'] ) { |
| 602 |
if ( ! defined( 'DOING_AUTOSAVE' ) ) { |
| 603 |
define( 'DOING_AUTOSAVE', true ); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
$this->save_settings( $data['settings'] ); |
| 608 |
|
| 609 |
// Refresh post after save settings. |
| 610 |
$this->post = get_post( $this->post->ID ); |
| 611 |
} |
| 612 |
|
| 613 |
// Don't check is_empty, because an empty array should be saved. |
| 614 |
if ( isset( $data['elements'] ) && is_array( $data['elements'] ) ) { |
| 615 |
$this->save_elements( $data['elements'] ); |
| 616 |
} |
| 617 |
|
| 618 |
$this->save_template_type(); |
| 619 |
|
| 620 |
$this->save_version(); |
| 621 |
|
| 622 |
// Remove Post CSS |
| 623 |
$post_css = Post_CSS::create( $this->post->ID ); |
| 624 |
|
| 625 |
$post_css->delete(); |
| 626 |
|
| 627 |
/** |
| 628 |
* After document save. |
| 629 |
* |
| 630 |
* Fires when document save is complete. |
| 631 |
* |
| 632 |
* @since 2.5.12 |
| 633 |
* |
| 634 |
* @param \Elementor\Core\Base\Document $this The current document. |
| 635 |
* @param $data. |
| 636 |
*/ |
| 637 |
do_action( 'elementor/document/after_save', $this, $data ); |
| 638 |
|
| 639 |
$this->set_is_saving( false ); |
| 640 |
|
| 641 |
$this->remove_handle_revisions_changed_filter(); |
| 642 |
|
| 643 |
return true; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* @param array $new_settings |
| 648 |
* |
| 649 |
* @return static |
| 650 |
*/ |
| 651 |
public function update_settings( array $new_settings ) { |
| 652 |
$document_settings = $this->get_meta( PageManager::META_KEY ); |
| 653 |
|
| 654 |
if ( ! $document_settings ) { |
| 655 |
$document_settings = []; |
| 656 |
} |
| 657 |
|
| 658 |
$this->save_settings( |
| 659 |
array_replace_recursive( $document_settings, $new_settings ) |
| 660 |
); |
| 661 |
|
| 662 |
return $this; |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* Is built with Elementor. |
| 667 |
* |
| 668 |
* Check whether the post was built with Elementor. |
| 669 |
* |
| 670 |
* @since 2.0.0 |
| 671 |
* @access public |
| 672 |
* |
| 673 |
* @return bool Whether the post was built with Elementor. |
| 674 |
*/ |
| 675 |
public function is_built_with_elementor() { |
| 676 |
return ! ! $this->get_meta( self::BUILT_WITH_ELEMENTOR_META_KEY ); |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Mark the post as "built with elementor" or not. |
| 681 |
* |
| 682 |
* @param bool $is_built_with_elementor |
| 683 |
* |
| 684 |
* @return $this |
| 685 |
*/ |
| 686 |
public function set_is_built_with_elementor( $is_built_with_elementor ) { |
| 687 |
if ( $is_built_with_elementor ) { |
| 688 |
// Use the string `builder` and not a boolean for rollback compatibility |
| 689 |
$this->update_meta( self::BUILT_WITH_ELEMENTOR_META_KEY, 'builder' ); |
| 690 |
} else { |
| 691 |
$this->delete_meta( self::BUILT_WITH_ELEMENTOR_META_KEY ); |
| 692 |
} |
| 693 |
|
| 694 |
return $this; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* @since 2.0.0 |
| 699 |
* @access public |
| 700 |
* @static |
| 701 |
* |
| 702 |
* @return mixed |
| 703 |
*/ |
| 704 |
public function get_edit_url() { |
| 705 |
$url = add_query_arg( |
| 706 |
[ |
| 707 |
'post' => $this->get_main_id(), |
| 708 |
'action' => 'elementor', |
| 709 |
], |
| 710 |
admin_url( 'post.php' ) |
| 711 |
); |
| 712 |
|
| 713 |
/** |
| 714 |
* Document edit url. |
| 715 |
* |
| 716 |
* Filters the document edit url. |
| 717 |
* |
| 718 |
* @since 2.0.0 |
| 719 |
* |
| 720 |
* @param string $url The edit url. |
| 721 |
* @param Document $this The document instance. |
| 722 |
*/ |
| 723 |
$url = apply_filters( 'elementor/document/urls/edit', $url, $this ); |
| 724 |
|
| 725 |
return $url; |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
* @since 2.0.0 |
| 730 |
* @access public |
| 731 |
*/ |
| 732 |
public function get_preview_url() { |
| 733 |
/** |
| 734 |
* Use a static var - to avoid change the `ver` parameter on every call. |
| 735 |
*/ |
| 736 |
static $url; |
| 737 |
|
| 738 |
if ( empty( $url ) ) { |
| 739 |
|
| 740 |
add_filter( 'pre_option_permalink_structure', '__return_empty_string' ); |
| 741 |
|
| 742 |
$url = set_url_scheme( add_query_arg( [ |
| 743 |
'elementor-preview' => $this->get_main_id(), |
| 744 |
'ver' => time(), |
| 745 |
], $this->get_permalink() ) ); |
| 746 |
|
| 747 |
remove_filter( 'pre_option_permalink_structure', '__return_empty_string' ); |
| 748 |
|
| 749 |
/** |
| 750 |
* Document preview URL. |
| 751 |
* |
| 752 |
* Filters the document preview URL. |
| 753 |
* |
| 754 |
* @since 2.0.0 |
| 755 |
* |
| 756 |
* @param string $url The preview URL. |
| 757 |
* @param Document $this The document instance. |
| 758 |
*/ |
| 759 |
$url = apply_filters( 'elementor/document/urls/preview', $url, $this ); |
| 760 |
} |
| 761 |
|
| 762 |
return $url; |
| 763 |
} |
| 764 |
|
| 765 |
/** |
| 766 |
* @since 2.0.0 |
| 767 |
* @access public |
| 768 |
* |
| 769 |
* @param string $key |
| 770 |
* |
| 771 |
* @return array |
| 772 |
*/ |
| 773 |
public function get_json_meta( $key ) { |
| 774 |
$meta = get_post_meta( $this->post->ID, $key, true ); |
| 775 |
|
| 776 |
if ( is_string( $meta ) && ! empty( $meta ) ) { |
| 777 |
$meta = json_decode( $meta, true ); |
| 778 |
} |
| 779 |
|
| 780 |
if ( empty( $meta ) ) { |
| 781 |
$meta = []; |
| 782 |
} |
| 783 |
|
| 784 |
return $meta; |
| 785 |
} |
| 786 |
|
| 787 |
/** |
| 788 |
* @since 2.0.0 |
| 789 |
* @access public |
| 790 |
* |
| 791 |
* @param null $data |
| 792 |
* @param bool $with_html_content |
| 793 |
* |
| 794 |
* @return array |
| 795 |
*/ |
| 796 |
public function get_elements_raw_data( $data = null, $with_html_content = false ) { |
| 797 |
if ( ! static::get_property( 'has_elements' ) ) { |
| 798 |
return []; |
| 799 |
} |
| 800 |
|
| 801 |
if ( is_null( $data ) ) { |
| 802 |
$data = $this->get_elements_data(); |
| 803 |
} |
| 804 |
|
| 805 |
// Change the current documents, so widgets can use `documents->get_current` and other post data |
| 806 |
Plugin::$instance->documents->switch_to_document( $this ); |
| 807 |
|
| 808 |
$editor_data = []; |
| 809 |
|
| 810 |
foreach ( $data as $element_data ) { |
| 811 |
$element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 812 |
|
| 813 |
if ( ! $element ) { |
| 814 |
continue; |
| 815 |
} |
| 816 |
|
| 817 |
$editor_data[] = $element->get_raw_data( $with_html_content ); |
| 818 |
} // End foreach(). |
| 819 |
|
| 820 |
Plugin::$instance->documents->restore_document(); |
| 821 |
|
| 822 |
return $editor_data; |
| 823 |
} |
| 824 |
|
| 825 |
/** |
| 826 |
* @since 2.0.0 |
| 827 |
* @access public |
| 828 |
* |
| 829 |
* @param string $status |
| 830 |
* |
| 831 |
* @return array |
| 832 |
*/ |
| 833 |
public function get_elements_data( $status = self::STATUS_PUBLISH ) { |
| 834 |
$elements = $this->get_json_meta( '_elementor_data' ); |
| 835 |
|
| 836 |
if ( self::STATUS_DRAFT === $status ) { |
| 837 |
$autosave = $this->get_newer_autosave(); |
| 838 |
|
| 839 |
if ( is_object( $autosave ) ) { |
| 840 |
$autosave_elements = Plugin::$instance->documents |
| 841 |
->get( $autosave->get_post()->ID ) |
| 842 |
->get_json_meta( '_elementor_data' ); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
if ( Plugin::$instance->editor->is_edit_mode() ) { |
| 847 |
if ( empty( $elements ) && empty( $autosave_elements ) ) { |
| 848 |
// Convert to Elementor. |
| 849 |
$elements = $this->convert_to_elementor(); |
| 850 |
if ( $this->is_autosave() ) { |
| 851 |
Plugin::$instance->db->copy_elementor_meta( $this->post->post_parent, $this->post->ID ); |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
if ( ! empty( $autosave_elements ) ) { |
| 857 |
$elements = $autosave_elements; |
| 858 |
} |
| 859 |
|
| 860 |
return $elements; |
| 861 |
} |
| 862 |
|
| 863 |
/** |
| 864 |
* Get document setting from DB. |
| 865 |
* |
| 866 |
* @return array |
| 867 |
*/ |
| 868 |
public function get_db_document_settings() { |
| 869 |
return $this->get_meta( static::PAGE_META_KEY ); |
| 870 |
} |
| 871 |
|
| 872 |
/** |
| 873 |
* @since 2.3.0 |
| 874 |
* @access public |
| 875 |
*/ |
| 876 |
public function convert_to_elementor() { |
| 877 |
$this->save( [] ); |
| 878 |
|
| 879 |
if ( empty( $this->post->post_content ) ) { |
| 880 |
return []; |
| 881 |
} |
| 882 |
|
| 883 |
// Check if it's only a shortcode. |
| 884 |
preg_match_all( '/' . get_shortcode_regex() . '/', $this->post->post_content, $matches, PREG_SET_ORDER ); |
| 885 |
if ( ! empty( $matches ) ) { |
| 886 |
foreach ( $matches as $shortcode ) { |
| 887 |
if ( trim( $this->post->post_content ) === $shortcode[0] ) { |
| 888 |
$widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'shortcode' ); |
| 889 |
$settings = [ |
| 890 |
'shortcode' => $this->post->post_content, |
| 891 |
]; |
| 892 |
break; |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
if ( empty( $widget_type ) ) { |
| 898 |
$widget_type = Plugin::$instance->widgets_manager->get_widget_types( 'text-editor' ); |
| 899 |
$settings = [ |
| 900 |
'editor' => $this->post->post_content, |
| 901 |
]; |
| 902 |
} |
| 903 |
|
| 904 |
// TODO: Better coding to start template for editor |
| 905 |
return [ |
| 906 |
[ |
| 907 |
'id' => Utils::generate_random_string(), |
| 908 |
'elType' => 'section', |
| 909 |
'elements' => [ |
| 910 |
[ |
| 911 |
'id' => Utils::generate_random_string(), |
| 912 |
'elType' => 'column', |
| 913 |
'elements' => [ |
| 914 |
[ |
| 915 |
'id' => Utils::generate_random_string(), |
| 916 |
'elType' => $widget_type::get_type(), |
| 917 |
'widgetType' => $widget_type->get_name(), |
| 918 |
'settings' => $settings, |
| 919 |
], |
| 920 |
], |
| 921 |
], |
| 922 |
], |
| 923 |
], |
| 924 |
]; |
| 925 |
} |
| 926 |
|
| 927 |
/** |
| 928 |
* @since 2.1.3 |
| 929 |
* @access public |
| 930 |
*/ |
| 931 |
public function print_elements_with_wrapper( $elements_data = null ) { |
| 932 |
if ( ! $elements_data ) { |
| 933 |
$elements_data = $this->get_elements_data(); |
| 934 |
} |
| 935 |
|
| 936 |
$is_dom_optimization_active = Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ); |
| 937 |
?> |
| 938 |
<div <?php echo Utils::render_html_attributes( $this->get_container_attributes() ); ?>> |
| 939 |
<?php if ( ! $is_dom_optimization_active ) { ?> |
| 940 |
<div class="elementor-inner"> |
| 941 |
<?php } ?> |
| 942 |
<div class="elementor-section-wrap"> |
| 943 |
<?php $this->print_elements( $elements_data ); ?> |
| 944 |
</div> |
| 945 |
<?php if ( ! $is_dom_optimization_active ) { ?> |
| 946 |
</div> |
| 947 |
<?php } ?> |
| 948 |
</div> |
| 949 |
<?php |
| 950 |
} |
| 951 |
|
| 952 |
/** |
| 953 |
* @since 2.0.0 |
| 954 |
* @access public |
| 955 |
*/ |
| 956 |
public function get_css_wrapper_selector() { |
| 957 |
return ''; |
| 958 |
} |
| 959 |
|
| 960 |
/** |
| 961 |
* @since 2.0.0 |
| 962 |
* @access public |
| 963 |
*/ |
| 964 |
public function get_panel_page_settings() { |
| 965 |
return [ |
| 966 |
/* translators: %s: Document title */ |
| 967 |
'title' => sprintf( __( '%s Settings', 'elementor' ), static::get_title() ), |
| 968 |
]; |
| 969 |
} |
| 970 |
|
| 971 |
/** |
| 972 |
* @since 2.0.0 |
| 973 |
* @access public |
| 974 |
*/ |
| 975 |
public function get_post() { |
| 976 |
return $this->post; |
| 977 |
} |
| 978 |
|
| 979 |
/** |
| 980 |
* @since 2.0.0 |
| 981 |
* @access public |
| 982 |
*/ |
| 983 |
public function get_permalink() { |
| 984 |
return get_permalink( $this->get_main_id() ); |
| 985 |
} |
| 986 |
|
| 987 |
/** |
| 988 |
* @since 2.0.8 |
| 989 |
* @access public |
| 990 |
*/ |
| 991 |
public function get_content( $with_css = false ) { |
| 992 |
return Plugin::$instance->frontend->get_builder_content( $this->post->ID, $with_css ); |
| 993 |
} |
| 994 |
|
| 995 |
/** |
| 996 |
* @since 2.0.0 |
| 997 |
* @access public |
| 998 |
*/ |
| 999 |
public function delete() { |
| 1000 |
if ( 'revision' === $this->post->post_type ) { |
| 1001 |
$deleted = wp_delete_post_revision( $this->post ); |
| 1002 |
} else { |
| 1003 |
$deleted = wp_delete_post( $this->post->ID ); |
| 1004 |
} |
| 1005 |
|
| 1006 |
return $deleted && ! is_wp_error( $deleted ); |
| 1007 |
} |
| 1008 |
|
| 1009 |
/** |
| 1010 |
* Save editor elements. |
| 1011 |
* |
| 1012 |
* Save data from the editor to the database. |
| 1013 |
* |
| 1014 |
* @since 2.0.0 |
| 1015 |
* @access protected |
| 1016 |
* |
| 1017 |
* @param array $elements |
| 1018 |
*/ |
| 1019 |
protected function save_elements( $elements ) { |
| 1020 |
$editor_data = $this->get_elements_raw_data( $elements ); |
| 1021 |
|
| 1022 |
// We need the `wp_slash` in order to avoid the unslashing during the `update_post_meta` |
| 1023 |
$json_value = wp_slash( wp_json_encode( $editor_data ) ); |
| 1024 |
|
| 1025 |
// Don't use `update_post_meta` that can't handle `revision` post type |
| 1026 |
$is_meta_updated = update_metadata( 'post', $this->post->ID, '_elementor_data', $json_value ); |
| 1027 |
|
| 1028 |
/** |
| 1029 |
* Before saving data. |
| 1030 |
* |
| 1031 |
* Fires before Elementor saves data to the database. |
| 1032 |
* |
| 1033 |
* @since 1.0.0 |
| 1034 |
* |
| 1035 |
* @param string $status Post status. |
| 1036 |
* @param int|bool $is_meta_updated Meta ID if the key didn't exist, true on successful update, false on failure. |
| 1037 |
*/ |
| 1038 |
do_action( 'elementor/db/before_save', $this->post->post_status, $is_meta_updated ); |
| 1039 |
|
| 1040 |
Plugin::$instance->db->save_plain_text( $this->post->ID ); |
| 1041 |
|
| 1042 |
/** |
| 1043 |
* After saving data. |
| 1044 |
* |
| 1045 |
* Fires after Elementor saves data to the database. |
| 1046 |
* |
| 1047 |
* @since 1.0.0 |
| 1048 |
* |
| 1049 |
* @param int $post_id The ID of the post. |
| 1050 |
* @param array $editor_data Sanitize posted data. |
| 1051 |
*/ |
| 1052 |
do_action( 'elementor/editor/after_save', $this->post->ID, $editor_data ); |
| 1053 |
} |
| 1054 |
|
| 1055 |
/** |
| 1056 |
* @since 2.0.0 |
| 1057 |
* @access public |
| 1058 |
* |
| 1059 |
* @param int $user_id Optional. User ID. Default value is `0`. |
| 1060 |
* |
| 1061 |
* @return bool|int |
| 1062 |
*/ |
| 1063 |
public function get_autosave_id( $user_id = 0 ) { |
| 1064 |
if ( ! $user_id ) { |
| 1065 |
$user_id = get_current_user_id(); |
| 1066 |
} |
| 1067 |
|
| 1068 |
$autosave = Utils::get_post_autosave( $this->post->ID, $user_id ); |
| 1069 |
if ( $autosave ) { |
| 1070 |
return $autosave->ID; |
| 1071 |
} |
| 1072 |
|
| 1073 |
return false; |
| 1074 |
} |
| 1075 |
|
| 1076 |
public function save_version() { |
| 1077 |
if ( ! defined( 'IS_ELEMENTOR_UPGRADE' ) ) { |
| 1078 |
// Save per revision. |
| 1079 |
$this->update_meta( '_elementor_version', ELEMENTOR_VERSION ); |
| 1080 |
|
| 1081 |
/** |
| 1082 |
* Document version save. |
| 1083 |
* |
| 1084 |
* Fires when document version is saved on Elementor. |
| 1085 |
* Will not fire during Elementor Upgrade. |
| 1086 |
* |
| 1087 |
* @since 2.5.12 |
| 1088 |
* |
| 1089 |
* @param \Elementor\Core\Base\Document $this The current document. |
| 1090 |
* |
| 1091 |
*/ |
| 1092 |
do_action( 'elementor/document/save_version', $this ); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
/** |
| 1097 |
* @since 2.3.0 |
| 1098 |
* @access public |
| 1099 |
*/ |
| 1100 |
public function save_template_type() { |
| 1101 |
return $this->update_main_meta( self::TYPE_META_KEY, $this->get_name() ); |
| 1102 |
} |
| 1103 |
|
| 1104 |
/** |
| 1105 |
* @since 2.3.0 |
| 1106 |
* @access public |
| 1107 |
*/ |
| 1108 |
public function get_template_type() { |
| 1109 |
return $this->get_main_meta( self::TYPE_META_KEY ); |
| 1110 |
} |
| 1111 |
|
| 1112 |
/** |
| 1113 |
* @since 2.0.0 |
| 1114 |
* @access public |
| 1115 |
* |
| 1116 |
* @param string $key Meta data key. |
| 1117 |
* |
| 1118 |
* @return mixed |
| 1119 |
*/ |
| 1120 |
public function get_main_meta( $key ) { |
| 1121 |
return get_post_meta( $this->get_main_id(), $key, true ); |
| 1122 |
} |
| 1123 |
|
| 1124 |
/** |
| 1125 |
* @since 2.0.4 |
| 1126 |
* @access public |
| 1127 |
* |
| 1128 |
* @param string $key Meta data key. |
| 1129 |
* @param string $value Meta data value. |
| 1130 |
* |
| 1131 |
* @return bool|int |
| 1132 |
*/ |
| 1133 |
public function update_main_meta( $key, $value ) { |
| 1134 |
return update_post_meta( $this->get_main_id(), $key, $value ); |
| 1135 |
} |
| 1136 |
|
| 1137 |
/** |
| 1138 |
* @since 2.0.4 |
| 1139 |
* @access public |
| 1140 |
* |
| 1141 |
* @param string $key Meta data key. |
| 1142 |
* @param string $value Optional. Meta data value. Default is an empty string. |
| 1143 |
* |
| 1144 |
* @return bool |
| 1145 |
*/ |
| 1146 |
public function delete_main_meta( $key, $value = '' ) { |
| 1147 |
return delete_post_meta( $this->get_main_id(), $key, $value ); |
| 1148 |
} |
| 1149 |
|
| 1150 |
/** |
| 1151 |
* @since 2.0.0 |
| 1152 |
* @access public |
| 1153 |
* |
| 1154 |
* @param string $key Meta data key. |
| 1155 |
* |
| 1156 |
* @return mixed |
| 1157 |
*/ |
| 1158 |
public function get_meta( $key ) { |
| 1159 |
return get_post_meta( $this->post->ID, $key, true ); |
| 1160 |
} |
| 1161 |
|
| 1162 |
/** |
| 1163 |
* @since 2.0.0 |
| 1164 |
* @access public |
| 1165 |
* |
| 1166 |
* @param string $key Meta data key. |
| 1167 |
* @param mixed $value Meta data value. |
| 1168 |
* |
| 1169 |
* @return bool|int |
| 1170 |
*/ |
| 1171 |
public function update_meta( $key, $value ) { |
| 1172 |
// Use `update_metadata` in order to work also with revisions. |
| 1173 |
return update_metadata( 'post', $this->post->ID, $key, $value ); |
| 1174 |
} |
| 1175 |
|
| 1176 |
/** |
| 1177 |
* @since 2.0.3 |
| 1178 |
* @access public |
| 1179 |
* |
| 1180 |
* @param string $key Meta data key. |
| 1181 |
* @param string $value Meta data value. |
| 1182 |
* |
| 1183 |
* @return bool |
| 1184 |
*/ |
| 1185 |
public function delete_meta( $key, $value = '' ) { |
| 1186 |
// Use `delete_metadata` in order to work also with revisions. |
| 1187 |
return delete_metadata( 'post', $this->post->ID, $key, $value ); |
| 1188 |
} |
| 1189 |
|
| 1190 |
/** |
| 1191 |
* @since 2.0.0 |
| 1192 |
* @access public |
| 1193 |
*/ |
| 1194 |
public function get_last_edited() { |
| 1195 |
$post = $this->post; |
| 1196 |
$autosave_post = $this->get_autosave(); |
| 1197 |
|
| 1198 |
if ( $autosave_post ) { |
| 1199 |
$post = $autosave_post->get_post(); |
| 1200 |
} |
| 1201 |
|
| 1202 |
$date = date_i18n( _x( 'M j, H:i', 'revision date format', 'elementor' ), strtotime( $post->post_modified ) ); |
| 1203 |
$display_name = get_the_author_meta( 'display_name', $post->post_author ); |
| 1204 |
|
| 1205 |
if ( $autosave_post || 'revision' === $post->post_type ) { |
| 1206 |
/* translators: 1: Saving date, 2: Author display name */ |
| 1207 |
$last_edited = sprintf( __( 'Draft saved on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name ); |
| 1208 |
} else { |
| 1209 |
/* translators: 1: Editing date, 2: Author display name */ |
| 1210 |
$last_edited = sprintf( __( 'Last edited on %1$s by %2$s', 'elementor' ), '<time>' . $date . '</time>', $display_name ); |
| 1211 |
} |
| 1212 |
|
| 1213 |
return $last_edited; |
| 1214 |
} |
| 1215 |
|
| 1216 |
|
| 1217 |
/** |
| 1218 |
* @return bool |
| 1219 |
*/ |
| 1220 |
public function is_saving() { |
| 1221 |
return $this->is_saving; |
| 1222 |
} |
| 1223 |
|
| 1224 |
/** |
| 1225 |
* @param $is_saving |
| 1226 |
* |
| 1227 |
* @return $this |
| 1228 |
*/ |
| 1229 |
public function set_is_saving( $is_saving ) { |
| 1230 |
$this->is_saving = $is_saving; |
| 1231 |
|
| 1232 |
return $this; |
| 1233 |
} |
| 1234 |
|
| 1235 |
/** |
| 1236 |
* @since 2.0.0 |
| 1237 |
* @access public |
| 1238 |
* |
| 1239 |
* @param array $data |
| 1240 |
* |
| 1241 |
* @throws \Exception If the post does not exist. |
| 1242 |
*/ |
| 1243 |
public function __construct( array $data = [] ) { |
| 1244 |
if ( $data ) { |
| 1245 |
if ( empty( $data['post_id'] ) ) { |
| 1246 |
$this->post = new \WP_Post( (object) [] ); |
| 1247 |
} else { |
| 1248 |
$this->post = get_post( $data['post_id'] ); |
| 1249 |
|
| 1250 |
if ( ! $this->post ) { |
| 1251 |
throw new \Exception( sprintf( 'Post ID #%s does not exist.', $data['post_id'] ), Exceptions::NOT_FOUND ); |
| 1252 |
} |
| 1253 |
} |
| 1254 |
|
| 1255 |
// Each Control_Stack is based on a unique ID. |
| 1256 |
$data['id'] = $data['post_id']; |
| 1257 |
|
| 1258 |
if ( ! isset( $data['settings'] ) ) { |
| 1259 |
$data['settings'] = []; |
| 1260 |
} |
| 1261 |
|
| 1262 |
$saved_settings = get_post_meta( $this->post->ID, '_elementor_page_settings', true ); |
| 1263 |
if ( ! empty( $saved_settings ) && is_array( $saved_settings ) ) { |
| 1264 |
$data['settings'] += $saved_settings; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
|
| 1268 |
parent::__construct( $data ); |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* |
| 1272 |
* Get Export Data |
| 1273 |
* |
| 1274 |
* Filters a document's data on export |
| 1275 |
* |
| 1276 |
* @since 3.2.0 |
| 1277 |
* @access public |
| 1278 |
* |
| 1279 |
* @return array The data to export |
| 1280 |
*/ |
| 1281 |
public function get_export_data() { |
| 1282 |
$content = Plugin::$instance->db->iterate_data( $this->get_elements_data(), function( $element_data ) { |
| 1283 |
$element_data['id'] = Utils::generate_random_string(); |
| 1284 |
|
| 1285 |
$element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 1286 |
|
| 1287 |
// If the widget/element isn't exist, like a plugin that creates a widget but deactivated |
| 1288 |
if ( ! $element ) { |
| 1289 |
return null; |
| 1290 |
} |
| 1291 |
|
| 1292 |
return $this->process_element_import_export( $element, 'on_export' ); |
| 1293 |
} ); |
| 1294 |
|
| 1295 |
return [ |
| 1296 |
'content' => $content, |
| 1297 |
'settings' => $this->get_data( 'settings' ), |
| 1298 |
]; |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* |
| 1302 |
* Get Import Data |
| 1303 |
* |
| 1304 |
* Filters a document's data on import |
| 1305 |
* |
| 1306 |
* @since 3.2.0 |
| 1307 |
* @access public |
| 1308 |
* |
| 1309 |
* @return array The data to import |
| 1310 |
*/ |
| 1311 |
public function get_import_data( array $data ) { |
| 1312 |
$data['content'] = Plugin::$instance->db->iterate_data( $data['content'], function( $element_data ) { |
| 1313 |
$element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 1314 |
|
| 1315 |
// If the widget/element isn't exist, like a plugin that creates a widget but deactivated |
| 1316 |
if ( ! $element ) { |
| 1317 |
return null; |
| 1318 |
} |
| 1319 |
|
| 1320 |
return $this->process_element_import_export( $element, 'on_import' ); |
| 1321 |
} ); |
| 1322 |
|
| 1323 |
if ( ! empty( $data['settings'] ) ) { |
| 1324 |
$template_model = new Page_Model( [ |
| 1325 |
'id' => 0, |
| 1326 |
'settings' => $data['settings'], |
| 1327 |
] ); |
| 1328 |
|
| 1329 |
$page_data = $this->process_element_import_export( $template_model, 'on_import' ); |
| 1330 |
|
| 1331 |
$data['settings'] = $page_data['settings']; |
| 1332 |
} |
| 1333 |
|
| 1334 |
return $data; |
| 1335 |
} |
| 1336 |
|
| 1337 |
/** |
| 1338 |
* Import |
| 1339 |
* |
| 1340 |
* Allows to import an external data to a document |
| 1341 |
* |
| 1342 |
* @since 3.2.0 |
| 1343 |
* @access public |
| 1344 |
* |
| 1345 |
* @param array $data |
| 1346 |
*/ |
| 1347 |
public function import( array $data ) { |
| 1348 |
$data = $this->get_import_data( $data ); |
| 1349 |
|
| 1350 |
$this->save( [ |
| 1351 |
'elements' => $data['content'], |
| 1352 |
'settings' => $data['settings'], |
| 1353 |
] ); |
| 1354 |
} |
| 1355 |
|
| 1356 |
private function process_element_import_export( Controls_Stack $element, $method ) { |
| 1357 |
$element_data = $element->get_data(); |
| 1358 |
|
| 1359 |
if ( method_exists( $element, $method ) ) { |
| 1360 |
// TODO: Use the internal element data without parameters. |
| 1361 |
$element_data = $element->{$method}( $element_data ); |
| 1362 |
} |
| 1363 |
|
| 1364 |
foreach ( $element->get_controls() as $control ) { |
| 1365 |
$control_class = Plugin::$instance->controls_manager->get_control( $control['type'] ); |
| 1366 |
|
| 1367 |
// If the control isn't exist, like a plugin that creates the control but deactivated. |
| 1368 |
if ( ! $control_class ) { |
| 1369 |
return $element_data; |
| 1370 |
} |
| 1371 |
|
| 1372 |
if ( method_exists( $control_class, $method ) ) { |
| 1373 |
$element_data['settings'][ $control['name'] ] = $control_class->{$method}( $element->get_settings( $control['name'] ), $control ); |
| 1374 |
} |
| 1375 |
|
| 1376 |
// On Export, check if the control has an argument 'export' => false. |
| 1377 |
if ( 'on_export' === $method && isset( $control['export'] ) && false === $control['export'] ) { |
| 1378 |
unset( $element_data['settings'][ $control['name'] ] ); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
return $element_data; |
| 1383 |
} |
| 1384 |
|
| 1385 |
protected function get_remote_library_config() { |
| 1386 |
$config = [ |
| 1387 |
'type' => 'block', |
| 1388 |
'default_route' => 'templates/blocks', |
| 1389 |
'category' => $this->get_name(), |
| 1390 |
'autoImportSettings' => false, |
| 1391 |
]; |
| 1392 |
|
| 1393 |
return $config; |
| 1394 |
} |
| 1395 |
|
| 1396 |
/** |
| 1397 |
* @since 2.0.4 |
| 1398 |
* @access protected |
| 1399 |
* |
| 1400 |
* @param $settings |
| 1401 |
*/ |
| 1402 |
protected function save_settings( $settings ) { |
| 1403 |
$page_settings_manager = SettingsManager::get_settings_managers( 'page' ); |
| 1404 |
$page_settings_manager->ajax_before_save_settings( $settings, $this->post->ID ); |
| 1405 |
$page_settings_manager->save_settings( $settings, $this->post->ID ); |
| 1406 |
} |
| 1407 |
|
| 1408 |
/** |
| 1409 |
* @since 2.1.3 |
| 1410 |
* @access protected |
| 1411 |
*/ |
| 1412 |
protected function print_elements( $elements_data ) { |
| 1413 |
foreach ( $elements_data as $element_data ) { |
| 1414 |
$element = Plugin::$instance->elements_manager->create_element_instance( $element_data ); |
| 1415 |
|
| 1416 |
if ( ! $element ) { |
| 1417 |
continue; |
| 1418 |
} |
| 1419 |
|
| 1420 |
$element->print_element(); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
protected function register_document_controls() { |
| 1425 |
$this->start_controls_section( |
| 1426 |
'document_settings', |
| 1427 |
[ |
| 1428 |
'label' => __( 'General Settings', 'elementor' ), |
| 1429 |
'tab' => Controls_Manager::TAB_SETTINGS, |
| 1430 |
] |
| 1431 |
); |
| 1432 |
|
| 1433 |
$this->add_control( |
| 1434 |
'post_title', |
| 1435 |
[ |
| 1436 |
'label' => __( 'Title', 'elementor' ), |
| 1437 |
'type' => Controls_Manager::TEXT, |
| 1438 |
'default' => $this->post->post_title, |
| 1439 |
'label_block' => true, |
| 1440 |
'separator' => 'none', |
| 1441 |
] |
| 1442 |
); |
| 1443 |
|
| 1444 |
$post_type_object = get_post_type_object( $this->post->post_type ); |
| 1445 |
|
| 1446 |
$can_publish = $post_type_object && current_user_can( $post_type_object->cap->publish_posts ); |
| 1447 |
$is_published = self::STATUS_PUBLISH === $this->post->post_status || self::STATUS_PRIVATE === $this->post->post_status; |
| 1448 |
|
| 1449 |
if ( $is_published || $can_publish || ! Plugin::$instance->editor->is_edit_mode() ) { |
| 1450 |
|
| 1451 |
$statuses = $this->get_post_statuses(); |
| 1452 |
if ( 'future' === $this->get_main_post()->post_status ) { |
| 1453 |
$statuses['future'] = __( 'Future', 'elementor' ); |
| 1454 |
} |
| 1455 |
|
| 1456 |
$this->add_control( |
| 1457 |
'post_status', |
| 1458 |
[ |
| 1459 |
'label' => __( 'Status', 'elementor' ), |
| 1460 |
'type' => Controls_Manager::SELECT, |
| 1461 |
'default' => $this->get_main_post()->post_status, |
| 1462 |
'options' => $statuses, |
| 1463 |
] |
| 1464 |
); |
| 1465 |
} |
| 1466 |
|
| 1467 |
$this->end_controls_section(); |
| 1468 |
} |
| 1469 |
|
| 1470 |
protected function get_post_statuses() { |
| 1471 |
return get_post_statuses(); |
| 1472 |
} |
| 1473 |
|
| 1474 |
protected function get_have_a_look_url() { |
| 1475 |
return $this->get_permalink(); |
| 1476 |
} |
| 1477 |
|
| 1478 |
public function handle_revisions_changed( $post_has_changed, $last_revision, $post ) { |
| 1479 |
// In case default, didn't determine the changes. |
| 1480 |
if ( ! $post_has_changed ) { |
| 1481 |
$last_revision_id = $last_revision->ID; |
| 1482 |
$last_revision_document = Plugin::instance()->documents->get( $last_revision_id ); |
| 1483 |
$post_document = Plugin::instance()->documents->get( $post->ID ); |
| 1484 |
|
| 1485 |
$last_revision_settings = $last_revision_document->get_settings(); |
| 1486 |
$post_settings = $post_document->get_settings(); |
| 1487 |
|
| 1488 |
// TODO: Its better to add crc32 signature for each revision and then only compare one part of the checksum. |
| 1489 |
$post_has_changed = $last_revision_settings !== $post_settings; |
| 1490 |
} |
| 1491 |
|
| 1492 |
return $post_has_changed; |
| 1493 |
} |
| 1494 |
|
| 1495 |
private function add_handle_revisions_changed_filter() { |
| 1496 |
add_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ], 10, 3 ); |
| 1497 |
} |
| 1498 |
|
| 1499 |
private function remove_handle_revisions_changed_filter() { |
| 1500 |
remove_filter( 'wp_save_post_revision_post_has_changed', [ $this, 'handle_revisions_changed' ] ); |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|