| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 |
|
| 5 |
use Plugin_Upgrader; |
| 6 |
use WP_Error; |
| 7 |
use WP_Upgrader; |
| 8 |
use Yoast\WP\SEO\Conditionals\Check_Required_Version_Conditional; |
| 9 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 10 |
|
| 11 |
/** |
| 12 |
* The Check_Required_Version class. |
| 13 |
* |
| 14 |
* This class checks if the required version of Yoast SEO is installed. |
| 15 |
* It also adds the `Requires Yoast SEO` header to the list of headers and updates the comparison table for the plugin overwrite. |
| 16 |
*/ |
| 17 |
class Check_Required_Version implements Integration_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* Initializes the integration. |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function register_hooks() { |
| 25 |
\add_filter( 'upgrader_source_selection', [ $this, 'check_required_version' ], 10, 3 ); |
| 26 |
\add_filter( 'install_plugin_overwrite_comparison', [ $this, 'update_comparison_table' ], 10, 3 ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns the conditionals based on which this loadable should be active. |
| 31 |
* |
| 32 |
* @return string[] The conditionals based on which this loadable should be active. |
| 33 |
*/ |
| 34 |
public static function get_conditionals() { |
| 35 |
return [ Check_Required_Version_Conditional::class ]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Checks if the required version of Yoast SEO is installed. |
| 40 |
* |
| 41 |
* The code is partly inspired by Plugin_Upgrader::check_package() in wp-admin/includes/class-plugin-upgrader.php. |
| 42 |
* |
| 43 |
* @param string $source File source location. |
| 44 |
* @param string|null $remote_source Remote file source location. |
| 45 |
* @param WP_Upgrader|null $upgrader WP_Upgrader instance. |
| 46 |
* |
| 47 |
* @return string|WP_Error The source location or a WP_Error object if the required version is not installed. |
| 48 |
*/ |
| 49 |
public function check_required_version( $source, $remote_source = null, $upgrader = null ) { |
| 50 |
global $wp_filesystem; |
| 51 |
|
| 52 |
// Bail out early if we are not installing/upgrading a plugin. |
| 53 |
if ( ! $upgrader instanceof Plugin_Upgrader ) { |
| 54 |
return $source; |
| 55 |
} |
| 56 |
|
| 57 |
$info = []; |
| 58 |
|
| 59 |
if ( \is_wp_error( $source ) ) { |
| 60 |
return $source; |
| 61 |
} |
| 62 |
|
| 63 |
$working_directory = \str_replace( $wp_filesystem->wp_content_dir(), \trailingslashit( \WP_CONTENT_DIR ), $source ); |
| 64 |
if ( ! \is_dir( $working_directory ) ) { // Confidence check, if the above fails, let's not prevent installation. |
| 65 |
return $source; |
| 66 |
} |
| 67 |
|
| 68 |
// Check that the folder contains at least 1 valid plugin. |
| 69 |
$files = \glob( $working_directory . '*.php' ); |
| 70 |
if ( $files ) { |
| 71 |
foreach ( $files as $file ) { |
| 72 |
$info = \get_plugin_data( $file, false, false ); |
| 73 |
if ( ! empty( $info['Name'] ) ) { |
| 74 |
break; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$requires_yoast_seo = ! empty( $info['Requires Yoast SEO'] ) ? $info['Requires Yoast SEO'] : false; |
| 80 |
|
| 81 |
if ( ! $this->check_requirement( $requires_yoast_seo ) ) { |
| 82 |
$error = \sprintf( |
| 83 |
/* translators: 1: Current Yoast SEO version, 2: Version required by the uploaded plugin. */ |
| 84 |
\__( 'The Yoast SEO version on your site is %1$s, however the uploaded plugin requires %2$s.', 'wordpress-seo' ), |
| 85 |
\WPSEO_VERSION, |
| 86 |
\esc_html( $requires_yoast_seo ), |
| 87 |
); |
| 88 |
|
| 89 |
return new WP_Error( |
| 90 |
'incompatible_yoast_seo_required_version', |
| 91 |
\__( 'The package could not be installed because it\'s not supported by the currently installed Yoast SEO version.', 'wordpress-seo' ), |
| 92 |
$error, |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
return $source; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Update the comparison table for the plugin installation when overwriting an existing plugin. |
| 101 |
* |
| 102 |
* @param string $table The output table with Name, Version, Author, RequiresWP, and RequiresPHP info. |
| 103 |
* @param array<string> $current_plugin_data Array with current plugin data. |
| 104 |
* @param array<string> $new_plugin_data Array with uploaded plugin data. |
| 105 |
* |
| 106 |
* @return string The updated comparison table. |
| 107 |
*/ |
| 108 |
public function update_comparison_table( $table, $current_plugin_data, $new_plugin_data ) { |
| 109 |
$requires_yoast_seo_current = ! empty( $current_plugin_data['Requires Yoast SEO'] ) ? $current_plugin_data['Requires Yoast SEO'] : false; |
| 110 |
$requires_yoast_seo_new = ! empty( $new_plugin_data['Requires Yoast SEO'] ) ? $new_plugin_data['Requires Yoast SEO'] : false; |
| 111 |
|
| 112 |
if ( $requires_yoast_seo_current !== false || $requires_yoast_seo_new !== false ) { |
| 113 |
$new_row = \sprintf( |
| 114 |
'<tr><td class="name-label">%1$s</td><td>%2$s</td><td>%3$s</td></tr>', |
| 115 |
\__( 'Required Yoast SEO version', 'wordpress-seo' ), |
| 116 |
( $requires_yoast_seo_current !== false ) ? \esc_html( $requires_yoast_seo_current ) : '-', |
| 117 |
( $requires_yoast_seo_new !== false ) ? \esc_html( $requires_yoast_seo_new ) : '-', |
| 118 |
); |
| 119 |
|
| 120 |
$table = \str_replace( '</tbody>', $new_row . '</tbody>', $table ); |
| 121 |
} |
| 122 |
|
| 123 |
return $table; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Check whether the required Yoast SEO version is installed. |
| 128 |
* |
| 129 |
* @param string|bool $required_version The required version. |
| 130 |
* |
| 131 |
* @return bool Whether the required version is installed, or no version is required. |
| 132 |
*/ |
| 133 |
private function check_requirement( $required_version ) { |
| 134 |
if ( $required_version === false ) { |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
return \version_compare( \WPSEO_VERSION, $required_version . '-RC0', '>=' ); |
| 139 |
} |
| 140 |
} |
| 141 |
|