Changeset 19217 for trunk/wp-includes/class.wp-scripts.php
- Timestamp:
- 11/08/2011 06:05:59 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/class.wp-scripts.php
r18813 r19217 48 48 } 49 49 50 // Deprecated since 3.3, see print_ script_data()50 // Deprecated since 3.3, see print_extra_script() 51 51 function print_scripts_l10n( $handle, $echo = true ) { 52 _deprecated_function( __FUNCTION__, '3.3', 'print_script_data()' ); 53 return $this->print_script_data( $handle, $echo, true ); 54 } 55 56 function print_script_data( $handle, $echo = true, $_l10n = false ) { 57 if ( $_l10n ) { 58 list( $name, $data ) = $this->get_data( $handle, 'l10n' ); 59 $after = ''; 60 61 if ( is_array($data) && isset($data['l10n_print_after']) ) { 62 $after = $data['l10n_print_after']; 63 unset($data['l10n_print_after']); 64 } 65 66 $data = $this->decode_html_entities($data); 67 $output = "var $name = " . json_encode( $data ) . "; $after\n"; 68 } else { 69 $data = $this->get_data( $handle, 'data' ); 70 71 if ( empty( $data ) ) 72 return false; 73 74 foreach ( (array) $data as $name => $value ) { 75 $value = $this->decode_html_entities($value); 76 $output = "var $name = " . json_encode( $value ) . ";\n"; 77 } 78 } 52 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' ); 53 return $this->print_extra_script( $handle, $echo ); 54 } 55 56 function print_extra_script( $handle, $echo = true ) { 57 if ( !$output = $this->get_data( $handle, 'data' ) ) 58 return; 79 59 80 60 if ( !$echo ) 81 61 return $output; 82 62 83 echo "<script type='text/javascript'>\n"; 84 echo "/* <![CDATA[ */\n"; // CDATA is not needed for HTML 585 echo $output;63 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5 64 echo "/* <![CDATA[ */\n"; 65 echo "$output\n"; 86 66 echo "/* ]]> */\n"; 87 67 echo "</script>\n"; … … 115 95 $srce = apply_filters( 'script_loader_src', $src, $handle ); 116 96 if ( $this->in_default_dir($srce) ) { 117 $this->print_code .= $this->print_ script_data( $handle, false );97 $this->print_code .= $this->print_extra_script( $handle, false ); 118 98 $this->concat .= "$handle,"; 119 99 $this->concat_version .= "$handle$ver"; … … 125 105 } 126 106 127 $this->print_ script_data( $handle );107 $this->print_extra_script( $handle ); 128 108 if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { 129 109 $src = $this->base_url . $src; … … 132 112 if ( !empty($ver) ) 133 113 $src = add_query_arg('ver', $ver, $src); 134 $src = esc_url(apply_filters( 'script_loader_src', $src, $handle )); 114 115 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); 135 116 136 117 if ( $this->do_concat ) … … 143 124 144 125 /** 145 * Localizes a script (Deprecated) 146 * 147 * Localizes only if script has already been added 148 * 149 * @deprecated WP 3.3 126 * Localizes a script 127 * 128 * Localizes only if the script has already been added 150 129 */ 151 130 function localize( $handle, $object_name, $l10n ) { 152 _deprecated_function( __FUNCTION__, '3.3', 'add_script_data()' ); 153 return $this->add_script_data( $handle, $object_name, $l10n ); 131 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present 132 $after = $l10n['l10n_print_after']; 133 unset($l10n['l10n_print_after']); 134 } 135 136 foreach ( (array) $l10n as $key => $value ) { 137 if ( !is_scalar($value) ) 138 continue; 139 140 $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8'); 141 } 142 143 $script = "var $object_name = " . json_encode($l10n) . ';'; 144 145 if ( !empty($after) ) 146 $script .= "\n$after"; 147 148 return $this->add_script_data( $handle, $script ); 154 149 } 155 150 … … 160 155 * 161 156 * @param string $handle Script name 162 * @param string $name Name of JS object to hold the data 163 * @param array $args Associative array of JS object attributes 157 * @param string $script Extra JS to add before the script 164 158 * @return bool Successful or not 165 159 */ 166 function add_script_data( $handle, $ name, $args) {167 if ( ! $name || !is_array( $args) )160 function add_script_data( $handle, $script ) { 161 if ( !is_string( $script ) ) 168 162 return false; 169 163 170 164 $data = $this->get_data( $handle, 'data' ); 171 165 172 if ( !empty( $data [$name]) )173 $ args = array_merge( $data[$name], $args );174 175 return $this->add_data( $handle, 'data', array( $name => $args ));166 if ( !empty( $data ) ) 167 $script = "$data;\n$script"; 168 169 return $this->add_data( $handle, 'data', $script ); 176 170 } 177 171 … … 218 212 } 219 213 return false; 220 }221 222 function decode_html_entities($data) {223 foreach ( (array) $data as $key => $value ) {224 if ( is_array($value) )225 $data[$key] = $this->decode_html_entities($value);226 elseif ( is_string($value) )227 $data[$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');228 }229 return $data;230 214 } 231 215
Note: See TracChangeset
for help on using the changeset viewer.