WordPress.org

Make WordPress Core

Changeset 44978


Ignore:
Timestamp:
03/22/2019 12:36:30 AM (3 months ago)
Author:
SergeyBiryukov
Message:

Plugins: Block plugin activation if it requires a higher version of PHP or WordPress.

Introduce validate_plugin_requirements() for validating a plugin's WordPress and PHP version requirements.

Introduce wp_is_wp_compatible() and wp_is_php_compatible() for checking compatibility with the current WordPress or PHP version.

Props afragen, joyously, DrewAPicture, TimothyBlynJacobs, desrosj, flixos90, SergeyBiryukov.
See #43992.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/plugin.php

    r44973 r44978  
    598598 *
    599599 * @since 2.5.0
     600 * @since 5.2.0 Test for WordPress version and PHP version compatibility.
    600601 *
    601602 * @param string $plugin       Path to the plugin file relative to the plugins directory.
     
    620621    if ( is_wp_error( $valid ) ) {
    621622        return $valid;
     623    }
     624
     625    $requirements = validate_plugin_requirements( $plugin );
     626    if ( is_wp_error( $requirements ) ) {
     627        return $requirements;
    622628    }
    623629
     
    10591065    }
    10601066    return 0;
     1067}
     1068
     1069/**
     1070 * Validate the plugin requirements for WP version and PHP version.
     1071 *
     1072 * @since 5.2.0
     1073 *
     1074 * @param string $plugin Path to the plugin file relative to the plugins directory.
     1075 * @return true|WP_Error True if requirements are met, WP_Error on failure.
     1076 */
     1077function validate_plugin_requirements( $plugin ) {
     1078    $readme_file = WP_PLUGIN_DIR . '/' . dirname( $plugin ) . '/readme.txt';
     1079
     1080    if ( file_exists( $readme_file ) ) {
     1081        $plugin_data = get_file_data(
     1082            $readme_file,
     1083            array(
     1084                'requires'     => 'Requires at least',
     1085                'requires_php' => 'Requires PHP',
     1086            ),
     1087            'plugin'
     1088        );
     1089    } else {
     1090        return true;
     1091    }
     1092
     1093    $plugin_data['wp_compatible']  = wp_is_wp_compatible( $plugin_data['requires'] );
     1094    $plugin_data['php_compatible'] = wp_is_php_compatible( $plugin_data['requires_php'] );
     1095
     1096    $plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) );
     1097
     1098    if ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ) {
     1099        return new WP_Error( 'plugin_wp_php_incompatible', sprintf(
     1100            /* translators: %s: plugin name */
     1101            __( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ), $plugin_data['Name'] )
     1102        );
     1103    } elseif ( ! $plugin_data['php_compatible'] ) {
     1104        return new WP_Error( 'plugin_php_incompatible', sprintf(
     1105            /* translators: %s: plugin name */
     1106            __( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ), $plugin_data['Name'] )
     1107        );
     1108    } elseif ( ! $plugin_data['wp_compatible'] ) {
     1109        return new WP_Error( 'plugin_wp_incompatible', sprintf(
     1110            /* translators: %s: plugin name */
     1111            __( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ), $plugin_data['Name'] )
     1112        );
     1113    }
     1114
     1115    return true;
    10611116}
    10621117
  • trunk/src/wp-includes/functions.php

    r44935 r44978  
    68986898    echo '</p>';
    68996899}
     6900
     6901/**
     6902 * Checks compatibility with the current WordPress version.
     6903 *
     6904 * @since 5.2.0
     6905 *
     6906 * @param string $required Minimum required WordPress version.
     6907 * @return bool True if required version is compatible or empty, false if not.
     6908 */
     6909function wp_is_wp_compatible( $required ) {
     6910    $wp_version = get_bloginfo( 'version' );
     6911
     6912    return empty( $required ) || version_compare( $wp_version, $required, '>=' );
     6913}
     6914
     6915/**
     6916 * Checks compatibility with the current PHP version.
     6917 *
     6918 * @since 5.2.0
     6919 *
     6920 * @param string $required Minimum required PHP version.
     6921 * @return bool True if required version is compatible or empty, false if not.
     6922 */
     6923function wp_is_php_compatible( $required ) {
     6924    return empty( $required ) || version_compare( phpversion(), $required, '>=' );
     6925}
Note: See TracChangeset for help on using the changeset viewer.