py_elementor_meta()` method instead.
*
* @since 1.1.0
* @access public
*
* @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 );
$core_meta = [
'_wp_page_template',
'_thumbnail_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 ) ) {
$value = $values[0];
// The elementor JSON needs slashes before saving.
if ( '_elementor_data' === $meta_key ) {
$value = wp_slash( $value );
} else {
$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.
*
* @since 1.0.10
* @deprecated 3.2.0 Use `Plugin::$instance->documents->get( $post_id )->is_built_with_elementor()` instead.
* @access public
*
* @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' )
->deprecation
->deprecated_function(
__METHOD__,
'3.2.0',
'Plugin::$instance->documents->get( $post_id )->is_built_with_elementor()'
);
$document = Plugin::$instance->documents->get( $post_id );
if ( ! $document ) {
return false;
}
return $document->is_built_with_elementor();
}
/**
* Switch to post.
*
* Change the global WordPress post to the requested post.
*
* @since 1.5.0
* @access public
*
* @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;
return;
}
$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'] );
}
/**
* Restore current post.
*
* Rollback to the previous global post, rolling back from `DB::switch_to_post()`.
*
* @since 1.5.0
* @access public
*/
public function restore_current_post() {
$data = array_pop( $this->switched_post_data );
// If not switched, return.
if ( ! $data ) {
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'] );
return;
}
$GLOBALS['post'] = get_post( $data['original_id'] ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $GLOBALS['post'] );
}
/**
* Switch to query.
*
* Change the WordPress query to a new query with the requested
* query variables.
*
* @since 2.0.0
* @access public
*
* @param array $query_vars New query variables.
* @param bool $force_global_post
*/
public function switch_to_query( $query_vars, $force_global_post = false ) {
global $wp_query;
$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;
return;
}
$new_query = new \WP_Query( $query_vars );
$switched_data = [
'switched' => $new_query,
'original' => $wp_query,
];
if ( ! empty( $GLOBALS['post'] ) ) {
$switched_data['post'] = $GLOBALS['post'];
}
$this->switched_data[] = $switched_data;
$wp_query = $new_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// Ensure the global post is set only if needed.
unset( $GLOBALS['post'] );
if ( isset( $new_query->posts[0] ) ) {
if ( $force_global_post || $new_query->is_singular() ) {
$GLOBALS['post'] = $new_query->posts[0]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $GLOBALS['post'] );
}
}
if ( $new_query->is_author() ) {
$GLOBALS['authordata'] = get_userdata( $new_query->get( 'author' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
/**
* Restore current query.
*
* Rollback to the previous query, rolling back from `DB::switch_to_query()`.
*
* @since 2.0.0
* @access public
*/
public function restore_current_query() {
$data = array_pop( $this->switched_data );
// If not switched, return.
if ( ! $data ) {
return;
}
global $wp_query;
$wp_query = $data['original']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// Ensure the global post/authordata is set only if needed.
unset( $GLOBALS['post'] );
unset( $GLOBALS['authordata'] );
if ( ! empty( $data['post'] ) ) {
$GLOBALS['post'] = $data['post']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $GLOBALS['post'] );
}
if ( $wp_query->is_author() ) {
$GLOBALS['authordata'] = get_userdata( $wp_query->get( 'author' ) ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
}
}
/**
* Get plain text.
*
* Retrieve the post plain text.
*
* @since 1.9.0
* @access public
*
* @param int $post_id Post ID.
*
* @return string Post plain text.
*/
public function get_plain_text( $post_id ) {
$document = Plugin::$instance->documents->get( $post_id );
$data = $document ? $document->get_elements_data() : [];
return $this->get_plain_text_from_data( $data );
}
/**
* Get plain text from data.
*
* Retrieve the post plain text from any given Elementor data.
*
* @since 1.9.2
* @access public
*
* @param array $data Post ID.
*
* @return string Post plain text.
*/
public function get_plain_text_from_data( $data ) {
ob_start();
if ( $data ) {
foreach ( $data as $element_data ) {
$this->render_element_plain_content( $element_data );
}
}
$plain_text = ob_get_clean();
// Remove unnecessary tags.
$plain_text = preg_replace( '/<\/?div[^>]*\>/i', '', $plain_text );
$plain_text = preg_replace( '/<\/?span[^>]*\>/i', '', $plain_text );
$plain_text = preg_replace( '##is', '', $plain_text );
$plain_text = preg_replace( '/]*><\\/i[^>]*>/', '', $plain_text );
$plain_text = preg_replace( '/ class=".*?"/', '', $plain_text );
// Remove empty lines.
$plain_text = preg_replace( '/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', "\n", $plain_text );
$plain_text = trim( $plain_text );
return $plain_text;
}
}