WordPress.org

Make WordPress Core

Ticket #43992: 43992.15.diff

File 43992.15.diff, 6.0 KB (added by afragen, 21 months ago)

Updated from feedback

  • wp-admin/includes/plugin.php

    diff --git a/wp-admin/includes/plugin.php b/wp-admin/includes/plugin.php
    index 05e3861f17..323ed07c0e 100644
    a b  
    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 * @uses get_file_data() to parse local `readme.txt`.
     227 * If data not in `readme.txt` 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 array $plugin_data Array of plugin data for validation.
     235 */
     236function get_plugin_validation_data( $plugin_file ) {
     237        $validation_headers = array(
     238                'requires'     => 'requires at least',
     239                'requires_php' => 'requires php',
     240        );
     241        $plugin_data        = null;
     242        $readme_file        = WP_PLUGIN_DIR . '/' . dirname( $plugin_file ) . '/readme.txt';
     243        if ( file_exists( $readme_file ) ) {
     244                $plugin_data = get_file_data( $readme_file, $validation_headers );
     245        }
     246
     247        /*
     248         * Plugin has no `readme.txt` file but might have
     249         * `Requires WP` and/or `Requires PHP` headers we can use.
     250         */
     251        if ( null === $plugin_data ) {
     252                $plugin_data['file']         = $plugin_file;
     253                $plugin_headers              = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file, false, false );
     254                $plugin_data['requires']     = $plugin_headers['RequiresWP'];
     255                $plugin_data['requires_php'] = $plugin_headers['RequiresPHP'];
     256        }
     257
     258        return $plugin_data;
     259}
     260
    216261/**
    217262 * Get a list of a plugin's files.
    218263 *
    function is_network_only_plugin( $plugin ) { 
    675720 * ensure that the success redirection will update the error redirection.
    676721 *
    677722 * @since 2.5.0
     723 * @since 5.2.0 Test for WordPress version and PHP version compatibility.
    678724 *
    679725 * @param string $plugin       Path to the plugin file relative to the plugins directory.
    680726 * @param string $redirect     Optional. URL to redirect to.
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen 
    699745                return $valid;
    700746        }
    701747
     748        if ( ! validate_plugin_requirements( $plugin ) ) {
     749                return new WP_Error( 'unmet_requirements', __( 'Plugin does not meet minimum WordPress and/or PHP requirements.' ) );
     750        }
     751
    702752        if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
    703753                if ( ! empty( $redirect ) ) {
    704754                        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 ) { 
    11991249        return 0;
    12001250}
    12011251
     1252/**
     1253 * Validate the plugin requirements for WP version and PHP version.
     1254 *
     1255 * @since 5.2.0
     1256 * @see activate_plugin()
     1257 *
     1258 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1259 *
     1260 * @return bool Default to true and if requirements met, false if not.
     1261 */
     1262function validate_plugin_requirements( $plugin ) {
     1263        $plugin_data  = get_plugin_validation_data( $plugin );
     1264        $wp_requires  = isset( $plugin_data['requires'] ) ? $plugin_data['requires'] : null;
     1265        $php_requires = isset( $plugin_data['requires_php'] ) ? $plugin_data['requires_php'] : null;
     1266
     1267        return is_wp_compatible( $wp_requires ) && is_php_compatible( $php_requires );
     1268}
     1269
    12021270/**
    12031271 * Whether the plugin can be uninstalled.
    12041272 *
  • wp-includes/functions.php

    diff --git a/wp-includes/functions.php b/wp-includes/functions.php
    index 214c134d01..b25bc6e74a 100644
    a b function wp_update_php_annotation() { 
    68306830        );
    68316831        echo'</p>';
    68326832}
     6833
     6834/**
     6835 * Check compatibility with current WordPress version.
     6836 *
     6837 * @since 5.2.0
     6838 *
     6839 * @param string $requires Minimum WordPress version from API.
     6840 *
     6841 * @return bool True if is compatible or empty, false if not.
     6842 */
     6843function is_wp_compatible( $requires ) {
     6844        $wp_version = get_bloginfo( 'version' );
     6845        return empty( $requires ) || version_compare( $wp_version, $requires, '>=' );
     6846}
     6847
     6848/**
     6849 * Check compatibility with current PHP version.
     6850 *
     6851 * @since 5.2.0
     6852 *
     6853 * @param string $requires Minimum PHP version from API.
     6854 *
     6855 * @return bool True if is compatible or empty, false if not.
     6856 */
     6857function is_php_compatible( $requires ) {
     6858        return empty( $requires ) || version_compare( phpversion(), $requires, '>=' );
     6859}