| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! function_exists('get_comment_delimited_block_content')) { |
| 4 |
/** |
| 5 |
* Returns the content of a block, including comment delimiters. |
| 6 |
* |
| 7 |
* @since 5.3.1 |
| 8 |
* |
| 9 |
* @param string|null $block_name Block name. Null if the block name is unknown, |
| 10 |
* e.g. Classic blocks have their name set to null. |
| 11 |
* @param array $block_attributes Block attributes. |
| 12 |
* @param string $block_content Block save content. |
| 13 |
* |
| 14 |
* @return string Comment-delimited block content. |
| 15 |
*/ |
| 16 |
function get_comment_delimited_block_content($block_name, $block_attributes, $block_content) |
| 17 |
{ |
| 18 |
if (is_null($block_name)) { |
| 19 |
return $block_content; |
| 20 |
} |
| 21 |
|
| 22 |
$serialized_block_name = strip_core_block_namespace($block_name); |
| 23 |
$serialized_attributes = empty($block_attributes) ? '' : serialize_block_attributes($block_attributes) . ' '; |
| 24 |
|
| 25 |
if (empty($block_content)) { |
| 26 |
return sprintf('<!-- wp:%s %s/-->', $serialized_block_name, $serialized_attributes); |
| 27 |
} |
| 28 |
|
| 29 |
return sprintf( |
| 30 |
'<!-- wp:%s %s-->%s<!-- /wp:%s -->', |
| 31 |
$serialized_block_name, |
| 32 |
$serialized_attributes, |
| 33 |
$block_content, |
| 34 |
$serialized_block_name |
| 35 |
); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! function_exists('strip_core_block_namespace')) { |
| 40 |
/** |
| 41 |
* Returns the block name to use for serialization. This will remove the default |
| 42 |
* "core/" namespace from a block name. |
| 43 |
* |
| 44 |
* @since 5.3.1 |
| 45 |
* |
| 46 |
* @param string $block_name Original block name. |
| 47 |
* |
| 48 |
* @return string Block name to use for serialization. |
| 49 |
*/ |
| 50 |
function strip_core_block_namespace($block_name = null) |
| 51 |
{ |
| 52 |
if (is_string($block_name) && 0 === strpos($block_name, 'core/')) { |
| 53 |
return substr($block_name, 5); |
| 54 |
} |
| 55 |
|
| 56 |
return $block_name; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! function_exists('serialize_block_attributes')) { |
| 61 |
/** |
| 62 |
* Given an array of attributes, returns a string in the serialized attributes |
| 63 |
* format prepared for post content. |
| 64 |
* |
| 65 |
* The serialized result is a JSON-encoded string, with unicode escape sequence |
| 66 |
* substitution for characters which might otherwise interfere with embedding |
| 67 |
* the result in an HTML comment. |
| 68 |
* |
| 69 |
* @since 5.3.1 |
| 70 |
* |
| 71 |
* @param array $block_attributes Attributes object. |
| 72 |
* |
| 73 |
* @return string Serialized attributes. |
| 74 |
*/ |
| 75 |
function serialize_block_attributes($block_attributes) |
| 76 |
{ |
| 77 |
$encoded_attributes = json_encode($block_attributes); |
| 78 |
$encoded_attributes = preg_replace('/--/', '\\u002d\\u002d', $encoded_attributes); |
| 79 |
$encoded_attributes = preg_replace('/</', '\\u003c', $encoded_attributes); |
| 80 |
$encoded_attributes = preg_replace('/>/', '\\u003e', $encoded_attributes); |
| 81 |
$encoded_attributes = preg_replace('/&/', '\\u0026', $encoded_attributes); |
| 82 |
// Regex: /\\"/ |
| 83 |
$encoded_attributes = preg_replace('/\\\\"/', '\\u0022', $encoded_attributes); |
| 84 |
|
| 85 |
return $encoded_attributes; |
| 86 |
} |
| 87 |
} |
| 88 |
|