Skip to content

Commit

Permalink
Fix MediaUpload override issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ravichdev committed Jul 29, 2020
1 parent dee982a commit 146a5ab
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion assets/src/media-selector/featured-image-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ addFilter(
'editor.MediaUpload',
'unsplash/extend-featured-image',
() => UnsplashMediaUpload,
20
10
);
51 changes: 28 additions & 23 deletions php/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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.
*
Expand Down
15 changes: 13 additions & 2 deletions tests/phpunit/php/class-test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}

/**
Expand Down

0 comments on commit 146a5ab

Please sign in to comment.