Make WordPress Core

Ticket #43992: 43992.22.diff

File 43992.22.diff, 7.2 KB (added by afragen, 4 years ago)

oops, missed @param in docblock

  • wp-admin/includes/plugin.php

    diff --git a/wp-admin/includes/plugin.php b/wp-admin/includes/plugin.php
    index 5873a33466..2668cf5beb 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 * Return plugin data for validation with WP version and PHP version.
     225 *
     226 * Uses get_file_data() to parse local readme.txt.
     227 * Valid data in readme.txt takes priority.
     228 * If valid data is not in readme.txt check if plugin header `Requires WP` or `Requires PHP` exists and use that.
     229 *
     230 * @since 5.2.0
     231 * @see validate_plugin_requirements()
     232 *
     233 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
     234 *
     235 * @return array $plugin_data Array of plugin data for validation.
     236 */
     237function get_plugin_validation_data( $plugin_file ) {
     238        $validation_headers = array(
     239                'requires'     => 'requires at least',
     240                'requires_php' => 'requires php',
     241        );
     242        $plugin_data        = null;
     243        $readme_file        = WP_PLUGIN_DIR . '/' . dirname( $plugin_file ) . '/readme.txt';
     244        if ( file_exists( $readme_file ) ) {
     245                $plugin_data = get_file_data( $readme_file, $validation_headers );
     246        }
     247
     248        // Plugin might have `Requires WP` and/or `Requires PHP` headers we can use.
     249        $plugin_headers = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file, false, false );
     250
     251        $plugin_data['file']           = $plugin_file;
     252        $plugin_data['name']           = $plugin_headers['Name'];
     253        $plugin_data['requires']       = empty( $plugin_data['requires'] ) ? $plugin_headers['RequiresWP'] : $plugin_data['requires'];
     254        $plugin_data['requires_php']   = empty( $plugin_data['requires_php'] ) ? $plugin_headers['RequiresPHP'] : $plugin_data['requires_php'];
     255        $plugin_data['wp_compatible']  = wp_is_wp_compatible( $plugin_data['requires'] );
     256        $plugin_data['php_compatible'] = wp_is_php_compatible( $plugin_data['requires_php'] );
     257
     258        return $plugin_data;
     259}
     260
    216261/**
    217262 * Get a list of a plugin's files.
    218263 *
    function is_network_only_plugin( $plugin ) { 
    595640 * ensure that the success redirection will update the error redirection.
    596641 *
    597642 * @since 2.5.0
     643 * @since 5.2.0 Test for WordPress version and PHP version compatibility.
    598644 *
    599645 * @param string $plugin       Path to the plugin file relative to the plugins directory.
    600646 * @param string $redirect     Optional. URL to redirect to.
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen 
    619665                return $valid;
    620666        }
    621667
     668        if ( ! validate_plugin_requirements( $plugin ) ) {
     669                return plugin_validation_error_message( $plugin );
     670        }
     671
    622672        if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
    623673                if ( ! empty( $redirect ) ) {
    624674                        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 ) { 
    10581108        return 0;
    10591109}
    10601110
     1111/**
     1112 * Validate the plugin requirements for WP version and PHP version.
     1113 *
     1114 * @since 5.2.0
     1115 * @see activate_plugin()
     1116 *
     1117 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1118 *
     1119 * @return bool Default to true and if requirements met, false if not.
     1120 */
     1121function validate_plugin_requirements( $plugin ) {
     1122        $plugin_data = get_plugin_validation_data( $plugin );
     1123
     1124        return $plugin_data['wp_compatible'] && $plugin_data['php_compatible'];
     1125}
     1126
     1127/**
     1128 * Return appropriate plugin validation error message.
     1129 *
     1130 * @since 5.2.0
     1131 *
     1132 * string $plugin_file Path to the plugin file relative to the plugins directory.
     1133 *
     1134 * @return WP_Error
     1135 */
     1136function plugin_validation_error_message( $plugin_file ) {
     1137        $plugin_data = get_plugin_validation_data( $plugin_file );
     1138
     1139        switch ( $plugin_data ) {
     1140                case ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ):
     1141                        $code    = __( 'plugin_wp_php_incompatible' );
     1142                        $message = sprintf( __( 'Current WordPress and PHP versions do not meet minimum requirements for %s.' ), $plugin_data['name'] );
     1143                        break;
     1144                case ! $plugin_data['php_compatible']:
     1145                        $code    = __( 'plugin_php_incompatible' );
     1146                        $message = sprintf( __( 'Current PHP version does not meet minimum requirements for %s.' ), $plugin_data['name'] );
     1147                        break;
     1148                case ! $plugin_data['wp_compatible']:
     1149                        $code    = __( 'plugin_wp_incompatible' );
     1150                        $message = sprintf( __( 'Current WordPress version does not meet minimum requirements for %s.' ), $plugin_data['name'] );
     1151                        break;
     1152        }
     1153
     1154        return new WP_Error( $code, $message );
     1155}
     1156
    10611157/**
    10621158 * Whether the plugin can be uninstalled.
    10631159 *
  • wp-includes/functions.php

    diff --git a/wp-includes/functions.php b/wp-includes/functions.php
    index 86cdabe7e0..09e18be372 100644
    a b function wp_direct_php_update_button() { 
    68976897        );
    68986898        echo '</p>';
    68996899}
     6900
     6901/**
     6902 * Check compatibility with current WordPress version.
     6903 *
     6904 * @since 5.2.0
     6905 *
     6906 * @param string $requires Minimum WordPress version from API.
     6907 *
     6908 * @return bool True if is compatible or empty, false if not.
     6909 */
     6910function wp_is_wp_compatible( $requires ) {
     6911        $wp_version = get_bloginfo( 'version' );
     6912
     6913        return empty( $requires ) || version_compare( $wp_version, $requires, '>=' );
     6914}
     6915
     6916/**
     6917 * Check compatibility with current PHP version.
     6918 *
     6919 * @since 5.2.0
     6920 *
     6921 * @param string $requires Minimum PHP version from API.
     6922 *
     6923 * @return bool True if is compatible or empty, false if not.
     6924 */
     6925function wp_is_php_compatible( $requires ) {
     6926        return empty( $requires ) || version_compare( phpversion(), $requires, '>=' );
     6927}