WordPress.org

Make WordPress Core

Ticket #43992: 43992-9.diff

File 43992-9.diff, 5.7 KB (added by afragen, 23 months ago)

fix version_compare so 5.1.10 is greater than 5.1.2 and true

  • src/wp-admin/includes/plugin.php

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index c898fc5169..31f6ae2b7d 100644
     
    3131 *     Network: Optional. Specify "Network: true" to require that a plugin is activated
    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 WP: 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 *
    3638 * Some users have issues with opening large files and manipulating the contents
     
    6365 *     @type string $TextDomain  Plugin textdomain.
    6466 *     @type string $DomainPath  Plugins relative directory path to .mo files.
    6567 *     @type bool   $Network     Whether the plugin can only be activated network-wide.
     68 *     @type string $RequiresWP  Minimum required version of WordPress.
     69 *     @type string $RequiresPHP Minimum required version of PHP.
    6670 * }
    6771 */
    6872function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
    function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { 
    7781                'TextDomain'  => 'Text Domain',
    7882                'DomainPath'  => 'Domain Path',
    7983                'Network'     => 'Network',
     84                'RequiresWP'  => 'Requires WP',
     85                'RequiresPHP' => 'Requires PHP',
    8086                // Site Wide Only is deprecated in favor of Network.
    8187                '_sitewide'   => 'Site Wide Only',
    8288        );
    function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup 
    189195        return $plugin_data;
    190196}
    191197
     198/**
     199 * Get the and return plugin data used for validation.
     200 *
     201 * Initially use the Plugin API as there's no current method to parse the local plugin readme.txt file.
     202 * Alternately see if a plugin header `Requires WP` or `Requires PHP` exists and use that.
     203 *
     204 * @since 5.1.0
     205 * @see validate_plugin_requirements()
     206 *
     207 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
     208 *
     209 * @return object $plugin_data Object of plugin data for validation.
     210 */
     211function get_plugin_validation_data( $plugin_file ) {
     212        $plugin_data = new stdClass();
     213        $slug        = dirname( $plugin_file );
     214        $url         = 'https://api.wordpress.org/plugins/info/1.2/';
     215        $url         = add_query_arg(
     216                array(
     217                        'action'                        => 'plugin_information',
     218                        rawurlencode( 'request[slug]' ) => $slug,
     219                ),
     220                $url
     221        );
     222        $response    = wp_remote_get( $url );
     223        if ( ! is_wp_error( $response ) ) {
     224                $plugin_data = json_decode( wp_remote_retrieve_body( $response ) );
     225        }
     226
     227        $invalid_check = isset( $plugin_data->error ) || is_wp_error( $response ) || $slug !== $plugin_data->slug;
     228
     229        /*
     230         * Plugin is likley not in the WP Plugin Directory but if they have designated
     231         * `Requires WP` and/or `Requires PHP` headers we can use those.
     232         */
     233        if ( $invalid_check ) {
     234                $plugin_data               = new stdClass();
     235                $plugin_data->file         = $plugin_file;
     236                $plugin_headers            = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file );
     237                $plugin_data->requires     = $plugin_headers['RequiresWP'];
     238                $plugin_data->requires_php = $plugin_headers['RequiresPHP'];
     239        }
     240
     241        return $plugin_data;
     242}
     243
    192244/**
    193245 * Get a list of a plugin's files.
    194246 *
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen 
    589641                return $valid;
    590642        }
    591643
     644        if ( ! validate_plugin_requirements( $plugin ) ) {
     645                return new WP_Error( 'plugin_activation_error', __( 'Plugin does not meet minimum WordPress and/or PHP requirements.' ) );
     646        }
     647
    592648        if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
    593649                if ( ! empty( $redirect ) ) {
    594650                        wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) ); // we'll override this later if the plugin can be included without fatal error
    function validate_plugin( $plugin ) { 
    10231079        return 0;
    10241080}
    10251081
     1082/**
     1083 * Validate the plugin requirements for WP version and PHP version.
     1084 *
     1085 * @since 5.1.0
     1086 * @see activate_plugin()
     1087 *
     1088 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1089 *
     1090 * @return bool Default to true and if requirements met, false if not.
     1091 */
     1092function validate_plugin_requirements( $plugin ) {
     1093        $plugin_data  = get_plugin_validation_data( $plugin );
     1094        $wp_requires  = isset( $plugin_data->requires ) ? $plugin_data->requires : null;
     1095        $php_requires = isset( $plugin_data->requires_php ) ? $plugin_data->requires_php : null;
     1096
     1097        return is_compatible_wp( $wp_requires ) && is_compatible_php( $php_requires );
     1098}
     1099
    10261100/**
    10271101 * Whether the plugin can be uninstalled.
    10281102 *
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index b00a47c1d8..d4cd0582b2 100644
    function wp_privacy_delete_old_export_files() { 
    65806580                }
    65816581        }
    65826582}
     6583
     6584/**
     6585 * Check compatibility with current WordPress version.
     6586 *
     6587 * @since 5.1.0
     6588 *
     6589 * @param string $requires Minimum WordPress version from API.
     6590 *
     6591 * @return bool True if is compatible or empty, false if not.
     6592 */
     6593function is_compatible_wp( $requires ) {
     6594        $wp_version = get_bloginfo( 'version' );
     6595        return empty( $requires ) || version_compare( $wp_version, $requires, '>=' );
     6596}
     6597
     6598/**
     6599 * Check compatibility with current PHP version.
     6600 *
     6601 * @since 5.1.0
     6602 *
     6603 * @param string $requires Minimum PHP version from API.
     6604 *
     6605 * @return bool True if is compatible or empty, false if not.
     6606 */
     6607function is_compatible_php( $requires ) {
     6608        return empty( $requires ) || version_compare( phpversion(), $requires, '>=' );
     6609}