wp_get_script_polyfill

The timeline below displays how wordpress function wp_get_script_polyfill has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Returns contents of an inline script used in appending polyfill scripts for
 * browsers which fail the provided tests. The provided array is a mapping from
 * a condition to verify feature support to its polyfill script handle.
 *
 * @since 5.0.0
 *
 * @param WP_Scripts $scripts WP_Scripts object.
 * @param string[]   $tests   Features to detect.
 * @return string Conditional polyfill inline script.
 */
function wp_get_script_polyfill($scripts, $tests)
{
    $polyfill = '';
    foreach ($tests as $test => $handle) {
        if (!array_key_exists($handle, $scripts->registered)) {
            continue;
        }
        $src = $scripts->registered[$handle]->src;
        $ver = $scripts->registered[$handle]->ver;
        if (!preg_match('|^(https?:)?//|', $src) && !($scripts->content_url && str_starts_with($src, $scripts->content_url))) {
            $src = $scripts->base_url . $src;
        }
        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) {
            continue;
        }
        $polyfill .= '( ' . $test . ' ) || ' . 'document.write( \'<script src="' . $src . '"></scr\' + \'ipt>\' );';
    }
    return $polyfill;
}

WordPress Version: 6.1

/**
 * Returns contents of an inline script used in appending polyfill scripts for
 * browsers which fail the provided tests. The provided array is a mapping from
 * a condition to verify feature support to its polyfill script handle.
 *
 * @since 5.0.0
 *
 * @param WP_Scripts $scripts WP_Scripts object.
 * @param string[]   $tests   Features to detect.
 * @return string Conditional polyfill inline script.
 */
function wp_get_script_polyfill($scripts, $tests)
{
    $polyfill = '';
    foreach ($tests as $test => $handle) {
        if (!array_key_exists($handle, $scripts->registered)) {
            continue;
        }
        $src = $scripts->registered[$handle]->src;
        $ver = $scripts->registered[$handle]->ver;
        if (!preg_match('|^(https?:)?//|', $src) && !($scripts->content_url && 0 === strpos($src, $scripts->content_url))) {
            $src = $scripts->base_url . $src;
        }
        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) {
            continue;
        }
        $polyfill .= '( ' . $test . ' ) || ' . 'document.write( \'<script src="' . $src . '"></scr\' + \'ipt>\' );';
    }
    return $polyfill;
}

WordPress Version: 5.4

/**
 * Returns contents of an inline script used in appending polyfill scripts for
 * browsers which fail the provided tests. The provided array is a mapping from
 * a condition to verify feature support to its polyfill script handle.
 *
 * @since 5.0.0
 *
 * @param WP_Scripts $scripts WP_Scripts object.
 * @param array      $tests   Features to detect.
 * @return string Conditional polyfill inline script.
 */
function wp_get_script_polyfill($scripts, $tests)
{
    $polyfill = '';
    foreach ($tests as $test => $handle) {
        if (!array_key_exists($handle, $scripts->registered)) {
            continue;
        }
        $src = $scripts->registered[$handle]->src;
        $ver = $scripts->registered[$handle]->ver;
        if (!preg_match('|^(https?:)?//|', $src) && !($scripts->content_url && 0 === strpos($src, $scripts->content_url))) {
            $src = $scripts->base_url . $src;
        }
        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) {
            continue;
        }
        $polyfill .= '( ' . $test . ' ) || ' . 'document.write( \'<script src="' . $src . '"></scr\' + \'ipt>\' );';
    }
    return $polyfill;
}

WordPress Version: 5.0

/**
 * Returns contents of an inline script used in appending polyfill scripts for
 * browsers which fail the provided tests. The provided array is a mapping from
 * a condition to verify feature support to its polyfill script handle.
 *
 * @since 5.0.0
 *
 * @param WP_Scripts $scripts WP_Scripts object.
 * @param array      $tests   Features to detect.
 * @return string Conditional polyfill inline script.
 */
function wp_get_script_polyfill(&$scripts, $tests)
{
    $polyfill = '';
    foreach ($tests as $test => $handle) {
        if (!array_key_exists($handle, $scripts->registered)) {
            continue;
        }
        $src = $scripts->registered[$handle]->src;
        $ver = $scripts->registered[$handle]->ver;
        if (!preg_match('|^(https?:)?//|', $src) && !($scripts->content_url && 0 === strpos($src, $scripts->content_url))) {
            $src = $scripts->base_url . $src;
        }
        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) {
            continue;
        }
        $polyfill .= '( ' . $test . ' ) || ' . 'document.write( \'<script src="' . $src . '"></scr\' + \'ipt>\' );';
    }
    return $polyfill;
}