| 1 |
<?php |
| 2 |
|
| 3 |
namespace cookiebot_addons; |
| 4 |
|
| 5 |
use cookiebot_addons\config\Settings_Config; |
| 6 |
use cookiebot_addons\controller\Plugin_Controller; |
| 7 |
use cookiebot_addons\lib\Settings_Service_Interface; |
| 8 |
use DI\ContainerBuilder; |
| 9 |
use DI; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} // Exit if accessed directly |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* __DIR__ of this plugin |
| 18 |
*/ |
| 19 |
define( 'COOKIEBOT_ADDONS_DIR', __DIR__ . DIRECTORY_SEPARATOR ); |
| 20 |
|
| 21 |
define( 'COOKIEBOT_ADDONS_BASE_NAME', dirname( plugin_basename( __FILE__ ) ) ); |
| 22 |
|
| 23 |
/** |
| 24 |
* Same version as the CookiebotWP |
| 25 |
*/ |
| 26 |
define( 'COOKIEBOT_ADDONS_VERSION', '3.4.0' ); |
| 27 |
|
| 28 |
/** |
| 29 |
* Register autoloader to load files/classes dynamically |
| 30 |
*/ |
| 31 |
include_once COOKIEBOT_ADDONS_DIR . 'lib/autoloader.php'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Load global functions for this plugin |
| 35 |
*/ |
| 36 |
include_once COOKIEBOT_ADDONS_DIR . 'lib/helper.php'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Load composer |
| 40 |
* |
| 41 |
* "php-di/php-di": "5.0" |
| 42 |
*/ |
| 43 |
include_once COOKIEBOT_ADDONS_DIR . 'lib/ioc/autoload.php'; |
| 44 |
|
| 45 |
class Cookiebot_Addons { |
| 46 |
|
| 47 |
/** |
| 48 |
* IoC Container - is used for dependency injections |
| 49 |
* |
| 50 |
* @var \DI\Container |
| 51 |
* |
| 52 |
* @since 1.3.0 |
| 53 |
*/ |
| 54 |
public $container; |
| 55 |
|
| 56 |
/** |
| 57 |
* List of all supported addons |
| 58 |
* |
| 59 |
* @var object |
| 60 |
* |
| 61 |
* @since 1.3.0 |
| 62 |
*/ |
| 63 |
public $plugins; |
| 64 |
|
| 65 |
/** |
| 66 |
* @var Cookiebot_Addons The single instance of the class |
| 67 |
* @since 1.0.0 |
| 68 |
*/ |
| 69 |
protected static $_instance = null; |
| 70 |
|
| 71 |
/** |
| 72 |
* Main Cookiebot_WP Instance |
| 73 |
* |
| 74 |
* Ensures only one instance of Cookiebot_Addons is loaded or can be loaded. |
| 75 |
* |
| 76 |
* @version 2.2.0 |
| 77 |
* @since 2.2.0 |
| 78 |
* @static |
| 79 |
* |
| 80 |
* @return Cookiebot_Addons |
| 81 |
*/ |
| 82 |
public static function instance() { |
| 83 |
if ( is_null( self::$_instance ) ) { |
| 84 |
try { |
| 85 |
self::$_instance = new self(); |
| 86 |
} catch ( \DI\DependencyException $e ) { |
| 87 |
echo 'Dependencies are not loaded.'; |
| 88 |
} catch ( DI\NotFoundException $e ) { |
| 89 |
echo 'Dependencies are not found.'; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return self::$_instance; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Cookiebot_Addons constructor. |
| 98 |
* |
| 99 |
* @throws DI\DependencyException |
| 100 |
* @throws DI\NotFoundException |
| 101 |
* |
| 102 |
* @since 1.3.0 |
| 103 |
*/ |
| 104 |
public function __construct() { |
| 105 |
$this->get_plugins(); |
| 106 |
$this->build_container(); |
| 107 |
$this->assign_addons_to_container(); |
| 108 |
|
| 109 |
/** |
| 110 |
* Load plugin controller to check if addons are active |
| 111 |
* If active then load the plugin addon configuration class |
| 112 |
* Else skip it |
| 113 |
* |
| 114 |
* @since 1.1.0 |
| 115 |
*/ |
| 116 |
add_action( 'plugins_loaded', array( |
| 117 |
new Plugin_Controller( $this->container->get( 'Settings_Service_Interface' ) ), |
| 118 |
'load_active_addons', |
| 119 |
) ); |
| 120 |
/** |
| 121 |
* Load settings config |
| 122 |
* |
| 123 |
* @since 1.1.0 |
| 124 |
*/ |
| 125 |
$settings = new Settings_Config( $this->container->get( 'Settings_Service_Interface' ) ); |
| 126 |
$settings->load(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* if the cookiebot is deactivated |
| 131 |
* run this script to clean up addons. |
| 132 |
* |
| 133 |
* @since 2.2.0 |
| 134 |
*/ |
| 135 |
public function cookiebot_deactivated() { |
| 136 |
$settings_service = $this->container->get( 'Settings_Service_Interface' ); |
| 137 |
$settings_service->cookiebot_deactivated(); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Loads plugins from json file |
| 142 |
* |
| 143 |
* All the addon plugins are defined there. |
| 144 |
* |
| 145 |
* The file is located at the root map of this plugin |
| 146 |
* |
| 147 |
* @since 1.3.0 |
| 148 |
*/ |
| 149 |
protected function get_plugins() { |
| 150 |
$file = file_get_contents( COOKIEBOT_ADDONS_DIR . 'addons.json' ); |
| 151 |
$this->plugins = json_decode( $file ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Build IoC container |
| 156 |
* |
| 157 |
* @since 1.3.0 |
| 158 |
*/ |
| 159 |
protected function build_container() { |
| 160 |
$builder = new ContainerBuilder(); |
| 161 |
|
| 162 |
$builder->addDefinitions( |
| 163 |
array( |
| 164 |
'Script_Loader_Tag_Interface' => DI\object( 'cookiebot_addons\lib\script_loader_tag\Script_Loader_Tag' ), |
| 165 |
'Cookie_Consent_Interface' => DI\object( 'cookiebot_addons\lib\Cookie_Consent' ), |
| 166 |
'Buffer_Output_Interface' => DI\object( 'cookiebot_addons\lib\buffer\Buffer_Output' ), |
| 167 |
'plugins' => DI\value( $this->plugins ), |
| 168 |
) |
| 169 |
); |
| 170 |
|
| 171 |
$this->container = $builder->build(); |
| 172 |
|
| 173 |
$this->container->set( 'Settings_Service_Interface', DI\object( 'cookiebot_addons\lib\Settings_Service' ) |
| 174 |
->constructor( $this->container ) ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Assign addon class to the container to use it later |
| 179 |
* |
| 180 |
* @throws DI\DependencyException |
| 181 |
* @throws DI\NotFoundException |
| 182 |
* |
| 183 |
* @since 1.3.0 |
| 184 |
*/ |
| 185 |
protected function assign_addons_to_container() { |
| 186 |
/** |
| 187 |
* Check plugins one by one and load addon configuration |
| 188 |
*/ |
| 189 |
foreach ( $this->plugins as $plugin_class => $plugin ) { |
| 190 |
/** |
| 191 |
* Load addon class to the container |
| 192 |
*/ |
| 193 |
$this->container->set( $plugin->class, \DI\object( $plugin->class ) |
| 194 |
->constructor( |
| 195 |
$this->container->get( 'Settings_Service_Interface' ), |
| 196 |
$this->container->get( 'Script_Loader_Tag_Interface' ), |
| 197 |
$this->container->get( 'Cookie_Consent_Interface' ), |
| 198 |
$this->container->get( 'Buffer_Output_Interface' ) ) |
| 199 |
); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Initiate the cookiebot addons framework plugin |
| 206 |
*/ |
| 207 |
Cookiebot_Addons::instance(); |
| 208 |
|