plugin-controller.php
130 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 | use Cookiebot_WP; |
| 9 | use Cybot\Dependencies\DI; |
| 10 | use Exception; |
| 11 | |
| 12 | /** |
| 13 | * Class Plugin_Controller |
| 14 | * @package cookiebot_addons\controller |
| 15 | */ |
| 16 | class Plugin_Controller { |
| 17 | |
| 18 | /** |
| 19 | * IoC container - Dependency Injection |
| 20 | * |
| 21 | * @var Settings_Service_Interface |
| 22 | * |
| 23 | * @since 1.1.0 |
| 24 | */ |
| 25 | private $settings_service; |
| 26 | |
| 27 | /** |
| 28 | * Plugin_Controller constructor. |
| 29 | * |
| 30 | * @param $settings_service Settings_Service_Interface IoC Container |
| 31 | * |
| 32 | * @since 1.2.0 |
| 33 | */ |
| 34 | public function __construct( Settings_Service_Interface $settings_service ) { |
| 35 | $this->settings_service = $settings_service; |
| 36 | $this->load_init_files(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Load init files to use 'validate_plugin' and 'is_plugin_active' |
| 41 | * |
| 42 | * @since 1.3.0 |
| 43 | */ |
| 44 | protected function load_init_files() { |
| 45 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 46 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 47 | require_once ABSPATH . '/wp-includes/l10n.php'; |
| 48 | require_once ABSPATH . '/wp-admin/includes/translation-install.php'; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Load addon configuration if the plugin is activated |
| 54 | * |
| 55 | * @throws Exception |
| 56 | * @since 1.2.0 |
| 57 | * @version 1.3.0 |
| 58 | */ |
| 59 | public function load_active_addons() { |
| 60 | /** |
| 61 | * Check if Cookiebot is activated and active. Return if cookiebot is inactive |
| 62 | */ |
| 63 | if ( ! function_exists( 'cookiebot_active' ) || ! cookiebot_active() ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if ( Cookiebot_WP::cookiebot_disabled_in_admin() === true && is_admin() ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check plugins one by one and load configuration if it is active |
| 73 | * |
| 74 | * @var $plugin Cookiebot_Addons_Interface |
| 75 | */ |
| 76 | $addons_enabled_counter = 0; |
| 77 | foreach ( $this->settings_service->get_active_addons() as $plugin ) { |
| 78 | if ( ! $plugin->cookie_consent->are_cookie_states_accepted( $plugin->get_cookie_types() ) |
| 79 | || cookiebot_addons_enabled_cache_plugin() ) { |
| 80 | $plugin->load_configuration(); |
| 81 | $addons_enabled_counter++; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * After WordPress is fully loaded |
| 87 | * |
| 88 | * Run buffer output actions - this runs after scanning of every addons |
| 89 | */ |
| 90 | add_action( 'parse_request', array( $this, 'run_buffer_output_manipulations' ) ); |
| 91 | |
| 92 | /** |
| 93 | * Add notice for the user if any addons is enabled and cookie |
| 94 | * blocking mode is set to auto. |
| 95 | */ |
| 96 | if ( $addons_enabled_counter > 0 && Cookiebot_WP::get_cookie_blocking_mode() === 'auto' ) { |
| 97 | //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 98 | if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'cookiebot', 'cookiebot-addons' ), true ) ) { |
| 99 | add_action( |
| 100 | 'admin_notices', |
| 101 | function() { |
| 102 | echo '<div class="notice notice-warning"> |
| 103 | <p> |
| 104 | <strong>' . esc_html__( 'You enabled Cookiebot auto blocking mode but still using addons' ) . '</strong><br> |
| 105 | ' . esc_html__( 'In some occasions this may cause client side errors. If you notice any errors please try to disable Cookiebot addons or contact Cookiebot support.' ) . ' |
| 106 | </p> |
| 107 | </div>'; |
| 108 | } |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Runs every added action hooks to manipulate script tag |
| 116 | * |
| 117 | * @since 1.3.0 |
| 118 | */ |
| 119 | public function run_buffer_output_manipulations() { |
| 120 | /** |
| 121 | * @var $buffer_output Buffer_Output_Interface |
| 122 | */ |
| 123 | $buffer_output = $this->settings_service->container->get( 'Buffer_Output_Interface' ); |
| 124 | |
| 125 | if ( $buffer_output->has_action() ) { |
| 126 | $buffer_output->run_actions(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 |