Opened 13 months ago

Last modified 13 months ago

#20513 new enhancement

Refactor $wp_scripts init checking code

Reported by: GaryJ Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version: 3.4
Severity: normal Keywords: has-patch needs-testing
Cc: ocean90

Description

The functions in functions.wp-scripts.php seem to contain a lot of duplicate code in terms of checking and initializing the global $wp_scripts in each function.

Attached is rough first pass at creating a function to do this. I expect aspects of it could be done better.

I've included the first argument, to avoid relying on a global so that the function might be unit tested (pass something in, see what gets returned).

Attachments (3)

20513.diff (5.5 KB) - added by GaryJ 13 months ago.
First pass
20513.2.diff (5.8 KB) - added by scribu 13 months ago.
Second pass
20513.3.diff (5.7 KB) - added by scribu 13 months ago.
wp_scripts()

Download all attachments as: .zip

Change History (14)

GaryJ13 months ago

First pass

+1 in general. Note that __FUNCTION__ would have to be passed along.

Second pass

20513.2.diff:

  • accept first parameter by reference, instead of returning
  • replaced $fallback parameter with $function

Not really sure if the extra check in wp_localize_script() is worth it. Could just instantiate anyway.

Last edited 13 months ago by scribu (previous) (diff)

wp_scripts()

20513.3.diff replaces wp_scripts_maybe_initialize() with wp_scripts(), so you can do:

wp_scripts()->do_scripts( $handle );

etc.

Version 0, edited 13 months ago by scribu (next)

I don't think this init code is relevant for unit testing. If you want to unit test WP_Scripts, just create an instance manually.

Last edited 13 months ago by scribu (previous) (diff)

That looks good to me.

Might be another ticket, but I suspect something similar can be done with $wp_styles too.

  • Cc ocean90 added

Think I attempted to do something similar for 3.2 (or was it 3.3). The argument against it is that this breaks the backtrace so if a plugin author calls any of these functions too early _doing_it_wrong() would show the wrong reference.

If the function reference is so important, we could pass it along:

wp_scripts( __FUNCTION__ )->do_items( $handle );

comment:10 follow-up: ↓ 11   GaryJ13 months ago

Related discussion: #11526.

How expensive is debug_backtrace( false )?

How many themes and plugins are still firing these functions before init action has occurred? (Nacin's relatively recent post might have reminded some of the authors to be using the wp_enqueue_scripts hook.)

Could wp_debug_backtrace_summary() be used to pinpoint the original function called?

While scribu's suggestion may well work, it seems dirty to be including an argument purely for debugging purposes.

comment:11 in reply to: ↑ 10   SergeyBiryukov13 months ago

Replying to GaryJ:

How many themes and plugins are still firing these functions before init action has occurred?

Plugins: http://pastebin.com/szGUhsp0 (may have some false positives due to a lack of whitespace).
Themes: http://pastebin.com/bbM47tG8 (tried to filter out false positives).

Note: See TracTickets for help on using tickets.