Skip to content

Commit

Permalink
Add polyfill for get_theme_file_uri() (#72)
Browse files Browse the repository at this point in the history
Add polyfill for `get_theme_file_uri()`
  • Loading branch information
schlessera authored Nov 8, 2019
2 parents 593ec79 + f32ffe6 commit 006eca0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
7 changes: 5 additions & 2 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,12 @@ public function download_wp( $subdir = '' ) {

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

// disable emailing
// Disable emailing.
mkdir( $dest_dir . '/wp-content/mu-plugins' );
copy( __DIR__ . '/../extra/no-mail.php', $dest_dir . '/wp-content/mu-plugins/no-mail.php' );
copy( dirname( __DIR__ ) . '/extra/no-mail.php', $dest_dir . '/wp-content/mu-plugins/no-mail.php' );

// Add polyfills.
copy( dirname( __DIR__ ) . '/extra/polyfills.php', $dest_dir . '/wp-content/mu-plugins/polyfills.php' );
}

public function create_config( $subdir = '', $extra_php = false ) {
Expand Down
1 change: 0 additions & 1 deletion features/bootstrap/support.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,3 @@ function check_that_yaml_string_contains_yaml_string( $actual_yaml, $expected_ya

return compare_contents( $expected_value, $actual_value );
}

54 changes: 54 additions & 0 deletions features/extra/polyfills.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Polyfills used by Behat to support multiple versions of WP.
*
* This file will get installed as a must-use plugin in WP installs that are run
* by the functional tests.
*/

/*
* Add a polyfill for the get_theme_file_uri(), as it is required for the
* TwentyTwenty theme's starter content, and will fatal the site if you install
* WP 5.3 first (setting TwentyTwenty as the active theme) and then downgrade
* to a version of WP lower than 4.7.
*
* Note: This is a quick fix, and a cleaner solution would be to change the
* active theme on downgrading, if the current theme declares it is not
* supported.
*
* See: https://github.com/WordPress/twentytwenty/issues/973
*/
if ( ! function_exists( 'get_theme_file_uri' ) ) {
/**
* Retrieves the URL of a file in the theme.
*
* Searches in the stylesheet directory before the template directory so themes
* which inherit from a parent theme can just override one file.
*
* @since 4.7.0
*
* @param string $file Optional. File to search for in the stylesheet directory.
* @return string The URL of the file.
*/
function get_theme_file_uri( $file = '' ) {
$file = ltrim( $file, '/' );

if ( empty( $file ) ) {
$url = get_stylesheet_directory_uri();
} elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
$url = get_stylesheet_directory_uri() . '/' . $file;
} else {
$url = get_template_directory_uri() . '/' . $file;
}

/**
* Filters the URL to a file in the theme.
*
* @since 4.7.0
*
* @param string $url The file URL.
* @param string $file The requested file to search for.
*/
return apply_filters( 'theme_file_uri', $url, $file );
}
}
5 changes: 5 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
</rule>

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

0 comments on commit 006eca0

Please sign in to comment.