Skip to content

Commit 012151f

Browse files
committed
Add polyfill for get_theme_file_uri()
1 parent ad3eb31 commit 012151f

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

features/bootstrap/support.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,49 @@ function check_that_yaml_string_contains_yaml_string( $actual_yaml, $expected_ya
216216
return compare_contents( $expected_value, $actual_value );
217217
}
218218

219+
/*
220+
* Add a polyfill for the get_theme_file_uri(), as it is required for the
221+
* TwentyTwenty theme's starter content, and will fatal the site if you install
222+
* WP 5.3 first (setting TwentyTwenty as the active theme) and then downgrade
223+
* to a version of WP lower than 4.7.
224+
*
225+
* Note: This is a quick fix, and a cleaner solution would be to change the
226+
* active theme on downgrading, if the current theme declares it is not
227+
* supported.
228+
*
229+
* See: https://github.com/WordPress/twentytwenty/issues/973
230+
*/
231+
if ( ! function_exists( 'get_theme_file_uri' ) ) {
232+
/**
233+
* Retrieves the URL of a file in the theme.
234+
*
235+
* Searches in the stylesheet directory before the template directory so themes
236+
* which inherit from a parent theme can just override one file.
237+
*
238+
* @since 4.7.0
239+
*
240+
* @param string $file Optional. File to search for in the stylesheet directory.
241+
* @return string The URL of the file.
242+
*/
243+
function get_theme_file_uri( $file = '' ) {
244+
$file = ltrim( $file, '/' );
245+
246+
if ( empty( $file ) ) {
247+
$url = get_stylesheet_directory_uri();
248+
} elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
249+
$url = get_stylesheet_directory_uri() . '/' . $file;
250+
} else {
251+
$url = get_template_directory_uri() . '/' . $file;
252+
}
253+
254+
/**
255+
* Filters the URL to a file in the theme.
256+
*
257+
* @since 4.7.0
258+
*
259+
* @param string $url The file URL.
260+
* @param string $file The requested file to search for.
261+
*/
262+
return apply_filters( 'theme_file_uri', $url, $file );
263+
}
264+
}

0 commit comments

Comments
 (0)