plugin-controller.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cookiebot_addons\controller; |
| 4 | |
| 5 | use cookiebot_addons\controller\addons\Cookiebot_Addons_Interface; |
| 6 | use cookiebot_addons\lib\buffer\Buffer_Output_Interface; |
| 7 | use cookiebot_addons\lib\Settings_Service_Interface; |
| 8 | |
| 9 | class Plugin_Controller { |
| 10 | |
| 11 | /** |
| 12 | * IoC container - Dependency Injection |
| 13 | * |
| 14 | * @var Settings_Service_Interface |
| 15 | * |
| 16 | * @since 1.1.0 |
| 17 | */ |
| 18 | private $settings_service; |
| 19 | |
| 20 | /** |
| 21 | * Plugin_Controller constructor. |
| 22 | * |
| 23 | * @param $settings_service Settings_Service_Interface IoC Container |
| 24 | * |
| 25 | * @since 1.2.0 |
| 26 | */ |
| 27 | public function __construct( Settings_Service_Interface $settings_service ) { |
| 28 | $this->settings_service = $settings_service; |
| 29 | |
| 30 | $this->load_init_files(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Load init files to use 'validate_plugin' and 'is_plugin_active' |
| 35 | * |
| 36 | * @since 1.3.0 |
| 37 | */ |
| 38 | protected function load_init_files() { |
| 39 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 40 | require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
| 41 | require_once( ABSPATH . '/wp-includes/l10n.php' ); |
| 42 | require_once( ABSPATH . '/wp-admin/includes/translation-install.php' ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Load addon configuration if the plugin is activated |
| 48 | * |
| 49 | * @throws \DI\DependencyException |
| 50 | * @throws \DI\NotFoundException |
| 51 | * |
| 52 | * @version 1.3.0 |
| 53 | * @since 1.2.0 |
| 54 | */ |
| 55 | public function load_active_addons() { |
| 56 | /** |
| 57 | * Check if Cookiebot is activated and active. Return if cookiebot is inactive |
| 58 | */ |
| 59 | if ( ! function_exists( 'cookiebot_active' ) || ! cookiebot_active() ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Check plugins one by one and load configuration if it is active |
| 65 | * |
| 66 | * @var $plugin Cookiebot_Addons_Interface |
| 67 | */ |
| 68 | foreach ( $this->settings_service->get_active_addons() as $plugin ) { |
| 69 | if ( ! $plugin->cookie_consent->are_cookie_states_accepted( $plugin->get_cookie_types() ) |
| 70 | || cookiebot_addons_enabled_cache_plugin() ) { |
| 71 | $plugin->load_configuration(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * After WordPress is fully loaded |
| 77 | * |
| 78 | * Run buffer output actions - this runs after scanning of every addons |
| 79 | */ |
| 80 | add_action( 'parse_request', array( $this, 'run_buffer_output_manipulations' ) ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Runs every added action hooks to manipulate script tag |
| 85 | * |
| 86 | * @since 1.3.0 |
| 87 | */ |
| 88 | public function run_buffer_output_manipulations() { |
| 89 | /** |
| 90 | * @var $buffer_output Buffer_Output_Interface |
| 91 | */ |
| 92 | $buffer_output = $this->settings_service->container->get( 'Buffer_Output_Interface' ); |
| 93 | |
| 94 | if ( $buffer_output->has_action() ) { |
| 95 | $buffer_output->run_actions(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 |