| 1 |
<?php |
| 2 |
|
| 3 |
namespace cybot\cookiebot\addons\controller\addons; |
| 4 |
|
| 5 |
use cybot\cookiebot\exceptions\addons\InstallationException; |
| 6 |
use cybot\cookiebot\lib\buffer\Buffer_Output_Interface; |
| 7 |
use cybot\cookiebot\lib\Cookie_Consent_Interface; |
| 8 |
use cybot\cookiebot\lib\script_loader_tag\Script_Loader_Tag_Interface; |
| 9 |
use cybot\cookiebot\lib\Settings_Service_Interface; |
| 10 |
use Exception; |
| 11 |
|
| 12 |
abstract class Base_Cookiebot_Plugin_Addon extends Base_Cookiebot_Addon { |
| 13 |
|
| 14 |
|
| 15 |
const PLUGIN_FILE_PATH = ''; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param $settings Settings_Service_Interface |
| 19 |
* @param $script_loader_tag Script_Loader_Tag_Interface |
| 20 |
* @param $cookie_consent Cookie_Consent_Interface |
| 21 |
* @param $buffer_output Buffer_Output_Interface |
| 22 |
* |
| 23 |
* @throws Exception |
| 24 |
* @since 1.3.0 |
| 25 |
*/ |
| 26 |
protected function __construct( |
| 27 |
Settings_Service_Interface $settings, |
| 28 |
Script_Loader_Tag_Interface $script_loader_tag, |
| 29 |
Cookie_Consent_Interface $cookie_consent, |
| 30 |
Buffer_Output_Interface $buffer_output |
| 31 |
) { |
| 32 |
parent::__construct( $settings, $script_loader_tag, $cookie_consent, $buffer_output ); |
| 33 |
$this->validate_required_string_class_constant( 'PLUGIN_FILE_PATH' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
final public function is_addon_installed() { |
| 40 |
$path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . static::PLUGIN_FILE_PATH; |
| 41 |
return ( file_exists( $path ) && ! is_wp_error( validate_plugin( static::PLUGIN_FILE_PATH ) ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Checks if addon plugin is activated |
| 46 |
* |
| 47 |
* @since 1.3.0 |
| 48 |
*/ |
| 49 |
final public function is_addon_activated() { |
| 50 |
return $this->is_addon_installed() && is_plugin_active( static::PLUGIN_FILE_PATH ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return string |
| 55 |
* @throws InstallationException |
| 56 |
*/ |
| 57 |
final public function get_version() { |
| 58 |
$plugin_data = $this->get_plugin_data(); |
| 59 |
if ( ! isset( $plugin_data['Version'] ) ) { |
| 60 |
throw new InstallationException( 'Check if plugin is installed before calling get_version()' ); |
| 61 |
} |
| 62 |
return $plugin_data['Version']; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @return string[] |
| 67 |
*/ |
| 68 |
private function get_plugin_data() { |
| 69 |
return get_file_data( |
| 70 |
WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . static::PLUGIN_FILE_PATH, |
| 71 |
array( 'Version' => 'version' ), |
| 72 |
false |
| 73 |
); |
| 74 |
} |
| 75 |
} |
| 76 |
|