diff --git a/better-core-video-embeds.php b/better-core-video-embeds.php index 7435519..32d82d7 100644 --- a/better-core-video-embeds.php +++ b/better-core-video-embeds.php @@ -4,7 +4,7 @@ Description: A plugin which enhances the core video embeds for Youtube and Vimeo videos by not loading unnecessary scripts until they are needed. Requires at least: 6.0 Requires PHP: 7.0 -Version: 1.3.6 +Version: 1.3.7 Author: Highrise Digital Author URI: https://highrise.digital/ License: GPL-2.0-or-later @@ -15,7 +15,7 @@ // define variable for path to this plugin file. define( 'HD_BCVE_LOCATION', dirname( __FILE__ ) ); define( 'HD_BCVE_LOCATION_URL', plugins_url( '', __FILE__ ) ); -define( 'HD_BCVE_VERSION', '1.3.6' ); +define( 'HD_BCVE_VERSION', '1.3.7' ); /** * Function to run on plugins load. @@ -33,24 +33,45 @@ function hd_bcve_plugins_loaded() { /** * Enqueues the frontend JS for the plugin. */ -function hd_bcve_enqueue_scripts() { +function hd_bcve_register_enqueue_scripts() { + + // register the script for the front end. + wp_register_script( + 'better-core-video-embeds-js', + HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.min.js', + false, + HD_BCVE_VERSION, + true + ); - // only if the page has a core embed block present. - if ( has_block( 'core/embed' ) ) { +} - // enqueue the front end script to invoking the video embed on image click. - wp_enqueue_script( - 'better-core-video-embeds-js', - HD_BCVE_LOCATION_URL . '/assets/js/better-core-video-embeds.min.js', - false, - HD_BCVE_VERSION, - true +add_action( 'init', 'hd_bcve_register_enqueue_scripts' ); + +/** + * Add the front end JS to the core/embed block metadata. + * + * @param array $metadata The block metadata. + * @return array $metadata The block metadata. + */ +function hd_bcve_filter_embed_metadata( $metadata ) { + + // if this is the core/embed block. + if ( 'core/embed' === $metadata['name'] ) { + + $metadata['viewScript'] = array_merge( + (array) ( $metadata['viewScript'] ?? array() ), + ['better-core-video-embeds-js'] ); } + + // return the metadata. + return $metadata; + } -add_action( 'wp_enqueue_scripts', 'hd_bcve_enqueue_scripts' ); +add_filter( 'block_type_metadata', 'hd_bcve_filter_embed_metadata' ); /** * Enqueues the block editor JS for the plugin. @@ -78,22 +99,25 @@ function hd_bcve_enqueue_block_editor_assets() { */ function hd_bcve_register_block_style() { - // only if the page has a core embed block present. - if ( has_block( 'core/embed' ) ) { - - // register the style for this block. - wp_enqueue_style( - 'better-core-video-embeds-styles', - HD_BCVE_LOCATION_URL . '/assets/css/better-core-video-embeds.min.css', - [], - HD_BCVE_VERSION - ); + // register the block styles. + wp_register_style( + 'better-core-video-embeds-styles', + HD_BCVE_LOCATION_URL . '/assets/css/better-core-video-embeds.min.css', + [], + HD_BCVE_VERSION + ); - } + // enqueue the registered block style. + wp_enqueue_block_style( + 'core/embed', + [ + 'handle' => 'better-core-video-embeds-styles', + ] + ); } -add_action( 'wp_enqueue_scripts', 'hd_bcve_register_block_style' ); +add_action( 'init', 'hd_bcve_register_block_style' ); /** * Filters the code embed block output for improved performance on Youtube videos. diff --git a/readme.txt b/readme.txt index 9d3fc57..aaf9f00 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://example.com/ Tags: embed, oembed, youtube, vimeo, performance, blocks Requires at least: 6.0 Tested up to: 6.5.4 -Stable tag: 1.3.6 +Stable tag: 1.3.7 Requires PHP: 8.0 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html @@ -49,6 +49,10 @@ Yes! As of version 1.3, you can set a custom thumbnail on the embed block and th == Changelog == += 1.3.7 (30/10/2024) = +* Ensures that better core embeds work even when they are included in a synced pattern. +* Improves the enqueuing of the JS and CSS to be more robust. + = 1.3.6 (12/06/2024) = * Improve the play button CSS to ensure it works with videos at aspect rations other than 16:9 and 4:3.