From 54c7af16666f2d3984cd5dd8bee9f2020c3b0e7f Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 6 Jul 2021 03:15:34 +0000 Subject: [PATCH 1/6] Allow `tc_filename()` to return theme-relative filenames when themes are loaded from custom directories, or are nested within other folders in wp-content/themes. --- checkbase.php | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/checkbase.php b/checkbase.php index 13207a00..ae200827 100644 --- a/checkbase.php +++ b/checkbase.php @@ -91,7 +91,12 @@ 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. + if ( isset( $context['theme'] ) ) { + $theme_check_current_theme = $context['theme']; + } $pass = true; @@ -107,6 +112,8 @@ function run_themechecks( $php, $css, $other, $context = array() ) { } } + unset( $theme_check_current_theme ); + return $pass; } @@ -205,7 +212,40 @@ 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 ( ! empty( $theme_check_current_theme ) ) { + + $root = trailingslashit( $theme_check_current_theme->get_theme_root() ); + if ( $root === substr( $file, 0, strlen( $root ) ) ) { + // Trim the root path off first. + $filename = substr( $file, strlen( $root ) ); + + // Trim off the Stylesheet or Template from the file path. + $stylesheet = $theme_check_current_theme->get_stylesheet(); + $template = $theme_check_current_theme->get_template(); + if ( 0 === strpos( $filename, $stylesheet ) ) { + $filename = substr( $filename, strlen( $stylesheet ) + 1 ); + } elseif ( 0 === strpos( $filename, $template ) ) { + $filename = substr( $filename, strlen( $template ) + 1 ); + } + } + } + + // If the $file exists within a theme-like folder, use that/ + // does not support themes nested in directories such as wp-content/themes/public/my-theme/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; } From fb9f99b191b2547f2a84adae17cc99ac382c4097 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 6 Jul 2021 03:24:47 +0000 Subject: [PATCH 2/6] Pre-define the global theme_check_current_theme --- checkbase.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/checkbase.php b/checkbase.php index ae200827..53dba51e 100644 --- a/checkbase.php +++ b/checkbase.php @@ -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 { @@ -217,7 +221,7 @@ function tc_filename( $file ) { $filename = false; // If we know the WP_Theme object, we can get the exact path. - if ( ! empty( $theme_check_current_theme ) ) { + if ( $theme_check_current_theme ) { $root = trailingslashit( $theme_check_current_theme->get_theme_root() ); if ( $root === substr( $file, 0, strlen( $root ) ) ) { From f0dabbbae470fb0e99bb4b11bf0ddd6d25bc3a61 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 6 Jul 2021 03:26:07 +0000 Subject: [PATCH 3/6] Reset the global variable to false when the Theme is no longer known. --- checkbase.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/checkbase.php b/checkbase.php index 53dba51e..82b49328 100644 --- a/checkbase.php +++ b/checkbase.php @@ -98,9 +98,7 @@ function run_themechecks( $php, $css, $other, $context = array() ) { global $themechecks, $theme_check_current_theme; // Provide context to some functions that need to know the current theme, but aren't passed the object. - if ( isset( $context['theme'] ) ) { - $theme_check_current_theme = $context['theme']; - } + $theme_check_current_theme = isset( $context['theme'] ) ? $context['theme'] : false; $pass = true; @@ -116,7 +114,7 @@ function run_themechecks( $php, $css, $other, $context = array() ) { } } - unset( $theme_check_current_theme ); + $theme_check_current_theme = false; return $pass; } From 2665df9ef3238f25578f1474364329bb8b508b2c Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 6 Jul 2021 03:33:02 +0000 Subject: [PATCH 4/6] Add changelog --- changelog.txt | 4 ++++ readme.txt | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index bebc6cc9..92251910 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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) diff --git a/readme.txt b/readme.txt index b9b456cc..772c6a26 100644 --- a/readme.txt +++ b/readme.txt @@ -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). From 04044a38db76281b72f3d4360b92a902a0ebe7de Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 6 Jul 2021 04:01:08 +0000 Subject: [PATCH 5/6] Remove custom file path handling logic, rely upon WP_Theme to find the theme-relative path if possible. --- checkbase.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/checkbase.php b/checkbase.php index 82b49328..e7ec9584 100644 --- a/checkbase.php +++ b/checkbase.php @@ -220,21 +220,13 @@ function tc_filename( $file ) { // 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 */ + ); - $root = trailingslashit( $theme_check_current_theme->get_theme_root() ); - if ( $root === substr( $file, 0, strlen( $root ) ) ) { - // Trim the root path off first. - $filename = substr( $file, strlen( $root ) ); - - // Trim off the Stylesheet or Template from the file path. - $stylesheet = $theme_check_current_theme->get_stylesheet(); - $template = $theme_check_current_theme->get_template(); - if ( 0 === strpos( $filename, $stylesheet ) ) { - $filename = substr( $filename, strlen( $stylesheet ) + 1 ); - } elseif ( 0 === strpos( $filename, $template ) ) { - $filename = substr( $filename, strlen( $template ) + 1 ); - } - } + $filename = array_search( $file, $theme_files, true ); } // If the $file exists within a theme-like folder, use that/ From 3712213f04b1737ad2afa66622eeecd318a7e76e Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 6 Jul 2021 04:21:31 +0000 Subject: [PATCH 6/6] Clarify comment --- checkbase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/checkbase.php b/checkbase.php index e7ec9584..261cd444 100644 --- a/checkbase.php +++ b/checkbase.php @@ -229,8 +229,8 @@ function tc_filename( $file ) { $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/public/my-theme/index.php + // 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]; }