| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugins Checker class |
| 4 |
* |
| 5 |
* @package micropackage/requirements |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Micropackage\Requirements\Checker; |
| 9 |
|
| 10 |
use Micropackage\Requirements\Abstracts; |
| 11 |
use Micropackage\Requirements\Requirements; |
| 12 |
|
| 13 |
/** |
| 14 |
* Plugins Checker class |
| 15 |
*/ |
| 16 |
class Plugins extends Abstracts\Checker { |
| 17 |
|
| 18 |
/** |
| 19 |
* Checker name |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $name = 'plugins'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Checks if the requirement is met |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @throws \Exception When provided value is not an array of arrays with keys: file*, name*, version. |
| 30 |
* @param mixed $value Value to check against. |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function check( $value ) { |
| 34 |
|
| 35 |
if ( ! is_array( $value ) ) { |
| 36 |
throw new \Exception( 'Plugins Check requires array of arrays parameter with inner keys: file, name, version (optional)' ); |
| 37 |
} |
| 38 |
|
| 39 |
$active_plugins_raw = wp_get_active_and_valid_plugins(); |
| 40 |
|
| 41 |
if ( is_multisite() ) { |
| 42 |
$active_plugins_raw = array_merge( $active_plugins_raw, wp_get_active_network_plugins() ); |
| 43 |
} |
| 44 |
|
| 45 |
$active_plugins = array(); |
| 46 |
$active_plugins_versions = array(); |
| 47 |
|
| 48 |
foreach ( $active_plugins_raw as $plugin_full_path ) { |
| 49 |
$plugin_file = str_replace( WP_PLUGIN_DIR . '/', '', $plugin_full_path ); |
| 50 |
$active_plugins[] = $plugin_file; |
| 51 |
|
| 52 |
if ( file_exists( $plugin_full_path ) ) { |
| 53 |
$plugin_api_data = @get_file_data( $plugin_full_path, array( 'Version' ) ); // phpcs:ignore |
| 54 |
$active_plugins_versions[ $plugin_file ] = $plugin_api_data[0]; |
| 55 |
} else { |
| 56 |
$active_plugins_versions[ $plugin_file ] = 0; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $value as $plugin_data ) { |
| 61 |
if ( ! in_array( $plugin_data['file'], $active_plugins, true ) ) { |
| 62 |
$this->add_error( |
| 63 |
sprintf( |
| 64 |
// Translators: Plugin name. |
| 65 |
__( 'Required plugin: %s', Requirements::$textdomain ), |
| 66 |
$plugin_data['name'] |
| 67 |
) |
| 68 |
); |
| 69 |
} elseif ( isset( $plugin_data['version'] ) && version_compare( $active_plugins_versions[ $plugin_data['file'] ], $plugin_data['version'], '<' ) ) { |
| 70 |
$this->add_error( |
| 71 |
sprintf( |
| 72 |
// Translators: 1. Plugin name, 2. Required version, 3. Used version. |
| 73 |
__( 'Minimum required version of %1$s plugin is %2$s. Your version is %3$s', Requirements::$textdomain ), |
| 74 |
$plugin_data['name'], |
| 75 |
$plugin_data['version'], |
| 76 |
$active_plugins_versions[ $plugin_data['file'] ] |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|