Skip to content

Commit 006eca0

Browse files
authored
Add polyfill for get_theme_file_uri() (#72)
Add polyfill for `get_theme_file_uri()`
2 parents 593ec79 + f32ffe6 commit 006eca0

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

features/bootstrap/FeatureContext.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,12 @@ public function download_wp( $subdir = '' ) {
782782

783783
self::copy_dir( self::$cache_dir, $dest_dir );
784784

785-
// disable emailing
785+
// Disable emailing.
786786
mkdir( $dest_dir . '/wp-content/mu-plugins' );
787-
copy( __DIR__ . '/../extra/no-mail.php', $dest_dir . '/wp-content/mu-plugins/no-mail.php' );
787+
copy( dirname( __DIR__ ) . '/extra/no-mail.php', $dest_dir . '/wp-content/mu-plugins/no-mail.php' );
788+
789+
// Add polyfills.
790+
copy( dirname( __DIR__ ) . '/extra/polyfills.php', $dest_dir . '/wp-content/mu-plugins/polyfills.php' );
788791
}
789792

790793
public function create_config( $subdir = '', $extra_php = false ) {

features/bootstrap/support.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,3 @@ function check_that_yaml_string_contains_yaml_string( $actual_yaml, $expected_ya
215215

216216
return compare_contents( $expected_value, $actual_value );
217217
}
218-

features/extra/polyfills.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Polyfills used by Behat to support multiple versions of WP.
4+
*
5+
* This file will get installed as a must-use plugin in WP installs that are run
6+
* by the functional tests.
7+
*/
8+
9+
/*
10+
* Add a polyfill for the get_theme_file_uri(), as it is required for the
11+
* TwentyTwenty theme's starter content, and will fatal the site if you install
12+
* WP 5.3 first (setting TwentyTwenty as the active theme) and then downgrade
13+
* to a version of WP lower than 4.7.
14+
*
15+
* Note: This is a quick fix, and a cleaner solution would be to change the
16+
* active theme on downgrading, if the current theme declares it is not
17+
* supported.
18+
*
19+
* See: https://github.com/WordPress/twentytwenty/issues/973
20+
*/
21+
if ( ! function_exists( 'get_theme_file_uri' ) ) {
22+
/**
23+
* Retrieves the URL of a file in the theme.
24+
*
25+
* Searches in the stylesheet directory before the template directory so themes
26+
* which inherit from a parent theme can just override one file.
27+
*
28+
* @since 4.7.0
29+
*
30+
* @param string $file Optional. File to search for in the stylesheet directory.
31+
* @return string The URL of the file.
32+
*/
33+
function get_theme_file_uri( $file = '' ) {
34+
$file = ltrim( $file, '/' );
35+
36+
if ( empty( $file ) ) {
37+
$url = get_stylesheet_directory_uri();
38+
} elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
39+
$url = get_stylesheet_directory_uri() . '/' . $file;
40+
} else {
41+
$url = get_template_directory_uri() . '/' . $file;
42+
}
43+
44+
/**
45+
* Filters the URL to a file in the theme.
46+
*
47+
* @since 4.7.0
48+
*
49+
* @param string $url The file URL.
50+
* @param string $file The requested file to search for.
51+
*/
52+
return apply_filters( 'theme_file_uri', $url, $file );
53+
}
54+
}

phpcs.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@
7373
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
7474
</rule>
7575

76+
<!-- This is a procedural stand-alone file that is adding polyfills when
77+
applicable only. -->
78+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
79+
<exclude-pattern>*/extra/polyfills\.php$</exclude-pattern>
80+
</rule>
7681
</ruleset>

0 commit comments

Comments
 (0)