'; echo ''; } function samtyk_cmp_get_available_languages(): array { $languages = ['auto' => samtyk_cmp_get_admin_text('automatic')]; $locales = function_exists('get_available_languages') ? get_available_languages() : []; if (function_exists('get_locale')) { $locales[] = get_locale(); } foreach ($locales as $locale) { $language = samtyk_cmp_normalize_language($locale); if ($language !== 'auto') { $languages[$language] = strtoupper($language); } } ksort($languages); return ['auto' => samtyk_cmp_get_admin_text('automatic')] + $languages; } function samtyk_cmp_save_settings(): void { if (!current_user_can('manage_options') || empty($_POST['samtyk_cmp_action'])) { return; } if (sanitize_key(wp_unslash($_POST['samtyk_cmp_action'])) !== 'save_settings') { return; } check_admin_referer('samtyk_cmp_save_settings'); $language = samtyk_cmp_normalize_language( isset($_POST['samtyk_cmp_language']) && is_string($_POST['samtyk_cmp_language']) ? sanitize_text_field(wp_unslash($_POST['samtyk_cmp_language'])) : 'auto' ); $gtm_requested = !empty($_POST['samtyk_cmp_gtm_enabled']); $gtm_id = isset($_POST['samtyk_cmp_gtm_id']) && is_string($_POST['samtyk_cmp_gtm_id']) ? strtoupper(trim(sanitize_text_field(wp_unslash($_POST['samtyk_cmp_gtm_id'])))) : ''; $gtm_enabled = $gtm_requested && samtyk_cmp_is_valid_gtm_id($gtm_id); update_option('samtyk_cmp_settings', [ 'enabled' => !empty($_POST['samtyk_cmp_enabled']), 'language' => $language, 'consent_mode_v2_enabled' => !empty($_POST['samtyk_cmp_consent_mode_v2_enabled']), 'gtm_enabled' => $gtm_enabled, 'gtm_id' => $gtm_id, 'video_blocking_enabled' => !empty($_POST['samtyk_cmp_video_blocking_enabled']), ]); wp_safe_redirect(add_query_arg([ 'page' => SAMTYK_CMP_MENU_SLUG, 'updated' => '1', 'gtm_error' => $gtm_requested && !$gtm_enabled ? '1' : '0', ], admin_url('admin.php'))); exit; } function samtyk_cmp_get_menu_icon(): string { $icon_path = SAMTYK_CMP_PLUGIN_DIR . 'assets/samtyk-menu-icon.svg'; $icon_svg = is_readable($icon_path) ? file_get_contents($icon_path) : false; return is_string($icon_svg) && $icon_svg !== '' ? 'data:image/svg+xml;base64,' . base64_encode($icon_svg) : 'dashicons-shield-alt'; } function samtyk_cmp_register_admin_menu(): void { add_menu_page( 'Samtyk CMP', 'Samtyk CMP', 'manage_options', SAMTYK_CMP_MENU_SLUG, 'samtyk_cmp_render_admin_page', samtyk_cmp_get_menu_icon(), 80 ); } function samtyk_cmp_enqueue_admin_assets(string $hook_suffix): void { if ($hook_suffix === 'toplevel_page_' . SAMTYK_CMP_MENU_SLUG) { wp_enqueue_style('samtyk-cmp-admin', SAMTYK_CMP_PLUGIN_URL . 'assets/admin.css', [], SAMTYK_CMP_VERSION); wp_enqueue_script('samtyk-cmp-admin', SAMTYK_CMP_PLUGIN_URL . 'assets/admin.js', [], SAMTYK_CMP_VERSION, true); } } function samtyk_cmp_render_toggle(string $name, bool $enabled, string $label, string $description, string $items_label = '', array $items = [], string $description_link_url = '', string $description_link_label = ''): void { echo '
'; echo ''; echo '

' . esc_html($description); if ($description_link_url !== '' && $description_link_label !== '') { echo '
' . esc_html($description_link_label) . ''; } echo '

'; if ($items !== []) { if ($items_label !== '') { echo '

' . esc_html($items_label) . '

'; } echo ''; } echo '
'; } function samtyk_cmp_render_blocking_item(string $name, string $description, array $consents): void { echo '
'; echo '

' . esc_html($name) . '

'; if ($description !== '') { echo '

' . esc_html($description) . '

'; } echo '
'; } function samtyk_cmp_render_blocking_page(callable $t): void { $functional = $t('consent_functional'); $statistics = $t('consent_statistics'); $marketing = $t('consent_marketing'); echo '
'; echo '

' . esc_html($t('blocking_title')) . '

' . esc_html($t('blocking_description')) . '

'; echo '

' . esc_html($t('blocking_active_title')) . '

' . esc_html($t('blocking_active_description')) . '

'; samtyk_cmp_render_blocking_item('WooCommerce Sourcebuster', '', [$statistics]); samtyk_cmp_render_blocking_item($t('blocking_woocommerce_attribution'), '', [$statistics, $marketing]); samtyk_cmp_render_blocking_item($t('blocking_mailchimp_name'), $t('blocking_mailchimp_description'), [$marketing]); samtyk_cmp_render_blocking_item('Jetpack Stats', '', [$statistics]); echo '
'; echo '

' . esc_html($t('blocking_content_title')) . '

' . esc_html($t('blocking_content_description')) . '

'; samtyk_cmp_render_blocking_item('YouTube', '', [$functional, $statistics, $marketing]); samtyk_cmp_render_blocking_item('Vimeo', $t('blocking_vimeo_note'), [$statistics]); samtyk_cmp_render_blocking_item('Spotify', '', [$functional, $statistics, $marketing]); samtyk_cmp_render_blocking_item('Issuu', '', [$functional, $statistics, $marketing]); samtyk_cmp_render_blocking_item('Instagram', $t('blocking_instagram_description'), [$marketing]); echo '
'; } function samtyk_cmp_render_support_page(): void { echo '
'; echo '

' . esc_html__('Support and documentation', 'samtyk-cmp') . '

' . esc_html__('Find help with Samtyk and guides for setting up your website.', 'samtyk-cmp') . '

'; echo '

' . esc_html__('Support', 'samtyk-cmp') . '

' . esc_html__('Need help with Samtyk or your configuration? Contact us through the support page.', 'samtyk-cmp') . '

' . esc_html__('Contact support', 'samtyk-cmp') . '
'; echo '

' . esc_html__('Documentation', 'samtyk-cmp') . '

' . esc_html__('Read guides about setup, consent categories, scripts, and integrations in the Samtyk documentation.', 'samtyk-cmp') . '

' . esc_html__('Open documentation', 'samtyk-cmp') . '
'; } function samtyk_cmp_render_admin_page(): void { $settings = samtyk_cmp_get_settings(); $languages = samtyk_cmp_get_available_languages(); $t = 'samtyk_cmp_get_admin_text'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only navigation state. $view = isset($_GET['view']) ? sanitize_key(wp_unslash($_GET['view'])) : 'settings'; $view = in_array($view, ['blocking', 'scripts', 'support'], true) ? $view : 'settings'; $settings_url = '?page=' . rawurlencode(SAMTYK_CMP_MENU_SLUG); $blocking_url = $settings_url . '&view=blocking'; $scripts_url = $settings_url . '&view=scripts'; $support_url = $settings_url . '&view=support'; echo '

Samtyk CMP

'; echo ''; if ($view === 'support') { samtyk_cmp_render_support_page(); echo '
'; return; } if ($view === 'blocking') { samtyk_cmp_render_blocking_page($t); echo ''; return; } if ($view === 'scripts') { samtyk_cmp_render_snippet_page($t); echo ''; return; } echo '
'; echo '

' . esc_html($t('setup')) . '

' . esc_html($t('heading_description')) . '

'; echo '

' . esc_html($t('account_title')) . '

' . esc_html($t('account_description')) . ' admin.samtyk.com.

'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only redirect status set after a nonce-protected save. if (isset($_GET['updated']) && sanitize_key(wp_unslash($_GET['updated'])) === '1') { echo '
'; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only redirect status set after a nonce-protected save. if (isset($_GET['gtm_error']) && sanitize_key(wp_unslash($_GET['gtm_error'])) === '1') { echo '
'; } echo '
'; wp_nonce_field('samtyk_cmp_save_settings'); echo ''; samtyk_cmp_render_toggle('samtyk_cmp_enabled', $settings['enabled'], $t('enabled_label'), $t('enabled_description')); echo '
'; echo '
'; echo '

' . esc_html($t('language_title')) . '

' . esc_html($t('language_description')) . '
<html lang>.

'; echo ''; samtyk_cmp_render_dropdown('samtyk-cmp-language', 'samtyk_cmp_language', $languages, $settings['language']); echo '
'; samtyk_cmp_render_toggle('samtyk_cmp_consent_mode_v2_enabled', $settings['consent_mode_v2_enabled'], $t('consent_mode_label'), $t('consent_mode_description')); echo '
'; echo '

' . esc_html($t('gtm_help')) . '

'; samtyk_cmp_render_toggle( 'samtyk_cmp_video_blocking_enabled', $settings['video_blocking_enabled'], $t('video_label'), $t('video_description'), '', [], $blocking_url, $t('blocking_link') ); echo '
'; echo '
'; echo '
'; echo '
'; } add_action('admin_init', 'samtyk_cmp_save_settings'); add_action('admin_menu', 'samtyk_cmp_register_admin_menu'); add_action('admin_enqueue_scripts', 'samtyk_cmp_enqueue_admin_assets');