Make WordPress Core

Changeset 47574


Ignore:
Timestamp:
04/13/2020 03:29:17 PM (6 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/plugin.php

    r47557 r47574  
    11011101
    11021102/**
    1103  * Validate the plugin requirements for WP version and PHP version.
     1103 * Validates the plugin requirements for WordPress version and PHP version.
     1104 *
     1105 * Uses the information from `Requires at least` and `Requires PHP` headers
     1106 * defined in the plugin's main PHP file.
     1107 *
     1108 * If the headers are not present in the plugin's main PHP file,
     1109 * `readme.txt` is also checked as a fallback.
    11041110 *
    11051111 * @since 5.2.0
     1112 * @since 5.3.0 Added support for reading the headers from the plugin's
     1113 *              main PHP file, with `readme.txt` as a fallback.
    11061114 *
    11071115 * @param string $plugin Path to the plugin file relative to the plugins directory.
     
    11091117 */
    11101118function validate_plugin_requirements( $plugin ) {
     1119        $plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
     1120
     1121        $requirements = array(
     1122                'requires'     => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '',
     1123                'requires_php' => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '',
     1124        );
     1125
    11111126        $readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';
    1112         $plugin_data = array(
    1113                 'requires'     => '',
    1114                 'requires_php' => '',
    1115         );
    11161127
    11171128        if ( file_exists( $readme_file ) ) {
    1118                 $plugin_data = get_file_data(
     1129                $readme_headers = get_file_data(
    11191130                        $readme_file,
    11201131                        array(
     
    11241135                        'plugin'
    11251136                );
    1126         }
    1127 
    1128         $plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) );
    1129 
    1130         // Check for headers in the plugin's PHP file, give precedence to the plugin headers.
    1131         $plugin_data['requires']     = ! empty( $plugin_data['RequiresWP'] ) ? $plugin_data['RequiresWP'] : $plugin_data['requires'];
    1132         $plugin_data['requires_php'] = ! empty( $plugin_data['RequiresPHP'] ) ? $plugin_data['RequiresPHP'] : $plugin_data['requires_php'];
    1133 
    1134         $plugin_data['wp_compatible']  = is_wp_version_compatible( $plugin_data['requires'] );
    1135         $plugin_data['php_compatible'] = is_php_version_compatible( $plugin_data['requires_php'] );
    1136 
    1137         if ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ) {
     1137
     1138                $requirements = array_merge( $readme_headers, $requirements );
     1139        }
     1140
     1141        $compatible_wp  = is_wp_version_compatible( $requirements['requires'] );
     1142        $compatible_php = is_php_version_compatible( $requirements['requires_php'] );
     1143
     1144        if ( ! $compatible_wp && ! $compatible_php ) {
    11381145                return new WP_Error(
    11391146                        'plugin_wp_php_incompatible',
    11401147                        sprintf(
    11411148                                /* translators: %s: Plugin name. */
    1142                                 __( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ),
    1143                                 $plugin_data['Name']
     1149                                _x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'plugin' ),
     1150                                $plugin_headers['Name']
    11441151                        )
    11451152                );
    1146         } elseif ( ! $plugin_data['php_compatible'] ) {
     1153        } elseif ( ! $compatible_php ) {
    11471154                return new WP_Error(
    11481155                        'plugin_php_incompatible',
    11491156                        sprintf(
    11501157                                /* translators: %s: Plugin name. */
    1151                                 __( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ),
    1152                                 $plugin_data['Name']
     1158                                _x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'plugin' ),
     1159                                $plugin_headers['Name']
    11531160                        )
    11541161                );
    1155         } elseif ( ! $plugin_data['wp_compatible'] ) {
     1162        } elseif ( ! $compatible_wp ) {
    11561163                return new WP_Error(
    11571164                        'plugin_wp_incompatible',
    11581165                        sprintf(
    11591166                                /* translators: %s: Plugin name. */
    1160                                 __( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ),
    1161                                 $plugin_data['Name']
     1167                                _x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'plugin' ),
     1168                                $plugin_headers['Name']
    11621169                        )
    11631170                );
Note: See TracChangeset for help on using the changeset viewer.