Skip to content

Commit b5fe136

Browse files
committed
Plugins and Themes: Add filters to validation functions.
It is now possible to add additional validation requirements for themes and plugins besides the built in core checks. Developed in #1812 and #10361. Props kraftbj, mukesh27, jorbin, joedolson. Fixes #54381. git-svn-id: https://develop.svn.wordpress.org/trunk@60995 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f73bfb9 commit b5fe136

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

src/wp-admin/includes/plugin.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,20 @@ function validate_plugin_requirements( $plugin ) {
12471247
);
12481248
}
12491249

1250-
return true;
1250+
/**
1251+
* Filters the plugin requirement validation response.
1252+
*
1253+
* If a plugin fails due to a Core-provided validation (incompatible WP, PHP versions), this
1254+
* filter will not fire. A WP_Error response will already be returned.
1255+
*
1256+
* This filter is intended to add additional validation steps by site administrators.
1257+
*
1258+
* @since 6.9.0
1259+
*
1260+
* @param bool|WP_Error $met_requirements True if the plugin meets requirements, WP_Error if not.
1261+
* @param string $plugin Path to the plugin file relative to the plugins directory.
1262+
*/
1263+
return apply_filters( 'validate_plugin_requirements', true, $plugin );
12511264
}
12521265

12531266
/**

src/wp-includes/theme.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,20 @@ function validate_theme_requirements( $stylesheet ) {
10021002
);
10031003
}
10041004

1005-
return true;
1005+
/**
1006+
* Filters the theme requirement validation response.
1007+
*
1008+
* If a theme fails due to a Core-provided validation (incompatible WP, PHP versions), this
1009+
* filter will not fire. A WP_Error response will already be returned.
1010+
*
1011+
* This filter is intended to add additional validation steps by site administrators.
1012+
*
1013+
* @since 6.9.0
1014+
*
1015+
* @param bool|WP_Error $met_requirements True if the theme meets requirements, WP_Error if not.
1016+
* @param string $stylesheet Directory name for the theme.
1017+
*/
1018+
return apply_filters( 'validate_theme_requirements', true, $stylesheet );
10061019
}
10071020

10081021
/**

tests/phpunit/tests/theme.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,4 +1529,54 @@ public function test_switch_to_blog_uses_original_template_path() {
15291529

15301530
$this->assertSame( $template_path, $new_template_path, 'Switching blogs switches the template path' );
15311531
}
1532+
1533+
/**
1534+
* Verify the validate_theme_requirements theme responds as expected for twentyten.
1535+
*
1536+
* @ticket 54381
1537+
*/
1538+
public function test_validate_theme_requirements_filter_default() {
1539+
// Default expectation since twentyten has the least strict requirements.
1540+
$this->assertTrue( validate_theme_requirements( 'twentyten' ) );
1541+
}
1542+
1543+
/**
1544+
* Verify that a filtered failure of validate_theme_requirements returns WP_Error
1545+
*
1546+
* @ticket 54381
1547+
*/
1548+
public function test_validate_theme_requirements_filter_error() {
1549+
// Adds an extra requirement that always fails.
1550+
add_filter(
1551+
'validate_theme_requirements',
1552+
function () {
1553+
return new WP_Error( 'theme_test_failed_requirement' );
1554+
}
1555+
);
1556+
1557+
$this->assertInstanceOf( 'WP_Error', validate_theme_requirements( 'twentyten' ) );
1558+
}
1559+
1560+
/**
1561+
* Verify that the theme is passed through to the validate_theme_requirements filter by selectively erroring.
1562+
*
1563+
* @ticket 54381
1564+
*/
1565+
public function test_validate_theme_requirements_filter_selective_failure() {
1566+
// Adds an extra requirement only for a particular theme.
1567+
add_filter(
1568+
'validate_theme_requirements',
1569+
function ( $met_requirements, $stylesheet ) {
1570+
if ( 'twentytwenty' === $stylesheet ) {
1571+
return new WP_Error( 'theme_test_failed_requirement' );
1572+
}
1573+
return $met_requirements;
1574+
},
1575+
10,
1576+
2
1577+
);
1578+
1579+
$this->assertTrue( validate_theme_requirements( 'twentyten' ) );
1580+
$this->assertInstanceOf( 'WP_Error', validate_theme_requirements( 'twentytwenty' ) );
1581+
}
15321582
}

0 commit comments

Comments
 (0)