Skip to content
147 changes: 147 additions & 0 deletions includes/Checker/Preparations/Use_Minimal_Theme_Preparation.php
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
*
Comment thread
mukeshpanchal27 marked this conversation as resolved.
* @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 );
Comment thread
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 );
}
}
Comment thread
felixarntz marked this conversation as resolved.
1 change: 0 additions & 1 deletion plugin-check.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ function wp_plugin_check_display_composer_autoload_notice() {
echo '</p></div>';
}


wp_plugin_check_load();
77 changes: 77 additions & 0 deletions test-content/themes/wp-empty-theme/comments.php
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 -->
46 changes: 46 additions & 0 deletions test-content/themes/wp-empty-theme/footer.php
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>
Loading