Skip to content
15 changes: 14 additions & 1 deletion src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,20 @@ function validate_theme_requirements( $stylesheet ) {
);
}

return true;
/**
* Filters the theme requirement validation response.
*
* If a theme fails due to a Core-provided validation (incompatible WP, PHP versions), this
* filter will not fire. A WP_Error response will already be returned.
*
* This filter is intended to add additional validation steps by site administrators.
*
* @since 6.9.0
*
* @param bool|WP_Error $met_requirements True if the theme meets requirements, WP_Error if not.
* @param string $stylesheet Directory name for the theme.
*/
return apply_filters( 'validate_theme_requirements', true, $stylesheet );
}

/**
Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/tests/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -1469,4 +1469,48 @@ public function test_switch_to_blog_uses_original_template_path() {

$this->assertSame( $template_path, $new_template_path, 'Switching blogs switches the template path' );
}

/**
* Verify the validate_theme_requirements theme responds as expected for twentyten.
*/
public function test_validate_theme_requirements_filter_default() {
// Default expectation since twentyten has the least strict requirements.
$this->assertTrue( validate_theme_requirements( 'twentyten' ) );
}

/**
* Verify that a filtered failure of validate_theme_requirements returns WP_Error
*/
public function test_validate_theme_requirements_filter_error() {
// Adds an extra requirement that always fails.
add_filter(
'validate_theme_requirements',
function () {
return new WP_Error( 'theme_test_failed_requirement' );
}
);

$this->assertInstanceOf( 'WP_Error', validate_theme_requirements( 'twentyten' ) );
}

/**
* Verify that the theme is passed through to the validate_theme_requirements filter by selectively erroring.
*/
public function test_validate_theme_requirements_filter_selective_failure() {
// Adds an extra requirement only for a particular theme.
add_filter(
'validate_theme_requirements',
function( $met_requirements, $stylesheet ) {
if ( 'twentytwenty' === $stylesheet ) {
return new WP_Error( 'theme_test_failed_requirement' );
}
return $met_requirements;
},
10,
2
);

$this->assertTrue( validate_theme_requirements( 'twentyten' ) );
$this->assertInstanceOf( 'WP_Error', validate_theme_requirements( 'twentytwenty' ) );
}
}
Loading