Additional_Page.php
1 year ago
Dashboard_Page.php
1 year ago
Debug_Page.php
1 year ago
Embeddings_Page.php
1 year ago
Gcm_Page.php
1 year ago
General_Page.php
1 year ago
Gtm_Page.php
1 year ago
Iab_Page.php
1 year ago
Multiple_Page.php
1 year ago
Settings_Page.php
1 year ago
Settings_Page_Interface.php
1 year ago
Support_Page.php
1 year ago
Debug_Page.php
325 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\settings\pages; |
| 4 | |
| 5 | use cybot\cookiebot\addons\controller\addons\Base_Cookiebot_Addon; |
| 6 | use cybot\cookiebot\addons\Cookiebot_Addons; |
| 7 | use cybot\cookiebot\lib\Consent_API_Helper; |
| 8 | use cybot\cookiebot\lib\Cookiebot_Frame; |
| 9 | use cybot\cookiebot\lib\Cookiebot_Javascript_Helper; |
| 10 | use cybot\cookiebot\lib\Settings_Service_Interface; |
| 11 | use cybot\cookiebot\lib\Cookiebot_WP; |
| 12 | use cybot\cookiebot\shortcode\Cookiebot_Declaration_Shortcode; |
| 13 | use InvalidArgumentException; |
| 14 | use RuntimeException; |
| 15 | use function cybot\cookiebot\lib\asset_url; |
| 16 | use function cybot\cookiebot\lib\include_view; |
| 17 | use Exception; |
| 18 | |
| 19 | class Debug_Page implements Settings_Page_Interface { |
| 20 | |
| 21 | |
| 22 | const ADMIN_SLUG = 'cookiebot_debug'; |
| 23 | |
| 24 | public function menu() { |
| 25 | if ( Cookiebot_Frame::is_cb_frame_type() !== 'empty' ) { |
| 26 | add_submenu_page( |
| 27 | 'cookiebot', |
| 28 | __( 'Debug info', 'cookiebot' ), |
| 29 | __( 'Debug info', 'cookiebot' ), |
| 30 | 'manage_options', |
| 31 | self::ADMIN_SLUG, |
| 32 | array( $this, 'display' ), |
| 33 | 25 |
| 34 | ); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @throws InvalidArgumentException |
| 40 | */ |
| 41 | public function display() { |
| 42 | wp_enqueue_script( |
| 43 | 'cookiebot-debug-page-js', |
| 44 | asset_url( 'js/backend/debug-page.js' ), |
| 45 | null, |
| 46 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 47 | true |
| 48 | ); |
| 49 | |
| 50 | $style_sheets = array( |
| 51 | array( 'cookiebot-debug-css', 'css/backend/debug_info.css' ), |
| 52 | ); |
| 53 | |
| 54 | foreach ( $style_sheets as $style ) { |
| 55 | wp_enqueue_style( |
| 56 | $style[0], |
| 57 | asset_url( $style[1] ), |
| 58 | null, |
| 59 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | $debug_output = $this->prepare_debug_data(); |
| 64 | |
| 65 | include_view( Cookiebot_Frame::get_view_path() . 'debug-page.php', array( 'debug_output' => $debug_output ) ); |
| 66 | } |
| 67 | |
| 68 | private function get_ignored_scripts() { |
| 69 | $ignored_scripts = get_option( 'cookiebot-ignore-scripts' ); |
| 70 | |
| 71 | $ignored_scripts = array_map( |
| 72 | function ( $ignore_tag ) { |
| 73 | return trim( $ignore_tag ); |
| 74 | }, |
| 75 | explode( PHP_EOL, $ignored_scripts ) |
| 76 | ); |
| 77 | |
| 78 | $ignored_scripts = apply_filters( 'cybot_cookiebot_ignore_scripts', $ignored_scripts ); |
| 79 | |
| 80 | return implode( ', ', $ignored_scripts ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @throws InvalidArgumentException |
| 85 | */ |
| 86 | public function prepare_debug_data() { |
| 87 | global $wpdb; |
| 88 | |
| 89 | $cookiebot_javascript_helper = new Cookiebot_Javascript_Helper(); |
| 90 | |
| 91 | $debug_output = ''; |
| 92 | // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 93 | $debug_output .= '##### Debug Information for ' . get_site_url() . ' generated at ' . date( 'c' ) . " #####\n\n"; |
| 94 | $debug_output .= 'WordPress Version: ' . get_bloginfo( 'version' ) . "\n"; |
| 95 | $debug_output .= 'WordPress Language: ' . get_bloginfo( 'language' ) . "\n"; |
| 96 | $debug_output .= 'PHP Version: ' . phpversion() . "\n"; |
| 97 | $debug_output .= 'MySQL Version: ' . $wpdb->db_version() . "\n"; |
| 98 | $debug_output .= "\n--- Cookiebot Information ---\n"; |
| 99 | $debug_output .= 'Plugin Version: ' . Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION . "\n"; |
| 100 | $debug_output .= 'Settings ID: ' . Cookiebot_WP::get_cbid() . "\n"; |
| 101 | $debug_output .= 'Blocking mode: ' . get_option( 'cookiebot-cookie-blocking-mode' ) . "\n"; |
| 102 | if ( Cookiebot_Frame::is_cb_frame_type() !== false ) { |
| 103 | $debug_output .= 'Language: ' . get_option( 'cookiebot-language' ) . "\n"; |
| 104 | $debug_output .= 'Frontend Language: ' . $this->print_option_enabled( 'cookiebot-front-language' ) . "\n"; |
| 105 | } |
| 106 | $debug_output .= 'IAB: ' . $this->print_option_enabled( 'cookiebot-iab' ) . "\n"; |
| 107 | if ( Cookiebot_Frame::is_cb_frame_type() !== false ) { |
| 108 | $debug_output .= 'TCF version: ' . $this->print_tcf_version() . "\n"; |
| 109 | $debug_output .= 'TCF tag: ' . $cookiebot_javascript_helper->include_publisher_restrictions_js( true ) . "\n"; |
| 110 | $debug_output .= 'Multiple banners: ' . $this->print_option_enabled( 'cookiebot-multiple-config' ) . "\n"; |
| 111 | $debug_output .= $this->print_multiple_configuration_banners(); |
| 112 | $debug_output .= 'Add async/defer to banner tag: ' . $this->print_option_if_not_empty( 'cookiebot-script-tag-uc-attribute' ) . "\n"; |
| 113 | $debug_output .= 'Add async/defer to declaration tag: ' . $this->print_option_if_not_empty( 'cookiebot-script-tag-cd-attribute' ) . "\n"; |
| 114 | } |
| 115 | $debug_output .= 'Auto update: ' . $this->print_option_enabled( 'cookiebot-autoupdate' ) . "\n"; |
| 116 | $debug_output .= 'Hide Cookie Popup: ' . $this->print_option_active( 'cookiebot-nooutput' ) . "\n"; |
| 117 | $debug_output .= 'Enable Cookiebot on front end while logged in: ' . $this->print_option_active( 'cookiebot-output-logged-in' ) . "\n"; |
| 118 | if ( Cookiebot_Frame::is_cb_frame_type() !== false ) { |
| 119 | $debug_output .= 'List of ignored javascript files: ' . $this->get_ignored_scripts() . "\n"; |
| 120 | $debug_output .= 'Banner tag: ' . "\n" . $cookiebot_javascript_helper->include_cookiebot_js( true ) . "\n"; |
| 121 | $debug_output .= 'Declaration tag: ' . Cookiebot_Declaration_Shortcode::show_declaration() . "\n"; |
| 122 | } else { |
| 123 | $debug_output .= 'Banner tag: ' . "\n" . $cookiebot_javascript_helper->include_uc_cmp_js( true ) . "\n"; |
| 124 | } |
| 125 | |
| 126 | if ( get_option( 'cookiebot-gtm' ) !== false ) { |
| 127 | $debug_output .= 'GTM tag: ' . $cookiebot_javascript_helper->include_google_tag_manager_js( true ) . "\n"; |
| 128 | } |
| 129 | |
| 130 | if ( get_option( 'cookiebot-gcm' ) !== false ) { |
| 131 | $debug_output .= 'GCM tag: ' . $cookiebot_javascript_helper->include_google_consent_mode_js( true ) . "\n"; |
| 132 | } |
| 133 | |
| 134 | if ( is_multisite() ) { |
| 135 | $debug_output .= $this->print_multisite_network_settings(); |
| 136 | } |
| 137 | |
| 138 | $debug_output .= $this->print_wp_consent_level_api_mapping(); |
| 139 | $debug_output .= $this->print_activated_addons(); |
| 140 | $debug_output .= $this->print_activated_plugins(); |
| 141 | |
| 142 | $debug_output .= "\n##### Debug Information END #####"; |
| 143 | |
| 144 | return $debug_output; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Print the value of the option if it's not empty. |
| 149 | * |
| 150 | * @param string $option_name Name of the option to print. |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | private function print_option_if_not_empty( $option_name, $is_multisite = false ) { |
| 155 | $option_value = $is_multisite ? get_site_option( $option_name ) : get_option( $option_name ); |
| 156 | return $option_value !== '' ? $option_value : 'None'; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Print "Enabled" or "Not enabled" depending on the option value. Option value should be "1" or "0". |
| 161 | * |
| 162 | * @param string $option_name Name of the option to check. |
| 163 | * @param bool $is_multisite Is multisite option. |
| 164 | * |
| 165 | * @return string |
| 166 | */ |
| 167 | private function print_option_enabled( $option_name, $is_multisite = false ) { |
| 168 | return $this->print_option_active( $option_name, $is_multisite, 'Enabled', 'Not enabled' ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Print "Yes" or "No" depending on the option value. Option value should be "1" or "0". If <b>$active_text</b> or |
| 173 | * <b>$disabled_text</b> is set, it will be used instead of default values "Yes" or "No". |
| 174 | * |
| 175 | * @param string $option_name Name of the option to check. |
| 176 | * @param bool $is_multisite Is multisite option. |
| 177 | * @param string $active_text (Optional) Text to print if option is active. Default is "Yes". |
| 178 | * @param string $disabled_text (Optional) Text to print if option is disabled. Default is "No". |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | private function print_option_active( $option_name, $is_multisite = false, $active_text = 'Yes', $disabled_text = 'No' ) { |
| 183 | if ( $is_multisite ) { |
| 184 | return get_site_option( $option_name ) === '1' ? $active_text : $disabled_text; |
| 185 | } |
| 186 | return get_option( $option_name ) === '1' ? $active_text : $disabled_text; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Render debug information about WP Consent Level API mapping. |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | private function print_wp_consent_level_api_mapping() { |
| 195 | $output = ''; |
| 196 | |
| 197 | $consent_api_helper = new Consent_API_Helper(); |
| 198 | |
| 199 | if ( $consent_api_helper->is_wp_consent_api_active() ) { |
| 200 | $output .= "\n--- WP Consent API Mapping ---\n"; |
| 201 | $map = $consent_api_helper->get_wp_consent_api_mapping(); |
| 202 | if ( Cookiebot_Frame::is_cb_frame_type() !== false ) { |
| 203 | $output .= 'F = Functional, N = Necessary, P = Preferences, M = Marketing, S = Statistics, SA = Statistics Anonymous' . "\n"; |
| 204 | foreach ( $map as $key => $value ) { |
| 205 | $output .= strtoupper( str_replace( ';', ', ', $key ) ) . ' => '; |
| 206 | $output .= 'F=1, '; |
| 207 | $output .= 'P=' . $value['preferences'] . ', '; |
| 208 | $output .= 'M=' . $value['marketing'] . ', '; |
| 209 | $output .= 'S=' . $value['statistics'] . ', '; |
| 210 | $output .= 'SA=' . $value['statistics-anonymous'] . "\n"; |
| 211 | } |
| 212 | } else { |
| 213 | foreach ( $map as $key => $value ) { |
| 214 | $output .= $key . ' => ' . $value . "\n"; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | return $output; |
| 220 | } |
| 221 | |
| 222 | private function print_multiple_configuration_banners() { |
| 223 | $secondary_id = get_option( 'cookiebot-second-banner-id' ); |
| 224 | $secondary_regions = get_option( 'cookiebot-second-banner-regions' ); |
| 225 | $banners = get_option( 'cookiebot-multiple-banners' ); |
| 226 | $output = ''; |
| 227 | |
| 228 | if ( ! empty( $banners ) ) { |
| 229 | $counter = 1; |
| 230 | $output .= "\n--- Multiple Configuration Banners ---\n"; |
| 231 | if ( ! empty( $secondary_id ) ) { |
| 232 | $output .= '-Banner: ' . $counter . " -\n"; |
| 233 | $output .= 'Id: ' . $secondary_id . " \n"; |
| 234 | $output .= 'Regions: ' . $secondary_regions . " \n\n"; |
| 235 | ++$counter; |
| 236 | } |
| 237 | foreach ( $banners as $banner ) { |
| 238 | $output .= '-Banner: ' . $counter . " -\n"; |
| 239 | $output .= 'Id: ' . $banner['group'] . " \n"; |
| 240 | $output .= 'Regions: ' . $banner['region'] . " \n\n"; |
| 241 | ++$counter; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return $output; |
| 246 | } |
| 247 | |
| 248 | private function print_tcf_version() { |
| 249 | $version = get_option( 'cookiebot-tcf-version' ); |
| 250 | return ( empty( $version ) || $version === 'IAB' ) ? '2.0' : $version; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Print information about activated cookiebot addons. |
| 255 | * |
| 256 | * @return string |
| 257 | */ |
| 258 | private function print_activated_addons() { |
| 259 | $output = ''; |
| 260 | |
| 261 | if ( Cookiebot_Frame::is_cb_frame_type() === false ) { |
| 262 | return $output; |
| 263 | } |
| 264 | |
| 265 | try { |
| 266 | $cookiebot_addons = new Cookiebot_Addons(); |
| 267 | /** @var Settings_Service_Interface $settings_service */ |
| 268 | $settings_service = $cookiebot_addons->container->get( 'Settings_Service_Interface' ); |
| 269 | $addons = $settings_service->get_active_addons(); |
| 270 | $output .= "\n--- Activated Cookiebot Addons ---\n"; |
| 271 | /** @var Base_Cookiebot_Addon $addon */ |
| 272 | foreach ( $addons as $addon ) { |
| 273 | $output .= $addon::ADDON_NAME . ' (' . implode( ', ', $addon->get_cookie_types() ) . ")\n"; |
| 274 | } |
| 275 | } catch ( Exception $exception ) { |
| 276 | $output .= PHP_EOL . '--- Cookiebot Addons could not be activated ---' . PHP_EOL; |
| 277 | $output .= $exception->getMessage() . PHP_EOL; |
| 278 | } |
| 279 | |
| 280 | return $output; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Print information about activated plugins |
| 285 | * |
| 286 | * @return string |
| 287 | */ |
| 288 | private function print_activated_plugins() { |
| 289 | if ( ! function_exists( 'get_plugins' ) ) { |
| 290 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 291 | } |
| 292 | |
| 293 | $plugins = get_plugins(); |
| 294 | $active_plugins = get_option( 'active_plugins' ); |
| 295 | |
| 296 | $output = "\n--- Activated Plugins ---\n"; |
| 297 | |
| 298 | foreach ( $active_plugins as $plugin_key ) { |
| 299 | if ( $plugin_key !== 'cookiebot/cookiebot.php' ) { |
| 300 | $output .= $plugins[ $plugin_key ]['Name'] . ' (Version: ' . $plugins[ $plugin_key ]['Version'] . ")\n"; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return $output; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Print information about activated plugins |
| 309 | * |
| 310 | * @return string |
| 311 | */ |
| 312 | private function print_multisite_network_settings() { |
| 313 | $output = "\n--- Cookiebot Multisite Information ---\n"; |
| 314 | $output .= 'Cookiebot Network ID: ' . $this->print_option_if_not_empty( 'cookiebot-cbid', true ) . "\n"; |
| 315 | $output .= 'Network Blocking mode: ' . get_site_option( 'cookiebot-cookie-blocking-mode' ) . "\n"; |
| 316 | $output .= 'Network Add async/defer to banner tag: ' . $this->print_option_if_not_empty( 'cookiebot-script-tag-uc-attribute', true ) . "\n"; |
| 317 | $output .= 'Network Add async/defer to declaration tag: ' . $this->print_option_if_not_empty( 'cookiebot-script-tag-cd-attribute', true ) . "\n"; |
| 318 | $output .= 'Network Auto update: ' . $this->print_option_enabled( 'cookiebot-autoupdate', true ) . "\n"; |
| 319 | $output .= 'Network Hide Cookie Popup: ' . $this->print_option_enabled( 'cookiebot-nooutput', true ) . "\n"; |
| 320 | $output .= 'Network Disable Cookiebot in WP Admin: ' . $this->print_option_active( 'cookiebot-nooutput-admin', true ) . "\n"; |
| 321 | |
| 322 | return $output; |
| 323 | } |
| 324 | } |
| 325 |