From 78e868e4c750d5aeafe7e054b5415643f7310ace Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 12 Apr 2019 23:55:19 +0000 Subject: [PATCH] Plugins: Introduce `is_wp_version_compatible()` and `is_php_version_compatible()` for checking compatibility with the current WordPress or PHP version. Props afragen. Fixes #46599. git-svn-id: https://develop.svn.wordpress.org/trunk@45185 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-plugin-install-list-table.php | 9 ++++--- .../includes/class-wp-plugins-list-table.php | 3 ++- src/wp-admin/includes/plugin-install.php | 9 ++++--- src/wp-admin/includes/plugin.php | 4 +-- src/wp-admin/includes/update.php | 3 ++- src/wp-admin/update-core.php | 3 ++- src/wp-includes/functions.php | 26 ++++++++++++++++++- 7 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/wp-admin/includes/class-wp-plugin-install-list-table.php b/src/wp-admin/includes/class-wp-plugin-install-list-table.php index d47b388466..0b2de90613 100644 --- a/src/wp-admin/includes/class-wp-plugin-install-list-table.php +++ b/src/wp-admin/includes/class-wp-plugin-install-list-table.php @@ -502,11 +502,12 @@ class WP_Plugin_Install_List_Table extends WP_List_Table { $author = ' ' . sprintf( __( 'By %s' ), $author ) . ''; } - $wp_version = get_bloginfo( 'version' ); + $requires_php = isset( $plugin['requires_php'] ) ? $plugin['requires_php'] : null; + $requires_wp = isset( $plugin['requires'] ) ? $plugin['requires'] : null; - $compatible_php = ( empty( $plugin['requires_php'] ) || version_compare( phpversion(), $plugin['requires_php'], '>=' ) ); - $tested_wp = ( empty( $plugin['tested'] ) || version_compare( $wp_version, $plugin['tested'], '<=' ) ); - $compatible_wp = ( empty( $plugin['requires'] ) || version_compare( $wp_version, $plugin['requires'], '>=' ) ); + $compatible_php = is_php_version_compatible( $requires_php ); + $compatible_wp = is_wp_version_compatible( $requires_wp ); + $tested_wp = ( empty( $plugin['tested'] ) || version_compare( get_bloginfo( 'version' ), $plugin['tested'], '<=' ) ); $action_links = array(); diff --git a/src/wp-admin/includes/class-wp-plugins-list-table.php b/src/wp-admin/includes/class-wp-plugins-list-table.php index 79c0590f0b..90dec9dae5 100644 --- a/src/wp-admin/includes/class-wp-plugins-list-table.php +++ b/src/wp-admin/includes/class-wp-plugins-list-table.php @@ -761,7 +761,8 @@ class WP_Plugins_List_Table extends WP_List_Table { } - $compatible_php = ( empty( $plugin_data['requires_php'] ) || version_compare( phpversion(), $plugin_data['requires_php'], '>=' ) ); + $requires_php = isset( $plugin['requires_php'] ) ? $plugin['requires_php'] : null; + $compatible_php = is_php_version_compatible( $requires_php ); $class = $is_active ? 'active' : 'inactive'; $checkbox_id = 'checkbox_' . md5( $plugin_data['Name'] ); if ( $restrict_network_active || $restrict_network_only || in_array( $status, array( 'mustuse', 'dropins' ) ) || ! $compatible_php ) { diff --git a/src/wp-admin/includes/plugin-install.php b/src/wp-admin/includes/plugin-install.php index 99939b04b9..b5f770d65a 100644 --- a/src/wp-admin/includes/plugin-install.php +++ b/src/wp-admin/includes/plugin-install.php @@ -754,11 +754,12 @@ function install_plugin_information() {
requires_php ) ? $api->requires_php : null; + $requires_wp = isset( $api->requires ) ? $api->requires : null; - $compatible_php = ( empty( $api->requires_php ) || version_compare( phpversion(), $api->requires_php, '>=' ) ); - $tested_wp = ( empty( $api->tested ) || version_compare( $wp_version, $api->tested, '<=' ) ); - $compatible_wp = ( empty( $api->requires ) || version_compare( $wp_version, $api->requires, '>=' ) ); + $compatible_php = is_php_version_compatible( $requires_php ); + $compatible_wp = is_wp_version_compatible( $requires_wp ); + $tested_wp = ( empty( $api->tested ) || version_compare( get_bloginfo( 'version' ), $api->tested, '<=' ) ); if ( ! $compatible_php ) { echo '

'; diff --git a/src/wp-admin/includes/plugin.php b/src/wp-admin/includes/plugin.php index b8dcdbe53b..7a1b038518 100644 --- a/src/wp-admin/includes/plugin.php +++ b/src/wp-admin/includes/plugin.php @@ -1099,8 +1099,8 @@ function validate_plugin_requirements( $plugin ) { return true; } - $plugin_data['wp_compatible'] = version_compare( get_bloginfo( 'version' ), $plugin_data['requires'], '>=' ); - $plugin_data['php_compatible'] = version_compare( phpversion(), $plugin_data['requires_php'], '>=' ); + $plugin_data['wp_compatible'] = is_wp_version_compatible( $plugin_data['requires'] ); + $plugin_data['php_compatible'] = is_php_version_compatible( $plugin_data['requires_php'] ); $plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) ); diff --git a/src/wp-admin/includes/update.php b/src/wp-admin/includes/update.php index 9c365ff423..1a4bee003a 100644 --- a/src/wp-admin/includes/update.php +++ b/src/wp-admin/includes/update.php @@ -400,7 +400,8 @@ function wp_plugin_update_row( $file, $plugin_data ) { $active_class = is_plugin_active( $file ) ? ' active' : ''; } - $compatible_php = ( empty( $response->requires_php ) || version_compare( phpversion(), $response->requires_php, '>=' ) ); + $requires_php = isset( $response->requires_php ) ? $response->requires_php : null; + $compatible_php = is_php_version_compatible( $requires_php ); $notice_type = $compatible_php ? 'notice-warning' : 'notice-error'; echo '

'; diff --git a/src/wp-admin/update-core.php b/src/wp-admin/update-core.php index 555d67efab..004bd554c0 100644 --- a/src/wp-admin/update-core.php +++ b/src/wp-admin/update-core.php @@ -295,7 +295,8 @@ function list_plugin_updates() { } } - $compatible_php = ( empty( $plugin_data->update->requires_php ) || version_compare( phpversion(), $plugin_data->update->requires_php, '>=' ) ); + $requires_php = isset( $plugin_data->update->requires_php ) ? $plugin_data->update->requires_php : null; + $compatible_php = is_php_version_compatible( $requires_php ); if ( ! $compatible_php && current_user_can( 'update_php' ) ) { $compat .= '
' . __( 'This update doesn’t work with your version of PHP.' ) . ' '; diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index c222bc52a8..a30e7164fa 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3417,7 +3417,7 @@ function _xml_wp_die_handler( $message, $title = '', $args = array() ) { {$r['response']} - + EOD; @@ -7121,3 +7121,27 @@ function recurse_dirsize( $directory, $exclude = null, $max_execution_time = nul } return $size; } + +/** +* Checks compatibility with the current WordPress version. +* +* @since 5.2.0 +* +* @param string $required Minimum required WordPress version. +* @return bool True if required version is compatible or empty, false if not. +*/ +function is_wp_version_compatible( $required ) { + return empty( $required ) || version_compare( get_bloginfo( 'version' ), $required, '>=' ); +} + +/** + * Checks compatibility with the current PHP version. + * + * @since 5.2.0 + * + * @param string $required Minimum required PHP version. + * @return bool True if required version is compatible or empty, false if not. + */ +function is_php_version_compatible( $required ) { + return empty( $required ) || version_compare( phpversion(), $required, '>=' ); +}