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

Warning: Undefined array key "page_file_edit_line" in /home/sportsfever/public_html/filemanger/edit_text_line.php on line 32
/home/sportsfe.../httpdocs/wp-inclu...
File: blocks.php
// Markup for the hooked blocks has already been created (in `insert_hooked_blocks`).
[1000] Fix | Delete
return '';
[1001] Fix | Delete
}
[1002] Fix | Delete
[1003] Fix | Delete
/**
[1004] Fix | Delete
* Runs the hooked blocks algorithm on the given content.
[1005] Fix | Delete
*
[1006] Fix | Delete
* @since 6.6.0
[1007] Fix | Delete
* @access private
[1008] Fix | Delete
*
[1009] Fix | Delete
* @param string $content Serialized content.
[1010] Fix | Delete
* @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
[1011] Fix | Delete
* or pattern that the blocks belong to.
[1012] Fix | Delete
* @param callable $callback A function that will be called for each block to generate
[1013] Fix | Delete
* the markup for a given list of blocks that are hooked to it.
[1014] Fix | Delete
* Default: 'insert_hooked_blocks'.
[1015] Fix | Delete
* @return string The serialized markup.
[1016] Fix | Delete
*/
[1017] Fix | Delete
function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) {
[1018] Fix | Delete
$hooked_blocks = get_hooked_blocks();
[1019] Fix | Delete
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
[1020] Fix | Delete
return $content;
[1021] Fix | Delete
}
[1022] Fix | Delete
[1023] Fix | Delete
$blocks = parse_blocks( $content );
[1024] Fix | Delete
[1025] Fix | Delete
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $context, $callback );
[1026] Fix | Delete
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $context, $callback );
[1027] Fix | Delete
[1028] Fix | Delete
return traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
[1029] Fix | Delete
}
[1030] Fix | Delete
[1031] Fix | Delete
/**
[1032] Fix | Delete
* Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks.
[1033] Fix | Delete
*
[1034] Fix | Delete
* @since 6.6.0
[1035] Fix | Delete
* @access private
[1036] Fix | Delete
*
[1037] Fix | Delete
* @param string $serialized_block The serialized markup of a block and its inner blocks.
[1038] Fix | Delete
* @return string The serialized markup of the inner blocks.
[1039] Fix | Delete
*/
[1040] Fix | Delete
function remove_serialized_parent_block( $serialized_block ) {
[1041] Fix | Delete
$start = strpos( $serialized_block, '-->' ) + strlen( '-->' );
[1042] Fix | Delete
$end = strrpos( $serialized_block, '<!--' );
[1043] Fix | Delete
return substr( $serialized_block, $start, $end - $start );
[1044] Fix | Delete
}
[1045] Fix | Delete
[1046] Fix | Delete
/**
[1047] Fix | Delete
* Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
[1048] Fix | Delete
* Currently only supports `wp_navigation` post types.
[1049] Fix | Delete
*
[1050] Fix | Delete
* @since 6.6.0
[1051] Fix | Delete
* @access private
[1052] Fix | Delete
*
[1053] Fix | Delete
* @param stdClass $post Post object.
[1054] Fix | Delete
* @return stdClass The updated post object.
[1055] Fix | Delete
*/
[1056] Fix | Delete
function update_ignored_hooked_blocks_postmeta( $post ) {
[1057] Fix | Delete
/*
[1058] Fix | Delete
* In this scenario the user has likely tried to create a navigation via the REST API.
[1059] Fix | Delete
* In which case we won't have a post ID to work with and store meta against.
[1060] Fix | Delete
*/
[1061] Fix | Delete
if ( empty( $post->ID ) ) {
[1062] Fix | Delete
return $post;
[1063] Fix | Delete
}
[1064] Fix | Delete
[1065] Fix | Delete
/*
[1066] Fix | Delete
* Skip meta generation when consumers intentionally update specific Navigation fields
[1067] Fix | Delete
* and omit the content update.
[1068] Fix | Delete
*/
[1069] Fix | Delete
if ( ! isset( $post->post_content ) ) {
[1070] Fix | Delete
return $post;
[1071] Fix | Delete
}
[1072] Fix | Delete
[1073] Fix | Delete
/*
[1074] Fix | Delete
* Skip meta generation when the post content is not a navigation block.
[1075] Fix | Delete
*/
[1076] Fix | Delete
if ( ! isset( $post->post_type ) || 'wp_navigation' !== $post->post_type ) {
[1077] Fix | Delete
return $post;
[1078] Fix | Delete
}
[1079] Fix | Delete
[1080] Fix | Delete
$attributes = array();
[1081] Fix | Delete
[1082] Fix | Delete
$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
[1083] Fix | Delete
if ( ! empty( $ignored_hooked_blocks ) ) {
[1084] Fix | Delete
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
[1085] Fix | Delete
$attributes['metadata'] = array(
[1086] Fix | Delete
'ignoredHookedBlocks' => $ignored_hooked_blocks,
[1087] Fix | Delete
);
[1088] Fix | Delete
}
[1089] Fix | Delete
[1090] Fix | Delete
$markup = get_comment_delimited_block_content(
[1091] Fix | Delete
'core/navigation',
[1092] Fix | Delete
$attributes,
[1093] Fix | Delete
$post->post_content
[1094] Fix | Delete
);
[1095] Fix | Delete
[1096] Fix | Delete
$serialized_block = apply_block_hooks_to_content( $markup, get_post( $post->ID ), 'set_ignored_hooked_blocks_metadata' );
[1097] Fix | Delete
$root_block = parse_blocks( $serialized_block )[0];
[1098] Fix | Delete
[1099] Fix | Delete
$ignored_hooked_blocks = isset( $root_block['attrs']['metadata']['ignoredHookedBlocks'] )
[1100] Fix | Delete
? $root_block['attrs']['metadata']['ignoredHookedBlocks']
[1101] Fix | Delete
: array();
[1102] Fix | Delete
[1103] Fix | Delete
if ( ! empty( $ignored_hooked_blocks ) ) {
[1104] Fix | Delete
$existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
[1105] Fix | Delete
if ( ! empty( $existing_ignored_hooked_blocks ) ) {
[1106] Fix | Delete
$existing_ignored_hooked_blocks = json_decode( $existing_ignored_hooked_blocks, true );
[1107] Fix | Delete
$ignored_hooked_blocks = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) );
[1108] Fix | Delete
}
[1109] Fix | Delete
update_post_meta( $post->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) );
[1110] Fix | Delete
}
[1111] Fix | Delete
[1112] Fix | Delete
$post->post_content = remove_serialized_parent_block( $serialized_block );
[1113] Fix | Delete
return $post;
[1114] Fix | Delete
}
[1115] Fix | Delete
[1116] Fix | Delete
/**
[1117] Fix | Delete
* Returns the markup for blocks hooked to the given anchor block in a specific relative position and then
[1118] Fix | Delete
* adds a list of hooked block types to an anchor block's ignored hooked block types.
[1119] Fix | Delete
*
[1120] Fix | Delete
* This function is meant for internal use only.
[1121] Fix | Delete
*
[1122] Fix | Delete
* @since 6.6.0
[1123] Fix | Delete
* @access private
[1124] Fix | Delete
*
[1125] Fix | Delete
* @param array $parsed_anchor_block The anchor block, in parsed block array format.
[1126] Fix | Delete
* @param string $relative_position The relative position of the hooked blocks.
[1127] Fix | Delete
* Can be one of 'before', 'after', 'first_child', or 'last_child'.
[1128] Fix | Delete
* @param array $hooked_blocks An array of hooked block types, grouped by anchor block and relative position.
[1129] Fix | Delete
* @param WP_Block_Template|WP_Post|array $context The block template, template part, or pattern that the anchor block belongs to.
[1130] Fix | Delete
* @return string
[1131] Fix | Delete
*/
[1132] Fix | Delete
function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
[1133] Fix | Delete
$markup = insert_hooked_blocks( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
[1134] Fix | Delete
$markup .= set_ignored_hooked_blocks_metadata( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
[1135] Fix | Delete
[1136] Fix | Delete
return $markup;
[1137] Fix | Delete
}
[1138] Fix | Delete
[1139] Fix | Delete
/**
[1140] Fix | Delete
* Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks.
[1141] Fix | Delete
*
[1142] Fix | Delete
* @since 6.6.0
[1143] Fix | Delete
*
[1144] Fix | Delete
* @param WP_REST_Response $response The response object.
[1145] Fix | Delete
* @param WP_Post $post Post object.
[1146] Fix | Delete
* @return WP_REST_Response The response object.
[1147] Fix | Delete
*/
[1148] Fix | Delete
function insert_hooked_blocks_into_rest_response( $response, $post ) {
[1149] Fix | Delete
if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) {
[1150] Fix | Delete
return $response;
[1151] Fix | Delete
}
[1152] Fix | Delete
[1153] Fix | Delete
$attributes = array();
[1154] Fix | Delete
$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
[1155] Fix | Delete
if ( ! empty( $ignored_hooked_blocks ) ) {
[1156] Fix | Delete
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
[1157] Fix | Delete
$attributes['metadata'] = array(
[1158] Fix | Delete
'ignoredHookedBlocks' => $ignored_hooked_blocks,
[1159] Fix | Delete
);
[1160] Fix | Delete
}
[1161] Fix | Delete
$content = get_comment_delimited_block_content(
[1162] Fix | Delete
'core/navigation',
[1163] Fix | Delete
$attributes,
[1164] Fix | Delete
$response->data['content']['raw']
[1165] Fix | Delete
);
[1166] Fix | Delete
[1167] Fix | Delete
$content = apply_block_hooks_to_content( $content, $post );
[1168] Fix | Delete
[1169] Fix | Delete
// Remove mock Navigation block wrapper.
[1170] Fix | Delete
$content = remove_serialized_parent_block( $content );
[1171] Fix | Delete
[1172] Fix | Delete
$response->data['content']['raw'] = $content;
[1173] Fix | Delete
[1174] Fix | Delete
/** This filter is documented in wp-includes/post-template.php */
[1175] Fix | Delete
$response->data['content']['rendered'] = apply_filters( 'the_content', $content );
[1176] Fix | Delete
[1177] Fix | Delete
return $response;
[1178] Fix | Delete
}
[1179] Fix | Delete
[1180] Fix | Delete
/**
[1181] Fix | Delete
* Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
[1182] Fix | Delete
*
[1183] Fix | Delete
* The returned function can be used as `$pre_callback` argument to `traverse_and_serialize_block(s)`,
[1184] Fix | Delete
* where it will inject the `theme` attribute into all Template Part blocks, and prepend the markup for
[1185] Fix | Delete
* any blocks hooked `before` the given block and as its parent's `first_child`, respectively.
[1186] Fix | Delete
*
[1187] Fix | Delete
* This function is meant for internal use only.
[1188] Fix | Delete
*
[1189] Fix | Delete
* @since 6.4.0
[1190] Fix | Delete
* @since 6.5.0 Added $callback argument.
[1191] Fix | Delete
* @access private
[1192] Fix | Delete
*
[1193] Fix | Delete
* @param array $hooked_blocks An array of blocks hooked to another given block.
[1194] Fix | Delete
* @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
[1195] Fix | Delete
* or pattern that the blocks belong to.
[1196] Fix | Delete
* @param callable $callback A function that will be called for each block to generate
[1197] Fix | Delete
* the markup for a given list of blocks that are hooked to it.
[1198] Fix | Delete
* Default: 'insert_hooked_blocks'.
[1199] Fix | Delete
* @return callable A function that returns the serialized markup for the given block,
[1200] Fix | Delete
* including the markup for any hooked blocks before it.
[1201] Fix | Delete
*/
[1202] Fix | Delete
function make_before_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
[1203] Fix | Delete
/**
[1204] Fix | Delete
* Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup.
[1205] Fix | Delete
*
[1206] Fix | Delete
* If the current block is a Template Part block, inject the `theme` attribute.
[1207] Fix | Delete
* Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's
[1208] Fix | Delete
* `first_child`, respectively, to the serialized markup for the given block.
[1209] Fix | Delete
*
[1210] Fix | Delete
* @param array $block The block to inject the theme attribute into, and hooked blocks before. Passed by reference.
[1211] Fix | Delete
* @param array $parent_block The parent block of the given block. Passed by reference. Default null.
[1212] Fix | Delete
* @param array $prev The previous sibling block of the given block. Default null.
[1213] Fix | Delete
* @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it.
[1214] Fix | Delete
*/
[1215] Fix | Delete
return function ( &$block, &$parent_block = null, $prev = null ) use ( $hooked_blocks, $context, $callback ) {
[1216] Fix | Delete
_inject_theme_attribute_in_template_part_block( $block );
[1217] Fix | Delete
[1218] Fix | Delete
$markup = '';
[1219] Fix | Delete
[1220] Fix | Delete
if ( $parent_block && ! $prev ) {
[1221] Fix | Delete
// Candidate for first-child insertion.
[1222] Fix | Delete
$markup .= call_user_func_array(
[1223] Fix | Delete
$callback,
[1224] Fix | Delete
array( &$parent_block, 'first_child', $hooked_blocks, $context )
[1225] Fix | Delete
);
[1226] Fix | Delete
}
[1227] Fix | Delete
[1228] Fix | Delete
$markup .= call_user_func_array(
[1229] Fix | Delete
$callback,
[1230] Fix | Delete
array( &$block, 'before', $hooked_blocks, $context )
[1231] Fix | Delete
);
[1232] Fix | Delete
[1233] Fix | Delete
return $markup;
[1234] Fix | Delete
};
[1235] Fix | Delete
}
[1236] Fix | Delete
[1237] Fix | Delete
/**
[1238] Fix | Delete
* Returns a function that injects the hooked blocks after a given block.
[1239] Fix | Delete
*
[1240] Fix | Delete
* The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`,
[1241] Fix | Delete
* where it will append the markup for any blocks hooked `after` the given block and as its parent's
[1242] Fix | Delete
* `last_child`, respectively.
[1243] Fix | Delete
*
[1244] Fix | Delete
* This function is meant for internal use only.
[1245] Fix | Delete
*
[1246] Fix | Delete
* @since 6.4.0
[1247] Fix | Delete
* @since 6.5.0 Added $callback argument.
[1248] Fix | Delete
* @access private
[1249] Fix | Delete
*
[1250] Fix | Delete
* @param array $hooked_blocks An array of blocks hooked to another block.
[1251] Fix | Delete
* @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
[1252] Fix | Delete
* or pattern that the blocks belong to.
[1253] Fix | Delete
* @param callable $callback A function that will be called for each block to generate
[1254] Fix | Delete
* the markup for a given list of blocks that are hooked to it.
[1255] Fix | Delete
* Default: 'insert_hooked_blocks'.
[1256] Fix | Delete
* @return callable A function that returns the serialized markup for the given block,
[1257] Fix | Delete
* including the markup for any hooked blocks after it.
[1258] Fix | Delete
*/
[1259] Fix | Delete
function make_after_block_visitor( $hooked_blocks, $context, $callback = 'insert_hooked_blocks' ) {
[1260] Fix | Delete
/**
[1261] Fix | Delete
* Injects hooked blocks after the given block, and returns the serialized markup.
[1262] Fix | Delete
*
[1263] Fix | Delete
* Append the markup for any blocks hooked `after` the given block and as its parent's
[1264] Fix | Delete
* `last_child`, respectively, to the serialized markup for the given block.
[1265] Fix | Delete
*
[1266] Fix | Delete
* @param array $block The block to inject the hooked blocks after. Passed by reference.
[1267] Fix | Delete
* @param array $parent_block The parent block of the given block. Passed by reference. Default null.
[1268] Fix | Delete
* @param array $next The next sibling block of the given block. Default null.
[1269] Fix | Delete
* @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it.
[1270] Fix | Delete
*/
[1271] Fix | Delete
return function ( &$block, &$parent_block = null, $next = null ) use ( $hooked_blocks, $context, $callback ) {
[1272] Fix | Delete
$markup = call_user_func_array(
[1273] Fix | Delete
$callback,
[1274] Fix | Delete
array( &$block, 'after', $hooked_blocks, $context )
[1275] Fix | Delete
);
[1276] Fix | Delete
[1277] Fix | Delete
if ( $parent_block && ! $next ) {
[1278] Fix | Delete
// Candidate for last-child insertion.
[1279] Fix | Delete
$markup .= call_user_func_array(
[1280] Fix | Delete
$callback,
[1281] Fix | Delete
array( &$parent_block, 'last_child', $hooked_blocks, $context )
[1282] Fix | Delete
);
[1283] Fix | Delete
}
[1284] Fix | Delete
[1285] Fix | Delete
return $markup;
[1286] Fix | Delete
};
[1287] Fix | Delete
}
[1288] Fix | Delete
[1289] Fix | Delete
/**
[1290] Fix | Delete
* Given an array of attributes, returns a string in the serialized attributes
[1291] Fix | Delete
* format prepared for post content.
[1292] Fix | Delete
*
[1293] Fix | Delete
* The serialized result is a JSON-encoded string, with unicode escape sequence
[1294] Fix | Delete
* substitution for characters which might otherwise interfere with embedding
[1295] Fix | Delete
* the result in an HTML comment.
[1296] Fix | Delete
*
[1297] Fix | Delete
* This function must produce output that remains in sync with the output of
[1298] Fix | Delete
* the serializeAttributes JavaScript function in the block editor in order
[1299] Fix | Delete
* to ensure consistent operation between PHP and JavaScript.
[1300] Fix | Delete
*
[1301] Fix | Delete
* @since 5.3.1
[1302] Fix | Delete
*
[1303] Fix | Delete
* @param array $block_attributes Attributes object.
[1304] Fix | Delete
* @return string Serialized attributes.
[1305] Fix | Delete
*/
[1306] Fix | Delete
function serialize_block_attributes( $block_attributes ) {
[1307] Fix | Delete
$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
[1308] Fix | Delete
$encoded_attributes = preg_replace( '/--/', '\\u002d\\u002d', $encoded_attributes );
[1309] Fix | Delete
$encoded_attributes = preg_replace( '/</', '\\u003c', $encoded_attributes );
[1310] Fix | Delete
$encoded_attributes = preg_replace( '/>/', '\\u003e', $encoded_attributes );
[1311] Fix | Delete
$encoded_attributes = preg_replace( '/&/', '\\u0026', $encoded_attributes );
[1312] Fix | Delete
// Regex: /\\"/
[1313] Fix | Delete
$encoded_attributes = preg_replace( '/\\\\"/', '\\u0022', $encoded_attributes );
[1314] Fix | Delete
[1315] Fix | Delete
return $encoded_attributes;
[1316] Fix | Delete
}
[1317] Fix | Delete
[1318] Fix | Delete
/**
[1319] Fix | Delete
* Returns the block name to use for serialization. This will remove the default
[1320] Fix | Delete
* "core/" namespace from a block name.
[1321] Fix | Delete
*
[1322] Fix | Delete
* @since 5.3.1
[1323] Fix | Delete
*
[1324] Fix | Delete
* @param string|null $block_name Optional. Original block name. Null if the block name is unknown,
[1325] Fix | Delete
* e.g. Classic blocks have their name set to null. Default null.
[1326] Fix | Delete
* @return string Block name to use for serialization.
[1327] Fix | Delete
*/
[1328] Fix | Delete
function strip_core_block_namespace( $block_name = null ) {
[1329] Fix | Delete
if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
[1330] Fix | Delete
return substr( $block_name, 5 );
[1331] Fix | Delete
}
[1332] Fix | Delete
[1333] Fix | Delete
return $block_name;
[1334] Fix | Delete
}
[1335] Fix | Delete
[1336] Fix | Delete
/**
[1337] Fix | Delete
* Returns the content of a block, including comment delimiters.
[1338] Fix | Delete
*
[1339] Fix | Delete
* @since 5.3.1
[1340] Fix | Delete
*
[1341] Fix | Delete
* @param string|null $block_name Block name. Null if the block name is unknown,
[1342] Fix | Delete
* e.g. Classic blocks have their name set to null.
[1343] Fix | Delete
* @param array $block_attributes Block attributes.
[1344] Fix | Delete
* @param string $block_content Block save content.
[1345] Fix | Delete
* @return string Comment-delimited block content.
[1346] Fix | Delete
*/
[1347] Fix | Delete
function get_comment_delimited_block_content( $block_name, $block_attributes, $block_content ) {
[1348] Fix | Delete
if ( is_null( $block_name ) ) {
[1349] Fix | Delete
return $block_content;
[1350] Fix | Delete
}
[1351] Fix | Delete
[1352] Fix | Delete
$serialized_block_name = strip_core_block_namespace( $block_name );
[1353] Fix | Delete
$serialized_attributes = empty( $block_attributes ) ? '' : serialize_block_attributes( $block_attributes ) . ' ';
[1354] Fix | Delete
[1355] Fix | Delete
if ( empty( $block_content ) ) {
[1356] Fix | Delete
return sprintf( '<!-- wp:%s %s/-->', $serialized_block_name, $serialized_attributes );
[1357] Fix | Delete
}
[1358] Fix | Delete
[1359] Fix | Delete
return sprintf(
[1360] Fix | Delete
'<!-- wp:%s %s-->%s<!-- /wp:%s -->',
[1361] Fix | Delete
$serialized_block_name,
[1362] Fix | Delete
$serialized_attributes,
[1363] Fix | Delete
$block_content,
[1364] Fix | Delete
$serialized_block_name
[1365] Fix | Delete
);
[1366] Fix | Delete
}
[1367] Fix | Delete
[1368] Fix | Delete
/**
[1369] Fix | Delete
* Returns the content of a block, including comment delimiters, serializing all
[1370] Fix | Delete
* attributes from the given parsed block.
[1371] Fix | Delete
*
[1372] Fix | Delete
* This should be used when preparing a block to be saved to post content.
[1373] Fix | Delete
* Prefer `render_block` when preparing a block for display. Unlike
[1374] Fix | Delete
* `render_block`, this does not evaluate a block's `render_callback`, and will
[1375] Fix | Delete
* instead preserve the markup as parsed.
[1376] Fix | Delete
*
[1377] Fix | Delete
* @since 5.3.1
[1378] Fix | Delete
*
[1379] Fix | Delete
* @param array $block {
[1380] Fix | Delete
* A representative array of a single parsed block object. See WP_Block_Parser_Block.
[1381] Fix | Delete
*
[1382] Fix | Delete
* @type string $blockName Name of block.
[1383] Fix | Delete
* @type array $attrs Attributes from block comment delimiters.
[1384] Fix | Delete
* @type array[] $innerBlocks List of inner blocks. An array of arrays that
[1385] Fix | Delete
* have the same structure as this one.
[1386] Fix | Delete
* @type string $innerHTML HTML from inside block comment delimiters.
[1387] Fix | Delete
* @type array $innerContent List of string fragments and null markers where
[1388] Fix | Delete
* inner blocks were found.
[1389] Fix | Delete
* }
[1390] Fix | Delete
* @return string String of rendered HTML.
[1391] Fix | Delete
*/
[1392] Fix | Delete
function serialize_block( $block ) {
[1393] Fix | Delete
$block_content = '';
[1394] Fix | Delete
[1395] Fix | Delete
$index = 0;
[1396] Fix | Delete
foreach ( $block['innerContent'] as $chunk ) {
[1397] Fix | Delete
$block_content .= is_string( $chunk ) ? $chunk : serialize_block( $block['innerBlocks'][ $index++ ] );
[1398] Fix | Delete
}
[1399] Fix | Delete
[1400] Fix | Delete
if ( ! is_array( $block['attrs'] ) ) {
[1401] Fix | Delete
$block['attrs'] = array();
[1402] Fix | Delete
}
[1403] Fix | Delete
[1404] Fix | Delete
return get_comment_delimited_block_content(
[1405] Fix | Delete
$block['blockName'],
[1406] Fix | Delete
$block['attrs'],
[1407] Fix | Delete
$block_content
[1408] Fix | Delete
);
[1409] Fix | Delete
}
[1410] Fix | Delete
[1411] Fix | Delete
/**
[1412] Fix | Delete
* Returns a joined string of the aggregate serialization of the given
[1413] Fix | Delete
* parsed blocks.
[1414] Fix | Delete
*
[1415] Fix | Delete
* @since 5.3.1
[1416] Fix | Delete
*
[1417] Fix | Delete
* @param array[] $blocks {
[1418] Fix | Delete
* Array of block structures.
[1419] Fix | Delete
*
[1420] Fix | Delete
* @type array ...$0 {
[1421] Fix | Delete
* A representative array of a single parsed block object. See WP_Block_Parser_Block.
[1422] Fix | Delete
*
[1423] Fix | Delete
* @type string $blockName Name of block.
[1424] Fix | Delete
* @type array $attrs Attributes from block comment delimiters.
[1425] Fix | Delete
* @type array[] $innerBlocks List of inner blocks. An array of arrays that
[1426] Fix | Delete
* have the same structure as this one.
[1427] Fix | Delete
* @type string $innerHTML HTML from inside block comment delimiters.
[1428] Fix | Delete
* @type array $innerContent List of string fragments and null markers where
[1429] Fix | Delete
* inner blocks were found.
[1430] Fix | Delete
* }
[1431] Fix | Delete
* }
[1432] Fix | Delete
* @return string String of rendered HTML.
[1433] Fix | Delete
*/
[1434] Fix | Delete
function serialize_blocks( $blocks ) {
[1435] Fix | Delete
return implode( '', array_map( 'serialize_block', $blocks ) );
[1436] Fix | Delete
}
[1437] Fix | Delete
[1438] Fix | Delete
/**
[1439] Fix | Delete
* Traverses a parsed block tree and applies callbacks before and after serializing it.
[1440] Fix | Delete
*
[1441] Fix | Delete
* Recursively traverses the block and its inner blocks and applies the two callbacks provided as
[1442] Fix | Delete
* arguments, the first one before serializing the block, and the second one after serializing it.
[1443] Fix | Delete
* If either callback returns a string value, it will be prepended and appended to the serialized
[1444] Fix | Delete
* block markup, respectively.
[1445] Fix | Delete
*
[1446] Fix | Delete
* The callbacks will receive a reference to the current block as their first argument, so that they
[1447] Fix | Delete
* can also modify it, and the current block's parent block as second argument. Finally, the
[1448] Fix | Delete
* `$pre_callback` receives the previous block, whereas the `$post_callback` receives
[1449] Fix | Delete
* the next block as third argument.
[1450] Fix | Delete
*
[1451] Fix | Delete
* Serialized blocks are returned including comment delimiters, and with all attributes serialized.
[1452] Fix | Delete
*
[1453] Fix | Delete
* This function should be used when there is a need to modify the saved block, or to inject markup
[1454] Fix | Delete
* into the return value. Prefer `serialize_block` when preparing a block to be saved to post content.
[1455] Fix | Delete
*
[1456] Fix | Delete
* This function is meant for internal use only.
[1457] Fix | Delete
*
[1458] Fix | Delete
* @since 6.4.0
[1459] Fix | Delete
* @access private
[1460] Fix | Delete
*
[1461] Fix | Delete
* @see serialize_block()
[1462] Fix | Delete
*
[1463] Fix | Delete
* @param array $block A representative array of a single parsed block object. See WP_Block_Parser_Block.
[1464] Fix | Delete
* @param callable $pre_callback Callback to run on each block in the tree before it is traversed and serialized.
[1465] Fix | Delete
* It is called with the following arguments: &$block, $parent_block, $previous_block.
[1466] Fix | Delete
* Its string return value will be prepended to the serialized block markup.
[1467] Fix | Delete
* @param callable $post_callback Callback to run on each block in the tree after it is traversed and serialized.
[1468] Fix | Delete
* It is called with the following arguments: &$block, $parent_block, $next_block.
[1469] Fix | Delete
* Its string return value will be appended to the serialized block markup.
[1470] Fix | Delete
* @return string Serialized block markup.
[1471] Fix | Delete
*/
[1472] Fix | Delete
function traverse_and_serialize_block( $block, $pre_callback = null, $post_callback = null ) {
[1473] Fix | Delete
$block_content = '';
[1474] Fix | Delete
$block_index = 0;
[1475] Fix | Delete
[1476] Fix | Delete
foreach ( $block['innerContent'] as $chunk ) {
[1477] Fix | Delete
if ( is_string( $chunk ) ) {
[1478] Fix | Delete
$block_content .= $chunk;
[1479] Fix | Delete
} else {
[1480] Fix | Delete
$inner_block = $block['innerBlocks'][ $block_index ];
[1481] Fix | Delete
[1482] Fix | Delete
if ( is_callable( $pre_callback ) ) {
[1483] Fix | Delete
$prev = 0 === $block_index
[1484] Fix | Delete
? null
[1485] Fix | Delete
: $block['innerBlocks'][ $block_index - 1 ];
[1486] Fix | Delete
[1487] Fix | Delete
$block_content .= call_user_func_array(
[1488] Fix | Delete
$pre_callback,
[1489] Fix | Delete
array( &$inner_block, &$block, $prev )
[1490] Fix | Delete
);
[1491] Fix | Delete
}
[1492] Fix | Delete
[1493] Fix | Delete
if ( is_callable( $post_callback ) ) {
[1494] Fix | Delete
$next = count( $block['innerBlocks'] ) - 1 === $block_index
[1495] Fix | Delete
? null
[1496] Fix | Delete
: $block['innerBlocks'][ $block_index + 1 ];
[1497] Fix | Delete
[1498] Fix | Delete
$post_markup = call_user_func_array(
[1499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function