bin
7 years ago
config
5 years ago
controller
4 years ago
inc
5 years ago
js
7 years ago
lib
4 years ago
style
7 years ago
tests
4 years ago
vendor
5 years ago
view
4 years ago
.travis.yml
7 years ago
addons.json
4 years ago
composer.json
5 years ago
composer.lock
5 years ago
cookiebot-addons-init.php
4 years ago
cookiebot-addons-init.php
231 lines
| 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\buffer\Buffer_Output; |
| 8 | use cookiebot_addons\lib\Cookie_Consent; |
| 9 | use cookiebot_addons\lib\Dependency_Container; |
| 10 | use cookiebot_addons\lib\script_loader_tag\Script_Loader_Tag; |
| 11 | use cookiebot_addons\lib\Settings_Service; |
| 12 | use cookiebot_addons\lib\Theme_Settings_Service; |
| 13 | use Exception; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } // Exit if accessed directly |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * __DIR__ of this plugin |
| 22 | */ |
| 23 | define( 'COOKIEBOT_ADDONS_DIR', __DIR__ . DIRECTORY_SEPARATOR ); |
| 24 | |
| 25 | if ( ! defined( 'COOKIEBOT_ADDONS_URL' ) ) { |
| 26 | define( 'COOKIEBOT_ADDONS_URL', plugin_dir_url( __FILE__ ) ); |
| 27 | } |
| 28 | |
| 29 | define( 'COOKIEBOT_ADDONS_BASE_NAME', dirname( plugin_basename( __FILE__ ) ) ); |
| 30 | |
| 31 | /** |
| 32 | * Same version as the CookiebotWP |
| 33 | */ |
| 34 | define( 'COOKIEBOT_ADDONS_VERSION', '3.9.0' ); |
| 35 | |
| 36 | /** |
| 37 | * Register autoloader to load files/classes dynamically |
| 38 | */ |
| 39 | require_once COOKIEBOT_ADDONS_DIR . 'lib/autoloader.php'; |
| 40 | |
| 41 | /** |
| 42 | * Load global functions for this plugin |
| 43 | */ |
| 44 | require_once COOKIEBOT_ADDONS_DIR . 'lib/helper.php'; |
| 45 | |
| 46 | class Cookiebot_Addons { |
| 47 | |
| 48 | /** |
| 49 | * Dependency Container - is used for dependency injections |
| 50 | * |
| 51 | * @var Dependency_Container |
| 52 | * |
| 53 | * @since 1.3.0 |
| 54 | */ |
| 55 | public $container; |
| 56 | |
| 57 | /** |
| 58 | * List of all supported addons |
| 59 | * |
| 60 | * @var object |
| 61 | * |
| 62 | * @since 1.3.0 |
| 63 | */ |
| 64 | public $plugins; |
| 65 | |
| 66 | /** |
| 67 | * @var Cookiebot_Addons The single instance of the class |
| 68 | * @since 1.0.0 |
| 69 | */ |
| 70 | private static $instance = null; |
| 71 | |
| 72 | /** |
| 73 | * Main Cookiebot_WP Instance |
| 74 | * |
| 75 | * Ensures only one instance of Cookiebot_Addons is loaded or can be loaded. |
| 76 | * |
| 77 | * @return Cookiebot_Addons |
| 78 | * @since 2.2.0 |
| 79 | * @static |
| 80 | * |
| 81 | * @version 2.2.0 |
| 82 | */ |
| 83 | public static function instance() { |
| 84 | if ( is_null( self::$instance ) ) { |
| 85 | try { |
| 86 | self::$instance = new self(); |
| 87 | } catch ( Exception $e ) { |
| 88 | echo 'Could not initialize Cookiebot addons: ' . esc_html( $e->getMessage() ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return self::$instance; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Cookiebot_Addons constructor. |
| 97 | * |
| 98 | * @throws Exception |
| 99 | * |
| 100 | * @since 1.3.0 |
| 101 | */ |
| 102 | public function __construct() { |
| 103 | $this->get_plugins(); |
| 104 | $this->build_container(); |
| 105 | $this->assign_addons_to_container(); |
| 106 | |
| 107 | /** |
| 108 | * Load plugin controller to check if addons are active |
| 109 | * If active then load the plugin addon configuration class |
| 110 | * Else skip it |
| 111 | * |
| 112 | * @since 1.1.0 |
| 113 | */ |
| 114 | add_action( |
| 115 | 'after_setup_theme', |
| 116 | array( |
| 117 | new Plugin_Controller( $this->container->get( 'Settings_Service_Interface' ) ), |
| 118 | 'load_active_addons', |
| 119 | ) |
| 120 | ); |
| 121 | /** |
| 122 | * Load settings config |
| 123 | * |
| 124 | * @since 1.1.0 |
| 125 | */ |
| 126 | $settings = new Settings_Config( $this->container->get( 'Settings_Service_Interface' ) ); |
| 127 | $settings->load(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * if the cookiebot is activated |
| 132 | * run this script to start up |
| 133 | * |
| 134 | * @since 2.2.0 |
| 135 | */ |
| 136 | public function cookiebot_activated() { |
| 137 | $settings_service = $this->container->get( 'Settings_Service_Interface' ); |
| 138 | $settings_service->cookiebot_activated(); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * if the cookiebot is deactivated |
| 143 | * run this script to clean up addons. |
| 144 | * |
| 145 | * @since 2.2.0 |
| 146 | */ |
| 147 | public function cookiebot_deactivated() { |
| 148 | $settings_service = $this->container->get( 'Settings_Service_Interface' ); |
| 149 | $settings_service->cookiebot_deactivated(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Loads plugins from json file |
| 154 | * |
| 155 | * All the addon plugins are defined there. |
| 156 | * |
| 157 | * The file is located at the root map of this plugin |
| 158 | * |
| 159 | * @throws Exception |
| 160 | * @since 1.3.0 |
| 161 | */ |
| 162 | protected function get_plugins() { |
| 163 | $file = cookiebot_get_local_file_json_contents( COOKIEBOT_ADDONS_DIR . 'addons.json' ); |
| 164 | |
| 165 | $this->plugins = apply_filters( 'cookiebot_addons_list', $file ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @throws Exception |
| 170 | */ |
| 171 | protected function build_container() { |
| 172 | $dependencies = array( |
| 173 | 'Script_Loader_Tag_Interface' => new Script_Loader_Tag(), |
| 174 | 'Cookie_Consent_Interface' => new Cookie_Consent(), |
| 175 | 'Buffer_Output_Interface' => new Buffer_Output(), |
| 176 | 'plugins' => $this->plugins, |
| 177 | ); |
| 178 | |
| 179 | $this->container = new Dependency_Container( $dependencies ); |
| 180 | |
| 181 | $this->container->set( |
| 182 | 'Settings_Service_Interface', |
| 183 | new Settings_Service( $this->container ) |
| 184 | ); |
| 185 | |
| 186 | $this->container->set( |
| 187 | 'Theme_Settings_Service_Interface', |
| 188 | new Theme_Settings_Service( $this->container ) |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Assign addon class to the container to use it later |
| 194 | * |
| 195 | * @throws Exception |
| 196 | * |
| 197 | * @since 1.3.0 |
| 198 | */ |
| 199 | protected function assign_addons_to_container() { |
| 200 | /** |
| 201 | * Check plugins one by one and load addon configuration |
| 202 | */ |
| 203 | foreach ( $this->plugins as $plugin_class => $plugin ) { |
| 204 | /** |
| 205 | * Load addon class to the container |
| 206 | */ |
| 207 | |
| 208 | if ( class_exists( $plugin->class ) ) { |
| 209 | $this->container->set( |
| 210 | $plugin->class, |
| 211 | new $plugin->class( |
| 212 | isset( $plugin->is_theme ) && $plugin->is_theme |
| 213 | ? $this->container->get( 'Theme_Settings_Service_Interface' ) |
| 214 | : $this->container->get( 'Settings_Service_Interface' ), |
| 215 | $this->container->get( 'Script_Loader_Tag_Interface' ), |
| 216 | $this->container->get( 'Cookie_Consent_Interface' ), |
| 217 | $this->container->get( 'Buffer_Output_Interface' ) |
| 218 | ) |
| 219 | ); |
| 220 | } else { |
| 221 | throw new Exception( 'Class ' . $plugin->class . ' not found' ); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Initiate the cookiebot addons framework plugin |
| 229 | */ |
| 230 | Cookiebot_Addons::instance(); |
| 231 |