should_redirect_after_activation(); // Consumed as soon as it's read, whether or not this particular // request ends up redirecting — a skipped (bulk/network) // activation must not leave the flag armed for some later, // unrelated single page load to pick up and redirect on. delete_transient( self::REDIRECT_TRANSIENT ); if ( ! $should_redirect ) { return; } wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ); exit; } /** * The redirect decision itself, split out from * maybe_redirect_after_activation() above (which calls exit() and * so can't be exercised inside a PHPUnit process) — same * process-vs-handle split as AlpineConsent_Connection's own * process_callback()/handle_callback(), see test-class-connection.php's * own docblock for that precedent. Never touches the transient * itself, so a test can set it up and check this in isolation. */ private function should_redirect_after_activation() { if ( ! get_transient( self::REDIRECT_TRANSIENT ) ) { return false; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- activate-multi is WordPress core's own bulk-activation query arg, read-only, never trusted for anything beyond this one skip check. if ( wp_doing_ajax() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { return false; } return true; } /** * Hidden from every admin menu (null parent) — reachable only by * its own URL, same convention WooCommerce/Jetpack use for their * own onboarding screens so it never clutters the nav for an admin * who already finished or skipped it. See this class's own top * docblock for why $page_hook is still captured and used below. */ public function add_page() { $this->page_hook = add_submenu_page( null, __( 'AlpineConsent Setup', 'alpineconsent' ), __( 'AlpineConsent Setup', 'alpineconsent' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render' ) ); if ( $this->page_hook ) { add_action( 'load-' . $this->page_hook, array( $this, 'set_page_title' ) ); } } /** See this class's own top docblock — fixes get_admin_page_title()'s own null-parent gap directly. */ public function set_page_title() { global $title; $title = __( 'AlpineConsent Setup', 'alpineconsent' ); } public function add_body_class( $classes ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- selects a CSS class only; never written anywhere or used for any state-changing decision. if ( isset( $_GET['page'] ) && self::PAGE_SLUG === $_GET['page'] ) { $classes .= ' alpineconsent-setup-wizard'; } return $classes; } /** * Scoped to this one page's own hook, same pattern as * class-admin.php::enqueue_assets() — this CSS (which hides the * normal wp-admin chrome for a true full-screen look, the exact * comparison point the ticket raised against Complianz) has no * reason to load anywhere else. * * Also pulls in the regular settings screen's own admin.css: the * wizard's own markup below is wrapped in .alpineconsent-admin * (same as class-admin.php's render_page()), so it gets that * stylesheet's color tokens, buttons, and form-table styling for * free instead of a second, drifting copy of the same rules. */ public function enqueue_assets( $hook ) { if ( $hook !== $this->page_hook ) { return; } wp_enqueue_style( 'alpineconsent-admin', ALPINECONSENT_PLUGIN_URL . 'assets/css/admin.css', array(), ALPINECONSENT_VERSION ); wp_enqueue_style( 'alpineconsent-setup-wizard', ALPINECONSENT_PLUGIN_URL . 'assets/css/setup-wizard.css', array( 'alpineconsent-admin' ), ALPINECONSENT_VERSION ); } // ── Step handlers ──────────────────────────────────────────────── /** * Every step handler below merges into the FULL current settings * array (never just the fields this one step's form posted) before * calling update_option() — AlpineConsent_General_Settings::sanitize() * reads a checkbox field straight off $input with no existing-value * fallback (a real HTML checkbox send nothing at all when unchecked, * which is indistinguishable from "this step's form never had that * field"), so posting a partial array through it would silently * flip every OTHER boolean toggle to off. Merging first is exactly * the same non-destructive pattern the plugin's own single mega-form * submission already relies on — it just happens across several * requests here instead of one. */ public function handle_banner_step() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Not allowed.', 'alpineconsent' ) ); } check_admin_referer( self::NONCE_ACTION ); $this->process_banner_step( $_POST ); $this->redirect_to_step( 'company' ); } public function handle_company_step() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Not allowed.', 'alpineconsent' ) ); } check_admin_referer( self::NONCE_ACTION ); $this->process_company_step( $_POST ); $this->redirect_to_step( 'legal' ); } /** * The actual save, split out from handle_banner_step() above (same * reasoning as should_redirect_after_activation()'s own docblock — * the public handler's capability check + exit-terminated redirect * can't be exercised inside a PHPUnit process, so the real logic * lives here instead, called directly by this plugin's own tests). * * @param array $post $_POST, or an equivalent array in tests. */ private function process_banner_step( $post ) { $settings = AlpineConsent_General_Settings::get(); $settings['show_banner'] = ! empty( $post['show_banner'] ); update_option( AlpineConsent_General_Settings::OPTION_NAME, wp_unslash( $settings ) ); } /** @param array $post $_POST, or an equivalent array in tests. */ private function process_company_step( $post ) { $settings = AlpineConsent_General_Settings::get(); foreach ( AlpineConsent_Setup_Score::COMPANY_FIELDS as $field ) { if ( isset( $post[ $field ] ) ) { $settings[ $field ] = (string) $post[ $field ]; } } update_option( AlpineConsent_General_Settings::OPTION_NAME, wp_unslash( $settings ) ); } /** * "Generate + publish" — both managed pages, same * AlpineConsent_Managed_Page::create_managed_page() worker * handle_create() itself calls, so there is exactly one place that * actually builds the wp_insert_post() args (AlpineConsent#521). A * page already managed (e.g. the admin went back and re-submitted * this step) is left alone — create_managed_page() already no-ops * on that, same as the regular admin panel's own button. */ public function handle_legal_step() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Not allowed.', 'alpineconsent' ) ); } check_admin_referer( self::NONCE_ACTION ); AlpineConsent_Policy_Page::create_managed_page( 'publish' ); AlpineConsent_Legal_Notice_Page::create_managed_page( 'publish' ); $this->redirect_to_step( $this->pending_suggestions() ? 'categorize' : 'finish' ); } /** * Handles both buttons render_categorize_step() can submit — same * single-form/two-submit-values shape AlpineConsent_Managed_Page's * own "Create & publish" / "Create as a draft" buttons already use * (see class-managed-page.php::render_manage_controls()), not two * separate actions, since both do the exact same "move on to * finish" afterward and differ only in whether they touch any data * first. */ public function handle_categorize_step() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Not allowed.', 'alpineconsent' ) ); } check_admin_referer( self::NONCE_ACTION ); $this->process_categorize_step( $_POST ); $this->redirect_to_step( 'finish' ); } /** * "Accept all suggestions" writes AlpineConsent_Suggestions' own * suggested_category straight into category for every entry that * doesn't already have one — the exact thing turning a suggestion * into a real, admin-confirmed category normally takes a trip * through the regular Scripts & Categories table for (one row, or * a bulk-apply, at a time). This is the ONE step in this entire * wizard that has a real, live effect beyond this plugin's own * settings: AlpineConsent_Blocker::blockable_raw_scripts() (and the * enqueued-script equivalent in filter_tag()) only ever reads * 'category', deliberately never 'suggested_category' — "an * unreviewed suggestion is never enough to actually hold a script * back" (see that method's own docblock) — so accepting here is * what actually starts enforcing consent for whatever it already * found, not just a cosmetic setup-score bump. * * "I'll set this up myself" (accept_suggestions not sent, or "0") * intentionally touches nothing at all: the admin was already told, * right on this step, that doing it later from Settings earns the * same setup-score credit once they do — this wizard doesn't need * its own separate bookkeeping for that promise. * * Writes through AlpineConsent_Admin::write_discovered_handles()/ * write_discovered_cookies(), never a plain update_option() — found * live, the first time this ran from a real admin-post.php request * rather than a direct method call: AlpineConsent_Admin::sanitize()/ * sanitize_cookies() are registered sanitize_callbacks that run on * EVERY update_option() for these two options (admin_init has * already fired by the time any admin-post.php handler runs — see * sanitize()'s own docblock for the exact precedent, a previously * reported "Scan site now" regression this same shape caused), and * they expect $input shaped like a Settings API submission * ({handle: category}), not the full {handle: {src, category, * suggested_category, ...}} shape this method (same as * maybe_discover()/discover_from_html()) actually writes. Every * value here being a sub-array rather than a scalar made * sanitize()'s own is_scalar() guard treat each one as an invalid * submission and reset 'category' to '' right back — silently * undoing this step's entire save on write, confirmed with a real * browser click, not just read from the code. * * @param array $post $_POST, or an equivalent array in tests. */ private function process_categorize_step( $post ) { if ( empty( $post['accept_suggestions'] ) ) { return; } $scripts = get_option( AlpineConsent_Blocker::OPTION_NAME, array() ); if ( is_array( $scripts ) && ! empty( $scripts ) ) { AlpineConsent_Admin::write_discovered_handles( $this->accept_pending_categories( $scripts ) ); } $cookies = get_option( AlpineConsent_Admin::COOKIE_OPTION_NAME, array() ); if ( is_array( $cookies ) && ! empty( $cookies ) ) { AlpineConsent_Admin::write_discovered_cookies( $this->accept_pending_categories( $cookies ) ); } } /** @param array $entries @return array */ private function accept_pending_categories( array $entries ) { foreach ( $entries as $key => $entry ) { if ( is_array( $entry ) && empty( $entry['category'] ) && ! empty( $entry['suggested_category'] ) ) { $entries[ $key ]['category'] = $entry['suggested_category']; } } return $entries; } /** * Every discovered script/cookie that AlpineConsent_Suggestions * already has a suggestion for but no admin has confirmed yet — * the real, observable signal that decides whether * render_categorize_step() has anything to show at all. Reused by * handle_legal_step() (to decide whether 'categorize' belongs in * this particular run of the wizard at all) and by render() (to * size the "Step X of Y" count correctly either way) so there is * exactly one place that defines "pending" — the same resolution * AlpineConsent_Setup_Score::effective_category() and * class-admin.php's own copy already use, just filtered down to * the ones still missing a real 'category'. * * @return array */ private function pending_suggestions() { $pending = array(); foreach ( array( AlpineConsent_Blocker::OPTION_NAME, AlpineConsent_Admin::COOKIE_OPTION_NAME ) as $option_name ) { $entries = get_option( $option_name, array() ); if ( ! is_array( $entries ) ) { continue; } foreach ( $entries as $key => $entry ) { if ( ! is_array( $entry ) || ! empty( $entry['category'] ) || empty( $entry['suggested_category'] ) ) { continue; } if ( ! isset( AlpineConsent_Admin::CATEGORY_LABELS[ $entry['suggested_category'] ] ) ) { continue; } $pending[] = array( 'label' => (string) $key, 'category_label' => AlpineConsent_Admin::CATEGORY_LABELS[ $entry['suggested_category'] ], ); } } return $pending; } private function redirect_to_step( $step ) { wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE_SLUG . '&step=' . $step ) ); exit; } // ── Rendering ──────────────────────────────────────────────────── private function dashboard_url() { return add_query_arg( array( 'page' => 'alpineconsent' ), admin_url( 'admin.php' ) ); } /** * add_submenu_page()'s own callback — WordPress's normal admin page * template (admin-header.php/admin-footer.php) wraps this, same as * class-admin.php::render_page(). The chrome that template prints * (menu, admin bar, footer) is hidden via setup-wizard.css, scoped * to the body class add_body_class() adds above — the true * full-screen look the ticket's own Complianz comparison asked for. */ public function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $valid_steps = array( 'banner', 'company', 'legal', 'categorize', 'finish' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- selects which already-rendered step to show, sanitized + whitelisted against $valid_steps, never written anywhere — same reasoning as class-admin.php's own $requested_tab. $requested_step = isset( $_GET['step'] ) ? sanitize_key( wp_unslash( $_GET['step'] ) ) : 'banner'; $step = in_array( $requested_step, $valid_steps, true ) ? $requested_step : 'banner'; // 'categorize' only counts as a real step on a run of the // wizard that actually has something pending for it — see // pending_suggestions()'s own docblock. Computed once here so // the "Step X of Y" badge stays consistent across every step // of the SAME run, not just whichever step happens to render // it — a brand-new install (the common case) always shows "of // 3", never a "3 of 4" that then jumps straight to "finish". $real_steps = $this->pending_suggestions() ? array( 'banner', 'company', 'legal', 'categorize' ) : array( 'banner', 'company', 'legal' ); $step_index = array_search( $step, $real_steps, true ); ?>
render_company_step(); break; case 'legal': $this->render_legal_step(); break; case 'categorize': $this->render_categorize_step(); break; case 'finish': $this->render_finish_step(); break; default: $this->render_banner_step(); break; } ?>

__( 'Company name', 'alpineconsent' ), 'company_address' => __( 'Company address', 'alpineconsent' ), 'company_email' => __( 'Company email', 'alpineconsent' ), 'company_phone' => __( 'Company phone', 'alpineconsent' ), 'company_managing_director' => __( 'Managing director / representative', 'alpineconsent' ), 'company_register' => __( 'Commercial register', 'alpineconsent' ), 'company_vat_id' => __( 'VAT ID', 'alpineconsent' ), ); ?>

$label ) : ?>

pending_suggestions(); if ( empty( $pending ) ) { ?>