| 1 |
<?php |
| 2 |
|
| 3 |
namespace cybot\cookiebot\settings\pages; |
| 4 |
|
| 5 |
use cybot\cookiebot\lib\Cookiebot_Frame; |
| 6 |
use cybot\cookiebot\lib\Cookiebot_WP; |
| 7 |
use InvalidArgumentException; |
| 8 |
use function cybot\cookiebot\lib\include_view; |
| 9 |
|
| 10 |
// Import WordPress functions from global namespace |
| 11 |
use function \add_menu_page; |
| 12 |
use function \add_submenu_page; |
| 13 |
use function \do_action; |
| 14 |
use function \wp_enqueue_style; |
| 15 |
use function \wp_enqueue_script; |
| 16 |
use function \wp_localize_script; |
| 17 |
use function \admin_url; |
| 18 |
use function \wp_create_nonce; |
| 19 |
use function \__; |
| 20 |
use function \defined; |
| 21 |
use function \constant; |
| 22 |
use function cybot\cookiebot\lib\asset_url; |
| 23 |
|
| 24 |
// Add constant for WP_DEBUG |
| 25 |
if ( ! defined( 'WP_DEBUG' ) ) { |
| 26 |
define( 'WP_DEBUG', false ); |
| 27 |
} |
| 28 |
|
| 29 |
class Dashboard_Page implements Settings_Page_Interface { |
| 30 |
|
| 31 |
|
| 32 |
const ICON = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAiIGhlaWdodD0iNDAiIHZpZXdCb3g9IjAgMCA0MCA0MCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGcgY2xpcC1wYXRoPSJ1cmwoI2NsaXAwXzY0ODFfMzE4MTUpIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik01Ljg2MTYgNS44MDUyVjE5LjgxMTRDNS44NjE2IDI3LjUzNjMgMTIuMjAzOSAzMy44MTc2IDIwLjAwMzkgMzMuODE3NkMyNy44MDM4IDMzLjgxNzYgMzQuMTQ2MiAyNy41MzYzIDM0LjE0NjIgMTkuODExNFY1LjgwNTJINS44NjE2Wk0yMCAzOS42MjI4QzguOTc2MzggMzkuNjIwNyAwIDMwLjczNzEgMCAxOS44MTE0VjBINDBWMTkuODExNEM0MCAzMC43Mjk0IDMxLjAzMTQgMzkuNjIwNyAyMCAzOS42MjI4Wk0yMi42ODk0IDI2Ljk0ODZMMjIuNjg4OCAyNi45NDk5SDE1LjkyTDE1LjkzMTIgMjYuOTI2Nkw5Ljk4OTIxIDE2LjU4MjFIMTYuNzY1N0wxOS4wMTA2IDIwLjQ5MDJMMjMuNzEyMiAxMC42NjMxSDMwLjQ4ODhMMjIuNzAzNSAyNi45MTkyTDIyLjcyMDQgMjYuOTQ4NkgyMi42ODk0WiIgZmlsbD0iYmxhY2siLz4KPC9nPgo8ZGVmcz4KPGNsaXBQYXRoIGlkPSJjbGlwMF82NDgxXzMxODE1Ij4KPHJlY3Qgd2lkdGg9IjQwIiBoZWlnaHQ9IjQwIiBmaWxsPSJ3aGl0ZSIvPgo8L2NsaXBQYXRoPgo8L2RlZnM+Cjwvc3ZnPgo='; |
| 33 |
|
| 34 |
const ADMIN_SLUG = 'cookiebot'; |
| 35 |
|
| 36 |
public function menu() { |
| 37 |
add_menu_page( |
| 38 |
'Cookiebot', |
| 39 |
__( 'Cookiebot', 'cookiebot' ), |
| 40 |
'manage_options', |
| 41 |
self::ADMIN_SLUG, |
| 42 |
array( $this, 'display' ), |
| 43 |
self::ICON |
| 44 |
); |
| 45 |
|
| 46 |
if ( Cookiebot_Frame::is_cb_frame_type() !== 'empty' ) { |
| 47 |
add_submenu_page( |
| 48 |
'cookiebot', |
| 49 |
__( 'Cookiebot Dashboard', 'cookiebot' ), |
| 50 |
__( 'Dashboard', 'cookiebot' ), |
| 51 |
'manage_options', |
| 52 |
self::ADMIN_SLUG, |
| 53 |
array( $this, 'display' ), |
| 54 |
1 |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @throws InvalidArgumentException |
| 61 |
*/ |
| 62 |
public function display() { |
| 63 |
// Get all necessary data upfront |
| 64 |
$cbid = Cookiebot_WP::get_cbid(); |
| 65 |
$auth_token = Cookiebot_WP::get_auth_token(); |
| 66 |
$user_data = Cookiebot_WP::get_user_data(); |
| 67 |
$scan_status = Cookiebot_WP::get_scan_status(); |
| 68 |
$scan_id = get_option( 'cookiebot-scan-id' ); |
| 69 |
$banner_enabled = Cookiebot_WP::get_banner_enabled(); |
| 70 |
$auto_blocking_mode = Cookiebot_WP::get_auto_blocking_mode(); |
| 71 |
$gcm_enabled = Cookiebot_WP::get_gcm_enabled(); |
| 72 |
$subscription = Cookiebot_WP::get_subscription_type(); |
| 73 |
$legal_framework = Cookiebot_WP::get_legal_framwework(); |
| 74 |
$is_authenticated = ! empty( Cookiebot_WP::get_auth_token() ); |
| 75 |
$is_cb_user = Cookiebot_Frame::is_cb_frame_type(); |
| 76 |
|
| 77 |
// Get user data and check if they were onboarded via signup |
| 78 |
$was_onboarded = Cookiebot_WP::was_onboarded_via_signup(); |
| 79 |
$user_email = isset( $user_data['email'] ) ? $user_data['email'] : ''; |
| 80 |
|
| 81 |
// Prepare variables for the template |
| 82 |
$template_args = array( |
| 83 |
'cbid' => $cbid, |
| 84 |
'user_data' => $user_data, |
| 85 |
'scan_status' => $scan_status, |
| 86 |
'scan_id' => $scan_id, |
| 87 |
'cb_wp' => asset_url( 'img/cb-wp.png' ), |
| 88 |
'europe_icon' => asset_url( 'img/europe.png' ), |
| 89 |
'usa_icon' => asset_url( 'img/usa.png' ), |
| 90 |
'check_icon' => asset_url( 'img/icons/check.svg' ), |
| 91 |
'link_icon' => asset_url( 'img/icons/link.svg' ), |
| 92 |
'banner_enabled' => $banner_enabled, |
| 93 |
'auto_blocking_mode' => $auto_blocking_mode, |
| 94 |
'gcm_enabled' => $gcm_enabled, |
| 95 |
'preview_link' => Cookiebot_WP::get_preview_link(), |
| 96 |
'subscription' => $subscription, |
| 97 |
'legal_framework' => $legal_framework, |
| 98 |
'customize_banner_link' => 'https://admin.usercentrics.eu/#/v3/appearance/layout?settingsId=' . $cbid, |
| 99 |
'cookiebot_admin_link' => 'https://admin.cookiebot.com', |
| 100 |
'uc_admin_link' => 'https://admin.usercentrics.eu', |
| 101 |
'configure_banner_link' => 'https://support.usercentrics.com/hc/en-us/articles/18225055002908-WordPress-Plugin-FAQ', |
| 102 |
'has_user_data' => ! empty( $user_data ), |
| 103 |
'has_cbid' => ! empty( $cbid ), |
| 104 |
'is_authenticated' => ! empty( Cookiebot_WP::get_auth_token() ), |
| 105 |
'was_onboarded' => $was_onboarded, |
| 106 |
'user_email' => $user_email, |
| 107 |
); |
| 108 |
|
| 109 |
wp_enqueue_script( |
| 110 |
'cookiebot-amplitude-js', |
| 111 |
asset_url( 'js/backend/amplitude.js' ), |
| 112 |
array( 'jquery' ), |
| 113 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 114 |
true |
| 115 |
); |
| 116 |
|
| 117 |
wp_localize_script( |
| 118 |
'cookiebot-amplitude-js', |
| 119 |
'cookiebot_amplitude', |
| 120 |
array() |
| 121 |
); |
| 122 |
|
| 123 |
if ( ! $is_authenticated && ! empty( $cbid ) && ! empty( $user_data ) && ! empty( $was_onboarded ) ) { |
| 124 |
wp_enqueue_style( |
| 125 |
'cookiebot-dashboard-css', |
| 126 |
asset_url( 'css/backend/dashboard.css' ), |
| 127 |
array(), |
| 128 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 129 |
); |
| 130 |
|
| 131 |
wp_enqueue_script( |
| 132 |
'cookiebot-account-static-js', |
| 133 |
asset_url( 'js/backend/account-static.js' ), |
| 134 |
array( 'jquery' ), |
| 135 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 136 |
true |
| 137 |
); |
| 138 |
|
| 139 |
wp_localize_script( |
| 140 |
'cookiebot-account-static-js', |
| 141 |
'cookiebot_account', |
| 142 |
array( |
| 143 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 144 |
'nonce' => wp_create_nonce( 'cookiebot-account' ), |
| 145 |
'cbid' => $cbid, |
| 146 |
'scan_id' => $scan_id, |
| 147 |
'has_user_data' => ! empty( $user_data ), |
| 148 |
'has_cbid' => ! empty( $cbid ), |
| 149 |
'was_onboarded' => $was_onboarded, |
| 150 |
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
| 151 |
'auth_expired_flow' => true, |
| 152 |
) |
| 153 |
); |
| 154 |
|
| 155 |
require_once CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/admin/common/dashboard-page-session-expired.php'; |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
// Check if the trial has expired |
| 160 |
if ( Cookiebot_WP::is_trial_expired() && ! Cookiebot_WP::has_upgraded() ) { |
| 161 |
wp_enqueue_style( |
| 162 |
'cookiebot-dashboard-css', |
| 163 |
asset_url( 'css/backend/dashboard.css' ), |
| 164 |
array(), |
| 165 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 166 |
); |
| 167 |
|
| 168 |
wp_enqueue_script( |
| 169 |
'cookiebot-account-js', |
| 170 |
asset_url( 'js/backend/account.js' ), |
| 171 |
array( 'jquery' ), |
| 172 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 173 |
true |
| 174 |
); |
| 175 |
|
| 176 |
wp_localize_script( |
| 177 |
'cookiebot-account-js', |
| 178 |
'cookiebot_account', |
| 179 |
array( |
| 180 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 181 |
'nonce' => wp_create_nonce( 'cookiebot-account' ), |
| 182 |
'cbid' => $cbid, |
| 183 |
'scan_id' => $scan_id, |
| 184 |
'has_user_data' => ! empty( $user_data ), |
| 185 |
'has_cbid' => ! empty( $cbid ), |
| 186 |
'was_onboarded' => $was_onboarded, |
| 187 |
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
| 188 |
) |
| 189 |
); |
| 190 |
|
| 191 |
require_once CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/admin/common/dashboard-trial-expired.php'; |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// Redirect CB users to previous dashboard page |
| 196 |
if ( ( ! empty( $cbid ) && $is_cb_user ) || ( ! empty( $cbid ) && empty( $user_data ) ) ) { |
| 197 |
wp_enqueue_style( |
| 198 |
'cookiebot-dashboard-backup-css', |
| 199 |
asset_url( 'css/backend/dashboard-old.css' ), |
| 200 |
array(), |
| 201 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 202 |
); |
| 203 |
|
| 204 |
wp_enqueue_script( |
| 205 |
'cookiebot-account-js', |
| 206 |
asset_url( 'js/backend/account.js' ), |
| 207 |
array( 'jquery' ), |
| 208 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 209 |
true |
| 210 |
); |
| 211 |
|
| 212 |
wp_localize_script( |
| 213 |
'cookiebot-account-js', |
| 214 |
'cookiebot_account', |
| 215 |
array( |
| 216 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 217 |
'nonce' => wp_create_nonce( 'cookiebot-account' ), |
| 218 |
'cbid' => $cbid, |
| 219 |
'scan_id' => $scan_id, |
| 220 |
'has_user_data' => ! empty( $user_data ), |
| 221 |
'has_cbid' => ! empty( $cbid ), |
| 222 |
'was_onboarded' => $was_onboarded, |
| 223 |
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
| 224 |
) |
| 225 |
); |
| 226 |
|
| 227 |
require_once CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/admin/common/dashboard-page-old.php'; |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
// Redirect new users to cookiebot dashboard page |
| 232 |
if ( empty( $cbid ) ) { |
| 233 |
$args = array( |
| 234 |
'cbid' => Cookiebot_WP::get_cbid(), |
| 235 |
'cb_wp' => asset_url( 'img/cb-wp.png' ), |
| 236 |
'europe_icon' => asset_url( 'img/europe.png' ), |
| 237 |
'usa_icon' => asset_url( 'img/usa.png' ), |
| 238 |
'check_icon' => asset_url( 'img/icons/check.svg' ), |
| 239 |
'link_icon' => asset_url( 'img/icons/link.svg' ), |
| 240 |
); |
| 241 |
|
| 242 |
$style_sheets = array( |
| 243 |
array( 'cookiebot-dashboard-css', 'css/backend/dashboard-old.css' ), |
| 244 |
); |
| 245 |
|
| 246 |
foreach ( $style_sheets as $style ) { |
| 247 |
wp_enqueue_style( |
| 248 |
$style[0], |
| 249 |
asset_url( $style[1] ), |
| 250 |
null, |
| 251 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 252 |
); |
| 253 |
} |
| 254 |
|
| 255 |
include_view( Cookiebot_Frame::get_view_path() . 'dashboard-cookiebot.php', $args ); |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
wp_enqueue_style( |
| 260 |
'cookiebot-dashboard-css', |
| 261 |
asset_url( 'css/backend/dashboard.css' ), |
| 262 |
array(), |
| 263 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 264 |
); |
| 265 |
|
| 266 |
wp_enqueue_script( |
| 267 |
'cookiebot-dashboard-js', |
| 268 |
asset_url( 'js/backend/dashboard.js' ), |
| 269 |
array( 'jquery' ), |
| 270 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 271 |
true |
| 272 |
); |
| 273 |
|
| 274 |
wp_localize_script( |
| 275 |
'cookiebot-dashboard-js', |
| 276 |
'cookiebot_dashboard', |
| 277 |
array( |
| 278 |
'cbid' => $cbid, |
| 279 |
'user_data' => $user_data, |
| 280 |
) |
| 281 |
); |
| 282 |
|
| 283 |
wp_enqueue_script( |
| 284 |
'cookiebot-account-js', |
| 285 |
asset_url( 'js/backend/account.js' ), |
| 286 |
array( 'jquery' ), |
| 287 |
Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 288 |
true |
| 289 |
); |
| 290 |
|
| 291 |
wp_localize_script( |
| 292 |
'cookiebot-account-js', |
| 293 |
'cookiebot_account', |
| 294 |
array( |
| 295 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 296 |
'nonce' => wp_create_nonce( 'cookiebot-account' ), |
| 297 |
'cbid' => $cbid, |
| 298 |
'scan_id' => $scan_id, |
| 299 |
'has_user_data' => ! empty( $user_data ), |
| 300 |
'has_cbid' => ! empty( $cbid ), |
| 301 |
'was_onboarded' => $was_onboarded, |
| 302 |
'is_authenticated' => ! empty( Cookiebot_WP::get_auth_token() ), |
| 303 |
'messages' => array( |
| 304 |
'success_create' => __( 'Account created successfully!', 'cookiebot' ), |
| 305 |
'error_create' => __( 'Failed to create account.', 'cookiebot' ), |
| 306 |
'success_verify' => __( 'CBID verified successfully!', 'cookiebot' ), |
| 307 |
'error_verify' => __( 'Invalid CBID.', 'cookiebot' ), |
| 308 |
), |
| 309 |
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG, |
| 310 |
) |
| 311 |
); |
| 312 |
|
| 313 |
require_once CYBOT_COOKIEBOT_PLUGIN_DIR . 'src/view/admin/common/dashboard-page.php'; |
| 314 |
} |
| 315 |
} |
| 316 |
|