287 |
287 |
288 if ( isset( $this->args[ $handle ] ) ) { |
288 if ( isset( $this->args[ $handle ] ) ) { |
289 $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; |
289 $ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; |
290 } |
290 } |
291 |
291 |
292 $src = $obj->src; |
292 $src = $obj->src; |
293 $strategy = $this->get_eligible_loading_strategy( $handle ); |
293 $strategy = $this->get_eligible_loading_strategy( $handle ); |
294 $intended_strategy = (string) $this->get_data( $handle, 'strategy' ); |
294 $intended_strategy = (string) $this->get_data( $handle, 'strategy' ); |
295 $cond_before = ''; |
295 $ie_conditional_prefix = ''; |
296 $cond_after = ''; |
296 $ie_conditional_suffix = ''; |
297 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
297 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
298 |
298 |
299 if ( ! $this->is_delayed_strategy( $intended_strategy ) ) { |
299 if ( ! $this->is_delayed_strategy( $intended_strategy ) ) { |
300 $intended_strategy = ''; |
300 $intended_strategy = ''; |
301 } |
301 } |
302 |
302 |
318 $this->in_footer[] = $handle; |
318 $this->in_footer[] = $handle; |
319 return false; |
319 return false; |
320 } |
320 } |
321 |
321 |
322 if ( $conditional ) { |
322 if ( $conditional ) { |
323 $cond_before = "<!--[if {$conditional}]>\n"; |
323 $ie_conditional_prefix = "<!--[if {$conditional}]>\n"; |
324 $cond_after = "<![endif]-->\n"; |
324 $ie_conditional_suffix = "<![endif]-->\n"; |
325 } |
325 } |
326 |
326 |
327 $before_script = $this->get_inline_script_tag( $handle, 'before' ); |
327 $before_script = $this->get_inline_script_tag( $handle, 'before' ); |
328 $after_script = $this->get_inline_script_tag( $handle, 'after' ); |
328 $after_script = $this->get_inline_script_tag( $handle, 'after' ); |
329 |
329 |
330 if ( $before_script || $after_script ) { |
330 if ( $before_script || $after_script ) { |
331 $inline_script_tag = $cond_before . $before_script . $after_script . $cond_after; |
331 $inline_script_tag = $ie_conditional_prefix . $before_script . $after_script . $ie_conditional_suffix; |
332 } else { |
332 } else { |
333 $inline_script_tag = ''; |
333 $inline_script_tag = ''; |
334 } |
334 } |
335 |
335 |
336 /* |
336 /* |
351 * @since 2.2.0 |
351 * @since 2.2.0 |
352 * |
352 * |
353 * @param string $src Script loader source path. |
353 * @param string $src Script loader source path. |
354 * @param string $handle Script handle. |
354 * @param string $handle Script handle. |
355 */ |
355 */ |
356 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
356 $filtered_src = apply_filters( 'script_loader_src', $src, $handle ); |
357 |
357 |
358 if ( |
358 if ( |
359 $this->in_default_dir( $srce ) |
359 $this->in_default_dir( $filtered_src ) |
360 && ( $before_script || $after_script || $translations_stop_concat || $this->is_delayed_strategy( $strategy ) ) |
360 && ( $before_script || $after_script || $translations_stop_concat || $this->is_delayed_strategy( $strategy ) ) |
361 ) { |
361 ) { |
362 $this->do_concat = false; |
362 $this->do_concat = false; |
363 |
363 |
364 // Have to print the so-far concatenated scripts right away to maintain the right order. |
364 // Have to print the so-far concatenated scripts right away to maintain the right order. |
365 _print_scripts(); |
365 _print_scripts(); |
366 $this->reset(); |
366 $this->reset(); |
367 } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { |
367 } elseif ( $this->in_default_dir( $filtered_src ) && ! $conditional ) { |
368 $this->print_code .= $this->print_extra_script( $handle, false ); |
368 $this->print_code .= $this->print_extra_script( $handle, false ); |
369 $this->concat .= "$handle,"; |
369 $this->concat .= "$handle,"; |
370 $this->concat_version .= "$handle$ver"; |
370 $this->concat_version .= "$handle$ver"; |
371 return true; |
371 return true; |
372 } else { |
372 } else { |
624 * Default false. |
624 * Default false. |
625 * @return bool Not already in the group or a lower group. |
625 * @return bool Not already in the group or a lower group. |
626 */ |
626 */ |
627 public function set_group( $handle, $recursion, $group = false ) { |
627 public function set_group( $handle, $recursion, $group = false ) { |
628 if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) { |
628 if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) { |
629 $grp = 1; |
629 $calculated_group = 1; |
630 } else { |
630 } else { |
631 $grp = (int) $this->get_data( $handle, 'group' ); |
631 $calculated_group = (int) $this->get_data( $handle, 'group' ); |
632 } |
632 } |
633 |
633 |
634 if ( false !== $group && $grp > $group ) { |
634 if ( false !== $group && $calculated_group > $group ) { |
635 $grp = $group; |
635 $calculated_group = $group; |
636 } |
636 } |
637 |
637 |
638 return parent::set_group( $handle, $recursion, $grp ); |
638 return parent::set_group( $handle, $recursion, $calculated_group ); |
639 } |
639 } |
640 |
640 |
641 /** |
641 /** |
642 * Sets a translation textdomain. |
642 * Sets a translation textdomain. |
643 * |
643 * |
721 * @param int|false $group Optional. Group level: level (int), no groups (false). |
721 * @param int|false $group Optional. Group level: level (int), no groups (false). |
722 * Default false. |
722 * Default false. |
723 * @return bool True on success, false on failure. |
723 * @return bool True on success, false on failure. |
724 */ |
724 */ |
725 public function all_deps( $handles, $recursion = false, $group = false ) { |
725 public function all_deps( $handles, $recursion = false, $group = false ) { |
726 $r = parent::all_deps( $handles, $recursion, $group ); |
726 $result = parent::all_deps( $handles, $recursion, $group ); |
727 if ( ! $recursion ) { |
727 if ( ! $recursion ) { |
728 /** |
728 /** |
729 * Filters the list of script dependencies left to print. |
729 * Filters the list of script dependencies left to print. |
730 * |
730 * |
731 * @since 2.3.0 |
731 * @since 2.3.0 |
732 * |
732 * |
733 * @param string[] $to_do An array of script dependency handles. |
733 * @param string[] $to_do An array of script dependency handles. |
734 */ |
734 */ |
735 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
735 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
736 } |
736 } |
737 return $r; |
737 return $result; |
738 } |
738 } |
739 |
739 |
740 /** |
740 /** |
741 * Processes items and dependencies for the head group. |
741 * Processes items and dependencies for the head group. |
742 * |
742 * |
887 * |
887 * |
888 * @param string $handle The script handle. |
888 * @param string $handle The script handle. |
889 * @return string The best eligible loading strategy. |
889 * @return string The best eligible loading strategy. |
890 */ |
890 */ |
891 private function get_eligible_loading_strategy( $handle ) { |
891 private function get_eligible_loading_strategy( $handle ) { |
892 $intended = (string) $this->get_data( $handle, 'strategy' ); |
892 $intended_strategy = (string) $this->get_data( $handle, 'strategy' ); |
893 |
893 |
894 // Bail early if there is no intended strategy. |
894 // Bail early if there is no intended strategy. |
895 if ( ! $intended ) { |
895 if ( ! $intended_strategy ) { |
896 return ''; |
896 return ''; |
897 } |
897 } |
898 |
898 |
899 /* |
899 /* |
900 * If the intended strategy is 'defer', limit the initial list of eligible |
900 * If the intended strategy is 'defer', limit the initial list of eligible |
901 * strategies, since 'async' can fallback to 'defer', but not vice-versa. |
901 * strategies, since 'async' can fallback to 'defer', but not vice-versa. |
902 */ |
902 */ |
903 $initial = ( 'defer' === $intended ) ? array( 'defer' ) : null; |
903 $initial_strategy = ( 'defer' === $intended_strategy ) ? array( 'defer' ) : null; |
904 |
904 |
905 $eligible = $this->filter_eligible_strategies( $handle, $initial ); |
905 $eligible_strategies = $this->filter_eligible_strategies( $handle, $initial_strategy ); |
906 |
906 |
907 // Return early once we know the eligible strategy is blocking. |
907 // Return early once we know the eligible strategy is blocking. |
908 if ( empty( $eligible ) ) { |
908 if ( empty( $eligible_strategies ) ) { |
909 return ''; |
909 return ''; |
910 } |
910 } |
911 |
911 |
912 return in_array( 'async', $eligible, true ) ? 'async' : 'defer'; |
912 return in_array( 'async', $eligible_strategies, true ) ? 'async' : 'defer'; |
913 } |
913 } |
914 |
914 |
915 /** |
915 /** |
916 * Filter the list of eligible loading strategies for a script. |
916 * Filter the list of eligible loading strategies for a script. |
917 * |
917 * |
918 * @since 6.3.0 |
918 * @since 6.3.0 |
919 * |
919 * |
920 * @param string $handle The script handle. |
920 * @param string $handle The script handle. |
921 * @param string[]|null $eligible Optional. The list of strategies to filter. Default null. |
921 * @param string[]|null $eligible_strategies Optional. The list of strategies to filter. Default null. |
922 * @param array<string, true> $checked Optional. An array of already checked script handles, used to avoid recursive loops. |
922 * @param array<string, true> $checked Optional. An array of already checked script handles, used to avoid recursive loops. |
923 * @return string[] A list of eligible loading strategies that could be used. |
923 * @return string[] A list of eligible loading strategies that could be used. |
924 */ |
924 */ |
925 private function filter_eligible_strategies( $handle, $eligible = null, $checked = array() ) { |
925 private function filter_eligible_strategies( $handle, $eligible_strategies = null, $checked = array() ) { |
926 // If no strategies are being passed, all strategies are eligible. |
926 // If no strategies are being passed, all strategies are eligible. |
927 if ( null === $eligible ) { |
927 if ( null === $eligible_strategies ) { |
928 $eligible = $this->delayed_strategies; |
928 $eligible_strategies = $this->delayed_strategies; |
929 } |
929 } |
930 |
930 |
931 // If this handle was already checked, return early. |
931 // If this handle was already checked, return early. |
932 if ( isset( $checked[ $handle ] ) ) { |
932 if ( isset( $checked[ $handle ] ) ) { |
933 return $eligible; |
933 return $eligible_strategies; |
934 } |
934 } |
935 |
935 |
936 // Mark this handle as checked. |
936 // Mark this handle as checked. |
937 $checked[ $handle ] = true; |
937 $checked[ $handle ] = true; |
938 |
938 |
939 // If this handle isn't registered, don't filter anything and return. |
939 // If this handle isn't registered, don't filter anything and return. |
940 if ( ! isset( $this->registered[ $handle ] ) ) { |
940 if ( ! isset( $this->registered[ $handle ] ) ) { |
941 return $eligible; |
941 return $eligible_strategies; |
942 } |
942 } |
943 |
943 |
944 // If the handle is not enqueued, don't filter anything and return. |
944 // If the handle is not enqueued, don't filter anything and return. |
945 if ( ! $this->query( $handle, 'enqueued' ) ) { |
945 if ( ! $this->query( $handle, 'enqueued' ) ) { |
946 return $eligible; |
946 return $eligible_strategies; |
947 } |
947 } |
948 |
948 |
949 $is_alias = (bool) ! $this->registered[ $handle ]->src; |
949 $is_alias = (bool) ! $this->registered[ $handle ]->src; |
950 $intended_strategy = $this->get_data( $handle, 'strategy' ); |
950 $intended_strategy = $this->get_data( $handle, 'strategy' ); |
951 |
951 |
959 return array(); |
959 return array(); |
960 } |
960 } |
961 |
961 |
962 // If the intended strategy is 'defer', filter out 'async'. |
962 // If the intended strategy is 'defer', filter out 'async'. |
963 if ( 'defer' === $intended_strategy ) { |
963 if ( 'defer' === $intended_strategy ) { |
964 $eligible = array( 'defer' ); |
964 $eligible_strategies = array( 'defer' ); |
965 } |
965 } |
966 |
966 |
967 $dependents = $this->get_dependents( $handle ); |
967 $dependents = $this->get_dependents( $handle ); |
968 |
968 |
969 // Recursively filter eligible strategies for dependents. |
969 // Recursively filter eligible strategies for dependents. |
970 foreach ( $dependents as $dependent ) { |
970 foreach ( $dependents as $dependent ) { |
971 // Bail early once we know the eligible strategy is blocking. |
971 // Bail early once we know the eligible strategy is blocking. |
972 if ( empty( $eligible ) ) { |
972 if ( empty( $eligible_strategies ) ) { |
973 return array(); |
973 return array(); |
974 } |
974 } |
975 |
975 |
976 $eligible = $this->filter_eligible_strategies( $dependent, $eligible, $checked ); |
976 $eligible_strategies = $this->filter_eligible_strategies( $dependent, $eligible_strategies, $checked ); |
977 } |
977 } |
978 |
978 |
979 return $eligible; |
979 return $eligible_strategies; |
980 } |
980 } |
981 |
981 |
982 /** |
982 /** |
983 * Gets data for inline scripts registered for a specific handle. |
983 * Gets data for inline scripts registered for a specific handle. |
984 * |
984 * |