$classes . (($_GET['page'] ?? '') === 'apeari-setup' ? ' apeari-setup-screen' : ''), ); } public static function activate(bool $network = false): void { if (!$network && !isset($_GET['activate-multi']) && !get_option('apeari_setup_complete', false)) { update_option('apeari_setup_pending', true, false); } } public static function url(): string { return admin_url('admin.php?page=apeari-setup'); } public static function should_redirect(): bool { global $pagenow; return current_user_can('manage_options') && !is_network_admin() && !wp_doing_ajax() && (in_array($pagenow, ['plugins.php', 'index.php'], true) || (in_array($pagenow, ['themes.php', 'admin.php'], true) && ($_GET['page'] ?? '') === 'apeari')) && !isset($_GET['activate-multi']) && (bool) get_option('apeari_setup_pending', false); } public static function redirect(): void { if (self::should_redirect()) { wp_safe_redirect(self::url()); exit(); } } public static function assets(string $hook): void { if ($hook !== 'admin_page_apeari-setup') { return; } wp_enqueue_style('apeari-setup', APEARI_URL . 'assets/css/admin/setup.css', [], APEARI_VERSION); wp_enqueue_script( 'apeari-settings', APEARI_URL . 'assets/js/admin/settings.js', [], APEARI_VERSION, true, ); wp_enqueue_script( 'apeari-setup-select', APEARI_URL . 'assets/js/admin/setup-select.js', ['apeari-settings'], APEARI_VERSION, true, ); wp_enqueue_script( 'apeari-setup', APEARI_URL . 'assets/js/admin/setup.js', ['apeari-settings', 'apeari-setup-select'], APEARI_VERSION, true, ); self::localize(); } private static function localize(): void { wp_localize_script('apeari-setup', 'apeariSetup', [ 'ajaxUrl' => admin_url('admin-ajax.php'), 'messages' => [ 'scope' => __('Wybierz co najmniej jeden zakres pracy AI.', 'apeari'), 'token' => __('Wpisz prawidłowy token instalacji Lite.', 'apeari'), 'key' => __('Uzupełnij prawidłowy klucz API.', 'apeari'), 'model' => __('Wybierz model lub wpisz jego identyfikator.', 'apeari'), 'consent' => __('Potwierdź wysyłanie kontekstu do wybranego dostawcy.', 'apeari'), 'service' => __('Potwierdź regulamin i korzystanie z usługi Apeari.', 'apeari'), 'proofBusy' => __('Generuję potwierdzenie…', 'apeari'), 'proofReady' => __('Gotowe. Skopiuj potwierdzenie do panelu Apeari.', 'apeari'), 'proofError' => __('Nie udało się wygenerować potwierdzenia. Spróbuj ponownie.', 'apeari'), 'saving' => __('Zapisuję konfigurację…', 'apeari'), ], ]); } public static function ownership_ajax(): void { if (!current_user_can('manage_options')) { wp_send_json_error([], 403); } check_ajax_referer('apeari_setup_ownership', '_wpnonce'); $result = APEARI_Ownership::generate(); if (is_wp_error($result)) { wp_send_json_error(['message' => $result->get_error_message()], 400); } wp_send_json_success(['token' => APEARI_Ownership::token()]); } public static function complete(array $input) { if (!current_user_can('manage_options')) { return new WP_Error('apeari_setup_access', __('Brak dostępu.', 'apeari'), ['status' => 403]); } if (!empty($input['scope_theme'])) { return new WP_Error('apeari_setup_scope', __('Lite nie udostępnia plików motywu.', 'apeari')); } $result = APEARI_Settings::update($input); if (is_wp_error($result)) { return $result; } update_option('apeari_setup_complete', true, false); delete_option('apeari_setup_pending'); return true; } public static function save(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('Brak dostępu.', 'apeari'), '', ['response' => 403]); } check_admin_referer('apeari_setup'); $input = wp_unslash($_POST); $result = self::complete($input); if (is_wp_error($result)) { set_transient( 'apeari_setup_error_' . get_current_user_id(), [ 'message' => $result->get_error_message(), 'scopes' => [ 'page' => ($input['scope_page'] ?? '') === '1', 'theme' => ($input['scope_theme'] ?? '') === '1', ], 'step' => str_ends_with($result->get_error_code(), '_scope') ? 1 : 2, 'mode' => is_string($input['mode'] ?? null) ? $input['mode'] : 'apeari', 'models' => self::models($input), ], 5 * MINUTE_IN_SECONDS, ); wp_safe_redirect(self::url()); } else { wp_safe_redirect(admin_url('themes.php?page=apeari')); } exit(); } public static function skip(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('Brak dostępu.', 'apeari'), '', ['response' => 403]); } check_admin_referer('apeari_setup_skip'); delete_option('apeari_setup_pending'); wp_safe_redirect(admin_url()); exit(); } public static function ownership(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('Brak dostępu.', 'apeari'), '', ['response' => 403]); } check_admin_referer('apeari_setup_ownership'); $result = APEARI_Ownership::generate(); if (is_wp_error($result)) { set_transient( 'apeari_setup_error_' . get_current_user_id(), ['message' => $result->get_error_message(), 'mode' => 'apeari'], 5 * MINUTE_IN_SECONDS, ); } wp_safe_redirect(add_query_arg('connection', '1', self::url())); exit(); } public static function render(): void { if (!current_user_can('manage_options')) { return; } $failure = get_transient('apeari_setup_error_' . get_current_user_id()); $setup_error = is_array($failure) ? (string) ($failure['message'] ?? '') : ''; delete_transient('apeari_setup_error_' . get_current_user_id()); $setup_mode = get_option('apeari_mode', 'apeari'); if ( is_array($failure) && in_array($failure['mode'] ?? '', ['apeari', 'openai', 'anthropic', 'demo'], true) ) { $setup_mode = $failure['mode']; } if (isset($_GET['connection'])) { $setup_mode = 'apeari'; } $setup_scopes = ['page' => true, 'theme' => false]; $setup_models = is_array($failure['models'] ?? null) ? $failure['models'] : []; $setup_start = isset($_GET['connection']) ? 2 : ($setup_error ? (int) ($failure['step'] ?? 2) : 0); require APEARI_PATH . 'views/admin/setup.php'; } private static function models(array $input): array { $input = APEARI_Settings_UI::normalize($input); $models = []; foreach (['openai', 'anthropic'] as $provider) { foreach (['model', 'router_model'] as $field) { $key = $provider . '_' . $field; $value = $input[$key] ?? null; if ( is_string($value) && ($value === '' || preg_match('/^[a-zA-Z0-9][a-zA-Z0-9._:-]{0,127}$/D', $value)) ) { $models[$key] = $value; } } } return $models; } }