@@ -1196,14 +1196,41 @@ protected function get_dependency_warning_message( $handle, $missing_dependency_
11961196 * @since 6.8.0
11971197 */
11981198 public function print_script_data () {
1199- $ scripts = array ();
1200-
1201- // Collect all enqueued scripts and their dependencies.
1202- foreach ( array_unique ( $ this ->queue ) as $ handle ) {
1203- $ scripts [ $ handle ] = true ;
1199+ /*
1200+ * Determine JSON encoding flags once, outside the loop.
1201+ * The charset won't change during iteration.
1202+ *
1203+ * This data will be printed as JSON inside a script tag like this:
1204+ * <script type="application/json"></script>
1205+ *
1206+ * A script tag must be closed by a sequence beginning with `</`. It's impossible to
1207+ * close a script tag without using `<`. We ensure that `<` is escaped and `/` can
1208+ * remain unescaped, so `</script>` will be printed as `\u003C/script>`.
1209+ *
1210+ * - JSON_HEX_TAG: All < and > are converted to \u003C and \u003E.
1211+ * - JSON_UNESCAPED_SLASHES: Don't escape /.
1212+ *
1213+ * If the page will use UTF-8 encoding, it's safe to print unescaped unicode:
1214+ *
1215+ * - JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (instead of as `\uXXXX`).
1216+ * - JSON_UNESCAPED_LINE_TERMINATORS: The line terminators are kept unescaped when
1217+ * JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was
1218+ * before PHP 7.1 without this constant. Available as of PHP 7.1.0.
1219+ *
1220+ * The JSON specification requires encoding in UTF-8, so if the generated HTML page
1221+ * is not encoded in UTF-8 then it's not safe to include those literals. They must
1222+ * be escaped to avoid encoding issues.
1223+ *
1224+ * @see https://www.rfc-editor.org/rfc/rfc8259.html for details on encoding requirements.
1225+ * @see https://www.php.net/manual/en/json.constants.php for details on these constants.
1226+ * @see https://html.spec.whatwg.org/#script-data-state for details on script tag parsing.
1227+ */
1228+ $ json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS ;
1229+ if ( ! is_utf8_charset () ) {
1230+ $ json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ;
12041231 }
12051232
1206- foreach ( array_keys ( $ scripts ) as $ handle ) {
1233+ foreach ( array_unique ( $ this -> queue ) as $ handle ) {
12071234 /**
12081235 * Filters data associated with a given script.
12091236 *
@@ -1253,38 +1280,7 @@ public function print_script_data() {
12531280 */
12541281 $ data = apply_filters ( "script_data_ {$ handle }" , array () );
12551282
1256- if ( is_array ( $ data ) && array () !== $ data ) {
1257- /*
1258- * This data will be printed as JSON inside a script tag like this:
1259- * <script type="application/json"></script>
1260- *
1261- * A script tag must be closed by a sequence beginning with `</`. It's impossible to
1262- * close a script tag without using `<`. We ensure that `<` is escaped and `/` can
1263- * remain unescaped, so `</script>` will be printed as `\u003C/script>`.
1264- *
1265- * - JSON_HEX_TAG: All < and > are converted to \u003C and \u003E.
1266- * - JSON_UNESCAPED_SLASHES: Don't escape /.
1267- *
1268- * If the page will use UTF-8 encoding, it's safe to print unescaped unicode:
1269- *
1270- * - JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (instead of as `\uXXXX`).
1271- * - JSON_UNESCAPED_LINE_TERMINATORS: The line terminators are kept unescaped when
1272- * JSON_UNESCAPED_UNICODE is supplied. It uses the same behaviour as it was
1273- * before PHP 7.1 without this constant. Available as of PHP 7.1.0.
1274- *
1275- * The JSON specification requires encoding in UTF-8, so if the generated HTML page
1276- * is not encoded in UTF-8 then it's not safe to include those literals. They must
1277- * be escaped to avoid encoding issues.
1278- *
1279- * @see https://www.rfc-editor.org/rfc/rfc8259.html for details on encoding requirements.
1280- * @see https://www.php.net/manual/en/json.constants.php for details on these constants.
1281- * @see https://html.spec.whatwg.org/#script-data-state for details on script tag parsing.
1282- */
1283- $ json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_LINE_TERMINATORS ;
1284- if ( ! is_utf8_charset () ) {
1285- $ json_encode_flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ;
1286- }
1287-
1283+ if ( is_array ( $ data ) && ! empty ( $ data ) ) {
12881284 wp_print_inline_script_tag (
12891285 (string ) wp_json_encode (
12901286 $ data ,
0 commit comments