fonts-api
2 years ago
interactivity-api
2 years ago
block-editor-settings-mobile.php
2 years ago
block-editor-settings.php
2 years ago
blocks.php
2 years ago
class-gutenberg-rest-global-styles-revisions-controller.php
2 years ago
class-wp-classic-to-block-menu-converter.php
2 years ago
class-wp-navigation-fallback-gutenberg.php
2 years ago
class-wp-rest-block-editor-settings-controller.php
2 years ago
class-wp-rest-customizer-nonces.php
2 years ago
class-wp-rest-navigation-fallback-controller.php
2 years ago
editor-settings.php
2 years ago
kses.php
2 years ago
l10n.php
2 years ago
navigation-fallback.php
2 years ago
navigation-theme-opt-in.php
2 years ago
rest-api.php
2 years ago
blocks.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Temporary compatibility shims for block APIs present in Gutenberg. |
| 4 | * |
| 5 | * @package gutenberg |
| 6 | */ |
| 7 | |
| 8 | if ( ! function_exists( 'wp_enqueue_block_view_script' ) ) { |
| 9 | /** |
| 10 | * Enqueues a frontend script for a specific block. |
| 11 | * |
| 12 | * Scripts enqueued using this function will only get printed |
| 13 | * when the block gets rendered on the frontend. |
| 14 | * |
| 15 | * @since 6.2.0 |
| 16 | * |
| 17 | * @param string $block_name The block name, including namespace. |
| 18 | * @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. |
| 19 | * |
| 20 | * @return void |
| 21 | */ |
| 22 | function wp_enqueue_block_view_script( $block_name, $args ) { |
| 23 | $args = wp_parse_args( |
| 24 | $args, |
| 25 | array( |
| 26 | 'handle' => '', |
| 27 | 'src' => '', |
| 28 | 'deps' => array(), |
| 29 | 'ver' => false, |
| 30 | 'in_footer' => false, |
| 31 | |
| 32 | // Additional args to allow translations for the script's textdomain. |
| 33 | 'textdomain' => '', |
| 34 | ) |
| 35 | ); |
| 36 | |
| 37 | /** |
| 38 | * Callback function to register and enqueue scripts. |
| 39 | * |
| 40 | * @param string $content When the callback is used for the render_block filter, |
| 41 | * the content needs to be returned so the function parameter |
| 42 | * is to ensure the content exists. |
| 43 | * @return string Block content. |
| 44 | */ |
| 45 | $callback = static function( $content, $block ) use ( $args, $block_name ) { |
| 46 | |
| 47 | // Sanity check. |
| 48 | if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { |
| 49 | return $content; |
| 50 | } |
| 51 | |
| 52 | // Register the stylesheet. |
| 53 | if ( ! empty( $args['src'] ) ) { |
| 54 | wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); |
| 55 | } |
| 56 | |
| 57 | // Enqueue the stylesheet. |
| 58 | wp_enqueue_script( $args['handle'] ); |
| 59 | |
| 60 | // If a textdomain is defined, use it to set the script translations. |
| 61 | if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { |
| 62 | wp_set_script_translations( $args['handle'], $args['textdomain'], $args['domainpath'] ); |
| 63 | } |
| 64 | |
| 65 | return $content; |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * The filter's callback here is an anonymous function because |
| 70 | * using a named function in this case is not possible. |
| 71 | * |
| 72 | * The function cannot be unhooked, however, users are still able |
| 73 | * to dequeue the script registered/enqueued by the callback |
| 74 | * which is why in this case, using an anonymous function |
| 75 | * was deemed acceptable. |
| 76 | */ |
| 77 | add_filter( 'render_block', $callback, 10, 2 ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Registers the metadata block attribute for block types. |
| 84 | * |
| 85 | * @param array $args Array of arguments for registering a block type. |
| 86 | * @return array $args |
| 87 | */ |
| 88 | function gutenberg_register_metadata_attribute( $args ) { |
| 89 | // Setup attributes if needed. |
| 90 | if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { |
| 91 | $args['attributes'] = array(); |
| 92 | } |
| 93 | |
| 94 | if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) { |
| 95 | $args['attributes']['metadata'] = array( |
| 96 | 'type' => 'object', |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return $args; |
| 101 | } |
| 102 | add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); |
| 103 |