From 450ec86dc1a61d1bc7b2ed495e4bd8fa943ead47 Mon Sep 17 00:00:00 2001 From: mattyrob Date: Tue, 17 Aug 2021 13:24:16 +0100 Subject: [PATCH] Add plugin header checking (#83) * Add plugin header checking This patch will scan plugin headers for plugins declaring a requirement for WordPress 5.0 and above. * Update patch to use WP_PLUGIN_DIR * Fix a HTML typos, add Requires at least: header to plugin file, and extend plugin checking to screen plugin header and readme as well as listing plugins that don't contain the Requires at least: header * Improved logic to prevent duplicate issue warnings under different sections based on testing feedback * Avoid array use when collecting plugin names from API conflict data --- lib/admin-page.php | 78 ++++++++++++++++++++++++++++++++++++-- switch-to-classicpress.php | 2 +- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/lib/admin-page.php b/lib/admin-page.php index b62784b..09d31cc 100644 --- a/lib/admin-page.php +++ b/lib/admin-page.php @@ -480,14 +480,63 @@ function classicpress_check_can_migrate() { // Check: Conflicting Plugins $plugins = get_option( 'active_plugins' ); - if ( isset( $parameters['plugins'] ) && $plugins !== array_diff( $plugins, $parameters['plugins'] ) ) { + $plugin_headers = array( 'Name' => 'Plugin Name', 'RequiresWP' => 'Requires at least' ); + $declared_incompatible_plugins = array(); + $undeclared_compatibility_plugins = array(); + + // Start by checking if plugins have declared they require WordPress 5.0 or higher + foreach ( $plugins as $plugin ) { + if ( isset( $parameters['plugins'] ) && in_array( $plugin, $parameters['plugins'] ) ) { + continue; + } + + $plugin_data = get_file_data( WP_PLUGIN_DIR . '/' . $plugin, $plugin_headers ); + $plugin_name = $plugin_data['Name']; + if ( version_compare( $plugin_data['RequiresWP'], '5.0' ) >= 0 ) { + $declared_incompatible_plugins[ $plugin ] = $plugin_name; + } else { + $plugin_files = get_plugin_files( $plugin ); + $readmes = array_filter( $plugin_files, function( $files ) { + return ( stripos( $files, 'readme') !== false ); + } ); + foreach( $readmes as $readme ) { + if ( empty( $readme ) ) { + continue; + } + $readme_data = get_file_data( WP_PLUGIN_DIR . '/' . $readme, $plugin_headers ); + if ( version_compare( $readme_data['RequiresWP'], '5.0' ) >= 0 ) { + $declared_incompatible_plugins[ $plugin ] = $plugin_name; + continue; + } + } + } + if ( + empty( $plugin_data['RequiresWP'] ) && + ( empty( $readmes ) || empty( $readme_data['RequiresWP'] ) ) && + false === array_key_exists( $plugin, $declared_incompatible_plugins ) + ) { + $undeclared_compatibility_plugins[ $plugin ] = $plugin_name; + } + } + + // Compare active plugins with API response of known conflicting plugins + if ( + isset( $parameters['plugins'] ) && $plugins !== array_diff( $plugins, $parameters['plugins'] ) || + ! empty( $declared_incompatible_plugins ) + ) { $preflight_checks['plugins'] = false; $conflicting_plugins = array_intersect( $parameters['plugins'], $plugins ); $conflicting_plugin_names = array(); foreach( $conflicting_plugins as $conflicting_plugin ) { - $conflicting_plugin_data[] = get_plugin_data( WP_CONTENT_DIR . '/plugins/' . $conflicting_plugin ); - $conflicting_plugin_names[] = $conflicting_plugin_data[0]['Name']; + $conflicting_plugin_data = get_plugin_data( WP_CONTENT_DIR . '/plugins/' . $conflicting_plugin ); + $conflicting_plugin_names[] = $conflicting_plugin_data['Name']; + } + + if ( ! empty( $declared_incompatible_plugins ) ) { + foreach( $declared_incompatible_plugins as $slug => $name ) { + $conflicting_plugin_names[] = $name; + } } echo "\n$icon_preflight_fail\n\n"; @@ -503,7 +552,7 @@ function classicpress_check_can_migrate() { echo "
\n"; /* translators: List of conflicting plugin names */ printf( __( - '%s', + '%s', 'switch-to-classicpress' ), implode( ', ', $conflicting_plugin_names ) ); } else { @@ -514,6 +563,27 @@ function classicpress_check_can_migrate() { 'switch-to-classicpress' ); } + echo "\n"; + + if ( ! empty( $undeclared_compatibility_plugins ) ) { + echo "\n$icon_preflight_warn\n\n"; + _e( + 'We have detected one or more plugins that fail to declare a minimum compatible WordPress version. They may prevent or impact on migrating your site to ClassicPress.', + 'switch-to-classicpress' + ); + echo "
\n"; + _e( + 'We would recommned deactivating the following plugins if you wish to continue migrating your site to ClassicPress:', + 'switch-to-classicpress' + ); + echo "
\n"; + /* translators: List of conflicting plugin names */ + printf( __( + '%s', + 'switch-to-classicpress' + ), implode( ', ', $undeclared_compatibility_plugins ) ); + echo "\n"; + } // Check: Supported PHP version $php_version_min = '5.6'; diff --git a/switch-to-classicpress.php b/switch-to-classicpress.php index 9320961..e93c131 100644 --- a/switch-to-classicpress.php +++ b/switch-to-classicpress.php @@ -10,7 +10,7 @@ * License URI: https://www.gnu.org/licenses/gpl-2.0.txt * Domain Path: /languages * Text Domain: switch-to-classicpress - * + * Requires at least: 4.9 * @package ClassicPress */