plugin-controller.php
123 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 | if( \Cookiebot_WP::cookiebot_disabled_in_admin() === true && is_admin() ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check plugins one by one and load configuration if it is active |
| 69 | * |
| 70 | * @var $plugin Cookiebot_Addons_Interface |
| 71 | */ |
| 72 | $addonsEnabled = 0; |
| 73 | foreach ( $this->settings_service->get_active_addons() as $plugin ) { |
| 74 | if ( ! $plugin->cookie_consent->are_cookie_states_accepted( $plugin->get_cookie_types() ) |
| 75 | || cookiebot_addons_enabled_cache_plugin() ) { |
| 76 | $plugin->load_configuration(); |
| 77 | $addonsEnabled++; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * After WordPress is fully loaded |
| 83 | * |
| 84 | * Run buffer output actions - this runs after scanning of every addons |
| 85 | */ |
| 86 | add_action( 'parse_request', array( $this, 'run_buffer_output_manipulations' ) ); |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Add notice for the user if any addons is enabled and cookie |
| 91 | * blocking mode is set to auto. |
| 92 | */ |
| 93 | if($addonsEnabled > 0 && \Cookiebot_WP::get_cookie_blocking_mode() == 'auto') { |
| 94 | if(isset($_GET['page']) && in_array($_GET['page'],array('cookiebot','cookiebot-addons'))) { |
| 95 | add_action('admin_notices', function() { |
| 96 | echo '<div class="notice notice-warning"> |
| 97 | <p> |
| 98 | <strong>'.__('You enabled Cookiebot auto blocking mode but still using addons').'</strong><br> |
| 99 | '.__('In some occasions this may cause client side errors. If you notice any errors please try to disable Cookiebot addons or contact Cookiebot support.').' |
| 100 | </p> |
| 101 | </div>'; |
| 102 | }); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Runs every added action hooks to manipulate script tag |
| 109 | * |
| 110 | * @since 1.3.0 |
| 111 | */ |
| 112 | public function run_buffer_output_manipulations() { |
| 113 | /** |
| 114 | * @var $buffer_output Buffer_Output_Interface |
| 115 | */ |
| 116 | $buffer_output = $this->settings_service->container->get( 'Buffer_Output_Interface' ); |
| 117 | |
| 118 | if ( $buffer_output->has_action() ) { |
| 119 | $buffer_output->run_actions(); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 |