WordPress.org

Make WordPress Core

Ticket #43992: 43992-4.diff

File 43992-4.diff, 5.6 KB (added by afragen, 2 years ago)

Allow for API response to error

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

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index c898fc5169..b6fa9ccce8 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.
     70
    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 
    189196        return $plugin_data;
    190197}
    191198
     199/**
     200 * Query the Plugin API to get and return plugin data.
     201 *
     202 * Alternately see if a plugin header `Requires WP` or `Requires PHP` exists and use that if there's no API response.
     203 *
     204 * @since 5.1.0
     205 * @see validate_plugin_requirements()
     206 *
     207 * @param string $plugin Path to the plugin file relative to the plugins directory.
     208 *
     209 * @return array $plugin_data Array of Plugin API data.
     210 */
     211function get_plugin_api_data( $plugin_file ) {
     212        $plugin_data = new stdClass();
     213        $url         = 'https://api.wordpress.org/plugins/info/1.2/';
     214        $url         = add_query_arg(
     215                array(
     216                        'action'                        => 'plugin_information',
     217                        rawurlencode( 'request[slug]' ) => dirname( $plugin_file ),
     218                ),
     219                $url
     220        );
     221        $response    = wp_remote_get( $url );
     222        if ( ! is_wp_error( $response ) ) {
     223                $plugin_data = json_decode( wp_remote_retrieve_body( $response ) );
     224        }
     225
     226        /*
     227         * Plugin is likley not in the WP Plugin Directory but if they have designated
     228         * `Requires WP` and/or `Requires PHP` headers we can use those.
     229         */
     230        if ( isset( $plugin_data->error ) || is_wp_error( $response ) ) {
     231                $plugin_headers            = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file );
     232                $plugin_data->requires     = $plugin_headers['RequiresWP'];
     233                $plugin_data->requires_php = $plugin_headers['RequiresPHP'];
     234        }
     235
     236        return $plugin_data;
     237}
     238
    192239/**
    193240 * Get a list of a plugin's files.
    194241 *
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen 
    589636                return $valid;
    590637        }
    591638
     639        if ( validate_plugin_requirements( $plugin ) ) {
     640                return new WP_Error( 'plugin_invalid', __( 'Plugin does not meet minimum WordPress and/or PHP requirements.' ) );
     641        }
     642
    592643        if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
    593644                if ( ! empty( $redirect ) ) {
    594645                        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 ) { 
    10231074        return 0;
    10241075}
    10251076
     1077/**
     1078 * Validate the plugin requirements for WP version and PHP version.
     1079 * Plugin data retrieved from Plugin API call.
     1080 * Default to true if data not available from Plugin API.
     1081 *
     1082 * @since 5.1.0
     1083 * @see activate_plugin()
     1084 *
     1085 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1086 *
     1087 * @return bool
     1088 */
     1089function validate_plugin_requirements( $plugin ) {
     1090        $plugin_data  = get_plugin_api_data( $plugin );
     1091        $wp_requires  = isset( $plugin_data->requires ) ? $plugin_data->requires : null;
     1092        $php_requires = isset( $plugin_data->requires_php ) ? $plugin_data->requires_php : null;
     1093
     1094        return ! ( is_compatible_wp( $wp_requires ) &&  is_compatible_php( $php_requires ) );
     1095}
     1096
    10261097/**
    10271098 * Whether the plugin can be uninstalled.
    10281099 *
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 471fcbb5d6..d0652ea436 100644
    function wp_privacy_delete_old_export_files() { 
    65536553                }
    65546554        }
    65556555}
     6556
     6557/**
     6558 * Check compatibility with current WordPress version.
     6559 *
     6560 * @since 5.1.0
     6561 *
     6562 * @param string $requires Minimum WordPress version from API.
     6563 *
     6564 * @return bool Returns true if requirement met or empty.
     6565 */
     6566function is_compatible_wp( $requires ) {
     6567        $wp_version = get_bloginfo( 'version' );
     6568        return ( empty( $requires ) || version_compare( substr( $wp_version, 0, strlen( $requires ) ), $requires, '>=' ) );
     6569}
     6570
     6571// add to wp-includes/functions.php
     6572/**
     6573 * Check compatibility with current PHP version.
     6574 *
     6575 * @since 5.1.0
     6576 *
     6577 * @param string $requires Minimum PHP version from API.
     6578 *
     6579 * @return bool Returns true if requirement met or empty.
     6580 */
     6581function is_compatible_php( $requires ) {
     6582        return ( empty( $requires ) || version_compare( substr( phpversion(), 0, strlen( $requires ) ), $requires, '>=' ) );
     6583}