WordPress.org

Make WordPress Core

Changeset 45546


Ignore:
Timestamp:
06/18/2019 03:23:53 AM (4 months ago)
Author:
SergeyBiryukov
Message:

Plugins: When validating plugin's WordPress and PHP requirements, check for Requires at least and Requires PHP headers in the plugin's main PHP file.

This allows for blocking plugin activation if it requires a higher version of PHP or WordPress, and does not have a readme.txt file.

If the headers are defined in both readme.txt and the main plugin file, precedence is given to the plugin file.

Props afragen, Otto42, Ipstenu.
Fixes #46938.

File:
1 edited

Legend:

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

    r45448 r45546  
    3232 *          across all sites in an installation. This will prevent a plugin from being
    3333 *          activated on a single site when Multisite is enabled.
     34 *     Requires at least: Optional. Specify the minimum required WordPress version.
     35 *     Requires PHP: Optional. Specify the minimum required PHP version.
    3436 *      * / # Remove the space to close comment
    3537 *
     
    4749 *
    4850 * @since 1.5.0
     51 * @since 5.3.0 Added support for `Requires at least` and `Requires PHP`.
    4952 *
    5053 * @param string $plugin_file Absolute path to the main plugin file.
     
    6467 *     @type string $DomainPath  Plugins relative directory path to .mo files.
    6568 *     @type bool   $Network     Whether the plugin can only be activated network-wide.
     69 *     @type string $RequiresWP  Minimum required version of WordPress.
     70 *     @type string $RequiresPHP Minimum required version of PHP.
    6671 * }
    6772 */
     
    7883        'DomainPath'  => 'Domain Path',
    7984        'Network'     => 'Network',
     85        'RequiresWP'  => 'Requires at least',
     86        'RequiresPHP' => 'Requires PHP',
    8087        // Site Wide Only is deprecated in favor of Network.
    8188        '_sitewide'   => 'Site Wide Only',
     
    10861093function validate_plugin_requirements( $plugin ) {
    10871094    $readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';
     1095    $plugin_data = array(
     1096        'requires'     => '',
     1097        'requires_php' => '',
     1098    );
    10881099
    10891100    if ( file_exists( $readme_file ) ) {
     
    10961107            'plugin'
    10971108        );
    1098     } else {
    1099         return true;
    1100     }
     1109    }
     1110
     1111    $plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) );
     1112
     1113    // Check for headers in the plugin's PHP file, give precedence to the plugin headers.
     1114    $plugin_data['requires']     = ! empty( $plugin_data['RequiresWP'] ) ? $plugin_data['RequiresWP'] : $plugin_data['requires'];
     1115    $plugin_data['requires_php'] = ! empty( $plugin_data['RequiresPHP'] ) ? $plugin_data['RequiresPHP'] : $plugin_data['requires_php'];
    11011116
    11021117    $plugin_data['wp_compatible']  = is_wp_version_compatible( $plugin_data['requires'] );
    11031118    $plugin_data['php_compatible'] = is_php_version_compatible( $plugin_data['requires_php'] );
    1104 
    1105     $plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) );
    11061119
    11071120    if ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ) {
Note: See TracChangeset for help on using the changeset viewer.