| 1 |
<?php |
| 2 |
/** |
| 3 |
* Onboarding service. |
| 4 |
* |
| 5 |
* Persists onboarding completion state, captured user-details, and the |
| 6 |
* one-shot activation redirect that drops a freshly-activated admin onto |
| 7 |
* the guided setup screen. |
| 8 |
* |
| 9 |
* Backed by the consolidated `suredonation_options` row via Helper so we |
| 10 |
* don't create extra option rows for a single boolean. |
| 11 |
* |
| 12 |
* @package SureDonation |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace SureDonation\Inc; |
| 16 |
|
| 17 |
use SureDonation\Inc\Traits\Get_Instance; |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Onboarding class. |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
*/ |
| 28 |
class Onboarding { |
| 29 |
use Get_Instance; |
| 30 |
|
| 31 |
/** |
| 32 |
* Key inside suredonation_options for the completion flag. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
private const STATUS_KEY = 'onboarding_completed'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Key inside suredonation_options for cached user-details capture. |
| 41 |
* |
| 42 |
* @since 1.0.0 |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
private const USER_DETAILS_KEY = 'onboarding_user_details'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Hidden admin page slug. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
public const PAGE_SLUG = 'suredonation-onboarding'; |
| 54 |
|
| 55 |
/** |
| 56 |
* Option key set during activation to trigger the one-shot redirect. |
| 57 |
* |
| 58 |
* Already declared by Plugin_Loader on activation; we just consume it |
| 59 |
* here. |
| 60 |
* |
| 61 |
* @since 1.0.0 |
| 62 |
* @var string |
| 63 |
*/ |
| 64 |
private const REDIRECT_FLAG = '__suredonation_do_redirect'; |
| 65 |
|
| 66 |
/** |
| 67 |
* Wire up activation redirect. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
*/ |
| 71 |
public function __construct() { |
| 72 |
add_action( 'admin_init', [ $this, 'maybe_redirect_to_onboarding' ] ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Read completion state. |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
* @since 1.0.0 |
| 80 |
*/ |
| 81 |
public function is_completed() { |
| 82 |
return 'yes' === Helper::get_suredonation_option( self::STATUS_KEY, 'no' ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Persist completion state. |
| 87 |
* |
| 88 |
* @param string $value 'yes' or 'no'. |
| 89 |
* @return void |
| 90 |
* @since 1.0.0 |
| 91 |
*/ |
| 92 |
public function set_completed( $value = 'yes' ) { |
| 93 |
Helper::update_suredonation_option( self::STATUS_KEY, 'yes' === $value ? 'yes' : 'no' ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Read the cached lead-capture payload. |
| 98 |
* |
| 99 |
* @return array<string,mixed> |
| 100 |
* @since 1.0.0 |
| 101 |
*/ |
| 102 |
public function get_user_details() { |
| 103 |
$details = Helper::get_suredonation_option( self::USER_DETAILS_KEY, [] ); |
| 104 |
return is_array( $details ) ? $details : []; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Persist the lead-capture payload. |
| 109 |
* |
| 110 |
* @param array<string,mixed> $details Raw payload (already sanitised). |
| 111 |
* @return void |
| 112 |
* @since 1.0.0 |
| 113 |
*/ |
| 114 |
public function set_user_details( array $details ) { |
| 115 |
Helper::update_suredonation_option( self::USER_DETAILS_KEY, $details ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* On the first admin pageload after activation, send the admin to the |
| 120 |
* onboarding screen — but only if onboarding isn't already done. |
| 121 |
* |
| 122 |
* @return void |
| 123 |
* @since 1.0.0 |
| 124 |
*/ |
| 125 |
public function maybe_redirect_to_onboarding() { |
| 126 |
if ( wp_doing_ajax() || wp_doing_cron() ) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
// admin_init fires for REST and network-admin contexts too — neither |
| 131 |
// should ever 302 to the onboarding screen. |
| 132 |
if ( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || is_network_admin() ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
// Only honour the redirect on the initial GET that lands the admin |
| 137 |
// on a wp-admin page — a POST hitting admin_init would otherwise |
| 138 |
// 302 mid-form-submission. |
| 139 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' !== strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading a flag, not processing form input. |
| 144 |
if ( isset( $_GET['activate-multi'] ) ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! get_option( self::REDIRECT_FLAG ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// One-shot — drop the row entirely so the option table doesn't |
| 153 |
// accumulate dead flags. The activation hook will recreate it on |
| 154 |
// the next plugin activation. |
| 155 |
delete_option( self::REDIRECT_FLAG ); |
| 156 |
|
| 157 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if ( $this->is_completed() ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE_SLUG ) ); |
| 166 |
exit; |
| 167 |
} |
| 168 |
} |
| 169 |
|