| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CompatibilityTag; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\Core\Utils\Version; |
| 6 |
use Elementor\Core\Base\Base_Object; |
| 7 |
use Elementor\Core\Utils\Collection; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Compatibility_Tag extends Base_Object { |
| 14 |
const PLUGIN_NOT_EXISTS = 'plugin_not_exists'; |
| 15 |
const HEADER_NOT_EXISTS = 'header_not_exists'; |
| 16 |
const INVALID_VERSION = 'invalid_version'; |
| 17 |
const INCOMPATIBLE = 'incompatible'; |
| 18 |
const COMPATIBLE = 'compatible'; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string Holds the header that should be checked. |
| 22 |
*/ |
| 23 |
private $header; |
| 24 |
|
| 25 |
/** |
| 26 |
* Compatibility_Tag constructor. |
| 27 |
* |
| 28 |
* @param string $header |
| 29 |
*/ |
| 30 |
public function __construct( $header ) { |
| 31 |
$this->header = $header; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Return if plugins is compatible or not. |
| 36 |
* |
| 37 |
* @param Version $version |
| 38 |
* @param array $plugins_names |
| 39 |
* |
| 40 |
* @return array |
| 41 |
* @throws \Exception If an error occurs during compatibility check. |
| 42 |
*/ |
| 43 |
public function check( Version $version, array $plugins_names ) { |
| 44 |
return ( new Collection( $plugins_names ) ) |
| 45 |
->map_with_keys( function ( $plugin_name ) use ( $version ) { |
| 46 |
return [ $plugin_name => $this->is_compatible( $version, $plugin_name ) ]; |
| 47 |
} ) |
| 48 |
->all(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Check single plugin if is compatible or not. |
| 53 |
* |
| 54 |
* @param Version $version |
| 55 |
* @param $plugin_name |
| 56 |
* |
| 57 |
* @return string |
| 58 |
* @throws \Exception If an error occurs during the compatibility check. |
| 59 |
*/ |
| 60 |
private function is_compatible( Version $version, $plugin_name ) { |
| 61 |
$plugins = Plugin::$instance->wp->get_plugins(); |
| 62 |
|
| 63 |
if ( ! isset( $plugins[ $plugin_name ] ) ) { |
| 64 |
return self::PLUGIN_NOT_EXISTS; |
| 65 |
} |
| 66 |
|
| 67 |
$requested_plugin = $plugins[ $plugin_name ]; |
| 68 |
|
| 69 |
if ( empty( $requested_plugin[ $this->header ] ) ) { |
| 70 |
return self::HEADER_NOT_EXISTS; |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! Version::is_valid_version( $requested_plugin[ $this->header ] ) ) { |
| 74 |
return self::INVALID_VERSION; |
| 75 |
} |
| 76 |
|
| 77 |
if ( $version->compare( '>', $requested_plugin[ $this->header ], Version::PART_MAJOR_2 ) ) { |
| 78 |
return self::INCOMPATIBLE; |
| 79 |
} |
| 80 |
|
| 81 |
return self::COMPATIBLE; |
| 82 |
} |
| 83 |
} |
| 84 |
|