use Elementor\Core\Base\Document;
use Elementor\Core\DynamicTags\Manager;
use Elementor\TemplateLibrary\Source_Local;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
* Elementor database handler class is responsible for communicating with the
* DB, save and retrieve Elementor data and meta data.
* Current DB version of the editor.
const DB_VERSION = '0.4';
* @deprecated 3.1.0 Use `Document::STATUS_PUBLISH` const instead.
const STATUS_PUBLISH = Document::STATUS_PUBLISH;
* @deprecated 3.1.0 Use `Document::STATUS_DRAFT` const instead.
const STATUS_DRAFT = Document::STATUS_DRAFT;
* @deprecated 3.1.0 Use `Document::STATUS_PRIVATE` const instead.
const STATUS_PRIVATE = Document::STATUS_PRIVATE;
* @deprecated 3.1.0 Use `Document::STATUS_AUTOSAVE` const instead.
const STATUS_AUTOSAVE = Document::STATUS_AUTOSAVE;
* @deprecated 3.1.0 Use `Document::STATUS_PENDING` const instead.
const STATUS_PENDING = Document::STATUS_PENDING;
* Holds the switched post data.
* @var array Switched post data. Default is an empty array.
protected $switched_post_data = [];
* Holds the switched data.
* @var array Switched data. Default is an empty array.
protected $switched_data = [];
* Retrieve editor data from the database.
* @deprecated 3.1.0 Use `Plugin::$instance->documents->get( $post_id )->get_elements_raw_data( null, true )` OR `Plugin::$instance->documents->get_doc_or_auto_save( $post_id )->get_elements_raw_data( null, true )` instead.
* @param int $post_id Post ID.
* @param string $status Optional. Post status. Default is `publish`.
* @return array Editor data.
public function get_builder( $post_id, $status = Document::STATUS_PUBLISH ) {
Plugin::$instance->modules_manager
->get_modules( 'dev-tools' )
'`Plugin::$instance->documents->get( $post_id )->get_elements_raw_data( null, true )` OR `Plugin::$instance->documents->get_doc_or_auto_save( $post_id )->get_elements_raw_data( null, true )`'
if ( Document::STATUS_DRAFT === $status ) {
$document = Plugin::$instance->documents->get_doc_or_auto_save( $post_id );
$document = Plugin::$instance->documents->get( $post_id );
$editor_data = $document->get_elements_raw_data( null, true );
* Retrieve post meta data, and return the JSON decoded data.
* @param int $post_id Post ID.
* @param string $key The meta key to retrieve.
* @return array Decoded JSON data from post meta.
protected function _get_json_meta( $post_id, $key ) {
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0' );
$meta = get_post_meta( $post_id, $key, true );
if ( is_string( $meta ) && ! empty( $meta ) ) {
$meta = json_decode( $meta, true );
* Set whether the page is using Elementor or not.
* @deprecated 3.1.0 Use `Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )` instead.
* @param int $post_id Post ID.
* @param bool $is_elementor Optional. Whether the page is elementor page.
public function set_is_elementor_page( $post_id, $is_elementor = true ) {
Plugin::$instance->modules_manager
->get_modules( 'dev-tools' )
'Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )'
$document = Plugin::$instance->documents->get( $post_id );
$document->set_is_built_with_elementor( $is_elementor );
* Render element plain content.
* When saving data in the editor, this method renders recursively the plain
* content containing only the content and the HTML. No CSS data.
* @param array $element_data Element data.
private function render_element_plain_content( $element_data ) {
if ( 'widget' === $element_data['elType'] ) {
/** @var Widget_Base $widget */
$widget = Plugin::$instance->elements_manager->create_element_instance( $element_data );
$widget->render_plain_content();
if ( ! empty( $element_data['elements'] ) ) {
foreach ( $element_data['elements'] as $element ) {
$this->render_element_plain_content( $element );
* Retrieves the raw content, removes all kind of unwanted HTML tags and saves
* the content as the `post_content` field in the database.
* @param int $post_id Post ID.
public function save_plain_text( $post_id ) {
// Switch $dynamic_tags to parsing mode = remove.
$dynamic_tags = Plugin::$instance->dynamic_tags;
$parsing_mode = $dynamic_tags->get_parsing_mode();
$dynamic_tags->set_parsing_mode( Manager::MODE_REMOVE );
$plain_text = $this->get_plain_text( $post_id );
'post_content' => $plain_text,
$dynamic_tags->set_parsing_mode( $parsing_mode );
* Accept any type of Elementor data and a callback function. The callback
* function runs recursively for each element and his child elements.
* @param array $data_container Any type of elementor data.
* @param callable $callback A function to iterate data by.
* @param array $args Array of args pointers for passing parameters in & out of the callback.
* @return mixed Iterated data.
public function iterate_data( $data_container, $callback, $args = [] ) {
if ( isset( $data_container['elType'] ) ) {
if ( ! empty( $data_container['elements'] ) ) {
$data_container['elements'] = $this->iterate_data( $data_container['elements'], $callback, $args );
return call_user_func( $callback, $data_container, $args );
foreach ( $data_container as $element_key => $element_value ) {
$element_data = $this->iterate_data( $data_container[ $element_key ], $callback, $args );
if ( null === $element_data ) {
$data_container[ $element_key ] = $element_data;
public static function iterate_elementor_documents( $callback, $batch_size = 100 ) {
'post_type' => [ Source_Local::CPT, 'post', 'page' ],
'post_status' => [ 'publish' ],
'posts_per_page' => $batch_size,
'meta_key' => Document::BUILT_WITH_ELEMENTOR_META_KEY,
'meta_value' => 'builder',
'offset' => $processed_posts,
$query = new \WP_Query( $args );
if ( empty( $query->posts ) ) {
foreach ( $query->posts as $post_id ) {
$document = Plugin::$instance->documents->get( $post_id );
$elements_data = $document->get_json_meta( Document::ELEMENTOR_DATA_META_KEY );
$callback( $document, $elements_data );
* Safely copy Elementor meta.
* Make sure the original page was built with Elementor and the post is not
* auto-save. Only then copy elementor meta from one post to another using
* `copy_elementor_meta()`.
* @param int $from_post_id Original post ID.
* @param int $to_post_id Target post ID.
public function safe_copy_elementor_meta( $from_post_id, $to_post_id ) {
// It's from WP-Admin & not from Elementor.
if ( ! did_action( 'elementor/db/before_save' ) ) {
$from_document = Plugin::$instance->documents->get( $from_post_id );
if ( ! $from_document || ! $from_document->is_built_with_elementor() ) {
// It's an exited Elementor auto-save.
if ( get_post_meta( $to_post_id, '_elementor_data', true ) ) {
$this->copy_elementor_meta( $from_post_id, $to_post_id );
* Duplicate the data from one post to another.
* Consider using `safe_copy_elementor_meta()` method instead.
* @param int $from_post_id Original post ID.
* @param int $to_post_id Target post ID.
public function copy_elementor_meta( $from_post_id, $to_post_id ) {
$from_post_meta = get_post_meta( $from_post_id );
foreach ( $from_post_meta as $meta_key => $values ) {
// Copy only meta with the `_elementor` prefix.
if ( 0 === strpos( $meta_key, '_elementor' ) || in_array( $meta_key, $core_meta, true ) ) {
// The elementor JSON needs slashes before saving.
if ( '_elementor_data' === $meta_key ) {
$value = wp_slash( $value );
$value = maybe_unserialize( $value );
// Don't use `update_post_meta` that can't handle `revision` post type.
update_metadata( 'post', $to_post_id, $meta_key, $value );
* Is built with Elementor.
* Check whether the post was built with Elementor.
* @deprecated 3.2.0 Use `Plugin::$instance->documents->get( $post_id )->is_built_with_elementor()` instead.
* @param int $post_id Post ID.
* @return bool Whether the post was built with Elementor.
public function is_built_with_elementor( $post_id ) {
Plugin::$instance->modules_manager
->get_modules( 'dev-tools' )
'Plugin::$instance->documents->get( $post_id )->is_built_with_elementor()'
$document = Plugin::$instance->documents->get( $post_id );
return $document->is_built_with_elementor();
* Change the global WordPress post to the requested post.
* @param int $post_id Post ID to switch to.
public function switch_to_post( $post_id ) {
$post_id = absint( $post_id );
// If is already switched, or is the same post, return.
if ( get_the_ID() === $post_id ) {
$this->switched_post_data[] = false;
$this->switched_post_data[] = [
'switched_id' => $post_id,
'original_id' => get_the_ID(), // Note, it can be false if the global isn't set.
$GLOBALS['post'] = get_post( $post_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $GLOBALS['post'] );
* Rollback to the previous global post, rolling back from `DB::switch_to_post()`.
public function restore_current_post() {
$data = array_pop( $this->switched_post_data );
// If not switched, return.
// It was switched from an empty global post, restore this state and unset the global post.
if ( false === $data['original_id'] ) {
unset( $GLOBALS['post'] );
$GLOBALS['post'] = get_post( $data['original_id'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $GLOBALS['post'] );
* Change the WordPress query to a new query with the requested
* @param array $query_vars New query variables.
* @param bool $force_global_post
public function switch_to_query( $query_vars, $force_global_post = false ) {
$current_query_vars = $wp_query->query;
// If is already switched, or is the same query, return.
if ( $current_query_vars === $query_vars ) {
$this->switched_data[] = false;
$new_query = new \WP_Query( $query_vars );
'switched' => $new_query,
if ( ! empty( $GLOBALS['post'] ) ) {
$switched_data['post'] = $GLOBALS['post'];