afragen / wp-dependency-installer

A lightweight class to add to WordPress plugins/themes to automatically install plugin dependencies.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Uncaught SyntaxError: Unexpected token <" when using the installer in a theme or child theme

JiveDig opened this issue · comments

Using 1.4.11 (also happens with 1.4.10) I see this error.
Screenshot 2019-03-11 11 42 53

When I comment out this out, the error goes away...

include_once( __DIR__ . '/vendor/autoload.php' );
WP_Dependency_Installer::instance()->run( __DIR__ );

It seems the script is still working correctly but I can't track down where this is coming from. I'm running Valet and testing all this locally, not sure if that is a factor. Wondering if you have any ideas.

EDIT: This is in the admin only.

Sorry, I have no idea. I’ve never seem a similar error before.

Maybe this helps?
https://stackoverflow.com/questions/3143698/uncaught-syntaxerror-unexpected-token

Interesting. Commenting this line out take away the error too:

add_action( 'admin_init', array( 'PAnD', 'init' ) );

In the PAnD class it tries to enqueue this, and commenting it out fixes the issue.

wp_enqueue_script(
	'dismissible-notices',
	plugins_url( 'dismiss-notice.js', __FILE__ ),
	array( 'jquery', 'common' ),
	false,
	true
);

So that's consistent with the comments in the SO article and other things I've read. Something is having an issue calling that file I guess.

This is false:
var_dump( file_exists( plugins_url( 'dismiss-notice.js', __FILE__ ) ) );

It's gotta be something with my local setup.

This outputs https://merlin.test/wp-content/plugins/Users/JiveDig/ValetSites/merlin/wp-content/themes/mai-business-theme/vendor/collizo4sky/persist-admin-notices-dismissal/persist-admin-notices-dismissal.php

var_dump( plugins_url( __FILE__ ) );

While this outputs https://merlin.test/wp-content/plugins/Users/JiveDig/ValetSites/merlin/wp-content/themes/mai-business-theme/vendor/collizo4sky/persist-admin-notices-dismissal/

var_dump( plugin_dir_url( __FILE__ ) );

Neither are correct and the main issue (I think) is that the script is in my theme, not a plugin so both are returning the wrong path/url to the actual file.

Interesting. Was this installed via composer?

Obviously not a real fix, but if I use:

get_stylesheet_directory_uri() . '/vendor/collizo4sky/persist-admin-notices-dismissal/dismiss-notice.js',

in my child theme, it fixes the issue and correctly loads the file.

I think I see the issue. I don’t think we take into account using PaND in a theme.

I’ll see what I can come up with as a solution.

Yeah probably not the typical usage, but since the dependency installer can be used in a theme it would make sense for this to as well. I don't mind defining some sort of parameter for base_url or something either.

I’ll see if I can reliably figure out how to determine it it’s being used in a plugin or theme and if not set up a filter.

If/when the above PR is committed make sure you do a composer update and then add the following hook to your functions.php

add_filter( 'pand_theme_loader', '__return_true' );

@JiveDig there is also a filter hook to return any URL you want for where to look for the JS. The hook referenced above calls this hook internally and returns what you have indicated above.

Sorry took so long to circle back, but this totally worked for me. Thanks so much Andy! 🙌