Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../themes/Divi/includes/builder/feature/gutenber.../utils
File: Conversion.php
<?php
[0] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[1] Fix | Delete
exit; // Exit if accessed directly
[2] Fix | Delete
}
[3] Fix | Delete
[4] Fix | Delete
/**
[5] Fix | Delete
* Class ET_GB_Utils_Conversion
[6] Fix | Delete
*
[7] Fix | Delete
* Handling Gutenberg serialized content conversion into builder shortcode layout
[8] Fix | Delete
*/
[9] Fix | Delete
class ET_GB_Utils_Conversion {
[10] Fix | Delete
// Populate all layout block which is placed inside other block. Layout block contains
[11] Fix | Delete
// section which has to be the first level element once converted into VB content
[12] Fix | Delete
private $deep_layout_blocks = array();
[13] Fix | Delete
[14] Fix | Delete
// Layout list. Layout block got its own section. Others are concatenated into text module
[15] Fix | Delete
private $layout_list = array();
[16] Fix | Delete
[17] Fix | Delete
// Temporary variable to hold non layout block into one
[18] Fix | Delete
private $text_module_content = '';
[19] Fix | Delete
[20] Fix | Delete
// Serialized layout
[21] Fix | Delete
private $shortcode_layout = '';
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Check if given block is layout block
[25] Fix | Delete
*
[26] Fix | Delete
* @since 4.1.0
[27] Fix | Delete
*
[28] Fix | Delete
* @todo being set as static so it is easier to be used outside this class. If being used quite
[29] Fix | Delete
* frequently, probably consider wrap this into function. Not needed at the moment tho
[30] Fix | Delete
*
[31] Fix | Delete
* @param array $block Parsed block.
[32] Fix | Delete
*
[33] Fix | Delete
* @return bool
[34] Fix | Delete
*/
[35] Fix | Delete
public static function is_layout_block( $block = array() ) {
[36] Fix | Delete
$block_name = et_()->array_get( $block, 'blockName', '' );
[37] Fix | Delete
[38] Fix | Delete
return 'divi/layout' === $block_name;
[39] Fix | Delete
}
[40] Fix | Delete
[41] Fix | Delete
/**
[42] Fix | Delete
* Check if given block is reusable block
[43] Fix | Delete
*
[44] Fix | Delete
* @since 4.1.0
[45] Fix | Delete
*
[46] Fix | Delete
* @todo being set as static so it is easier to be used outside this class. If being used quite
[47] Fix | Delete
* frequently, probably consider wrap this into function. Not needed at the moment tho
[48] Fix | Delete
*
[49] Fix | Delete
* @param array $block Parsed block.
[50] Fix | Delete
*
[51] Fix | Delete
* @return bool
[52] Fix | Delete
*/
[53] Fix | Delete
public static function is_reusable_block( $block = array() ) {
[54] Fix | Delete
$block_name = et_()->array_get( $block, 'blockName', '' );
[55] Fix | Delete
[56] Fix | Delete
return 'core/block' === $block_name && et_()->array_get( $block, 'attrs.ref' ) > 0;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Get reusable block's parsed content. NOTE: WordPress has built in `render_block_core_block()`
[61] Fix | Delete
* but it renders the block and its content instead of parse its content.
[62] Fix | Delete
*
[63] Fix | Delete
* @since 4.1.0
[64] Fix | Delete
*
[65] Fix | Delete
* @see render_block_core_block()
[66] Fix | Delete
*
[67] Fix | Delete
* @todo being set as static so it is easier to be used outside this class. If being used quite
[68] Fix | Delete
* frequently, probably consider wrap this into function. Not needed at the moment tho
[69] Fix | Delete
*
[70] Fix | Delete
* @param array $block Parsed block.
[71] Fix | Delete
*
[72] Fix | Delete
* @return array
[73] Fix | Delete
*/
[74] Fix | Delete
public static function get_reusable_block_content( $block ) {
[75] Fix | Delete
$block_id = et_()->array_get( $block, 'attrs.ref' );
[76] Fix | Delete
$block_data = get_post( $block_id );
[77] Fix | Delete
[78] Fix | Delete
if ( ! $block_data || 'wp_block' !== $block_data->post_type || 'publish' !== $block_data->post_status ) {
[79] Fix | Delete
return array() ;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
return parse_blocks( $block_data->post_content );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Parse reusable block by getting its content and append it as innerBlocks
[87] Fix | Delete
*
[88] Fix | Delete
* @since 4.1.0
[89] Fix | Delete
*
[90] Fix | Delete
* @param array Parsed block.
[91] Fix | Delete
*
[92] Fix | Delete
* @return array Modified parsed block.
[93] Fix | Delete
*/
[94] Fix | Delete
public static function parse_reusable_block( $block ) {
[95] Fix | Delete
$reusable_block_data = self::get_reusable_block_content( $block );
[96] Fix | Delete
$block['innerBlocks'] = array_merge( $block['innerBlocks'], $reusable_block_data );
[97] Fix | Delete
[98] Fix | Delete
// Unset reusable block's ref attribute so reusable block content is no longer fetched
[99] Fix | Delete
unset( $block['attrs']['ref'] );
[100] Fix | Delete
[101] Fix | Delete
// Change block into group so its content is being rendered
[102] Fix | Delete
$block['blockName'] = 'core/group';
[103] Fix | Delete
[104] Fix | Delete
// Recreate innerContent which is used by block parser to render innerBlock.
[105] Fix | Delete
// See: `render_block()`'s `$block['innerContent'] as $chunk` loop
[106] Fix | Delete
$block['innerContent'] = array_merge(
[107] Fix | Delete
array( '<div class="wp-block-group"><div class="wp-block-group__inner-container">' ),
[108] Fix | Delete
array_fill( 0, count( $block['innerBlocks'] ), null ),
[109] Fix | Delete
array( '</div></div>' )
[110] Fix | Delete
);
[111] Fix | Delete
[112] Fix | Delete
return $block;
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Pull layout block that is located deep inside inner blocks. Layout block contains section;
[117] Fix | Delete
* in builder, section has to be on the first level of document
[118] Fix | Delete
*
[119] Fix | Delete
* @since 4.1.0
[120] Fix | Delete
*
[121] Fix | Delete
* @param array $block Parsed block.
[122] Fix | Delete
*/
[123] Fix | Delete
private function pull_layout_block( $block ) {
[124] Fix | Delete
// Pull and populate layout block. Layout block contains section(s) so it should be rendered
[125] Fix | Delete
// on first level layout, below Gutenberg content inside text module
[126] Fix | Delete
if ( self::is_layout_block( $block ) ) {
[127] Fix | Delete
// Pull layout block and populate list of layout block located on inner blocks
[128] Fix | Delete
$this->deep_layout_blocks[] = $block;
[129] Fix | Delete
[130] Fix | Delete
// Remove innerContent and innerHTML value because inner block can't be simply removed
[131] Fix | Delete
// due to nested block rendering relies on `$block['innerContent']` making cross reference
[132] Fix | Delete
// on `$block['innerBlocks']` and removing them causes error (see: `render_block()`'s
[133] Fix | Delete
// `$block['innerContent'] as $chunk` loop). Thus, set deep layout block's content empty
[134] Fix | Delete
// so it doesn't get rendered
[135] Fix | Delete
$block['innerHTML'] = '';
[136] Fix | Delete
$block['innerContent'] = array();
[137] Fix | Delete
[138] Fix | Delete
return $block;
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
// Reusable block's content is not saved inside block; Thus Get reusable block's content,
[142] Fix | Delete
// append it as innerBlock, and pull layout block if exist.
[143] Fix | Delete
if ( self::is_reusable_block( $block ) ) {
[144] Fix | Delete
$block = self::parse_reusable_block( $block );
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
// Recursively loop over block then pull Layout Block
[148] Fix | Delete
if ( ! empty( $block['innerBlocks'] ) ) {
[149] Fix | Delete
$block['innerBlocks'] = array_map(
[150] Fix | Delete
array( $this, 'pull_layout_block' ),
[151] Fix | Delete
$block['innerBlocks']
[152] Fix | Delete
);
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
return $block;
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
/**
[159] Fix | Delete
* Convert serialized block into shortcode layout
[160] Fix | Delete
*
[161] Fix | Delete
* @since 4.1.0
[162] Fix | Delete
*
[163] Fix | Delete
* @param string $serialized_block
[164] Fix | Delete
*
[165] Fix | Delete
* @return string
[166] Fix | Delete
*/
[167] Fix | Delete
public function block_to_shortcode( $serialized_block = '' ) {
[168] Fix | Delete
// Wrapper div needs to be trimmed
[169] Fix | Delete
$layout_open_tag = '<div class="wp-block-divi-layout">';
[170] Fix | Delete
$layout_open_length = strlen( $layout_open_tag );
[171] Fix | Delete
$layout_close_tag = '</div>';
[172] Fix | Delete
$layout_close_length = strlen( $layout_close_tag );
[173] Fix | Delete
[174] Fix | Delete
// Parsed blocks
[175] Fix | Delete
$blocks = parse_blocks( $serialized_block );
[176] Fix | Delete
[177] Fix | Delete
// Loop blocks
[178] Fix | Delete
foreach ( $blocks as $block ) {
[179] Fix | Delete
if ( self::is_layout_block( $block ) ) {
[180] Fix | Delete
// Append currently populated non-Layout Block into one before layout block is appended
[181] Fix | Delete
if ( ! empty( $this->text_module_content ) ) {
[182] Fix | Delete
$this->layout_list[] = $this->text_module_content;
[183] Fix | Delete
[184] Fix | Delete
// Reset text module content so next non-layout block is placed below current layout block
[185] Fix | Delete
$this->text_module_content = '';
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
$this->layout_list[] = $block;
[189] Fix | Delete
} else {
[190] Fix | Delete
// Reusable block's content is not saved inside block; Thus Get reusable block's
[191] Fix | Delete
// content, append it as innerBlock, and pull layout block if exist.
[192] Fix | Delete
if ( self::is_reusable_block( $block ) ) {
[193] Fix | Delete
$block = self::parse_reusable_block( $block );
[194] Fix | Delete
}
[195] Fix | Delete
[196] Fix | Delete
// Pull any Layout Block inside nested block if there's any
[197] Fix | Delete
if ( ! empty( $block['innerBlocks'] ) ) {
[198] Fix | Delete
$block['innerBlocks'] = array_map(
[199] Fix | Delete
array( $this, 'pull_layout_block' ),
[200] Fix | Delete
$block['innerBlocks']
[201] Fix | Delete
);
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
// Populate block into temporary text module content buffer
[205] Fix | Delete
$this->text_module_content .= render_block( $block );
[206] Fix | Delete
}
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// Populate remaining non-layout block into layout list
[210] Fix | Delete
if ( ! empty( $this->text_module_content ) ) {
[211] Fix | Delete
$this->layout_list[] = $this->text_module_content;
[212] Fix | Delete
[213] Fix | Delete
// Reset
[214] Fix | Delete
$this->text_module_content = '';
[215] Fix | Delete
}
[216] Fix | Delete
[217] Fix | Delete
// Loop over populated content and render it into shortcode layout
[218] Fix | Delete
foreach( array_merge( $this->layout_list, $this->deep_layout_blocks ) as $item ) {
[219] Fix | Delete
if ( self::is_layout_block( $item ) ) {
[220] Fix | Delete
$shortcode_layout = trim( et_()->array_get( $item, 'innerHTML', '' ) );
[221] Fix | Delete
[222] Fix | Delete
// Remove layout content opening <div>
[223] Fix | Delete
if ( $layout_open_tag === substr( $shortcode_layout, 0, $layout_open_length ) ) {
[224] Fix | Delete
$shortcode_layout = substr( $shortcode_layout, $layout_open_length );
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
// Remove layout content closing </div>
[228] Fix | Delete
if ( $layout_close_tag === substr( $shortcode_layout, ( 0 - $layout_close_length ) ) ) {
[229] Fix | Delete
$shortcode_layout = substr( $shortcode_layout, 0, ( 0 - $layout_close_length ) );
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
$this->shortcode_layout .= $shortcode_layout;
[233] Fix | Delete
} else {
[234] Fix | Delete
$text_module = '[et_pb_text]' . $item . '[/et_pb_text]';
[235] Fix | Delete
$column = '[et_pb_column type="4_4"]' . $text_module . '[/et_pb_column]';
[236] Fix | Delete
$row = '[et_pb_row admin_label="row"]' . $column . '[/et_pb_row]';
[237] Fix | Delete
$this->shortcode_layout .= '[et_pb_section admin_label="section"]' . $row . '[/et_pb_section]';
[238] Fix | Delete
}
[239] Fix | Delete
}
[240] Fix | Delete
[241] Fix | Delete
return $this->shortcode_layout;
[242] Fix | Delete
}
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Convert gutenberg block layout into shortcode.
[247] Fix | Delete
* NOTE: There is JS version for activation via Gutenberg. See: `convertBlockToShortcode()`
[248] Fix | Delete
*
[249] Fix | Delete
* @since 4.1.0
[250] Fix | Delete
*
[251] Fix | Delete
* @param string $post_content Post content / serialized block.
[252] Fix | Delete
*
[253] Fix | Delete
* @return string Shortcode layout.
[254] Fix | Delete
*/
[255] Fix | Delete
function et_builder_convert_block_to_shortcode( $post_content ) {
[256] Fix | Delete
$conversion = new ET_GB_Utils_Conversion();
[257] Fix | Delete
[258] Fix | Delete
return $conversion->block_to_shortcode( $post_content );
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function