xwp / unsplash-wp

GNU General Public License v2.0
9 stars 3 forks source link

Missing dependency #214

Open pdewouters opened 4 years ago

pdewouters commented 4 years ago

Bug Description

On the front end, the unsplash-media-selector is added as a dependency to unsplash-featured-image-selector but it's not been registered or enqueued in that scope.

https://github.com/xwp/unsplash-wp/blob/develop/php/class-plugin.php#L222

Expected Behaviour

Steps to reproduce

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Screenshots

Screenshot 2020-09-19 at 11 36 34

Additional context


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation brief

QA testing instructions

Demo

Changelog entry

derekherman commented 4 years ago

Thank you for reporting the issue @pdewouters someone will look into it.

douglas-johnson commented 3 years ago

I was receiving this same warning. I added this to my theme code to alleviate it.

/**
 * Remove Unsplash Block Editor Script
 * 
 * "unsplash-featured-image-selector", which is enqueued in the front-end,
 * depends on the file "unsplash-media-selector", which is not.
 * 
 * We can check if "unsplash-media-selector" is registered before letting the dependent script through.
 */
function themename_remove_unsplash_block_editor_script() : void {
  if ( wp_script_is( 'unsplash-media-selector', 'registered' ) ) {
    return;
  }
  wp_dequeue_script( 'unsplash-featured-image-selector' );
}
add_action( 'enqueue_block_assets', 'themename_remove_unsplash_block_editor_script', 200 );

It seems like the problem is here: https://github.com/xwp/unsplash-wp/blob/develop/php/class-plugin.php#L210

The action enqueue_block_assets is called in both front-end and admin contexts. So unless unsplash-featured-image-selector is actually needed in the front-end, adding a check against $current_screen->is_block_editor() should fix it. https://core.trac.wordpress.org/browser/tags/5.5.1/src/wp-includes/script-loader.php#L2162

I test this locally by editing the plugin in place and it worked.

/**
 * Enqueue block editor assets.
 *
 * @global WP_Screen $current_screen
 */
public function enqueue_block_assets() {
  global $current_screen;

  if ( ! is_admin() ) {
    return;
  }

  if ( ! $current_screen instanceof WP_Screen ) {
    return;
  }

  if ( ! $current_screen->is_block_editor() ) {
    return;
  }