Make WordPress Core

Opened 13 years ago

Closed 10 years ago

#20513 closed enhancement (fixed)

Refactor $wp_scripts init checking code

Reported by: garyj's profile GaryJ Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.4
Component: Script Loader Keywords: has-patch needs-testing
Focuses: Cc:

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 (4)

20513.diff (5.5 KB) - added by GaryJ 13 years ago.
First pass
20513.2.diff (5.8 KB) - added by scribu 13 years ago.
Second pass
20513.3.diff (5.7 KB) - added by scribu 13 years ago.
wp_scripts()
20513.4.diff (6.2 KB) - added by wonderboymusic 10 years ago.

Download all attachments as: .zip

Change History (28)

@GaryJ
13 years ago

First pass

#1 @scribu
13 years ago

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

@scribu
13 years ago

Second pass

#2 @scribu
13 years ago

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 years ago by scribu (previous) (diff)

@scribu
13 years ago

wp_scripts()

#3 @scribu
13 years ago

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

wp_scripts()->do_items( $handle );

etc.

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

#4 @scribu
13 years ago

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 years ago by scribu (previous) (diff)

#5 @GaryJ
13 years ago

That looks good to me.

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

#6 @ocean90
13 years ago

  • Cc ocean90 added

#7 @azaozz
13 years ago

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.

#8 @scribu
13 years ago

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

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

#10 follow-up: @GaryJ
13 years 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.

#11 in reply to: ↑ 10 @SergeyBiryukov
13 years 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).

#12 @nacin
11 years ago

  • Component changed from General to Script Loader

#13 @wonderboymusic
10 years ago

  • Milestone changed from Awaiting Review to Future Release

20513.4.diff makes 2 funcs, could work

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

#15 @wonderboymusic
10 years ago

  • Milestone changed from Future Release to 4.2

#16 @wonderboymusic
10 years ago

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

In 31192:

functions.wp-scripts.php contains a lot of duplicated code. Make 2 new functions: wp_scripts() and wp_scripts_maybe_doing_it_wrong( $function ), to encapsulate the repeated logic.

Props GaryJ, scribu, wonderboymusic.
Fixes #20513.

#17 @wonderboymusic
10 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

#18 @wonderboymusic
10 years ago

In 31194:

After [31192], create a function, wp_styles(), to reduce duplicated code in functions.wp-styles.php. The style functions can reuse wp_scripts_maybe_doing_it_wrong( $function ) internally.

See #20513.

#19 @wonderboymusic
10 years ago

In 31196:

Make _wp_scripts_maybe_doing_it_wrong( $function ) "private".

Props obenland for the thought leadership.
See #20513.

#20 @wonderboymusic
10 years ago

In 31202:

Add @ignore to _wp_scripts_maybe_doing_it_wrong().

Props DrewAPicture for the thought leadership.
See #20513.

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


10 years ago

#22 @DrewAPicture
10 years ago

  • Keywords 4.2-beta added

This will ride over to beta, see [31192] [31194] [31196] [31202]. Anything left here?

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


10 years ago

#24 @DrewAPicture
10 years ago

  • Keywords 4.2-beta removed
  • Resolution set to fixed
  • Status changed from reopened to closed

This seems fixed. Please create new tickets for any resulting issues.

Note: See TracTickets for help on using tickets.