--- a/wp/wp-includes/class.wp-scripts.php Mon Oct 14 18:06:33 2019 +0200
+++ b/wp/wp-includes/class.wp-scripts.php Mon Oct 14 18:28:13 2019 +0200
@@ -35,7 +35,7 @@
public $content_url;
/**
- * Default version string for stylesheets.
+ * Default version string for scripts.
*
* @since 2.6.0
* @var string
@@ -145,7 +145,7 @@
*
* @param WP_Scripts $this WP_Scripts instance (passed by reference).
*/
- do_action_ref_array( 'wp_default_scripts', array(&$this) );
+ do_action_ref_array( 'wp_default_scripts', array( &$this ) );
}
/**
@@ -196,13 +196,15 @@
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
*/
public function print_extra_script( $handle, $echo = true ) {
- if ( !$output = $this->get_data( $handle, 'data' ) )
+ if ( ! $output = $this->get_data( $handle, 'data' ) ) {
return;
+ }
- if ( !$echo )
+ if ( ! $echo ) {
return $output;
+ }
- echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
+ echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5.
echo "/* <![CDATA[ */\n";
echo "$output\n";
echo "/* ]]> */\n";
@@ -219,23 +221,25 @@
*
* @see WP_Dependencies::do_item()
*
- * @param string $handle The script's registered handle.
+ * @param string $handle The script's registered handle.
* @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
* @return bool True on success, false on failure.
*/
public function do_item( $handle, $group = false ) {
- if ( !parent::do_item($handle) )
+ if ( ! parent::do_item( $handle ) ) {
return false;
+ }
- if ( 0 === $group && $this->groups[$handle] > 0 ) {
+ if ( 0 === $group && $this->groups[ $handle ] > 0 ) {
$this->in_footer[] = $handle;
return false;
}
- if ( false === $group && in_array($handle, $this->in_footer, true) )
+ if ( false === $group && in_array( $handle, $this->in_footer, true ) ) {
$this->in_footer = array_diff( $this->in_footer, (array) $handle );
+ }
- $obj = $this->registered[$handle];
+ $obj = $this->registered[ $handle ];
if ( null === $obj->ver ) {
$ver = '';
@@ -243,20 +247,21 @@
$ver = $obj->ver ? $obj->ver : $this->default_version;
}
- if ( isset($this->args[$handle]) )
- $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
+ if ( isset( $this->args[ $handle ] ) ) {
+ $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ];
+ }
- $src = $obj->src;
+ $src = $obj->src;
$cond_before = $cond_after = '';
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
if ( $conditional ) {
$cond_before = "<!--[if {$conditional}]>\n";
- $cond_after = "<![endif]-->\n";
+ $cond_after = "<![endif]-->\n";
}
$before_handle = $this->print_inline_script( $handle, 'before', false );
- $after_handle = $this->print_inline_script( $handle, 'after', false );
+ $after_handle = $this->print_inline_script( $handle, 'after', false );
if ( $before_handle ) {
$before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
@@ -266,6 +271,12 @@
$after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
}
+ if ( $before_handle || $after_handle ) {
+ $inline_script_tag = "{$cond_before}{$before_handle}{$after_handle}{$cond_after}";
+ } else {
+ $inline_script_tag = '';
+ }
+
if ( $this->do_concat ) {
/**
* Filters the script loader source.
@@ -284,8 +295,8 @@
_print_scripts();
$this->reset();
} elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
- $this->print_code .= $this->print_extra_script( $handle, false );
- $this->concat .= "$handle,";
+ $this->print_code .= $this->print_extra_script( $handle, false );
+ $this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
return true;
} else {
@@ -307,24 +318,39 @@
}
// A single item may alias a set of items, by having dependencies, but no source.
- if ( ! $obj->src ) {
+ if ( ! $src ) {
+ if ( $inline_script_tag ) {
+ if ( $this->do_concat ) {
+ $this->print_html .= $inline_script_tag;
+ } else {
+ echo $inline_script_tag;
+ }
+ }
+
return true;
}
+ $translations = $this->print_translations( $handle, false );
+ if ( $translations ) {
+ $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
+ }
+
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
$src = $this->base_url . $src;
}
- if ( ! empty( $ver ) )
+ if ( ! empty( $ver ) ) {
$src = add_query_arg( 'ver', $ver, $src );
+ }
/** This filter is documented in wp-includes/class.wp-scripts.php */
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
- if ( ! $src )
+ if ( ! $src ) {
return true;
+ }
- $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
+ $tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
/**
* Filters the HTML script tag of an enqueued script.
@@ -380,7 +406,7 @@
* @param string $handle Name of the script to add the inline script to. Must be lowercase.
* @param string $position Optional. Whether to add the inline script before the handle
* or after. Default 'after'.
- * @param bool $echo Optional. Whether to echo the script instead of just returning it.
+ * @param bool $echo Optional. Whether to echo the script instead of just returning it.
* Default true.
* @return string|false Script on success, false otherwise.
*/
@@ -405,36 +431,40 @@
*
* @since 2.1.0
*
- * @param string $handle
- * @param string $object_name
- * @param array $l10n
- * @return bool
+ * @param string $handle Name of the script to attach data to.
+ * @param string $object_name Name of the variable that will contain the data.
+ * @param array $l10n Array of data to localize.
+ * @return bool True on success, false on failure.
*/
public function localize( $handle, $object_name, $l10n ) {
- if ( $handle === 'jquery' )
+ if ( $handle === 'jquery' ) {
$handle = 'jquery-core';
+ }
- if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
+ if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present.
$after = $l10n['l10n_print_after'];
- unset($l10n['l10n_print_after']);
+ unset( $l10n['l10n_print_after'] );
}
foreach ( (array) $l10n as $key => $value ) {
- if ( !is_scalar($value) )
+ if ( ! is_scalar( $value ) ) {
continue;
+ }
- $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
+ $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
}
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
- if ( !empty($after) )
+ if ( ! empty( $after ) ) {
$script .= "\n$after;";
+ }
$data = $this->get_data( $handle, 'data' );
- if ( !empty( $data ) )
+ if ( ! empty( $data ) ) {
$script = "$data\n$script";
+ }
return $this->add_data( $handle, 'data', $script );
}
@@ -452,20 +482,88 @@
* @return bool Not already in the group or a lower group
*/
public function set_group( $handle, $recursion, $group = false ) {
- if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 )
+ if ( isset( $this->registered[ $handle ]->args ) && $this->registered[ $handle ]->args === 1 ) {
$grp = 1;
- else
+ } else {
$grp = (int) $this->get_data( $handle, 'group' );
+ }
- if ( false !== $group && $grp > $group )
+ if ( false !== $group && $grp > $group ) {
$grp = $group;
+ }
return parent::set_group( $handle, $recursion, $grp );
}
/**
+ * Sets a translation textdomain.
+ *
+ * @since 5.0.0
+ * @since 5.1.0 The `$domain` parameter was made optional.
+ *
+ * @param string $handle Name of the script to register a translation domain to.
+ * @param string $domain Optional. Text domain. Default 'default'.
+ * @param string $path Optional. The full file path to the directory containing translation files.
+ * @return bool True if the text domain was registered, false if not.
+ */
+ public function set_translations( $handle, $domain = 'default', $path = null ) {
+ if ( ! isset( $this->registered[ $handle ] ) ) {
+ return false;
+ }
+
+ /** @var \_WP_Dependency $obj */
+ $obj = $this->registered[ $handle ];
+
+ if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
+ $obj->deps[] = 'wp-i18n';
+ }
+
+ return $obj->set_translations( $domain, $path );
+ }
+
+ /**
+ * Prints translations set for a specific handle.
+ *
+ * @since 5.0.0
+ *
+ * @param string $handle Name of the script to add the inline script to. Must be lowercase.
+ * @param bool $echo Optional. Whether to echo the script instead of just returning it.
+ * Default true.
+ * @return string|false Script on success, false otherwise.
+ */
+ public function print_translations( $handle, $echo = true ) {
+ if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
+ return false;
+ }
+
+ $domain = $this->registered[ $handle ]->textdomain;
+ $path = $this->registered[ $handle ]->translations_path;
+
+ $json_translations = load_script_textdomain( $handle, $domain, $path );
+
+ if ( ! $json_translations ) {
+ // Register empty locale data object to ensure the domain still exists.
+ $json_translations = '{ "locale_data": { "messages": { "": {} } } }';
+ }
+
+ $output = <<<JS
+( function( domain, translations ) {
+ var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
+ localeData[""].domain = domain;
+ wp.i18n.setLocaleData( localeData, domain );
+} )( "{$domain}", {$json_translations} );
+JS;
+
+ if ( $echo ) {
+ printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
+ }
+
+ return $output;
+ }
+
+ /**
* Determines script dependencies.
- *
+ *
* @since 2.1.0
*
* @see WP_Dependencies::all_deps()
@@ -483,7 +581,7 @@
*
* @since 2.3.0
*
- * @param array $to_do An array of script dependencies.
+ * @param string[] $to_do An array of script dependency handles.
*/
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
}
@@ -500,7 +598,7 @@
* @return array Handles of items that have been processed.
*/
public function do_head_items() {
- $this->do_items(false, 0);
+ $this->do_items( false, 0 );
return $this->done;
}
@@ -514,7 +612,7 @@
* @return array Handles of items that have been processed.
*/
public function do_footer_items() {
- $this->do_items(false, 1);
+ $this->do_items( false, 1 );
return $this->done;
}
@@ -549,12 +647,12 @@
* @since 2.8.0
*/
public function reset() {
- $this->do_concat = false;
- $this->print_code = '';
- $this->concat = '';
+ $this->do_concat = false;
+ $this->print_code = '';
+ $this->concat = '';
$this->concat_version = '';
- $this->print_html = '';
- $this->ext_version = '';
- $this->ext_handles = '';
+ $this->print_html = '';
+ $this->ext_version = '';
+ $this->ext_handles = '';
}
}