: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
$ids = preg_match_all( '/data-id="(\d+)"/i', $gallery, $matches ) ? $matches[1] : array();
$columns = preg_match( '/<ul class="wp-block-gallery columns-(\d)[^"]*?">/i', $gallery, $matches ) ? $matches[1] : 3;
'[gallery columns="%s" ids="%s"]',
implode( ',', array_map( 'intval', $ids ) )
* Convert all GB galleries to shortcodes.
* @param string $content Post content
public function gb_galleries_to_shortcodes( $content ) {
return preg_replace_callback(
$this->gb_gallery_regexp,
array( $this, 'gb_gallery_to_shortcode' ),
* Check a specified post's content for GB gallery and, if present, return the first
* @param string $gallery Gallery data and srcs parsed from the expanded shortcode.
* @param int|WP_Post $post Post ID or object.
* @return string|array Gallery data and srcs parsed from the expanded shortcode.
public function get_post_gallery( $gallery, $post ) {
$content = get_post_field( 'post_content', $post );
if ( empty( $content ) ) {
if ( preg_match( $this->gb_gallery_regexp, $content, $matches ) ) {
if ( apply_filters( 'et_gb_gallery_to_shortcode', true ) ) {
return do_shortcode( $this->gb_gallery_to_shortcode( $matches[0] ) );
* Delete first GB gallery in content
* @param string $content Content.
* @param bool $deleted Whether a gallery has been already deleted or not.;
public function et_delete_post_gallery( $content, $deleted ) {
// If a gallery was already removed, do nothing
return preg_replace( $this->gb_gallery_regexp, '', $content, 1 );
* Remove custom style from our metabox when GB is showing it.
* @param string $post_type Post type.
public function add_meta_boxes( $post_type ) {
$is_block_editor_page = $this->_is_block_editor_page();
$is_metabox_exist = function_exists( 'et_single_settings_meta_box' );
$is_builder_enabled = et_builder_enabled_for_post_type( $post_type );
$is_metabox_allowed = et_pb_is_allowed( 'page_options' );
if ( $is_block_editor_page && $is_metabox_exist && $is_builder_enabled && $is_metabox_allowed ) {
// Change our metabox id so that no custom style is applied.
remove_meta_box( 'et_settings_meta_box', $post_type, 'side' );
'et_settings_meta_box_gutenberg',
esc_html__( 'Divi Page Settings', 'Divi' ),
'et_single_settings_meta_box',
* Hook into REST API page call.
public function rest_insert_page() {
add_filter( 'update_post_metadata', array( $this, 'update_post_metadata' ), 10, 4 );
* Custom auth function for meta updates via REST API.
* @param boolean $allowed True if allowed to view the meta field by default, false if else.
* @param string $meta_key The meta key.
* @param int $id Post ID.
public function meta_auth( $allowed, $meta_key, $id ) {
return current_user_can( 'edit_post', $id );
* Hook methods to WordPress
* Latest plugin version: 1.5
public function init_hooks() {
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 4 );
add_action( 'admin_print_scripts-edit.php', array( $this, 'add_new_button' ), 10 );
add_action( 'admin_init', array( $this, 'add_edit_link_filters' ) );
// Only need to add this filter is the nonce is present in the url request
// nonce value will be checked in the filter itself.
if ( isset( $_GET['et_fb_new_vb_nonce'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
add_action( 'new_to_auto-draft', array( $this, 'auto_draft' ), 1 );
add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 );
// If "Enable Divi Gallery" option is enabled
if ( apply_filters( 'et_gb_gallery_to_shortcode', false ) === true ) {
// Converts GB galleries to shortcodes
add_filter( 'the_content', array( $this, 'gb_galleries_to_shortcodes' ), 1 );
if ( apply_filters( 'et_gb_gallery_include_in_get_post_gallery', false ) === true ) {
// Makes sure `get_post_gallery` returns a GB gallery if no shortcode is found
add_filter( 'get_post_gallery', array( $this, 'get_post_gallery' ), 10, 2 );
// This filter gets called when Divi removes first gallery shortcode from
// a gallery post (as in post format). We hook into that to ensure that the first GB gallery
// is deleted if nothing else was
add_filter( 'et_delete_post_gallery', array( $this, 'et_delete_post_gallery' ), 10, 2 );
// Provide other code a simple way to access the conversion function via this custom filter
add_filter( 'et_gb_galleries_to_shortcodes', array( $this, 'gb_galleries_to_shortcodes' ) );
add_filter( 'et_fb_load_raw_post_content', array( $this, 'et_fb_load_raw_post_content' ) );
add_filter( 'init', array( $this, 'ensure_post_type_supports' ), 999999 );
// This is one of the most idiot things I had to do ever and its due to
// a 10 month old-yet not fixed WP bug: https://core.trac.wordpress.org/ticket/42069
// TLDR: when updating a post with meta via WP REST API, `update_metadata` should only
// be called for metas whose value changed.
// However, the equality check is fooled by values including characters that are
// slashed or converted to entities, like " or <.
// `update_metadata` is then called and returns `false` (because value didn't change) which results
// in REST API page update to abort with a 500 error code....
// To fix the issue, we hook into REST API page update and force `update_metadata` to return `true`
// when value didn't change (only applied to our own meta keys)
add_action( 'rest_insert_page', array( $this, 'rest_insert_page' ) );
// Need to deal with our metabox styling when inside GB
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 1 );
// To register the post metas is needed because we want to change their value within our custom GB blocks
// Editing a post meta via REST API is allowed by default unless its key is protected (starts with `_`)
// which is the case here so we also need to create a custom auth function.
$auth = array( $this, 'meta_auth' );
register_meta( 'post', '_et_pb_use_builder', array(
'auth_callback' => $auth,
register_meta( 'post', '_et_pb_old_content', array(
'auth_callback' => $auth,
register_meta( 'post', '_et_gb_content_width', array(
'auth_callback' => $auth,
if ( et_core_is_gutenberg_active() ) {
new ET_Builder_Block_Editor_Integration;