WordPress.org

Make WordPress Core

Ticket #43992: 43992-2.diff

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

Allow for plugins not in Plugin Directory but containing plugin headers Requires WP and/or Requires PHP

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

    diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
    index c898fc5169..9b101e6cc6 100644
    function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup 
    189189        return $plugin_data;
    190190}
    191191
     192/**
     193 * Query the Plugin API to get and return plugin data.
     194 *
     195 * Alternately see if a plugin header `Requires WP` or `Requires PHP` exists and use that if there's no API response.
     196 *
     197 * @since 5.1.0
     198 * @see validate_plugin_requirements()
     199 *
     200 * @param string $plugin Path to the plugin file relative to the plugins directory.
     201 *
     202 * @return array $plugin_data Array of Plugin API data.
     203 */
     204function get_plugin_api_data( $plugin_file ) {
     205        $plugin_data = new stdClass();
     206        $url         = 'https://api.wordpress.org/plugins/info/1.2/';
     207        $url         = add_query_arg(
     208                array(
     209                        'action'                        => 'plugin_information',
     210                        rawurlencode( 'request[slug]' ) => dirname( $plugin_file ),
     211                ),
     212                $url
     213        );
     214        $response    = wp_remote_get( $url );
     215        if ( ! is_wp_error( $response ) ) {
     216                $plugin_data = json_decode( wp_remote_retrieve_body( $response ) );
     217        }
     218
     219        /*
     220         * Plugin is likley not in the WP Plugin Directory but if they have designated
     221         * `Requires WP` and/or `Requires PHP` headers we can use those.
     222         */
     223        if ( isset( $plugin_data->error ) ) {
     224                $plugin_headers            = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_file );
     225                $plugin_data->requires     = $plugin_headers['Requires WP'];
     226                $plugin_data->requires_php = $plugin_headers['Requires PHP'];
     227        }
     228
     229        return $plugin_data;
     230}
     231
    192232/**
    193233 * Get a list of a plugin's files.
    194234 *
    function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silen 
    589629                return $valid;
    590630        }
    591631
     632        if ( validate_plugin_requirements( $plugin ) ) {
     633                return new WP_Error( 'plugin_invalid', __( 'Plugin does not meet minimum WordPress and/or PHP requirements.' ) );
     634        }
     635
    592636        if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
    593637                if ( ! empty( $redirect ) ) {
    594638                        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 ) { 
    10231067        return 0;
    10241068}
    10251069
     1070/**
     1071 * Validate the plugin requirements for WP version and PHP version.
     1072 * Plugin data retrieved from Plugin API call.
     1073 * Default to true if data not available from Plugin API.
     1074 *
     1075 * @since 5.1.0
     1076 * @see activate_plugin()
     1077 *
     1078 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1079 *
     1080 * @return bool
     1081 */
     1082function validate_plugin_requirements( $plugin ) {
     1083        $plugin_data  = get_plugin_api_data( $plugin );
     1084        $wp_requires  = isset( $plugin_data->requires ) ? $plugin_data->requires : null;
     1085        $php_requires = isset( $plugin_data->requires_php ) ? $plugin_data->requires_php : null;
     1086
     1087        return ! ( is_compatible_wp( $wp_requires ) &&  is_compatible_php( $php_requires ) );
     1088}
     1089
    10261090/**
    10271091 * Whether the plugin can be uninstalled.
    10281092 *
  • 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}