Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#31636 closed defect (bug) (fixed)

4.2-beta1 - PHP warning when enqueuing script on specific pages only

Reported by: harmr's profile harmr Owned by: ocean90's profile ocean90
Milestone: 4.2 Priority: normal
Severity: normal Version: 4.2
Component: Script Loader Keywords: has-patch
Focuses: Cc:

Description

Within my plugin I use the following code to enqueue scripts only on specific pages:

[...]
$page3 = add_submenu_page('leafletmapsmarker_markers', $menu_name . ' - ' . __('add/edit marker', 'lmm'), '<img src="' . LEAFLET_PLUGIN_URL_ENC . 'inc/img/icon-menu-add' . $mp6_icon . '.png"> ' . __('Add new marker', 'lmm'), $lmm_options[ 'capabilities_edit' ], 'leafletmapsmarker_marker', array(&$this, 'lmm_marker') );
[...]
add_action('admin_print_scripts-'.$page3, array(&$this, 'lmm_admin_enqueue_scripts_jquerydatepicker'));
[...]
function lmm_admin_enqueue_scripts_jquerydatepicker() {
    $plugin_version = get_option('leafletmapsmarker_version_pro');
    wp_enqueue_script( array ( 'jquery', 'jquery-ui-tabs','jquery-ui-datepicker','jquery-ui-slider' ) );
    wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL_ENC . 'inc/js/jquery-ui-timepicker-addon.js', array('jquery', 'jquery-ui-tabs','jquery-ui-datepicker'), $plugin_version);
}
[...]

Since 4.2 I am getting the following PHP warning when accessing $page3 (=create new marker page):

WARNING: wp-includes/functions.wp-scripts.php:227 - explode() expects parameter 2 to be string, array given
require_once('wp-admin/admin-header.php'), do_action('admin_print_scripts-maps-marker-pro_page_leafletmapsmarker_marker'), call_user_func_array, l4->lmm_admin_enqueue_scripts_jquerydatepicker, wp_enqueue_script, explode

I looked into the changed function.wp-scripts.php file to find out whats wrong, but did not succeed:

function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    $wp_scripts = wp_scripts();

    _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

    $_handle = explode( '?', $handle );

    if ( $src ) {
        $wp_scripts->add( $_handle[0], $src, $deps, $ver );
    }

    if ( $in_footer ) {
        $wp_scripts->add_data( $_handle[0], 'group', 1 );
    }

    $wp_scripts->enqueue( $handle );
}

The line

$_handle = explode( '?', $handle );

is responsible for creating this PHP warning.
Unfortunately I do not have an idea how to prevent this within my code.
Could a solution be to change that code into

$_handle = !is_array($_handle) : explode( '?', $handle ) ? $handle;

As I try not to generate any php error log entries with my plugin, any help here would be really appreciated!

Attachments (4)

31636.patch (507 bytes) - added by SergeyBiryukov 10 years ago.
31636.2.patch (541 bytes) - added by SergeyBiryukov 10 years ago.
31636.3.patch (1.1 KB) - added by ocean90 10 years ago.
31636.4.patch (780 bytes) - added by ocean90 10 years ago.

Download all attachments as: .zip

Change History (15)

#1 @ocean90
10 years ago

It's wp_enqueue_script( array ( 'jquery', 'jquery-ui-tabs','jquery-ui-datepicker','jquery-ui-slider' ) ); which is a "hidden feature" because WP_Dependencies->enqueue() supports an array. You should use script dependencies for this.

In 4.1:

// … init stuff …
if ( $src ) {
	$_handle = explode('?', $handle);
	$wp_scripts->add( $_handle[0], $src, $deps, $ver );
	if ( $in_footer )
		$wp_scripts->add_data( $_handle[0], 'group', 1 );
}
$wp_scripts->enqueue( $handle );

#2 @SergeyBiryukov
10 years ago

wp_enqueue_script( array ( 'jquery', 'jquery-ui-tabs','jquery-ui-datepicker','jquery-ui-slider' ) );
wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL_ENC . 'inc/js/jquery-ui-timepicker-addon.js', array('jquery', 'jquery-ui-tabs','jquery-ui-datepicker'), $plugin_version);

The first line is redundant, you should add 'jquery-ui-slider' to your second line, and that would be enough:

wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL_ENC . 'inc/js/jquery-ui-timepicker-addon.js', array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-datepicker', 'jquery-ui-slider' ), $plugin_version );

#3 @jorbin
10 years ago

related: r31028 and #14488

If wp_enqueue_script allowed an array before, we shouldn't change that. We should throw a doing_it_wrong if you pass in an array and handle things accordingly.

#4 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 4.2

#5 follow-up: @harmr
10 years ago

Thx! I changed my function to

function lmm_admin_enqueue_scripts_jquerydatepicker() {
	$plugin_version = get_option('leafletmapsmarker_version');
	wp_enqueue_script( 'jquery-ui-timepicker-addon', LEAFLET_PLUGIN_URL . 'inc/js/jquery-ui-timepicker-addon.js', array( 'jquery', 'jquery-ui-tabs', 'jquery-ui-datepicker', 'jquery-ui-slider' ), $plugin_version );
}

but that did not eliminate the warning. Then I tried to apply the patch, but I guess there is a bug in it - shouldnt it be

	if ( ! is_array( $handle ) ) { 
 		$_handle = explode( '?', $handle ); 
 	} 

instead of

	if ( ! is_array( $_handle ) ) { 
 		$_handle = explode( '?', $handle ); 
 	} 

(as this threw an error?)

Last edited 10 years ago by harmr (previous) (diff)

#6 in reply to: ↑ 5 @SergeyBiryukov
10 years ago

Replying to harmr:

Then I tried to apply the patch, but I guess there is a bug in it

Right, thanks.

@ocean90
10 years ago

#7 follow-up: @ocean90
10 years ago

  • Keywords has-patch added

I think we should simply revert the code as in comment:1. Makes it consistent with wp_enqueue_style().

#8 in reply to: ↑ 7 ; follow-up: @SergeyBiryukov
10 years ago

Replying to ocean90:

I think we should simply revert the code as in comment:1. Makes it consistent with wp_enqueue_style().

That would reintroduce #14488 though?

Looks like sorich87's patch there was more correct, should we reopen the ticket and commit that one instead?

#9 in reply to: ↑ 8 @ocean90
10 years ago

Replying to SergeyBiryukov:

That would reintroduce #14488 though?

Indeed. Seems like I have missed [31028] and not quite a fan of it. Anyway, I agree that 14488.diff:ticket:14488 is slightly better than 31636.2.patch.

This ticket was mentioned in Slack in #core by drew. View the logs.


10 years ago

@ocean90
10 years ago

#11 @ocean90
10 years ago

  • Owner set to ocean90
  • Resolution set to fixed
  • Status changed from new to closed

In 31887:

Avoid a PHP notice in wp_enqueue_script() if $handle is an array.

Calling wp_enqueue_script() with an array as the first argument is a "hidden feature" and should be avoided. Use dependencies instead.

props sorich87 for initial patch.
fixes #31636.
see #14488.

Note: See TracTickets for help on using tickets.