WordPress.org

Make WordPress Core

Ticket #43992: 43992.10.diff

File 43992.10.diff, 6.4 KB (added by afragen, 20 months ago)

patch refreshed for 5.2 milestone

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

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index 419ba1659c..6e2371e1f0 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
     
    4648 * reading.
    4749 *
    4850 * @since 1.5.0
     51 * @since 5.2.0 Added `RequiresWP` and `RequiresPHP`.
    4952 *
    5053 * @param string $plugin_file Absolute path to the main plugin file.
    5154 * @param bool   $markup      Optional. If the returned data should have HTML markup applied.
     
    6366 *     @type string $TextDomain  Plugin textdomain.
    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 */
    6873function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
    function get_plugin_data( $plugin_file, $markup = true, $translate = true ) { 
    7782                'TextDomain'  => 'Text Domain',
    7883                'DomainPath'  => 'Domain Path',
    7984                'Network'     => 'Network',
     85                'RequiresWP'  => 'Requires WP',
     86                'RequiresPHP' => 'Requires PHP',
    8087                // Site Wide Only is deprecated in favor of Network.
    8188                '_sitewide'   => 'Site Wide Only',
    8289        );
    function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup 
    213220        return $plugin_data;
    214221}
    215222
     223/**
     224 * Get and return plugin data used for validation.
     225 *
     226 * Initially use the Plugin API as there's no current method to parse the local plugin readme.txt file.
     227 * Alternately see if a plugin header `Requires WP` or `Requires PHP` exists and use that.
     228 *
     229 * @since 5.2.0
     230 * @see validate_plugin_requirements()
     231 *
     232 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
     233 *
     234 * @return object $plugin_data Object of plugin data for validation.
     235 */
     236function get_plugin_validation_data( $plugin_file ) {
     237        $plugin_data = new stdClass();
     238        $slug        = dirname( $plugin_file );
     239        $url         = 'https://api.wordpress.org/plugins/info/1.2/';
     240        $url         = add_query_arg(
     241                array(
     242                        'action'                        => 'plugin_information',
     243                        rawurlencode( 'request[slug]' ) => $slug,
     244                ),
     245                $url
     246        );
     247        $response    = wp_remote_get( $url );
     248        if ( ! is_wp_error( $response ) ) {
     249                $plugin_data = json_decode( wp_remote_retrieve_body( $response ) );
     250        }
     251
     252        $invalid_check = isset( $plugin_data->error ) || is_wp_error( $response ) || $slug !== $plugin_data->slug;
     253
     254        /*
     255         * Plugin is likley not in the WP Plugin Directory but if they have designated
     256         * `Requires WP` and/or `Requires PHP` headers we can use those.
     257         */
     258        if ( $invalid_check ) {
     259                $plugin_data               = new stdClass();
     260                $plugin_data->file         = $plugin_file;
     261                $plugin_headers            = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file );
     262                $plugin_data->requires     = $plugin_headers['RequiresWP'];
     263                $plugin_data->requires_php = $plugin_headers['RequiresPHP'];
     264        }
     265
     266        return $plugin_data;
     267}
     268
    216269/**
    217270 * Get a list of a plugin's files.
    218271 *
    function is_network_only_plugin( $plugin ) { 
    675728 * ensure that the success redirection will update the error redirection.
    676729 *
    677730 * @since 2.5.0
     731 * @since 5.2.0 Test for WordPress version and PHP version compatibility.
    678732 *
    679733 * @param string $plugin       Path to the plugin file relative to the plugins directory.
    680734 * @param string $redirect     Optional. URL to redirect to.
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen 
    699753                return $valid;
    700754        }
    701755
     756        if ( ! validate_plugin_requirements( $plugin ) ) {
     757                return new WP_Error( 'plugin_activation_error', __( 'Plugin does not meet minimum WordPress and/or PHP requirements.' ) );
     758        }
     759
    702760        if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
    703761                if ( ! empty( $redirect ) ) {
    704762                        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 ) { 
    11941252        return 0;
    11951253}
    11961254
     1255/**
     1256 * Validate the plugin requirements for WP version and PHP version.
     1257 *
     1258 * @since 5.2.0
     1259 * @see activate_plugin()
     1260 *
     1261 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1262 *
     1263 * @return bool Default to true and if requirements met, false if not.
     1264 */
     1265function validate_plugin_requirements( $plugin ) {
     1266        $plugin_data  = get_plugin_validation_data( $plugin );
     1267        $wp_requires  = isset( $plugin_data->requires ) ? $plugin_data->requires : null;
     1268        $php_requires = isset( $plugin_data->requires_php ) ? $plugin_data->requires_php : null;
     1269
     1270        return is_compatible_wp( $wp_requires ) && is_compatible_php( $php_requires );
     1271}
     1272
    11971273/**
    11981274 * Whether the plugin can be uninstalled.
    11991275 *
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 9552455d05..36b91c7523 100644
    function wp_update_php_annotation() { 
    67416741        );
    67426742        echo'</p>';
    67436743}
     6744
     6745/**
     6746 * Check compatibility with current WordPress version.
     6747 *
     6748 * @since 5.2.0
     6749 *
     6750 * @param string $requires Minimum WordPress version from API.
     6751 *
     6752 * @return bool True if is compatible or empty, false if not.
     6753 */
     6754function is_compatible_wp( $requires ) {
     6755        $wp_version = get_bloginfo( 'version' );
     6756        return empty( $requires ) || version_compare( $wp_version, $requires, '>=' );
     6757}
     6758
     6759/**
     6760 * Check compatibility with current PHP version.
     6761 *
     6762 * @since 5.2.0
     6763 *
     6764 * @param string $requires Minimum PHP version from API.
     6765 *
     6766 * @return bool True if is compatible or empty, false if not.
     6767 */
     6768function is_compatible_php( $requires ) {
     6769        return empty( $requires ) || version_compare( phpversion(), $requires, '>=' );
     6770}