* @param boolean $is_cache Whether to use WordPress Object Cache or not.
function et_builder_get_oembed( $url, $group = 'video', $is_cache = true ) {
$item_src = esc_url( $url );
// Temporarily save embedded item on page load only. Use the item source as the
// key, so some modules with the same item can share it.
$item_embed = $is_cache ? wp_cache_get( $item_src, $group ) : false;
$item_embed = wp_oembed_get( $item_src );
wp_cache_set( $item_src, $item_embed, $group );
return apply_filters( 'et_builder_get_oembed', $item_embed, $url, $group, $is_cache );
if ( ! function_exists( 'et_pb_set_video_oembed_thumbnail_resolution' ) ) :
* Replace YouTube video thumbnails to high resolution if the high resolution image exists.
* @param string $image_src thumbnail image src.
* @param string $resolution thumbnail image resolutions.
function et_pb_set_video_oembed_thumbnail_resolution( $image_src, $resolution = 'default' ) {
// Replace YouTube video thumbnails to high resolution if the high resolution image exists.
if ( 'high' === $resolution && false !== strpos( $image_src, 'hqdefault.jpg' ) ) {
$high_res_image_src = str_replace( 'hqdefault.jpg', 'maxresdefault.jpg', $image_src );
$protocol = is_ssl() ? 'https://' : 'http://';
$processed_image_url = esc_url( str_replace( '//', $protocol, $high_res_image_src ), array( 'http', 'https' ) );
$response = wp_remote_get( $processed_image_url, array( 'timeout' => 30 ) );
// Youtube doesn't guarantee that high res image exists for any video, so we need to check whether it exists and fallback to default image in case of error.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return $high_res_image_src;
* Return all registered sidebars.
function et_builder_get_widget_areas_list() {
global $wp_registered_sidebars;
foreach ( $wp_registered_sidebars as $sidebar_key => $sidebar ) {
$widget_areas[ $sidebar_key ] = array(
'name' => $sidebar['name'],
return apply_filters( 'et_builder_get_widget_areas_list', $widget_areas );
if ( ! function_exists( 'et_builder_get_widget_areas' ) ) :
* Return widget areas dropdown html.
function et_builder_get_widget_areas() {
$wp_registered_sidebars = et_builder_get_widget_areas_list();
$et_pb_widgets = get_theme_mod( 'et_pb_widgets' );
$output = '<select name="et_pb_area" id="et_pb_area">';
foreach ( $wp_registered_sidebars as $id => $options ) {
'<%%= typeof( et_pb_area ) !== "undefined" && "%1$s" === et_pb_area ? " selected=\'selected\'" : "" %%>',
'<option value="%1$s"%2$s>%3$s</option>',
esc_html( $options['name'] )
if ( ! function_exists( 'et_pb_export_layouts_interface' ) ) :
* Display 'Manage Categories' button at top in Layout wp admin edit screen.
function et_pb_export_layouts_interface() {
if ( ! current_user_can( 'export' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to export the content of this site.', 'et_builder' ) );
<a href="<?php echo et_core_esc_wp( admin_url( 'edit-tags.php?taxonomy=layout_category' ) ); ?>" id="et_load_category_page"><?php esc_html_e( 'Manage Categories', 'et_builder' ); ?></a>
echo et_core_esc_previously( et_builder_portability_link( 'et_builder_layouts', array( 'class' => 'et-pb-portability-button' ) ) );
add_action( 'export_wp', 'et_pb_edit_export_query' );
* Add filter for the export query.
function et_pb_edit_export_query() {
add_filter( 'query', 'et_pb_edit_export_query_filter' );
* @param WP_Query $query object.
function et_pb_edit_export_query_filter( $query ) {
// Apply filter only once.
remove_filter( 'query', 'et_pb_edit_export_query_filter' );
et_core_nonce_verified_previously();
// ensure user can export.
if ( ! current_user_can( 'export' ) ) {
$content = ! empty( $_GET['content'] ) ? sanitize_text_field( $_GET['content'] ) : '';
if ( ET_BUILDER_LAYOUT_POST_TYPE !== $content ) {
foreach ( $possible_types as $template_type ) {
$selected_type = 'et_pb_template_' . $template_type;
if ( isset( $_GET[ $selected_type ] ) ) {
$sql = " AND ( `{$wpdb->term_relationships}`.term_taxonomy_id = %d";
$sql .= " OR `{$wpdb->term_relationships}`.term_taxonomy_id = %d";
$sql_args[] = (int) $_GET[ $selected_type ];
INNER JOIN %3$s ON ( %4$s.ID = %3$s.object_id )
WHERE %4$s.post_type = "%1$s"
AND %4$s.post_status != "auto-draft"
ET_BUILDER_LAYOUT_POST_TYPE,
$wpdb->term_relationships,
$query = $wpdb->prepare( $sql, $sql_args ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Value of the $sql is safely prepared above.
* Initialize builder metabox in BFB.
function et_pb_setup_theme() {
add_action( 'add_meta_boxes', 'et_pb_add_custom_box', 10, 2 );
add_action( 'add_meta_boxes', 'et_builder_prioritize_meta_box', 999999 );
add_filter( 'hidden_meta_boxes', 'et_pb_hidden_meta_boxes' );
add_action( 'init', 'et_pb_setup_theme', 11 );
* Override metaboxes order to ensure Divi Builder metabox has top position.
* @param string $value Custom value.
function et_builder_override_meta_boxes_order( $value ) {
// Store the value on the first call;.
$custom = false === $custom ? $value : $custom;
* Forcefully prioritize the Divi Builder metabox to be at the top.
* User drag&drop metabox order customizations are still supported.
* Required since not all plugins properly register their metaboxes in the add_meta_boxes hook.
function et_builder_prioritize_meta_box() {
$screen = get_current_screen();
// Only prioritize Divi Builder metabox if current post type has Divi Builder enabled.
if ( ! in_array( $screen->post_type, et_builder_get_enabled_builder_post_types(), true ) ) {
$option_name = "meta-box-order_$page";
$custom = get_user_option( $option_name );
foreach ( $wp_meta_boxes as $page => $contexts ) {
foreach ( $contexts as $context => $priorities ) {
foreach ( $priorities as $priority => $boxes ) {
if ( ! isset( $boxes[ ET_BUILDER_LAYOUT_POST_TYPE ] ) ) {
$divi = $boxes[ ET_BUILDER_LAYOUT_POST_TYPE ];
unset( $boxes[ ET_BUILDER_LAYOUT_POST_TYPE ] );
// phpcs:ignore WordPress.WP.GlobalVariablesOverride -- Push "The Divi Builder" metabox at top by updating `$wp_meta_boxes`.
$wp_meta_boxes[ $page ][ $context ][ $priority ] = array_merge( array( ET_BUILDER_LAYOUT_POST_TYPE => $divi ), $boxes );
// If our mbox is the first one in custom ordering.
if ( is_array( $custom ) && 0 === strpos( $custom[ $context ], ET_BUILDER_LAYOUT_POST_TYPE ) ) {
// Find all metaboxes that are not included in custom order.
$sorted = explode( ',', $custom[ $context ] );
$add = array_diff( array_keys( $boxes ), $sorted );
$custom[ $context ] = implode( ',', array_merge( array( ET_BUILDER_LAYOUT_POST_TYPE ), $add, array_slice( $sorted, 1 ) ) );
// Store the custom value.
et_builder_override_meta_boxes_order( $custom );
// and override `get_user_option` so WP will use it.
add_filter( "get_user_option_{$option_name}", 'et_builder_override_meta_boxes_order' );
* The page builders require the WP Heartbeat script in order to function. We ensure the heartbeat
* is loaded with the page builders by scheduling this callback to run right before scripts
* are output to the footer. {@see 'admin_enqueue_scripts', 'wp_footer'}
function et_builder_maybe_ensure_heartbeat_script() {
// Don't perform any actions on 'wp_footer' if VB is not active.
if ( 'wp_footer' === current_filter() && empty( $_GET['et_fb'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- This function does not change any state, and is therefore not susceptible to CSRF.
// We have to check both 'registered' AND 'enqueued' to cover cases where heartbeat has been
// de-registered because 'enqueued' will return `true` for a de-registered script at this stage.
$heartbeat_okay = wp_script_is( 'heartbeat', 'registered' ) && wp_script_is( 'heartbeat', 'enqueued' );
$autosave_okay = wp_script_is( 'autosave', 'registered' ) && wp_script_is( 'autosave', 'enqueued' );
if ( '1' === et_()->array_get( $_GET, 'et_bfb', '0' ) ) { // phpcs:ignore WordPress.Security.NonceVerification -- This function does not change any state, and is therefore not susceptible to CSRF.
// Do not enqueue WP autosave in the BFB iframe because it doesn't include #content / #excerpt / #title
// and will result in empty (browser) backups (note: still included in top window).
wp_dequeue_script( 'autosave' );
if ( $heartbeat_okay && $autosave_okay ) {
$suffix = SCRIPT_DEBUG ? '' : '.min';
// phpcs:disable WordPress.WP.EnqueuedResourceParameters -- Version numbers are not set to load latest scripts from the WP Core.
if ( ! $heartbeat_okay ) {
$heartbeat_src = "/wp-includes/js/heartbeat{$suffix}.js";
// wp-hooks was introduced in WP 5.0.
$deps = wp_script_is( 'wp-hooks', 'registered' ) ? array( 'jquery', 'wp-hooks' ) : array( 'jquery' );
wp_enqueue_script( 'heartbeat', $heartbeat_src, $deps, false, true );
wp_localize_script( 'heartbeat', 'heartbeatSettings', apply_filters( 'heartbeat_settings', array() ) );
if ( ! $autosave_okay ) {
$autosave_src = "/wp-includes/js/autosave{$suffix}.js";
wp_enqueue_script( 'autosave', $autosave_src, array( 'heartbeat' ), false, true );
* Enqueue dashicons in front-end if they are not enqueued (that happens when not logged in as admin).
function et_builder_maybe_enqueue_dashicons() {
if ( wp_style_is( 'dashicons' ) ) {
wp_enqueue_style( 'dashicons' );
add_action( 'admin_print_scripts-post-new.php', 'et_builder_maybe_ensure_heartbeat_script', 9 );
add_action( 'admin_print_scripts-post.php', 'et_builder_maybe_ensure_heartbeat_script', 9 );
add_action( 'wp_enqueue_scripts', 'et_builder_maybe_enqueue_dashicons', 19 );
add_action( 'wp_footer', 'et_builder_maybe_ensure_heartbeat_script', 19 );
* @param string $post_type post type.
function et_builder_set_post_type( $post_type = '' ) {
global $et_builder_post_type, $post;
$et_builder_post_type = ! empty( $post_type ) ? $post_type : $post->post_type;
* Saves Metabox settings.
* @since 3.29.2 Included check to verify if constant exists before use.
* Throws error otherwise from PHP7.2.x
* @param int $post_id post id.
* @param WP_Post $post object.
function et_pb_metabox_settings_save_details( $post_id, $post ) {
if ( 'post.php' !== $pagenow ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
// do not update builder post meta when Preview is loading.
if ( isset( $_POST['wp-preview'] ) && 'dopreview' === $_POST['wp-preview'] ) {
$post_type = get_post_type_object( $post->post_type );
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
if ( ! isset( $_POST['et_pb_settings_nonce'] ) || ! wp_verify_nonce( $_POST['et_pb_settings_nonce'], basename( __FILE__ ) ) ) { // phpcs:ignore ET.Sniffs.ValidatedSanitizedInput -- The nonce value is used only for comparision in the `wp_verify_nonce`.
if ( isset( $_POST['et_pb_use_builder'] ) ) {
$et_pb_use_builder_input = sanitize_text_field( $_POST['et_pb_use_builder'] );
update_post_meta( $post_id, '_et_pb_use_builder', $et_pb_use_builder_input );
if ( ! empty( $_POST['et_builder_version'] ) ) {
update_post_meta( $post_id, '_et_builder_version', sanitize_text_field( $_POST['et_builder_version'] ) );
$et_pb_show_page_creation_input = isset( $_POST['et_pb_show_page_creation'] ) ? sanitize_text_field( $_POST['et_pb_show_page_creation'] ) : false;
if ( 'on' === $et_pb_show_page_creation_input ) {
// Set page creation flow to on.
update_post_meta( $post_id, '_et_pb_show_page_creation', 'on' );
} elseif ( 'off' === $et_pb_show_page_creation_input ) {
// Delete page creation flow.
delete_post_meta( $post_id, '_et_pb_show_page_creation' );
} elseif ( false === $et_pb_show_page_creation_input && 'on' === $et_pb_use_builder_input ) {
$et_pb_show_page_creation_meta = get_post_meta( $post_id, '_et_pb_show_page_creation', true );
// Strip non-printable characters.
$post_content_cleaned = preg_replace( '/[\x00-\x1F\x7F]/u', '', $post->post_content );
preg_match_all( '/\[et_pb_section(.*?)?\]\[et_pb_row(.*?)?\]\[et_pb_column(.*?)?\](.+?)\[\/et_pb_column\]\[\/et_pb_row\]\[\/et_pb_section\]/m', $post_content_cleaned, $matches );
if ( isset( $matches[4] ) && ! empty( $matches[4] ) ) {
if ( 'on' === $et_pb_show_page_creation_meta ) {
// Set page creation flow to on.
update_post_meta( $post_id, '_et_pb_show_page_creation', 'off' );
delete_post_meta( $post_id, '_et_pb_show_page_creation' );
if ( 'on' !== $et_pb_use_builder_input ) {
if ( defined( 'ET_BUILDER_WC_PRODUCT_PAGE_CONTENT_STATUS_META_KEY' ) ) {
delete_post_meta( $post_id, ET_BUILDER_WC_PRODUCT_PAGE_CONTENT_STATUS_META_KEY );
delete_post_meta( $post_id, '_et_pb_use_builder' );
delete_post_meta( $post_id, '_et_builder_version' );
if ( defined( 'ET_BUILDER_WC_PRODUCT_PAGE_CONTENT_STATUS_META_KEY' ) ) {
delete_post_meta( $post_id, ET_BUILDER_WC_PRODUCT_PAGE_CONTENT_STATUS_META_KEY );
* @hooked et_builder_set_product_page_layout_meta - 10
do_action( 'et_save_post', $post_id );
// Do not process Page Settings if BFB is enabled. Were saving it via ajax.
if ( et_builder_bfb_enabled() ) {
// But we still need to save old content.
if ( isset( $_POST['et_pb_old_content'] ) ) {
update_post_meta( $post_id, '_et_pb_old_content', $_POST['et_pb_old_content'] );
// WooCommerce Modules needs the following hook.
* Fires after the `_et_pb_old_content` post meta is updated.
* In case you want to over-ride `_et_pb_old_content` content, this is the hook you should use.
* @see et_builder_wc_long_description_metabox_save()
* @param int $post_id Post ID.
* $param WP_Post $post The Post.
* $param array $_POST Request variables. This could be used for Nonce verification, etc.
do_action( 'et_pb_old_content_updated', $post_id, $post, $_POST );
delete_post_meta( $post_id, '_et_pb_old_content' );
// Only run AB Testing-related update sequence if AB Testing is allowed.
if ( et_pb_is_allowed( 'ab_testing' ) ) {
// Delete AB Testing settings' autosave.
delete_post_meta( $post_id, '_et_pb_use_ab_testing_draft' );
delete_post_meta( $post_id, '_et_pb_ab_subjects_draft' );
if ( isset( $_POST['et_pb_use_ab_testing'] ) && in_array( $_POST['et_pb_use_ab_testing'], array( 'on', 'off' ), true ) ) {
update_post_meta( $post_id, '_et_pb_use_ab_testing', sanitize_text_field( $_POST['et_pb_use_ab_testing'] ) );
if ( 'on' === $_POST['et_pb_use_ab_testing'] ) {
if ( ! get_post_meta( $post_id, '_et_pb_ab_testing_id', true ) ) {
update_post_meta( $post_id, '_et_pb_ab_testing_id', wp_rand() );
delete_post_meta( $post_id, '_et_pb_ab_testing_id' );
delete_post_meta( $post_id, 'et_pb_subjects_cache' );
et_pb_ab_remove_stats( $post_id );
delete_post_meta( $post_id, '_et_pb_use_ab_testing' );
delete_post_meta( $post_id, '_et_pb_ab_testing_id' );
if ( isset( $_POST['et_pb_ab_subjects'] ) && '' !== $_POST['et_pb_ab_subjects'] ) {
update_post_meta( $post_id, '_et_pb_ab_subjects', et_prevent_duplicate_item( sanitize_text_field( $_POST['et_pb_ab_subjects'] ), ',' ) );