guard( AIEO_Personas::ACTION_SAVE ); AIEO_Wizard::save_personas( get_current_user_id(), wp_unslash( $_POST ) ); AIEO_Wizard::mark_passed( get_current_user_id(), 'persona' ); self::defer_profile_sync( get_current_user_id() ); $back = $this->back_url(); if ( $back === $this->get_url() ) { $back = add_query_arg( 'persona', 'saved', AIEO_Wizard::step_url( 'persona' ) ); } wp_safe_redirect( $back ); exit; } public function handle_decide_set() { $this->guard( self::ACTION_SET_DECIDE ); $stage = isset( $_POST['stage'] ) ? sanitize_key( wp_unslash( $_POST['stage'] ) ) : ''; $set = isset( $_POST['set'] ) ? sanitize_key( wp_unslash( $_POST['set'] ) ) : ''; $kind = isset( $_POST['kind'] ) ? sanitize_key( wp_unslash( $_POST['kind'] ) ) : ''; AIEO_Setup_Checklist::decide_set( $stage, $set, $kind, get_current_user_id() ); wp_safe_redirect( isset( $_POST['back'] ) ? $this->back_url() : $this->get_url() . '#aieo-gsu-stage-' . $stage ); exit; } public function handle_preset_import() { $this->guard( self::ACTION_PRESET ); $json = isset( $_POST['preset'] ) ? (string) wp_unslash( $_POST['preset'] ) : ''; $r = class_exists( 'AIEO_Shop_Preset' ) ? AIEO_Shop_Preset::import( $json, get_current_user_id() ) : array( 'ok' => false, 'code' => 'empty' ); $args = $r['ok'] ? array( 'preset' => 'applied', 'ps' => (int) ! empty( $r['shape'] ), 'pm' => (int) $r['modules'], 'po' => (int) $r['overlaps'] ) : array( 'preset' => 'invalid', 'pe' => (string) $r['code'] ); wp_safe_redirect( add_query_arg( $args, $this->get_url() ) . '#aieo-card-getting-set-up-preset' ); exit; } public function handle_strip_dismiss() { if ( ! current_user_can( self::cap() ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'ai-eshop-optimizer' ) ); } check_admin_referer( self::ACTION_STRIP_HIDE, 'aieo_nonce' ); AIEO_Setup_Checklist::dismiss_strip( get_current_user_id() ); wp_safe_redirect( wp_get_referer() ?: admin_url( 'admin.php?page=' . AIEO_DMM::MENU_SLUG ) ); exit; } private function back_url() { $back = isset( $_POST['back'] ) ? esc_url_raw( wp_unslash( $_POST['back'] ) ) : ''; if ( '' !== $back && 0 === strpos( $back, admin_url() ) ) { return $back; } return $this->get_url(); } public function handle_reduced_toggle() { $this->guard( 'aieo_reduced_toggle' ); AIEO_Shop_Shape::set_reduced( get_current_user_id(), ! empty( $_POST['on'] ) ); wp_safe_redirect( $this->back_url() ); exit; } public function handle_reduced_keep() { $this->guard( 'aieo_reduced_keep' ); $id = isset( $_POST['screen'] ) ? sanitize_key( wp_unslash( $_POST['screen'] ) ) : ''; if ( '' !== $id ) { AIEO_Shop_Shape::keep( get_current_user_id(), $id, empty( $_POST['unkeep'] ) ); } wp_safe_redirect( $this->back_url() ); exit; } public function handle_dark_forget() { $this->guard( self::ACTION_DARK_FORGET ); $page = isset( $_POST['screen'] ) ? sanitize_key( wp_unslash( $_POST['screen'] ) ) : ''; if ( class_exists( 'AIEO_Dark_Screens' ) && '' !== $page ) { AIEO_Dark_Screens::clear( $page ); } wp_safe_redirect( $this->get_url() . '#aieo-card-getting-set-up-dark' ); exit; } private static function cap() { return defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options'; } private function guard( $action ) { if ( ! current_user_can( self::cap() ) ) { wp_die( esc_html__( 'Insufficient permissions.', 'ai-eshop-optimizer' ) ); } check_admin_referer( $action ); } public function handle_shape_save() { $this->guard( self::ACTION_SHAPE_SAVE ); $r = AIEO_Wizard::save_shape( get_current_user_id(), wp_unslash( $_POST ) ); if ( isset( $_POST['back'] ) ) { if ( $r['ok'] ) { AIEO_Wizard::mark_passed( get_current_user_id(), 'shape' ); AIEO_Wizard::maybe_sync_profile( get_current_user_id() ); } $to = $r['ok'] ? $this->back_url() : add_query_arg( 'persona', 'incomplete', AIEO_Wizard::step_url( 'shape' ) ); } else { $to = add_query_arg( array( 'shape' => $r['ok'] ? 'saved' : 'incomplete', 'seeded' => (int) $r['seeded'] ), $this->get_url() ); } wp_safe_redirect( $to ); exit; } public function handle_shape_clear() { $this->guard( self::ACTION_SHAPE_CLEAR ); AIEO_Shop_Shape::clear(); wp_safe_redirect( add_query_arg( 'shape', 'edit', $this->get_url() ) ); exit; } public function handle_decide() { $this->guard( self::ACTION_STEP_DECIDE ); $step = isset( $_POST['step'] ) ? sanitize_key( wp_unslash( $_POST['step'] ) ) : ''; $kind = isset( $_POST['kind'] ) ? sanitize_key( wp_unslash( $_POST['kind'] ) ) : ''; AIEO_Setup_Checklist::decide( $step, $kind, get_current_user_id() ); wp_safe_redirect( isset( $_POST['back'] ) ? $this->back_url() : $this->get_url() . '#aieo-card-getting-set-up-checklist' ); exit; } public function render_subtab() { if ( ! class_exists( 'AIEO_Shop_Shape' ) || ! class_exists( 'AIEO_Setup_Checklist' ) ) { list( $aieo_gsu_open, $aieo_gsu_close ) = AIEO_DMM_UI::card( array( 'icon' => 'list-checks', 'title' => $this->get_label() ) ); echo $aieo_gsu_open; echo \Rooster\Admin\Ui::notice( esc_html__( 'This page is not available in this build.', 'ai-eshop-optimizer' ), 'destructive', array( 'inline' => true ) ); echo $aieo_gsu_close; return; } $flag = isset( $_GET['shape'] ) ? sanitize_key( wp_unslash( $_GET['shape'] ) ) : ''; $seeded = isset( $_GET['seeded'] ) ? (int) $_GET['seeded'] : 0; $preset = array( 'flag' => isset( $_GET['preset'] ) ? sanitize_key( wp_unslash( $_GET['preset'] ) ) : '', 'shape' => ! empty( $_GET['ps'] ), 'modules' => isset( $_GET['pm'] ) ? (int) $_GET['pm'] : 0, 'overlaps' => isset( $_GET['po'] ) ? (int) $_GET['po'] : 0, 'error' => ( isset( $_GET['pe'] ) && class_exists( 'AIEO_Shop_Preset' ) ) ? AIEO_Shop_Preset::message( sanitize_key( wp_unslash( $_GET['pe'] ) ) ) : '', ); $facts = AIEO_Shop_Shape::facts(); $shape = AIEO_Shop_Shape::get(); $screen = AIEO_Shop_Shape::screen(); $editing = ( 'edit' === $flag ) || ( 'incomplete' === $flag ); $stages = AIEO_Setup_Checklist::evaluate( 'blank' === $screen ? array( 'a' ) : null ); $first_win = null; if ( $shape ) { $fw = AIEO_Shop_Shape::first_win( $shape['first'] ); if ( AIEO_Shop_Shape::destination_exists( $fw['tab'] ) ) { $first_win = array( 'tab' => $fw['tab'], 'label' => $this->dest_label( $fw['tab'] ), 'url' => AIEO_DMM::instance()->get_subtab_url( $fw['tab'] ), 'why' => $fw['why'], 'needs_products' => $fw['needs_products'], ); } } $overlaps_url = AIEO_Shop_Shape::destination_exists( 'overlaps' ) ? AIEO_DMM::instance()->get_subtab_url( 'overlaps' ) : AIEO_DMM::instance()->get_subtab_url( 'seo_general' ) . '#aieo-card-general-provider-status'; $running = array(); foreach ( AIEO_Setup_Checklist::running_now() as $r ) { if ( empty( $r['tab'] ) || ! AIEO_Shop_Shape::destination_exists( $r['tab'] ) ) { continue; } $r['label'] = $this->dest_label( $r['tab'] ); $r['url'] = AIEO_DMM::instance()->get_subtab_url( $r['tab'] ); $running[] = $r; } $dark = array(); if ( class_exists( 'AIEO_Dark_Screens' ) ) { foreach ( AIEO_Dark_Screens::rows() as $pid => $r ) { if ( ! AIEO_Shop_Shape::destination_exists( $pid ) ) { continue; } $dark[] = array( 'id' => (string) $pid, 'label' => $this->dest_label( $pid ), 'url' => AIEO_DMM::instance()->get_subtab_url( $pid ), 'n' => (int) ( $r['n'] ?? 0 ), 'last' => (int) ( $r['last'] ?? 0 ), ); } usort( $dark, static function ( $a, $b ) { return $b['last'] <=> $a['last']; } ); } $edit_url = class_exists( 'AIEO_Wizard' ) ? AIEO_Wizard::step_url( 'shape' ) : add_query_arg( 'shape', 'edit', $this->get_url() ); $this->render_template( 'getting-set-up.php', array( 'dark' => $dark, 'forget_url' => $this->get_form_action_url( self::ACTION_DARK_FORGET ), 'reduced_on' => AIEO_Shop_Shape::reduced_on( get_current_user_id() ), 'reduced_url' => $this->get_form_action_url( 'aieo_reduced_toggle' ), 'running' => $running, 'screen' => $screen, 'facts' => $facts, 'shape' => $shape, 'summary' => AIEO_Shop_Shape::summary(), 'source' => AIEO_Shop_Shape::install_source(), 'editing' => $editing, 'flag' => $flag, 'seeded' => $seeded, 'questions' => AIEO_Shop_Shape::questions(), 'proposed' => AIEO_Shop_Shape::proposed(), 'stages' => $stages, 'first_win' => $first_win, 'cogs' => 'blank' === $screen ? null : AIEO_Setup_Checklist::cogs_coverage(), 'overlaps_url' => $overlaps_url, 'save_url' => $this->get_form_action_url( self::ACTION_SHAPE_SAVE ), 'clear_url' => $this->get_form_action_url( self::ACTION_SHAPE_CLEAR ), 'decide_url' => $this->get_form_action_url( self::ACTION_STEP_DECIDE ), 'decide_set_url' => $this->get_form_action_url( self::ACTION_SET_DECIDE ), 'edit_url' => $edit_url, 'page_url' => $this->get_url(), 'identity' => AIEO_Setup_Checklist::identity_preview(), 'preset' => $preset, 'preset_json' => class_exists( 'AIEO_Shop_Preset' ) ? AIEO_Shop_Preset::export_json() : '', 'preset_url' => $this->get_form_action_url( self::ACTION_PRESET ), 'docs_url' => self::CLOUDWAYS_DOCS_URL, ) ); } private function dest_label( $id ) { static $labels = null; if ( null === $labels ) { $labels = array(); foreach ( (array) AIEO_DMM::instance()->get_subtab_tree( '' ) as $entry ) { if ( isset( $entry['kind'] ) && 'group' === $entry['kind'] ) { foreach ( (array) $entry['children'] as $child ) { $labels[ (string) $child['id'] ] = wp_specialchars_decode( (string) $child['label'], ENT_QUOTES ); } } elseif ( isset( $entry['id'] ) ) { $labels[ (string) $entry['id'] ] = wp_specialchars_decode( (string) $entry['label'], ENT_QUOTES ); } } } return isset( $labels[ $id ] ) ? $labels[ $id ] : (string) $id; } }