Make WordPress Core

Opened 9 years ago

Last modified 5 months ago

#40485 new feature request

Add function for retrieving metadata from registered script

Reported by: desrosj's profile desrosj Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.2
Component: Script Loader Keywords: has-patch has-unit-tests needs-testing 2nd-opinion
Focuses: Cc:

Description

There is currently a function for adding metadata to a registered script (wp_script_add_data()), but no function for retrieving that metadata.

My use case
I am trying to write a unit test for a function in my plugin that generates data and calls wp_localize_script() to ensure the expected data is passed for localization.

Other Use Cases
I could see other use cases where a plugin could change a metadata value or add a new key if a certain value is assigned to a certain key.

Attachments (2)

40485.diff (2.6 KB) - added by desrosj 9 years ago.
Adds wp_script_get_data() with unit tests.
40485.2.diff (2.6 KB) - added by desrosj 8 years ago.
Refreshed patch.

Download all attachments as: .zip

Change History (7)

@desrosj
9 years ago

Adds wp_script_get_data() with unit tests.

#1 @desrosj
9 years ago

  • Keywords has-patch has-unit-tests needs-testing dev-feedback added
  • Type changed from defect (bug) to feature request

#2 @desrosj
9 years ago

  • Keywords dev-feedback removed

#3 @johnbillion
9 years ago

  • Version changed from trunk to 4.2

@desrosj
8 years ago

Refreshed patch.

#4 @jonsurrell
5 months ago

  • Keywords reporter-feedback added

The proposal seems reasonable and aligns with the way many other singleton classes have global functions to wrap methods on the singleton instance, so there's precedent.

The proposed function in the patch wraps wp_scripts()->get_data( $handle, $key );.

Are there policies or precedents for when these functions should or should not be added? It's worth considering that there's been little apparent interest in the 8 years since the ticket was created.

@desrosj do you still want to move ahead with this?

#5 @desrosj
5 months ago

  • Keywords 2nd-opinion added; reporter-feedback removed

Looking back and considering the original use case for this ticket, I think that there's a better approach. Instead of relying on a function or method to retrieve the value, confirming that the expected value is present by inspecting the global $wp_scripts variable directly would be a better methodology.

I do think it's good practice to provide symmetry for these types of wrapper functions. But we should consider compelling, real-world use cases.

A preliminary search of the plugin directory reveals that while it's relatively uncommon, some plugins do call $wp_scripts->get_data() directly. The search found 121 matches, including 13 across 7 plugins with over 100,000 installs. Interestingly, Twenty Twenty has 4 occurrences in class-twentytwenty-script-loader.php.

As for precedent, here are a couple of examples of introducing adding get_* functions to accompany (add|register)_* ones (though admittedly these had stronger rationale and aren't "wrapper" functions):

  • get_theme_support() was added in 3.1 ([16174]) while add_theme_support() was introduced in 2.9 ([12132]).
  • get_post_type_object() was added in 3.0 ([12597]) while register_post_type() was introduced in 2.9 ([11998]).

Also of note is that the Dependencies APi is consistent between scripts and styles: wp_style_add_data() exists but wp_style_get_data() does not.

Note: See TracTickets for help on using tickets.