[wp-trac] [WordPress Trac] #31281: Register JavaScript/Underscore templates using the WP Dependency API
WordPress Trac
noreply at wordpress.org
Mon Feb 16 07:07:51 UTC 2015
#31281: Register JavaScript/Underscore templates using the WP Dependency API
---------------------------+------------------------------
Reporter: F J Kaiser | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Script Loader | Version: trunk
Severity: normal | Resolution:
Keywords: | Focuses:
---------------------------+------------------------------
Comment (by ChriCo):
Shouldn't we implement a similar (working) function for
{{{wp_add_inline_style() }}} to add '''scripts'''?
Currently it is not possible via {{{ wp_enqueue_script() }}} to add
'''inline''' Scripts. It's only possible by using the {{{ wp_head/footer
}}}-Action and adding it inline with a {{{ <script> }}}-Tag. Maybe we
should switch from unflexible 5 parameters to a list (array, or better:
class) and adding a param for {{{"is_inline" => true|false}}}?
{{{
/**
* ..snip..
*
* @param array $args = array(
handle => String
src => String
deps => Array
ver => String|Int|Boolean by default false..
in_footer => Boolean by default false
is_inline => Boolean by default false
attributes => Array the <script>-Tag-
Attributes
);
*
* @return void
*/
function wp_enqueue_script( $args ) {
if ( ! is_array( $args ) ) {
// some _deprecated_argument()-message.
$function_args = func_get_args();
$args = _wp_convert_enqueue_args( $function_args );
}
// snip the init and wp-scripts check.
if ( ! isset( $args[ 'handle' ] ) ) {
// some _doing_it_wrong()-message.
return;
}
$default_args = array(
'deps' => array(),
'ver' => false,
'in_footer' => false,
'is_inline' => false,
'type' => '',
'attributes' => array()
);
$args = wp_parse_args( $args, $default_args );
// snip the rest.
}
/**
* Converting the max. old 5 parameters from
wp_[enqueue|register]_[script|style] to a single array list.
*
* @param array $old_args array
*
* @return array $args array
*/
_wp_convert_enqueue_or_register_args( $old_args = array() ) {
$args = array();
if ( isset( $old_args[ 1 ] ) ) {
$args[ 'handle' ] = (string)$old_args[ 0 ];
}
if ( isset( $old_args[ 1 ] ) ) {
$args[ 'src' ] = (string)$old_args[ 1 ];
}
if ( isset( $old_args[ 2 ] ) ) {
$args[ 'deps' ] = (string)$old_args[ 2 ];
}
if ( isset( $old_args[ 3 ] ) ) {
$args[ 'ver' ] = (string)$old_args[ 3 ];
}
if ( isset( $function_args[ 4 ] ) ) {
$args[ 'in_footer' ] = (bool)$old_args[ 4 ];
}
return $args;
}
}}}
With this "solution" we are more flexible. We can remove the not required
{{{type="javascript/text"}}}, add the missing "attributes"-Parameter to
add some custom attributes to the {{{<script>}}}-Tag like {{{data-
foo="bar"}}}, {{{id="baz"}}} or {{{async|defer}}}.
This could also be done for {{{ wp_[enqueue|register]_style() }}} which
currently lacks of support for adding inline-styles to the header/footer
without enqueue-ing a .css-File.
Some examples:
{{{
// e.G inline template
$script_args = array(
'handle' => 'inline-template',
'src' => '/path/to/template.tmpl',
'deps' => array( 'underscore', 'backbone', 'jquery',
'models' ),
'in_footer' => true,
'is_inline' => true,
'attributes' => array(
'type' => 'text/template'
),
);
wp_enqueue_script( $script_args );
// e.G. inline script
$script_args = array(
'handle' => 'inline-js',
'src' => '/path/to/inline.js',
'in_footer' => true
'is_inline' => true
);
wp_enqueue_script( $script_args );
// e.G. async script
$script_args = array(
'handle' => 'inline-js',
'src' => '/path/to/async.js',
'attributes' => array(
'async' => 'async'
)
);
wp_enqueue_script( $script_args );
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/31281#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list