admin管理员组文章数量:1125044
First, I'm aware that my question is happening in the context of my work with the WooCommerce plugin, which would normally make it off-topic. However, I think my question relates to wp_enqueue_script
, so hopefully it is still on topic.
So WooCommerce is registering a script on the admin_enqueue_scripts
hook. This script requires a bunch of dependencies:
wp_register_script( 'wc-admin-meta-boxes', WC()->plugin_url() . '/assets/js/admin/meta-boxes' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'accounting', 'round', 'ajax-chosen', 'chosen', 'plupload-all' ), WC_VERSION );
(it is enqueued specifically on the post.php and post-new.php page for the product post type a little later on in the code)
In the custom plugin that I am writing to work with WooCommerce I am also loading a script on the same hook.
wp_enqueue_script( 'My_Plugin_Metabox', My_Plugin_Class()->plugin_url() . '/assets/js/mnm-write-panel.js', array( 'jquery', 'wc-admin-meta-boxes'), My_Plugin_Class()->version, true );
If I enqueue my plugin's script and set the $in_footer
parameter to true
then inexplicably, the jQuery UI Datepicker script is not loaded (not in the source code at all) and the console shows corresponding script errors.
If I load my script in the header, this is not a problem. If I load my script without the wc-admin-meta-boxes
dependency, then that also resolves the problem
So what I am wondering is, why does loading my script in the footer effect the loading of the core datepicker script? (I'm not using datepicker in my script at all.) Or why not having the Woo script as a dependency would also effect the datepicker script? It seems to me that the datepicker script should be loaded no matter what as a dependency of the Woo metabox script, but this isn't happening.
Per Kaiser's comment, I created the following MU plugin (adjusted from comments because $GLOBALS['wp_scripts']
is an object:
/* Plugin Name: Dump jQUI Dp */
add_action( 'shutdown', 'so_dump_query_ui_dependencies' );
function so_dump_query_ui_dependencies() {
echo 'Does jQuery UI DatePicker script exist per default in…?<br>';
$s = 'jquery-ui-datepicker';
printf( 'The registered Dependencies Array: %s', isset( $GLOBALS['wp_scripts']->registered[ $s ] ) ? 'yep ' : 'nope ' );
printf( 'The Dependencies loaded in the footer: %s', isset( $GLOBALS['wp_scripts']->in_footer[ $s ] ) ? 'yep ' : 'nope ' );
printf( 'The Dependencies printed to the DOM: %s', isset( $GLOBALS['wp_scripts']->done[ $s ] ) ? 'yep ' : 'nope ' );
echo 'All nope? Well, then…';
}
With only WooCommerce 2.2.8 active, the result reads:
The registered Dependencies Array: yep
The Dependencies loaded in the footer: nope
The Dependencies printed to the DOM: nope
With WooCommerce 2.2.8 plus my new "dummy" plugin the result reads the same (whether my script is loaded in the footer or not):
The registered Dependencies Array: yep
The Dependencies loaded in the footer: nope
The Dependencies printed to the DOM: nope
Dummy Plugin
Also per the comments, here's a dummy plugin to hopefully reproduce the issue for others. I stripped my existing plugin all the way down to only load a script on the product post type admin pages. I am still seeing datepicker load when $in_footer
is false and not load when $in_footer
is true.
<?php
/*
Plugin Name: WooCommerce Dummy Plugin
Plugin URI:
Author: helgatheviking
Description: Enqueue a script, miraculously dequeue datepicker
*/
/**
* The Main My_Dummy_Plugin class
**/
if ( ! class_exists( 'My_Dummy_Plugin' ) ) :
class My_Dummy_Plugin {
/**
* @var My_Dummy_Plugin - the single instance of the class
*/
protected static $_instance = null;
/**
* variables
*/
public $version = '1.0.0';
/**
* Main My_Dummy_Plugin instance.
*
* Ensures only one instance of My_Dummy_Plugin is loaded or can be loaded
*
* @static
* @return My_Dummy_Plugin - Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Cloning is forbidden.
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ) );
}
/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ) );
}
/**
* My_Dummy_Plugin Constructor
*
* @access public
* @return My_Dummy_Plugin
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
/**
* Get the plugin url.
*
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
*
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/*-----------------------------------------------------------------------------------*/
/* Load scripts */
/*-----------------------------------------------------------------------------------*/
public function admin_scripts() {
// Get admin screen id
$screen = get_current_screen();
// Product post type page only
if ( in_array( $screen->id, array( 'product' ) ) ) {
wp_enqueue_script( 'My_Dummy_Plugin_Metabox', $this->plugin_url() . '/assets/js/metabox.js', array( 'jquery', 'wc-admin-meta-boxes'), $this->version, true );
}
}
} //end class: do not remove or there will be no more guacamole for you
endif; // end class_exists check
/**
* Returns the main instance of My_Dummy_Plugin
*
* @return WooCommerce
*/
function My_Dummy_Plugin() {
return My_Dummy_Plugin::instance();
}
// Launch the whole plugin
My_Dummy_Plugin();
First, I'm aware that my question is happening in the context of my work with the WooCommerce plugin, which would normally make it off-topic. However, I think my question relates to wp_enqueue_script
, so hopefully it is still on topic.
So WooCommerce is registering a script on the admin_enqueue_scripts
hook. This script requires a bunch of dependencies:
wp_register_script( 'wc-admin-meta-boxes', WC()->plugin_url() . '/assets/js/admin/meta-boxes' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable', 'accounting', 'round', 'ajax-chosen', 'chosen', 'plupload-all' ), WC_VERSION );
(it is enqueued specifically on the post.php and post-new.php page for the product post type a little later on in the code)
In the custom plugin that I am writing to work with WooCommerce I am also loading a script on the same hook.
wp_enqueue_script( 'My_Plugin_Metabox', My_Plugin_Class()->plugin_url() . '/assets/js/mnm-write-panel.js', array( 'jquery', 'wc-admin-meta-boxes'), My_Plugin_Class()->version, true );
If I enqueue my plugin's script and set the $in_footer
parameter to true
then inexplicably, the jQuery UI Datepicker script is not loaded (not in the source code at all) and the console shows corresponding script errors.
If I load my script in the header, this is not a problem. If I load my script without the wc-admin-meta-boxes
dependency, then that also resolves the problem
So what I am wondering is, why does loading my script in the footer effect the loading of the core datepicker script? (I'm not using datepicker in my script at all.) Or why not having the Woo script as a dependency would also effect the datepicker script? It seems to me that the datepicker script should be loaded no matter what as a dependency of the Woo metabox script, but this isn't happening.
Per Kaiser's comment, I created the following MU plugin (adjusted from comments because $GLOBALS['wp_scripts']
is an object:
/* Plugin Name: Dump jQUI Dp */
add_action( 'shutdown', 'so_dump_query_ui_dependencies' );
function so_dump_query_ui_dependencies() {
echo 'Does jQuery UI DatePicker script exist per default in…?<br>';
$s = 'jquery-ui-datepicker';
printf( 'The registered Dependencies Array: %s', isset( $GLOBALS['wp_scripts']->registered[ $s ] ) ? 'yep ' : 'nope ' );
printf( 'The Dependencies loaded in the footer: %s', isset( $GLOBALS['wp_scripts']->in_footer[ $s ] ) ? 'yep ' : 'nope ' );
printf( 'The Dependencies printed to the DOM: %s', isset( $GLOBALS['wp_scripts']->done[ $s ] ) ? 'yep ' : 'nope ' );
echo 'All nope? Well, then…';
}
With only WooCommerce 2.2.8 active, the result reads:
The registered Dependencies Array: yep
The Dependencies loaded in the footer: nope
The Dependencies printed to the DOM: nope
With WooCommerce 2.2.8 plus my new "dummy" plugin the result reads the same (whether my script is loaded in the footer or not):
The registered Dependencies Array: yep
The Dependencies loaded in the footer: nope
The Dependencies printed to the DOM: nope
Dummy Plugin
Also per the comments, here's a dummy plugin to hopefully reproduce the issue for others. I stripped my existing plugin all the way down to only load a script on the product post type admin pages. I am still seeing datepicker load when $in_footer
is false and not load when $in_footer
is true.
<?php
/*
Plugin Name: WooCommerce Dummy Plugin
Plugin URI: http://wordpress.stackexchange.com/q/168688/6477
Author: helgatheviking
Description: Enqueue a script, miraculously dequeue datepicker
*/
/**
* The Main My_Dummy_Plugin class
**/
if ( ! class_exists( 'My_Dummy_Plugin' ) ) :
class My_Dummy_Plugin {
/**
* @var My_Dummy_Plugin - the single instance of the class
*/
protected static $_instance = null;
/**
* variables
*/
public $version = '1.0.0';
/**
* Main My_Dummy_Plugin instance.
*
* Ensures only one instance of My_Dummy_Plugin is loaded or can be loaded
*
* @static
* @return My_Dummy_Plugin - Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Cloning is forbidden.
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ) );
}
/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?' ) );
}
/**
* My_Dummy_Plugin Constructor
*
* @access public
* @return My_Dummy_Plugin
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
/**
* Get the plugin url.
*
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
*
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/*-----------------------------------------------------------------------------------*/
/* Load scripts */
/*-----------------------------------------------------------------------------------*/
public function admin_scripts() {
// Get admin screen id
$screen = get_current_screen();
// Product post type page only
if ( in_array( $screen->id, array( 'product' ) ) ) {
wp_enqueue_script( 'My_Dummy_Plugin_Metabox', $this->plugin_url() . '/assets/js/metabox.js', array( 'jquery', 'wc-admin-meta-boxes'), $this->version, true );
}
}
} //end class: do not remove or there will be no more guacamole for you
endif; // end class_exists check
/**
* Returns the main instance of My_Dummy_Plugin
*
* @return WooCommerce
*/
function My_Dummy_Plugin() {
return My_Dummy_Plugin::instance();
}
// Launch the whole plugin
My_Dummy_Plugin();
Share
Improve this question
edited Nov 17, 2014 at 20:59
helgatheviking
asked Nov 17, 2014 at 19:08
helgathevikinghelgatheviking
14.5k8 gold badges64 silver badges115 bronze badges
6
|
Show 1 more comment
2 Answers
Reset to default 2Currently you can force a loading for the Libraries using wp_enqueue_script(), like so:
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui');
I know it shoud load automatically, but it works that way.
According to the comments, what you are describing is bug #25247 in WordPress. This bug report was filed in 2013, and it was marked as fixed in 2016. It years old now, just like this question.
本文标签: Loading a script with a dependencyis unloading another script39s dependency
版权声明:本文标题:Loading a script with a dependency, is unloading another script's dependency 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736651917a1946159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$in_footer
is true on my own script. – helgatheviking Commented Nov 18, 2014 at 8:39do_items
in "wp-includes/class.wp-dependencies.php", at lines 122-125, the code just unsets the item in the to_do list whether or notdo_item
succeeds. If you change those lines toif ( $this->do_item( $handle, $group ) ) { $this->done[] = $handle; unset( $this->to_do[$key] ); }
then the bug goes away... – bonger Commented Nov 18, 2014 at 17:16