2_P100-1998,swaplfnl ibm-12712-s390 ibm-16804_X110-1999,swaplfnl ibm-16804-s390 ebcdic-xml-us gsm-03.38-2000 GSM0338 ibm-1004_P100-1995 ibm-1004 ibm-1008_P100-1995 ibm-1008 ibm-1009_P100-1995 ibm-1009 ibm-1010_P100-1995 ibm-1010 NF_Z_62-010 iso-ir-69 ISO646-FR fr csISO69French ibm-1011_P100-1995 ibm-1011 DIN_66003 iso-ir-21 de ISO646-DE csISO21German ibm-1012_P100-1995 ibm-1012 IT iso-ir-15 ISO646-IT csISO15Italian ibm-1013_P100-1995 ibm-1013 BS_4730 iso-ir-4 ISO646-GB gb uk csISO4UnitedKingdom ibm-1014_P100-1995 ibm-1014 ES2 iso-ir-85 ISO646-ES2 csISO85Spanish2 ibm-1015_P100-1995 ibm-1015 PT2 iso-ir-84 ISO646-PT2 csISO84Portuguese2 ibm-1016_P100-1995 ibm-1016 NS_4551-1 iso-ir-60 ISO646-NO no csISO60DanishNorwegian csISO60Norwegian1 ibm-1017_P100-1995 ibm-1017 ibm-1018_P100-1995 ibm-1018 SEN_850200_B iso-ir-10 FI ISO646-FI ISO646-SE se csISO10Swedish ibm-1019_P100-1995 ibm-1019 ibm-1020_P100-2003 ibm-1020 CSA_Z243.4-1985-1 iso-ir-121 ISO646-CA csa7-1 ca csISO121Canadian1 ibm-1021_P100-2003 ibm-1021 ibm-1023_P100-2003 ibm-1023 ES iso-ir-17 ISO646-ES csISO17Spanish ibm-1027_P100-1995 ibm-1027 x-IBM1027 ibm-1041_P100-1995 ibm-1041 x-IBM1041 ibm-1043_P100-1995 ibm-1043 x-IBM1043 ibm-1046_X110-1999 ibm-1046 x-IBM1046 x-IBM1046S ibm-1088_P100-1995 ibm-1088 x-IBM1088 ibm-1100_P100-2003 ibm-1100 DEC-MCS dec csDECMCS ibm-1101_P100-2003 ibm-1101 ibm-1102_P100-2003 ibm-1102 ibm-1103_P100-2003 ibm-1103 ibm-1104_P100-2003 ibm-1104 NF_Z_62-010_1973 iso-ir-25 ISO646-FR1 csISO25French ibm-1105_P100-2003 ibm-1105 ibm-1106_P100-2003 ibm-1106 ibm-1107_P100-2003 ibm-1107 DS_2089 ISO646-DK dk csISO646Danish ibm-1127_P100-2004 ibm-1127 ibm-1161_P100-1999 ibm-1161 ibm-1163_P100-1999 ibm-1163 ibm-1165_P101-2000 ibm-1165 ibm-1166_P100-2002 ibm-1166 ibm-1167_P100-2002 ibm-1167 KOI8-RU x-KOI8_RU ibm-1174_X100-2007 ibm-1174 KZ-1048 STRK1048-2002 RK1048 csKZ1048 ibm-1277_P100-1995 ibm-1277 ibm-13125_P100-1997 ibm-13125 ibm-13140_P101-2000 ibm-13140 ibm-13218_P100-1996 ibm-13218 ibm-1350_P110-1997 ibm-1350 x-eucJP-Open eucJP-Open ibm-1351_P110-1997 ibm-1351 x-IBM1351 ibm-1362_P110-1999 ibm-1362 x-IBM1362 ibm-13676_P102-2001 ibm-13676 ibm-1380_P100-1995 ibm-1380 x-IBM1380 ibm-1381_P110-1999 ibm-1381 cp1381 1381 x-IBM1381 ibm-1382_P100-1995 ibm-1382 x-IBM1382 ibm-17221_P100-2001 ibm-17221 ibm-17248_X110-1999 ibm-17248 ibm-21344_P101-2000 ibm-21344 ibm-21427_P100-1999 ibm-21427 ibm-256_P100-1995 ibm-256 ibm-259_P100-1995 ibm-259 IBM-Symbols csIBMSymbols ibm-274_P100-2000 ibm-274 IBM274 EBCDIC-BE CP274 csIBM274 ibm-275_P100-1995 ibm-275 IBM275 EBCDIC-BR cp275 csIBM275 ibm-286_P100-2003 ibm-286 EBCDIC-AT-DE-A csEBCDICATDEA ibm-293_P100-1995 ibm-293 ibm-300_P120-2006 ibm-300 x-IBM300 ibm-301_P110-1997 ibm-301 x-IBM301 ibm-33058_P100-2000 ibm-33058 ibm-425_P101-2000 ibm-425 ibm-4930_P110-1999 ibm-4930 ibm-4933_P100-2002 ibm-4933 ibm-4948_P100-1995 ibm-4948 ibm-4951_P100-1995 ibm-4951 ibm-4952_P100-1995 ibm-4952 ibm-4960_P100-1995 ibm-4960 ibm-5039_P11A-1998 ibm-5039 ibm-5048_P100-1995 ibm-5048 ibm-5049_P100-1995 ibm-5049 ibm-5067_P100-1995 ibm-5067 ibm-5104_X110-1999 ibm-5104 ibm-5233_P100-2011 ibm-5233 ibm-806_P100-1998 ibm-806 ibm-808_P100-1999 ibm-808 x-IBM808 ibm-833_P100-1995 ibm-833 x-IBM833 ibm-834_P100-1995 ibm-834 x-IBM834 ibm-835_P100-1995 ibm-835 x-IBM835 ibm-836_P100-1995 ibm-836 x-IBM836 ibm-837_P100-2011 ibm-837 x-IBM837 ibm-848_P100-1999 ibm-848 ibm-849_P100-1999 ibm-849 ibm-859_P100-1999 ibm-859 x-IBM859 ibm-8612_P100-1995 ibm-8612 ibm-872_P100-1999 ibm-872 ibm-880_P100-1995 ibm-880 IBM880 cp880 EBCDIC-Cyrillic csIBM880 windows-20880 ibm-896_P100-1995 ibm-896 ibm-897_P100-1995 ibm-897 JIS_X0201 X0201 csHalfWidthKatakana x-IBM897 ibm-9027_P100-1999 ibm-9027 ibm-9048_P100-1998 ibm-9048 ibm-905_P100-1995 ibm-905 IBM905 CP905 ebcdic-cp-tr csIBM905 windows-20905 ibm-9056_P100-1995 ibm-9056 ibm-9061_P100-1999 ibm-9061 ibm-9145_P110-1997 ibm-9145 ibm-9238_X110-1999 ibm-elementor = true ) {
Plugin::$instance->modules_manager
->get_modules( 'dev-tools' )
->deprecation
->deprecated_function(
__METHOD__,
'3.1.0',
'Plugin::$instance->documents->get( $post_id )->set_is_build_with_elementor( $is_elementor )'
);
$document = Plugin::$instance->documents->get( $post_id );
if ( ! $document ) {
return;
}
$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.
*
* @since 2.0.0
* @access private
*
* @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 );
if ( $widget ) {
$widget->render_plain_content();
}
}
if ( ! empty( $element_data['elements'] ) ) {
foreach ( $element_data['elements'] as $element ) {
$this->render_element_plain_content( $element );
}
}
}
/**
* Save plain text.
*
* Retrieves the raw content, removes all kind of unwanted HTML tags and saves
* the content as the `post_content` field in the database.
*
* @since 1.9.0
* @access public
*
* @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 );
wp_update_post(
[
'ID' => $post_id,
'post_content' => $plain_text,
]
);
// Restore parsing mode.
$dynamic_tags->set_parsing_mode( $parsing_mode );
}
/**
* Iterate data.
*
* Accept any type of Elementor data and a callback function. The callback
* function runs recursively for each element and his child elements.
*
* @since 1.0.0
* @access public
*
* @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 ) {
continue;
}
$data_container[ $element_key ] = $element_data;
}
return $data_container;
}
/**
* 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()`.
*
* @since 1.9.2
* @access public
*
* @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() ) {
return;
}
// It's an exited Elementor auto-save
if ( get_post_meta( $to_post_id, '_elementor_data', true ) ) {
return;
}
}
$this->copy_elementor_meta( $from_post_id, $to_post_id );
}
/**
* Copy Elementor meta.
*
* Duplicate the data from one post to another.
*
* Consider using `safe_copy_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;
}
}