Plugin_Controller.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\addons\controller; |
| 4 | |
| 5 | use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Addon; |
| 6 | use cybot\cookiebot\lib\buffer\Buffer_Output_Interface; |
| 7 | use cybot\cookiebot\lib\Settings_Service_Interface; |
| 8 | use cybot\cookiebot\lib\Cookiebot_WP; |
| 9 | use Exception; |
| 10 | use function cybot\cookiebot\lib\cookiebot_addons_enabled_cache_plugin; |
| 11 | use function cybot\cookiebot\lib\cookiebot_active; |
| 12 | |
| 13 | class Plugin_Controller { |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * @var Settings_Service_Interface |
| 18 | */ |
| 19 | private $settings_service; |
| 20 | |
| 21 | /** |
| 22 | * @param Settings_Service_Interface $settings_service |
| 23 | */ |
| 24 | public function __construct( Settings_Service_Interface $settings_service ) { |
| 25 | $this->settings_service = $settings_service; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @throws Exception |
| 30 | */ |
| 31 | public function load_active_addons() { |
| 32 | if ( ! cookiebot_active() ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add notice for the user if any addons is enabled and cookie |
| 38 | * blocking mode is set to auto. |
| 39 | */ |
| 40 | if ( count( $this->settings_service->get_active_addons() ) > 0 && |
| 41 | Cookiebot_WP::get_cookie_blocking_mode() === 'auto' && |
| 42 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 43 | isset( $_GET['page'] ) && |
| 44 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 45 | in_array( $_GET['page'], array( 'cookiebot', 'cookiebot-addons' ), true ) ) { |
| 46 | add_action( |
| 47 | 'admin_notices', |
| 48 | function () { |
| 49 | echo '<div class="notice notice-warning"><p><strong>' . |
| 50 | esc_html__( |
| 51 | 'You enabled Cookiebot™ auto blocking mode but still using addons', |
| 52 | 'cookiebot' |
| 53 | ) . |
| 54 | '</strong><br>' . |
| 55 | esc_html__( |
| 56 | 'In some occasions this may cause client side errors. If you notice any errors please try to disable Cookiebot™ addons or contact Cookiebot™ support.', |
| 57 | 'cookiebot' |
| 58 | ) . |
| 59 | '</p></div>'; |
| 60 | } |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | if ( Cookiebot_WP::cookiebot_disabled_in_admin() === true && is_admin() ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $addons_enabled_counter = 0; |
| 69 | /** @var Base_Cookiebot_Addon $addon */ |
| 70 | foreach ( $this->settings_service->get_active_addons() as $addon ) { |
| 71 | if ( ! $addon->cookie_consent->are_cookie_states_accepted( $addon->get_cookie_types() ) |
| 72 | || cookiebot_addons_enabled_cache_plugin() ) { |
| 73 | $addon->load_addon_configuration(); |
| 74 | $addons_enabled_counter++; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * After WordPress is fully loaded |
| 80 | * |
| 81 | * Run buffer output actions - this runs after scanning of every addons |
| 82 | */ |
| 83 | add_action( 'parse_request', array( $this, 'run_buffer_output_manipulations' ) ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Runs every added action hooks to manipulate script tag |
| 88 | * |
| 89 | * @throws Exception |
| 90 | * @since 1.3.0 |
| 91 | */ |
| 92 | public function run_buffer_output_manipulations() { |
| 93 | /** |
| 94 | * @var $buffer_output Buffer_Output_Interface |
| 95 | */ |
| 96 | $buffer_output = $this->settings_service->container->get( 'Buffer_Output_Interface' ); |
| 97 | |
| 98 | if ( $buffer_output->has_action() ) { |
| 99 | $buffer_output->run_actions(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 |