Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tc_filename() to return theme-relative filenames #370

Merged
merged 6 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
= nextrelease =
* Fix: `tc_filename()` to always return theme-relative paths when possible.
* Fix: Correct the block theme adaptation check to work in WordPress.org Theme Uploader

= 20210617 =
* Introduced a generic function, run_themechecks_against_theme(), that allows it to be run against a WP_Theme instance, rather than relying upon global state
* Adapted checks for block themes. Create a list of the checks that are skipped for block themes (full site editing themes)
Expand Down
38 changes: 36 additions & 2 deletions checkbase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
global $checkcount;
$checkcount = 0;

// current WP_Theme being tested. Internal use only.
global $theme_check_current_theme;
$theme_check_current_theme = false;

// interface that all checks should implement.
interface themecheck {

Expand Down Expand Up @@ -91,7 +95,10 @@ function run_themechecks_against_theme( $theme, $theme_slug ) {
* @return bool
*/
function run_themechecks( $php, $css, $other, $context = array() ) {
global $themechecks;
global $themechecks, $theme_check_current_theme;

// Provide context to some functions that need to know the current theme, but aren't passed the object.
$theme_check_current_theme = isset( $context['theme'] ) ? $context['theme'] : false;

$pass = true;

Expand All @@ -107,6 +114,8 @@ function run_themechecks( $php, $css, $other, $context = array() ) {
}
}

$theme_check_current_theme = false;

return $pass;
}

Expand Down Expand Up @@ -205,7 +214,32 @@ function tc_preg( $preg, $file ) {
}

function tc_filename( $file ) {
$filename = ( preg_match( '/themes\/[a-z0-9-]*\/(.*)/', $file, $out ) ) ? $out[1] : basename( $file );
global $theme_check_current_theme;

$filename = false;

// If we know the WP_Theme object, we can get the exact path.
if ( $theme_check_current_theme ) {
$theme_files = $theme_check_current_theme->get_files(
null /* all file types */,
-1 /* infinite recursion */,
true /* include parent theme files */
);

$filename = array_search( $file, $theme_files, true );
}

// If the $file exists within a theme-like folder, use that.
// Does not support themes nested in directories such as wp-content/themes/pub/wporg-themes/index.php
if ( ! $filename && preg_match( '!/themes/[^/]+/(.*)$!i', $file, $out ) ) {
$filename = $out[1];
}

// If still nothing, use the basename.
if ( ! $filename ) {
$filename = basename( $file );
}

return $filename;
}

Expand Down
3 changes: 1 addition & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ If **either** of these two vars are defined a new trac tickbox will appear next

== Changelog ==

= 20200504.1 =
* Changes can be found in the changelog.txt file.
Changes can be found in [the changelog.txt file](https://github.com/WordPress/theme-check/blob/master/changelog.txt).