| 1 |
<?php |
| 2 |
namespace Elementor\Modules\CompatibilityTag; |
| 3 |
|
| 4 |
use Elementor\Plugin; |
| 5 |
use Elementor\Core\Utils\Version; |
| 6 |
use Elementor\Core\Utils\Collection; |
| 7 |
use Elementor\Core\Base\Module as BaseModule; |
| 8 |
use Elementor\Modules\System_Info\Module as System_Info; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
abstract class Base_Module extends BaseModule { |
| 15 |
const MODULE_NAME = 'compatibility-tag'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var Compatibility_Tag |
| 19 |
*/ |
| 20 |
private $compatibility_tag_service; |
| 21 |
|
| 22 |
/** |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function get_name() { |
| 26 |
return static::MODULE_NAME; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @return Compatibility_Tag |
| 31 |
*/ |
| 32 |
private function get_compatibility_tag_service() { |
| 33 |
if ( ! $this->compatibility_tag_service ) { |
| 34 |
$this->compatibility_tag_service = new Compatibility_Tag( $this->get_plugin_header() ); |
| 35 |
} |
| 36 |
|
| 37 |
return $this->compatibility_tag_service; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Add allowed headers to plugins. |
| 42 |
* |
| 43 |
* @param array $headers |
| 44 |
* @param $compatibility_tag_header |
| 45 |
* |
| 46 |
* @return array |
| 47 |
*/ |
| 48 |
protected function enable_elementor_headers( array $headers, $compatibility_tag_header ) { |
| 49 |
$headers[] = $compatibility_tag_header; |
| 50 |
|
| 51 |
return $headers; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return Collection |
| 56 |
*/ |
| 57 |
protected function get_plugins_to_check() { |
| 58 |
return $this->get_plugins_with_header(); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Append a compatibility message to the update plugin warning. |
| 63 |
* |
| 64 |
* @param array $args |
| 65 |
* |
| 66 |
* @throws \Exception |
| 67 |
*/ |
| 68 |
protected function on_plugin_update_message( array $args ) { |
| 69 |
$new_version = Version::create_from_string( $args['new_version'] ); |
| 70 |
|
| 71 |
if ( $new_version->compare( '=', $args['Version'], Version::PART_MAJOR_2 ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$plugins = $this->get_plugins_to_check(); |
| 76 |
$plugins_compatibility = $this->get_compatibility_tag_service()->check( $new_version, $plugins->keys() ); |
| 77 |
|
| 78 |
$plugins = $plugins->filter( function ( $data, $plugin_name ) use ( $plugins_compatibility ) { |
| 79 |
return Compatibility_Tag::COMPATIBLE !== $plugins_compatibility[ $plugin_name ]; |
| 80 |
} ); |
| 81 |
|
| 82 |
if ( $plugins->is_empty() ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
include __DIR__ . '/views/plugin-update-message-compatibility.php'; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get all plugins with specific header. |
| 91 |
* |
| 92 |
* @return Collection |
| 93 |
*/ |
| 94 |
private function get_plugins_with_header() { |
| 95 |
return Plugin::$instance->wp |
| 96 |
->get_plugins() |
| 97 |
->filter( function ( array $plugin ) { |
| 98 |
return ! empty( $plugin[ $this->get_plugin_header() ] ); |
| 99 |
} ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
abstract protected function get_plugin_header(); |
| 106 |
|
| 107 |
/** |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
abstract protected function get_plugin_label(); |
| 111 |
|
| 112 |
/** |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
abstract protected function get_plugin_name(); |
| 116 |
|
| 117 |
/** |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
abstract protected function get_plugin_version(); |
| 121 |
|
| 122 |
/** |
| 123 |
* Base_Module constructor. |
| 124 |
* |
| 125 |
* @throws \Exception |
| 126 |
*/ |
| 127 |
public function __construct() { |
| 128 |
add_filter( 'extra_plugin_headers', function ( array $headers ) { |
| 129 |
return $this->enable_elementor_headers( $headers, $this->get_plugin_header() ); |
| 130 |
} ); |
| 131 |
|
| 132 |
add_action( 'in_plugin_update_message-' . $this->get_plugin_name(), function ( array $args ) { |
| 133 |
$this->on_plugin_update_message( $args ); |
| 134 |
}, 11 /* After the warning message for backup */ ); |
| 135 |
|
| 136 |
add_action( 'admin_init', function () { |
| 137 |
System_Info::add_report( $this->get_plugin_name() . '_compatibility', [ |
| 138 |
'file_name' => __DIR__ . '/compatibility-tag-report.php', |
| 139 |
'class_name' => __NAMESPACE__ . '\Compatibility_Tag_Report', |
| 140 |
'fields' => [ |
| 141 |
'compatibility_tag_service' => $this->get_compatibility_tag_service(), |
| 142 |
'plugin_label' => $this->get_plugin_label(), |
| 143 |
'plugin_version' => Version::create_from_string( $this->get_plugin_version() ), |
| 144 |
'plugins_to_check' => $this->get_plugins_to_check() |
| 145 |
->only( get_option( 'active_plugins' ) ) |
| 146 |
->keys(), |
| 147 |
], |
| 148 |
] ); |
| 149 |
} ); |
| 150 |
} |
| 151 |
} |
| 152 |
|