new URL. * @return string */ function diet_installer_replace_urls( $content, $old_url, $new_url, $url_map = array() ) { if ( ! is_string( $content ) ) { return $content; } $pairs = array(); if ( $old_url && $old_url !== $new_url ) { $pairs[ $old_url ] = $new_url; } foreach ( $url_map as $o => $n ) { if ( $o !== $n ) { $pairs[ $o ] = $n; } } foreach ( $pairs as $old => $new ) { $content = str_replace( $old, $new, $content ); // Protocol variant (http ↔ https). if ( strncmp( $old, 'https://', 8 ) === 0 ) { $old_alt = 'http://' . substr( $old, 8 ); $new_alt = ( strncmp( $new, 'https://', 8 ) === 0 ) ? 'http://' . substr( $new, 8 ) : $new; } elseif ( strncmp( $old, 'http://', 7 ) === 0 ) { $old_alt = 'https://' . substr( $old, 7 ); $new_alt = ( strncmp( $new, 'http://', 7 ) === 0 ) ? 'https://' . substr( $new, 7 ) : $new; } else { $old_alt = $old; $new_alt = $new; } if ( $old_alt !== $old ) { $content = str_replace( $old_alt, $new_alt, $content ); } // JSON escaped-slash variants (Divi may store URLs with \/ in JSON). $old_esc = str_replace( '/', '\/', $old ); $new_esc = str_replace( '/', '\/', $new ); $old_alt_esc = str_replace( '/', '\/', $old_alt ); $new_alt_esc = str_replace( '/', '\/', $new_alt ); if ( $old_esc !== $old ) { $content = str_replace( $old_esc, $new_esc, $content ); } if ( $old_alt_esc !== $old_alt ) { $content = str_replace( $old_alt_esc, $new_alt_esc, $content ); } } return $content; } } /** * WP-Cron handler: generate thumbnails for a single attachment. * Scheduled by DIET_Media_Importer after all media batches complete. * * @param int $attachment_id */ function diet_generate_thumbnail_handler( $attachment_id ) { require_once ABSPATH . 'wp-admin/includes/image.php'; $file = get_attached_file( $attachment_id ); if ( $file && file_exists( $file ) ) { wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); } } add_action( 'diet_generate_thumbnail', 'diet_generate_thumbnail_handler' ); // Prevent third-party activation redirects (e.g. WooCommerce setup wizard) from // hijacking our AJAX import responses. The filter runs before admin_init fires, // so it blocks wp_redirect() calls made by newly-activated plugins during that hook. // Scoped strictly to our own AJAX action — no effect on any other request. if ( wp_doing_ajax() && isset( $_POST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing && 'diet_import_step' === $_POST['action'] // phpcs:ignore WordPress.Security.NonceVerification.Missing ) { add_filter( 'wp_redirect', '__return_false', PHP_INT_MAX ); } // Boot the installer admin UI directly. // functions.php is loaded AFTER plugins_loaded has already fired, // so we cannot use that hook — instantiate immediately instead. if ( is_admin() ) { new DIET_Installer_Admin(); } /** * Show a dismissible admin notice when the theme is first activated * and import has not yet been run. */ function diet_theme_installer_notice() { if ( get_option( 'diet_installer_completed' ) === get_stylesheet() ) { return; } $url = admin_url( 'admin.php?page=diet-installer' ); echo '

'; printf( wp_kses( /* translators: %s: URL to the installer page */ __( 'Divi Construction Theme Installer — Content ready to import. Start Import →', 'diet-installer' ), array( 'strong' => array(), 'a' => array( 'href' => array() ), ) ), esc_url( $url ) ); echo '

'; } add_action( 'admin_notices', 'diet_theme_installer_notice' ); // The diet-installer/ folder is no longer auto-deleted after import. // Use the "Cleanup" button in the Theme Installer admin page to remove it manually // once you no longer need the Restore functionality. /** * Auto-migrate Theme Builder _et_template meta ordering. * * Identical to the plugin installer migration: ensures the default TB * template is first in _et_template meta so Divi's array_filter/array_pick * pipeline finds it at index 0 and renders header/footer correctly. */ function diet_installer_maybe_fix_tb_settings() { $fixed = get_option( 'diet_installer_tb_fixed', '' ); if ( version_compare( $fixed, DIET_INSTALLER_VERSION, '>=' ) ) { return; } $tb_post_id = 0; if ( function_exists( 'et_theme_builder_get_theme_builder_post_id' ) ) { $tb_post_id = (int) et_theme_builder_get_theme_builder_post_id( true, false ); } if ( ! $tb_post_id ) { global $wpdb; $tb_post_id = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'et_theme_builder' AND post_status = 'publish' ORDER BY ID DESC LIMIT 1" ); } if ( ! $tb_post_id ) { update_option( 'diet_installer_tb_fixed', DIET_INSTALLER_VERSION ); return; } $template_ids = get_post_meta( $tb_post_id, '_et_template', false ); if ( is_array( $template_ids ) && count( $template_ids ) > 1 ) { $defaults = array(); $non_defaults = array(); foreach ( $template_ids as $tid ) { $tid = (int) $tid; if ( get_post_meta( $tid, '_et_default', true ) === '1' ) { $defaults[] = $tid; } else { $non_defaults[] = $tid; } } $sorted = array_merge( $defaults, $non_defaults ); if ( $sorted !== array_map( 'intval', $template_ids ) ) { delete_post_meta( $tb_post_id, '_et_template' ); foreach ( $sorted as $tid ) { add_post_meta( $tb_post_id, '_et_template', $tid ); } } } update_option( 'diet_installer_tb_fixed', DIET_INSTALLER_VERSION ); } add_action( 'admin_init', 'diet_installer_maybe_fix_tb_settings' );