Plugins: Simplify the logic of `validate_plugin_requirements()`, update documentation.

This updates the function for consistency with `validate_theme_requirements()`.

Follow-up to [44978], [45546], [47573].

Fixes #43992.
Built from https://develop.svn.wordpress.org/trunk@47574


git-svn-id: http://core.svn.wordpress.org/trunk@47349 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-04-13 15:31:05 +00:00
parent d341f20fb1
commit ae59482c80
2 changed files with 31 additions and 24 deletions

View File

@ -1100,22 +1100,33 @@ function validate_plugin( $plugin ) {
} }
/** /**
* Validate the plugin requirements for WP version and PHP version. * Validates the plugin requirements for WordPress version and PHP version.
*
* Uses the information from `Requires at least` and `Requires PHP` headers
* defined in the plugin's main PHP file.
*
* If the headers are not present in the plugin's main PHP file,
* `readme.txt` is also checked as a fallback.
* *
* @since 5.2.0 * @since 5.2.0
* @since 5.3.0 Added support for reading the headers from the plugin's
* main PHP file, with `readme.txt` as a fallback.
* *
* @param string $plugin Path to the plugin file relative to the plugins directory. * @param string $plugin Path to the plugin file relative to the plugins directory.
* @return true|WP_Error True if requirements are met, WP_Error on failure. * @return true|WP_Error True if requirements are met, WP_Error on failure.
*/ */
function validate_plugin_requirements( $plugin ) { function validate_plugin_requirements( $plugin ) {
$readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt'; $plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
$plugin_data = array(
'requires' => '', $requirements = array(
'requires_php' => '', 'requires' => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '',
'requires_php' => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '',
); );
$readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';
if ( file_exists( $readme_file ) ) { if ( file_exists( $readme_file ) ) {
$plugin_data = get_file_data( $readme_headers = get_file_data(
$readme_file, $readme_file,
array( array(
'requires' => 'Requires at least', 'requires' => 'Requires at least',
@ -1123,42 +1134,38 @@ function validate_plugin_requirements( $plugin ) {
), ),
'plugin' 'plugin'
); );
$requirements = array_merge( $readme_headers, $requirements );
} }
$plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) ); $compatible_wp = is_wp_version_compatible( $requirements['requires'] );
$compatible_php = is_php_version_compatible( $requirements['requires_php'] );
// Check for headers in the plugin's PHP file, give precedence to the plugin headers. if ( ! $compatible_wp && ! $compatible_php ) {
$plugin_data['requires'] = ! empty( $plugin_data['RequiresWP'] ) ? $plugin_data['RequiresWP'] : $plugin_data['requires'];
$plugin_data['requires_php'] = ! empty( $plugin_data['RequiresPHP'] ) ? $plugin_data['RequiresPHP'] : $plugin_data['requires_php'];
$plugin_data['wp_compatible'] = is_wp_version_compatible( $plugin_data['requires'] );
$plugin_data['php_compatible'] = is_php_version_compatible( $plugin_data['requires_php'] );
if ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ) {
return new WP_Error( return new WP_Error(
'plugin_wp_php_incompatible', 'plugin_wp_php_incompatible',
sprintf( sprintf(
/* translators: %s: Plugin name. */ /* translators: %s: Plugin name. */
__( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ), _x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'plugin' ),
$plugin_data['Name'] $plugin_headers['Name']
) )
); );
} elseif ( ! $plugin_data['php_compatible'] ) { } elseif ( ! $compatible_php ) {
return new WP_Error( return new WP_Error(
'plugin_php_incompatible', 'plugin_php_incompatible',
sprintf( sprintf(
/* translators: %s: Plugin name. */ /* translators: %s: Plugin name. */
__( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ), _x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'plugin' ),
$plugin_data['Name'] $plugin_headers['Name']
) )
); );
} elseif ( ! $plugin_data['wp_compatible'] ) { } elseif ( ! $compatible_wp ) {
return new WP_Error( return new WP_Error(
'plugin_wp_incompatible', 'plugin_wp_incompatible',
sprintf( sprintf(
/* translators: %s: Plugin name. */ /* translators: %s: Plugin name. */
__( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ), _x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'plugin' ),
$plugin_data['Name'] $plugin_headers['Name']
) )
); );
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.5-alpha-47573'; $wp_version = '5.5-alpha-47574';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.