diff --git a/assets/src/media-selector/featured-image-selector.js b/assets/src/media-selector/featured-image-selector.js index 5a8422d2..185a1027 100644 --- a/assets/src/media-selector/featured-image-selector.js +++ b/assets/src/media-selector/featured-image-selector.js @@ -17,5 +17,5 @@ addFilter( 'editor.MediaUpload', 'unsplash/extend-featured-image', () => UnsplashMediaUpload, - 20 + 10 ); diff --git a/php/class-plugin.php b/php/class-plugin.php index 24167889..e72136a3 100644 --- a/php/class-plugin.php +++ b/php/class-plugin.php @@ -185,29 +185,6 @@ public function enqueue_media_scripts() { ] ); - /* - * If the block editor is available, the featured image selector in the editor will need to be overridden. This - * is an extension of the media selector enqueued above and is separated from it because the required dependencies - * are not available in WP < 5.0. It would not make sense to polyfill these dependencies anyways since the block - * editor is not officially compatible with WP < 5.0. - */ - if ( has_action( 'enqueue_block_assets' ) ) { - $asset_file = $this->dir_path . '/assets/js/featured-image-selector.asset.php'; - $asset = is_readable( $asset_file ) ? require $asset_file : []; - $version = isset( $asset['version'] ) ? $asset['version'] : $this->asset_version(); - - $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : []; - $dependencies[] = 'unsplash-media-selector'; - - wp_enqueue_script( - 'unsplash-featured-image-selector', - $this->asset_url( 'assets/js/featured-image-selector.js' ), - $dependencies, - $version, - true - ); - } - // Enqueue media selector CSS. wp_enqueue_style( 'unsplash-media-selector-style', @@ -221,6 +198,34 @@ public function enqueue_media_scripts() { return true; } + /** + * Enqueue block editor assets. + * + * @action enqueue_block_assets, 100 + */ + public function enqueue_block_assets() { + /* + * If the block editor is available, the featured image selector in the editor will need to be overridden. This + * is an extension of the media selector enqueued above and is separated from it because the required dependencies + * are not available in WP < 5.0. It would not make sense to polyfill these dependencies anyways since the block + * editor is not officially compatible with WP < 5.0. + */ + $asset_file = $this->dir_path . '/assets/js/featured-image-selector.asset.php'; + $asset = is_readable( $asset_file ) ? require $asset_file : []; + $version = isset( $asset['version'] ) ? $asset['version'] : $this->asset_version(); + + $dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : []; + $dependencies[] = 'unsplash-media-selector'; + + wp_enqueue_script( + 'unsplash-featured-image-selector', + $this->asset_url( 'assets/js/featured-image-selector.js' ), + $dependencies, + $version, + true + ); + } + /** * Load our admin assets. * diff --git a/tests/phpunit/php/class-test-plugin.php b/tests/phpunit/php/class-test-plugin.php index b69ad85a..31004023 100644 --- a/tests/phpunit/php/class-test-plugin.php +++ b/tests/phpunit/php/class-test-plugin.php @@ -167,9 +167,20 @@ public function test_enqueue_media_scripts() { $plugin = get_plugin_instance(); $plugin->enqueue_media_scripts(); $this->assertTrue( wp_script_is( 'unsplash-media-selector', 'enqueued' ) ); + } + + /** + * Test for enqueue_block_assets() method. + * + * @see Plugin::enqueue_block_assets() + */ + public function test_enqueue_block_assets() { + wp_set_current_user( self::$admin_id ); + set_current_screen( 'post.php' ); + $plugin = get_plugin_instance(); + $plugin->enqueue_block_assets(); - $featured_image_script_loads = version_compare( '5.0', get_bloginfo( 'version' ), '<=' ); - $this->assertEquals( $featured_image_script_loads, wp_script_is( 'unsplash-featured-image-selector', 'enqueued' ) ); + $this->assertTrue( wp_script_is( 'unsplash-featured-image-selector', 'enqueued' ) ); } /**