/home/desid573/verdenaturopathy.com.au/wp-content/themes/Divi/core/components
Edit: /home/desid573/verdenaturopathy.com.au/wp-content/themes/Divi/core/components/Portability.php (170237B)
instance = et_core_cache_get( $context, 'et_core_portability' );
self::$_ = ET_Core_Data_Utils::instance();
if ( $this->instance && $this->instance->view ) {
if ( et_core_is_fb_enabled() ) {
$this->assets();
} else {
add_action( 'admin_footer', array( $this, 'modal' ) );
add_action( 'customize_controls_print_footer_scripts', array( $this, 'modal' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ), 5 );
}
}
}
public static function doing_import() {
return self::$_doing_import;
}
/**
* Whether options-type portability for this context may read or write WordPress Additional CSS (`wp_custom_css`).
*
* Theme Options and Customizer exports include Additional CSS for Customizer parity (Divi core 6d89d3417e).
* Role Editor (`et_pb_roles`) must not bundle or apply global CSS (#49101). New `options` contexts default
* to excluding Additional CSS unless explicitly allowlisted here.
*
* @since 5.2.0
*
* @return bool
*/
protected function _options_context_includes_wp_custom_css() {
$contexts_with_additional_css = array(
'epanel',
'epanel_temp',
'et_divi_mods',
'et_extra_mods',
);
return true === in_array( $this->instance->context, $contexts_with_additional_css, true );
}
/**
* Import a previously exported layout.
*
* @since 3.10 Return the result of the import instead of dieing.
* @since 2.7.0
*
* @param string $file_context Accepts 'upload', 'sideload'. Default 'upload'.
*
* @return bool|array
*/
public function import( $file_context = 'upload' ) {
global $shortname;
$this->prevent_failure();
self::$_doing_import = true;
$timestamp = $this->get_timestamp();
$filesystem = $this->set_filesystem();
$temp_file_id = sanitize_file_name( $timestamp );
$temp_file = $this->has_temp_file( $temp_file_id, 'et_core_import' );
// phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification is handled earlier.
$include_global_presets = isset( $_POST['include_global_presets'] ) ? wp_validate_boolean( sanitize_text_field( $_POST['include_global_presets'] ) ) : false;
$return_json = isset( $_POST['et_cloud_return_json'] ) ? wp_validate_boolean( sanitize_text_field( $_POST['et_cloud_return_json'] ) ) : false;
$temp_presets = isset( $_POST['et_cloud_use_temp_presets'] ) ? wp_validate_boolean( sanitize_text_field( $_POST['et_cloud_use_temp_presets'] ) ) : false;
$is_update_preset_id = isset( $_POST['onboarding'] ) ? wp_validate_boolean( sanitize_text_field( $_POST['onboarding'] ) ) : false;
$preset_prefix = isset( $_POST['preset_prefix'] ) ? sanitize_text_field( $_POST['preset_prefix'] ) : '';
// phpcs:enable WordPress.Security.NonceVerification.Missing
$global_presets = '';
if ( $temp_file ) {
$import = json_decode( $filesystem->get_contents( $temp_file ), true );
} else {
if ( ! isset( $_FILES['file']['name'] ) || ! et_()->ends_with( sanitize_file_name( $_FILES['file']['name'] ), '.json' ) ) {
return array( 'message' => 'invalideFile' );
}
if ( ! in_array( $file_context, array( 'upload', 'sideload' ) ) ) {
$file_context = 'upload';
}
$handle_file = "wp_handle_{$file_context}";
$upload = $handle_file( $_FILES['file'], array(
'test_size' => false,
'test_type' => false,
'test_form' => false,
) );
/**
* Fires before an uploaded Portability JSON file is processed.
*
* @since 3.0.99
*
* @param string $file The absolute path to the uploaded JSON file's temporary location.
*/
do_action( 'et_core_portability_import_file', $upload['file'] );
$temp_file = $this->temp_file( $temp_file_id, 'et_core_import', $upload['file'] );
$file_content = preg_replace( '/\x{FEFF}/u', '', $filesystem->get_contents( $temp_file ) ); // Replace BOM with empty string.
$import = json_decode( $file_content, true );
$import = $this->validate( $import );
if ( $return_json ) {
return array( 'jsonFromFile' => $import );
}
// Check if Import contains Google Api Settings.
if ( isset( $import['data']['et_google_api_settings'] ) && ( 'epanel' === $this->instance->context || 'epanel_temp' === $this->instance->context ) ) {
$et_google_api_settings = $import['data']['et_google_api_settings'];
}
if ( ! isset( $import['context'] ) || ( isset( $import['context'] ) && $import['context'] !== $this->instance->context ) ) {
$this->delete_temp_files( 'et_core_import', [ $temp_file_id => $temp_file ] );
return array( 'message' => 'importContextFail' );
}
$import['data'] = $this->apply_query( $import['data'], 'set' );
$filesystem->put_contents( $upload['file'], wp_json_encode( (array) $import ) );
}
// Upload images and replace current urls.
if ( isset( $import['images'] ) ) {
$images = $this->maybe_paginate_images( (array) $import['images'], 'upload_images', $timestamp );
$import['data'] = $this->replace_images_urls( $images, $import['data'] );
}
if ( ! empty( $import['global_colors'] ) ) {
$import['data'] = $this->_maybe_inject_gcid( $import['data'], $import['global_colors'] );
}
$data = $import['data'];
$success = array( 'timestamp' => $timestamp );
$this->delete_temp_files( 'et_core_import', [ $temp_file_id => $temp_file ] );
if ( 'options' === $this->instance->type ) {
if ( ! $this->_options_context_includes_wp_custom_css() ) {
unset( $data['wp_custom_css'] );
}
// Reset all data besides excluded data.
$current_data = $this->apply_query( get_option( $this->instance->target, array() ), 'unset' );
if ( $this->_options_context_includes_wp_custom_css() && isset( $data['wp_custom_css'] ) && function_exists( 'wp_update_custom_css_post' ) ) {
wp_update_custom_css_post( $data['wp_custom_css'] );
if ( 'yes' === get_theme_mod( 'et_pb_css_synced', 'no' ) ) {
// If synced, clear the legacy custom css value to avoid unwanted merging of old and new css.
$data[ "{$shortname}_custom_css" ] = '';
}
}
// Import Google API settings.
if ( isset( $et_google_api_settings ) ) {
// Get exising Google API key, sine it is not added to export.
$et_previous_google_api_settings = get_option( 'et_google_api_settings' );
$et_previous_google_api_key = isset( $et_previous_google_api_settings['api_key'] ) ? $et_previous_google_api_settings['api_key'] : '';
$et_google_api_settings['api_key'] = $et_previous_google_api_key;
update_option( 'et_google_api_settings', $et_google_api_settings );
}
// Merge remaining current data with new data and update options.
update_option( $this->instance->target, array_merge( $current_data, $data ) );
if ( $this->_options_context_includes_wp_custom_css() ) {
set_theme_mod( 'et_pb_css_synced', 'no' );
}
}
// Pass the post content and let js save the post.
if ( 'post' === $this->instance->type ) {
$success['postContent'] = reset( $data );
$is_gutenberg_format = false;
// Check if we are dealing with D5 format.
// We break the loop on the first post that has content, no need to check all posts.
foreach ( $data as $post ) {
if ( ! empty( $post['post_content'] ) ) {
$is_gutenberg_format = BlockParser::has_any_divi_block( $post['post_content'] );
if ( $is_gutenberg_format ) {
break;
}
}
}
// In some cases we receive the post array instaed of shortcode string. Handle this case.
$shortcode_string = is_array( $success['postContent'] ) && ! empty( $success['postContent']['post_content'] ) ? $success['postContent']['post_content'] : $success['postContent'];
// Load shortcode framework to proceed with the import.
et_load_shortcode_framework();
if ( ! empty( $import['presets'] ) ) {
$preset_rewrite_map = [];
if ( $include_global_presets && ! $is_update_preset_id ) {
if ( $is_gutenberg_format ) {
$preset_data = $this->prepare_to_import_layout_presets( $import['presets'] );
$preset_rewrite_map = $preset_data['preset_rewrite_map'];
} else {
$preset_rewrite_map = $this->prepare_to_import_d4_layout_presets( $import['presets'] );
$global_presets = $import['presets'];
}
}
$shortcode_object = et_fb_process_shortcode( $shortcode_string );
if ( $is_update_preset_id ) {
$global_presets = $import['presets'];
$vb_presets_lookup = $this->_get_importing_presets_id_name_pairs( $global_presets, $preset_prefix );
$this->_update_shortcode_with_onboarding_preset( $shortcode_object, $vb_presets_lookup );
} else {
$this->rewrite_module_preset_ids( $shortcode_object, $import['presets'], $preset_rewrite_map );
}
$shortcode_string = et_fb_process_to_shortcode( $shortcode_object, array(), '', false );
}
do_shortcode( $shortcode_string );
$success['postContent'] = $shortcode_string;
// Handle nested global module references in imported content (D5 format only).
// Fixes invalid nested global module structures from early D5 versions (public beta 8 and below).
if ( $is_gutenberg_format ) {
// Register portability context if not already registered.
// PortabilityPost constructor expects a registered context to populate $this->instance
// with configuration data (context, name, type, target, etc.). Even though
// maybe_deglobalize_nested_global_modules() doesn't use $this->instance, the constructor
// will set it to null if the context isn't registered, which could cause issues if
// other code accesses this public property.
if ( ! et_core_cache_get( 'et_builder', 'et_core_portability' ) ) {
et_core_portability_register(
'et_builder',
[
'name' => 'Divi Builder',
'type' => 'post',
'target' => 'et_pb_layout',
]
);
}
$portability_instance = new \ET\Builder\Framework\Portability\PortabilityPost( 'et_builder' );
$success['postContent'] = $portability_instance->maybe_deglobalize_nested_global_modules( $success['postContent'] );
}
require_once ET_BUILDER_DIR . 'module/settings/Migration.php';
$success['migrations'] = ET_Builder_Module_Settings_Migration::$migrated;
$success['presets'] = isset( $import['presets'] ) && is_array( $import['presets'] ) ? $import['presets'] : (object) array();
}
if ( 'post_type' === $this->instance->type ) {
$preset_rewrite_map = array();
$is_gutenberg_format = false;
// Check if we are dealing with D5 format.
// We break the loop on the first post that has content, no need to check all posts.
foreach ( $data as $post ) {
if ( ! empty( $post['post_content'] ) ) {
$is_gutenberg_format = BlockParser::has_any_divi_block( $post['post_content'] );
if ( $is_gutenberg_format ) {
break;
}
}
}
if ( ! empty( $import['presets'] ) && $include_global_presets ) {
if ( $is_gutenberg_format ) {
$preset_data = $this->prepare_to_import_layout_presets( $import['presets'] );
$preset_rewrite_map = $preset_data['preset_rewrite_map'];
$default_module_presets = $preset_data['default_module_presets'];
$default_group_presets = $preset_data['default_group_presets'];
$global_presets = $import['presets'];
} else {
$preset_rewrite_map = $this->prepare_to_import_d4_layout_presets( $import['presets'] );
$global_presets = $import['presets'];
}
}
foreach ( $data as &$post ) {
// If we are dealing with D5 format, for selected default presets, we need to update the post content with default preset IDs.
if ( ! empty( $import['presets'] ) && $include_global_presets && $is_gutenberg_format ) {
// Update the post content with default preset IDs.
$post['post_content'] = $this->_update_preset_ids_in_content(
$post['post_content'],
$default_module_presets,
$default_group_presets
);
}
$shortcode_object = et_fb_process_shortcode( $post['post_content'] );
if ( ! empty( $import['presets'] ) ) {
if ( $include_global_presets ) {
$this->rewrite_module_preset_ids( $shortcode_object, $import['presets'], $preset_rewrite_map );
} else {
$this->_apply_global_presets( $shortcode_object, $import['presets'] );
}
}
$post_content = et_fb_process_to_shortcode( $shortcode_object, array(), '', false );
// Handle nested global module references in imported content (D5 format only).
// Fixes invalid nested global module structures from early D5 versions (public beta 8 and below).
if ( $is_gutenberg_format ) {
// Register portability context if not already registered.
// PortabilityPost constructor expects a registered context to populate $this->instance
// with configuration data (context, name, type, target, etc.). Even though
// maybe_deglobalize_nested_global_modules() doesn't use $this->instance, the constructor
// will set it to null if the context isn't registered, which could cause issues if
// other code accesses this public property.
if ( ! et_core_cache_get( 'et_builder', 'et_core_portability' ) ) {
et_core_portability_register(
'et_builder',
[
'name' => 'Divi Builder',
'type' => 'post',
'target' => 'et_pb_layout',
]
);
}
$portability_instance = new \ET\Builder\Framework\Portability\PortabilityPost( 'et_builder' );
$post['post_content'] = $portability_instance->maybe_deglobalize_nested_global_modules( $post['post_content'] );
}
// Add slashes for post content to avoid unwanted unslashing (by wp_unslash) while post is inserting.
// If we are dealing with D5 format, we chose non-processed $post['post_content'].
$post['post_content'] = str_starts_with( $post['post_content'], '
// We test the passed value against the "{$migrated_content}";
$result['postContent'] = $formatted_content;
if ( $replace && 0 < $post_id && current_user_can( 'edit_post', $post_id ) ) {
wp_update_post(
[
'ID' => $post_id,
'post_content' => wp_slash( $formatted_content ),
'meta_input' => [ '_et_pb_use_divi_5' => 'on' ],
]
);
}
}
if ( isset( $result['imported_posts'] ) && is_array( $result['imported_posts'] ) ) {
foreach ( $result['imported_posts'] as $post_id ) {
if ( ! is_numeric( $post_id ) ) {
continue;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
continue;
}
$post = get_post( $post_id );
if ( ! $post || empty( $post->post_content ) ) {
continue;
}
if ( 'revision' === $post->post_type ) {
continue;
}
$original_content = $post->post_content;
$migrated_content = D5BuilderConversion::maybeConvertContent( $original_content );
$formatted_content = "{$migrated_content}";
if ( $original_content !== $formatted_content ) {
wp_update_post( [
'ID' => $post_id,
'post_content' => wp_slash( $formatted_content ),
'meta_input' => [ '_et_pb_use_divi_5' => 'on' ],
] );
} else {
update_post_meta( $post_id, '_et_pb_use_divi_5', 'on' );
}
}
}
wp_send_json_success( $result );
}
wp_send_json_error();
}
add_action( 'wp_ajax_et_core_portability_import', 'et_core_portability_ajax_import' );
endif;
if ( ! function_exists( 'et_core_portability_ajax_export' ) ) :
/**
* Ajax portability Export.
*
* @since 2.7.0
*/
function et_core_portability_ajax_export() {
if ( ! isset( $_POST['context'] ) ) {
wp_send_json_error();
return;
}
$context = sanitize_text_field( $_POST['context'] );
// phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure, WordPress.CodeAnalysis.AssignmentInCondition.Found -- Existing codebase.
if ( ! $capability = et_core_portability_cap( $context ) ) {
wp_send_json_error();
return;
}
if ( ! et_core_security_check_passed( $capability, 'et_core_portability_export', 'nonce' ) ) {
wp_send_json_error();
return;
}
$return = isset( $_POST['return'] ) && sanitize_text_field( $_POST['return'] );
if ( $return ) {
$data = et_core_portability_load( $context )->export( $return );
wp_send_json_success( $data );
} else {
et_core_portability_load( $context )->export();
}
wp_send_json_error();
}
add_action( 'wp_ajax_et_core_portability_export', 'et_core_portability_ajax_export' );
endif;
if ( ! function_exists( 'et_core_portability_ajax_cancel' ) ) :
/**
* Cancel portability action.
*
* @since 2.7.0
*/
function et_core_portability_ajax_cancel() {
if ( ! isset( $_POST['context'] ) ) {
et_core_die();
}
$context = sanitize_text_field( $_POST['context'] );
if ( ! $capability = et_core_portability_cap( $context ) ) {
et_core_die();
}
if ( ! et_core_security_check_passed( $capability, 'et_core_portability_cancel' ) ) {
et_core_die();
}
et_core_portability_load( $context )->delete_temp_files( true );
wp_send_json_error();
}
add_action( 'wp_ajax_et_core_portability_cancel', 'et_core_portability_ajax_cancel' );
endif;
if ( ! function_exists( 'et_core_upload_and_get_urls_from_presets_images' ) ) :
/**
* Upload images and return the new URLs for presets.
*
* @since 4.26.1
* @param array $data Array of images to upload.
*
* @return array
*/
function et_core_upload_and_get_urls_from_presets_images( $data ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Require the file that includes wp_generate_attachment_metadata().
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$mime_types = get_allowed_mime_types();
$wp_upload_dir = wp_upload_dir();
$uploaded_urls = [];
foreach ( $data as $imageInfo ) {
$image_data = base64_decode( $imageInfo['encoded'] );
$temp_file = tempnam( $wp_upload_dir['path'], 'preset_' );
$filetype = wp_check_filetype( basename( $temp_file ), null );
if ( ! in_array( $filetype['type'], $mime_types, true ) ) {
continue;
}
file_put_contents( $temp_file, $image_data );
$attachment = [
'guid' => $wp_upload_dir['url'] . '/' . basename( $temp_file ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $temp_file ) ),
'post_content' => '',
'post_status' => 'inherit'
];
$attach_id = wp_insert_attachment( $attachment, $temp_file );
$attach_data = wp_generate_attachment_metadata( $attach_id, $temp_file );
wp_update_attachment_metadata( $attach_id, $attach_data );
$uploaded_urls[] = [
'old_url' => $imageInfo['url'],
'new_url' => wp_get_attachment_url( $attach_id ),
];
unlink( $temp_file );
}
return $uploaded_urls;
}
endif;
if ( ! function_exists( 'et_core_replace_image_urls_in_presets' ) ) :
/**
* Replace image URLs in presets JSON string.
*/
function et_core_replace_image_urls_in_presets( $presets, $uploaded_urls ) {
foreach ( $uploaded_urls as $url ) {
$presets = str_replace( $url['old_url'], $url['new_url'], $presets );
}
return $presets;
}
endif;
if ( ! function_exists( 'et_core_portability_import_default_presets' ) ) :
/**
* Set the default preset for a modules.
*
* @since 4.26.0
*/
function et_core_portability_import_default_presets() {
if ( ! et_core_security_check_passed( 'manage_options', 'et_core_portability_import_default_presets', 'nonce' ) ) {
et_core_die();
}
$presets = isset( $_POST['presets'] ) ? sanitize_text_field( $_POST['presets'] ) : '';
$preset_prefix = isset( $_POST['presetPrefix'] ) ? sanitize_text_field( $_POST['presetPrefix'] ) : '';
$global_colors = isset( $_POST['globalColors'] ) ? sanitize_text_field( $_POST['globalColors'] ) : '';
$images = isset( $_POST['images'] ) ? sanitize_text_field( $_POST['images'] ) : '';
$uploaded_urls = [];
$portability = et_core_portability_load( 'et_builder' );
if ( $global_colors ) {
$global_colors = json_decode( stripslashes( $global_colors ), true );
$portability->import_global_colors( $global_colors );
}
if ( $images ) {
$images = json_decode( stripslashes( $images ), true );
$uploaded_urls = et_core_upload_and_get_urls_from_presets_images( $images );
}
if ( $presets ) {
if ( ! empty( $uploaded_urls ) ) {
$presets = et_core_replace_image_urls_in_presets( $presets, $uploaded_urls );
}
$presets = json_decode( stripslashes( $presets ), true );
$portability->import_global_presets( $presets, false, true, $preset_prefix );
}
ET_Core_PageResource::remove_static_resources( 'all', 'all' );
wp_send_json_success();
}
add_action( 'wp_ajax_et_core_portability_import_default_presets', 'et_core_portability_import_default_presets' );
endif;
if ( ! function_exists( 'et_core_portability_export' ) ) :
/**
* Portability export.
*
* @since 2.7.0
*/
function et_core_portability_export() {
if ( ! isset( $_GET['et_core_portability'], $_GET['timestamp'] ) ) {
return;
}
if ( ! et_core_security_check_passed( 'edit_posts' ) ) {
wp_die( esc_html__( 'The export process failed. Please refresh the page and try again.', ET_CORE_TEXTDOMAIN ) );
}
et_core_portability_load( sanitize_text_field( $_GET['timestamp'] ) )->download_export();
}
add_action( 'admin_init', 'et_core_portability_export', 20 );
endif;
if ( ! function_exists( 'et_core_portability_cap' ) ):
/**
* Returns the required WordPress Capability for a Portability context.
*
* @since 3.0.91
*
* @param string $context The Portability context
*
* @return string
*/
function et_core_portability_cap( $context ) {
$capability = '';
$options_contexts = array(
'et_pb_roles',
'epanel',
'epanel_temp',
'et_divi_mods',
'et_extra_mods',
);
$post_contexts = array(
'et_builder',
'et_theme_builder',
'et_code_snippets',
'et_builder_layouts',
);
if ( in_array( $context, $options_contexts, true ) ) {
$capability = 'edit_theme_options';
} else if ( in_array( $context, $post_contexts, true ) ) {
$capability = 'edit_posts';
}
return $capability;
}
endif;