| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block directory functions. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( |
| 9 |
! has_action( 'admin_enqueue_scripts', 'enqueue_block_editor_assets_block_directory' ) |
| 10 |
) { |
| 11 |
/** |
| 12 |
* Function responsible for enqueuing the assets required |
| 13 |
* for the block directory functionality in the editor. |
| 14 |
* |
| 15 |
* This filter can be removed when plugin support requires WordPress 5.5.0+. |
| 16 |
* |
| 17 |
* @see https://core.trac.wordpress.org/ticket/50321 |
| 18 |
* @see https://core.trac.wordpress.org/changeset/48242 |
| 19 |
*/ |
| 20 |
function gutenberg_enqueue_block_editor_assets_block_directory() { |
| 21 |
wp_enqueue_script( 'wp-block-directory' ); |
| 22 |
wp_enqueue_style( 'wp-block-directory' ); |
| 23 |
} |
| 24 |
add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_block_editor_assets_block_directory' ); |
| 25 |
|
| 26 |
/** |
| 27 |
* Add data attribute of handle to all script tags output in the wp-admin. |
| 28 |
* |
| 29 |
* This filter can be removed when plugin support requires WordPress 5.5.0+. |
| 30 |
* |
| 31 |
* @see https://core.trac.wordpress.org/ticket/48654 |
| 32 |
* @see https://core.trac.wordpress.org/changeset/48295 |
| 33 |
* |
| 34 |
* @param string $tag The `<script>` tag for the enqueued script. |
| 35 |
* @param string $handle The script's registered handle. |
| 36 |
* @param string $esc_src The script's pre-escaped registered src. |
| 37 |
* |
| 38 |
* @return string Filtered script tag. |
| 39 |
*/ |
| 40 |
function gutenberg_change_script_tag( $tag, $handle, $esc_src ) { |
| 41 |
if ( ! is_admin() ) { |
| 42 |
return $tag; |
| 43 |
} |
| 44 |
|
| 45 |
$tag = str_replace( |
| 46 |
sprintf( "<script src='%s'></script>", $esc_src ), |
| 47 |
sprintf( "<script data-handle='%s' src='%s'></script>", esc_attr( $handle ), $esc_src ), |
| 48 |
$tag |
| 49 |
); |
| 50 |
|
| 51 |
return $tag; |
| 52 |
} |
| 53 |
add_filter( 'script_loader_tag', 'gutenberg_change_script_tag', 1, 3 ); |
| 54 |
} |
| 55 |
|