-
Notifications
You must be signed in to change notification settings - Fork 98
Create Minimal_Theme_Preparation #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6b9b731
add Use_Minimal_Theme_Preparation
jjgrainger b4a0272
add tests
jjgrainger ec3a076
add wp-empty-theme
jjgrainger d427caa
update theme directories transient to ensure registered theme is avai…
jjgrainger 54a5b58
Merge branch 'trunk' into feature/create-minimal-theme-preparation
jjgrainger 1179f3c
use search_theme_directories in cleanup function
jjgrainger e7b26dd
update tests
jjgrainger c593fbd
update prepare tests
jjgrainger c962dac
update cleanup function and tests
jjgrainger 3865cbb
cleanup tests
jjgrainger 61e9d78
run cleanup callback before assertions
jjgrainger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
includes/Checker/Preparations/Use_Minimal_Theme_Preparation.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| <?php | ||
| /** | ||
| * Class WordPress\Plugin_Check\Checker\Preparations\Use_Minimal_Theme_Preparation | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace WordPress\Plugin_Check\Checker\Preparations; | ||
|
|
||
| use WordPress\Plugin_Check\Checker\Preparation; | ||
| use Exception; | ||
|
|
||
| /** | ||
| * Class for the preparation step to force usage of a minimal theme. | ||
| * | ||
| * This ensures the plugin is checked as much in isolation as possible. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| class Use_Minimal_Theme_Preparation implements Preparation { | ||
|
|
||
| /** | ||
| * Theme slug / directory name. | ||
| * | ||
| * @since n.e.x.t | ||
| * @var string | ||
| */ | ||
| protected $theme_slug; | ||
|
|
||
| /** | ||
| * Absolute path to themes root directory. | ||
| * | ||
| * @since n.e.x.t | ||
| * @var string | ||
| */ | ||
| protected $themes_dir; | ||
|
|
||
| /** | ||
| * Sets the theme slug and themes root directory. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $theme_slug Slug of the theme to enforce. | ||
| * @param string $themes_dir Optional. Absolute path to themes root directory, if not the regular wp-content/themes. | ||
| */ | ||
| public function __construct( $theme_slug, $themes_dir = '' ) { | ||
| $this->theme_slug = $theme_slug; | ||
| $this->themes_dir = $themes_dir; | ||
| } | ||
|
|
||
| /** | ||
| * Runs this preparation step for the environment and returns a cleanup function. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @global array $wp_theme_directories | ||
| * | ||
| * @return callable Cleanup function to revert any changes made here. | ||
| * | ||
| * @throws Exception Thrown when preparation fails. | ||
| */ | ||
| public function prepare() { | ||
| // Override the theme slug and name. | ||
| add_filter( 'template', array( $this, 'get_theme_slug' ) ); | ||
| add_filter( 'stylesheet', array( $this, 'get_theme_slug' ) ); | ||
| add_filter( 'pre_option_template', array( $this, 'get_theme_slug' ) ); | ||
| add_filter( 'pre_option_stylesheet', array( $this, 'get_theme_slug' ) ); | ||
| add_filter( 'pre_option_current_theme', array( $this, 'get_theme_name' ) ); | ||
|
|
||
| // Override the theme directory. | ||
| add_filter( 'pre_option_template_root', array( $this, 'get_theme_root' ) ); | ||
| add_filter( 'pre_option_stylesheet_root', array( $this, 'get_theme_root' ) ); | ||
|
|
||
| // Register the custom themes directory if relevant. | ||
| if ( ! empty( $this->themes_dir ) ) { | ||
| register_theme_directory( $this->themes_dir ); | ||
|
|
||
| // Force new directory scan to ensure the test theme directory is available. | ||
| search_theme_directories( true ); | ||
| } | ||
|
|
||
| // Return the cleanup function. | ||
| return function() { | ||
| global $wp_theme_directories; | ||
|
|
||
| remove_filter( 'template', array( $this, 'get_theme_slug' ) ); | ||
| remove_filter( 'stylesheet', array( $this, 'get_theme_slug' ) ); | ||
| remove_filter( 'pre_option_template', array( $this, 'get_theme_slug' ) ); | ||
| remove_filter( 'pre_option_stylesheet', array( $this, 'get_theme_slug' ) ); | ||
| remove_filter( 'pre_option_current_theme', array( $this, 'get_theme_name' ) ); | ||
|
|
||
| remove_filter( 'pre_option_template_root', array( $this, 'get_theme_root' ) ); | ||
| remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_theme_root' ) ); | ||
|
|
||
| if ( ! empty( $this->themes_dir ) ) { | ||
| $index = array_search( untrailingslashit( $this->themes_dir ), $wp_theme_directories, true ); | ||
| if ( false !== $index ) { | ||
| array_splice( $wp_theme_directories, $index, 1 ); | ||
| $wp_theme_directories = array_values( $wp_theme_directories ); | ||
|
felixarntz marked this conversation as resolved.
|
||
|
|
||
| // Force new directory scan to remove the test theme directory. | ||
| search_theme_directories( true ); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the theme slug. | ||
| * | ||
| * Used as a filter callback. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return string The theme slug. | ||
| */ | ||
| public function get_theme_slug() { | ||
| return $this->theme_slug; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the theme name. | ||
| * | ||
| * Used as a filter callback. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return string The theme name. | ||
| */ | ||
| public function get_theme_name() { | ||
| $theme = wp_get_theme( $this->theme_slug, $this->themes_dir ); | ||
| return $theme->display( 'Name' ); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the theme root. | ||
| * | ||
| * Used as a filter callback. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return string The theme root. | ||
| */ | ||
| public function get_theme_root() { | ||
| return get_raw_theme_root( $this->theme_slug, true ); | ||
| } | ||
| } | ||
|
felixarntz marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,5 +78,4 @@ function wp_plugin_check_display_composer_autoload_notice() { | |
| echo '</p></div>'; | ||
| } | ||
|
|
||
|
|
||
| wp_plugin_check_load(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
| /** | ||
| * The template for displaying comments | ||
| * | ||
| * @package wp-empty-theme | ||
| * @link https://developer.wordpress.org/themes/basics/template-hierarchy/ | ||
| */ | ||
|
|
||
| if ( post_password_required() ) { | ||
| return; | ||
| } | ||
|
|
||
| ?> | ||
|
|
||
| <div id="comments" class="comments-area default-max-width <?php echo get_option( 'show_avatars' ) ? 'show-avatars' : ''; ?>"> | ||
| <?php | ||
| if ( have_comments() ) { | ||
| ?> | ||
| <h2 class="comments-title"> | ||
| <?php | ||
| if ( '1' === get_comments_number() ) { | ||
| esc_html_e( '1 comment', 'wp-empty-theme' ); | ||
| } else { | ||
| printf( | ||
| /* translators: %s: Comment count number. */ | ||
| esc_html( _nx( '%s comment', '%s comments', get_comments_number(), 'Comments title', 'wp-empty-theme' ) ), | ||
| esc_html( number_format_i18n( get_comments_number() ) ) | ||
| ); | ||
| } | ||
| ?> | ||
| </h2><!-- .comments-title --> | ||
|
|
||
| <ol class="comment-list"> | ||
| <?php | ||
| wp_list_comments( | ||
| array( | ||
| 'avatar_size' => 60, | ||
| 'style' => 'ol', | ||
| 'short_ping' => true, | ||
| ) | ||
| ); | ||
| ?> | ||
| </ol><!-- .comment-list --> | ||
|
|
||
| <?php | ||
| the_comments_pagination( | ||
| array( | ||
| 'before_page_number' => esc_html__( 'Page', 'wp-empty-theme' ) . ' ', | ||
| 'mid_size' => 0, | ||
| 'prev_text' => sprintf( | ||
| '<span class="nav-prev-text">%s</span>', | ||
| esc_html__( 'Older comments', 'wp-empty-theme' ) | ||
| ), | ||
| 'next_text' => sprintf( | ||
| '<span class="nav-next-text">%s</span>', | ||
| esc_html__( 'Newer comments', 'wp-empty-theme' ) | ||
| ), | ||
| ) | ||
| ); | ||
|
|
||
| if ( ! comments_open() ) { | ||
| ?> | ||
| <p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'wp-empty-theme' ); ?></p> | ||
| <?php | ||
| } | ||
| } | ||
|
|
||
| comment_form( | ||
| array( | ||
| 'logged_in_as' => null, | ||
| 'title_reply' => esc_html__( 'Leave a comment', 'wp-empty-theme' ), | ||
| 'title_reply_before' => '<h2 id="reply-title" class="comment-reply-title">', | ||
| 'title_reply_after' => '</h2>', | ||
| ) | ||
| ); | ||
| ?> | ||
| </div><!-- #comments --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| <?php | ||
| /** | ||
| * The template for displaying the footer | ||
| * | ||
| * @package wp-empty-theme | ||
| * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials | ||
| */ | ||
|
|
||
| ?> | ||
| </main><!-- #main --> | ||
| </div><!-- #primary --> | ||
| </div><!-- #content --> | ||
|
|
||
| <footer id="colophon" class="site-footer" role="contentinfo"> | ||
| <div class="site-info"> | ||
| <div class="site-name"> | ||
| <?php | ||
| if ( get_bloginfo( 'name' ) ) { | ||
| if ( is_front_page() && ! is_paged() ) { | ||
| bloginfo( 'name' ); | ||
| } else { | ||
| ?> | ||
| <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a> | ||
| <?php | ||
| } | ||
| } | ||
| ?> | ||
| </div><!-- .site-name --> | ||
| <div class="powered-by"> | ||
| <?php | ||
| printf( | ||
| /* translators: %s: WordPress. */ | ||
| esc_html__( 'Proudly powered by %s.', 'wp-empty-theme' ), | ||
| '<a href="' . esc_url( __( 'https://wordpress.org/', 'wp-empty-theme' ) ) . '">WordPress</a>' | ||
| ); | ||
| ?> | ||
| </div><!-- .powered-by --> | ||
| </div><!-- .site-info --> | ||
| </footer><!-- #colophon --> | ||
|
|
||
| </div><!-- #page --> | ||
|
|
||
| <?php wp_footer(); ?> | ||
|
|
||
| </body> | ||
| </html> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.