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