| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php /** |
| 2 | 2 | * @version 1.0 |
| 3 | 3 | * @description Support functions for the Setup Wizard Page |
| 4 | 4 | * @category Setup Class |
| @@ -9,13 +9,29 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @modified 2024-09-06 |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 14 | - | |
| 15 | - | |
| 16 | -/** | |
| 17 | - * Do we need to start 'Setup Wizard' ? | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 14 | + | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Check whether this site was initialized by the current first-install flow. | |
| 18 | + * | |
| 19 | + * This persisted marker is intentionally separate from wizard completion and | |
| 20 | + * prompt dismissal. It prevents plugin updates from adding the native Setup menu | |
| 21 | + * while allowing a new administrator to postpone the popup and return through | |
| 22 | + * that menu later. | |
| 23 | + * | |
| 24 | + * @return bool True for sites marked during genuine first installation. | |
| 25 | + */ | |
| 26 | +function wpbc_setup_wizard_is_initial_install_site() { | |
| 27 | + | |
| 28 | + return 'On' === get_bk_option( 'booking_setup_wizard_initial_install' ); | |
| 29 | +} | |
| 30 | + | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * Do we need to start 'Setup Wizard' ? | |
| 18 | 34 | * |
| 19 | 35 | * @return bool |
| 20 | 36 | */ |
| 21 | 37 | function wpbc_setup_wizard_page__is_need_start(){ |
| @@ -46,16 +62,119 @@ | ||
| 46 | 62 | * Do we need to Continue 'Setup Wizard' or all steps already completed ? |
| 47 | 63 | * |
| 48 | 64 | * @return bool |
| 49 | 65 | */ |
| 50 | -function wpbc_setup_wizard_page__is_all_steps_completed() { | |
| 66 | +function wpbc_setup_wizard_page__is_all_steps_completed() { | |
| 51 | 67 | |
| 52 | 68 | $setup_steps = new WPBC_SETUP_WIZARD_STEPS(); |
| 53 | 69 | |
| 54 | 70 | $is_all_steps_completed = $setup_steps->db__is_all_steps_completed(); |
| 55 | 71 | |
| 56 | - return $is_all_steps_completed; | |
| 57 | -} | |
| 72 | + return $is_all_steps_completed; | |
| 73 | +} | |
| 74 | + | |
| 75 | + | |
| 76 | +/** | |
| 77 | + * Check whether the Setup Wizard has started and is not yet complete. | |
| 78 | + * | |
| 79 | + * Explicit Setup Wizard requests count as active before the first step has | |
| 80 | + * been saved. Persisted completed steps keep the wizard active while the user | |
| 81 | + * visits another Booking Calendar administration page. A completed or skipped | |
| 82 | + * wizard is not considered in progress. | |
| 83 | + * | |
| 84 | + * @return bool True while an unfinished Setup Wizard is in progress. | |
| 85 | + */ | |
| 86 | +function wpbc_setup_wizard_page__is_in_progress() { | |
| 87 | + | |
| 88 | + if ( wpbc_setup_wizard_page__is_all_steps_completed() ) { | |
| 89 | + return false; | |
| 90 | + } | |
| 91 | + | |
| 92 | + if ( function_exists( 'wpbc_is_setup_wizard_page' ) && wpbc_is_setup_wizard_page() ) { | |
| 93 | + return true; | |
| 94 | + } | |
| 95 | + | |
| 96 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI context. | |
| 97 | + $setup_context = isset( $_GET['wpbc_setup'] ) && is_scalar( $_GET['wpbc_setup'] ) | |
| 98 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI context. | |
| 99 | + ? sanitize_text_field( wp_unslash( $_GET['wpbc_setup'] ) ) | |
| 100 | + : ''; | |
| 101 | + | |
| 102 | + if ( '1' === $setup_context ) { | |
| 103 | + return true; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $steps_is_done = get_bk_option( 'booking_setup_wizard_page_steps_is_done' ); | |
| 107 | + | |
| 108 | + if ( ! is_array( $steps_is_done ) ) { | |
| 109 | + return false; | |
| 110 | + } | |
| 111 | + | |
| 112 | + foreach ( $steps_is_done as $is_step_done ) { | |
| 113 | + if ( ! empty( $is_step_done ) ) { | |
| 114 | + return true; | |
| 115 | + } | |
| 116 | + } | |
| 117 | + | |
| 118 | + return false; | |
| 119 | +} | |
| 120 | + | |
| 121 | + | |
| 122 | +/** | |
| 123 | + * Check whether the current request explicitly targets one Setup Wizard step. | |
| 124 | + * | |
| 125 | + * This read-only request check is intentionally independent from saved wizard | |
| 126 | + * completion state. It lets an authorized user follow an explicit external | |
| 127 | + * setup URL without causing ordinary administration-page visits to reopen the | |
| 128 | + * completed wizard. | |
| 129 | + * | |
| 130 | + * @param string $step_name Expected Setup Wizard step identifier. | |
| 131 | + * | |
| 132 | + * @return bool True when the current URL explicitly requests the step. | |
| 133 | + */ | |
| 134 | +function wpbc_setup_wizard_page__has_explicit_step_context( $step_name ) { | |
| 135 | + | |
| 136 | + $step_name = sanitize_key( (string) $step_name ); | |
| 137 | + | |
| 138 | + if ( '' === $step_name ) { | |
| 139 | + return false; | |
| 140 | + } | |
| 141 | + | |
| 142 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI context. | |
| 143 | + $setup_context = isset( $_GET['wpbc_setup'] ) && is_scalar( $_GET['wpbc_setup'] ) | |
| 144 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI context. | |
| 145 | + ? sanitize_text_field( wp_unslash( $_GET['wpbc_setup'] ) ) | |
| 146 | + : ''; | |
| 147 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI context. | |
| 148 | + $request_step = isset( $_GET['wpbc_setup_step'] ) && is_scalar( $_GET['wpbc_setup_step'] ) | |
| 149 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only UI context. | |
| 150 | + ? sanitize_key( wp_unslash( $_GET['wpbc_setup_step'] ) ) | |
| 151 | + : ''; | |
| 152 | + | |
| 153 | + return '1' === $setup_context && $step_name === $request_step; | |
| 154 | +} | |
| 155 | + | |
| 156 | + | |
| 157 | +/** | |
| 158 | + * Check whether the current request is one explicit external Setup Wizard step. | |
| 159 | + * | |
| 160 | + * The setup context flag prevents ordinary visits to the same administration | |
| 161 | + * page from receiving wizard-only defaults. | |
| 162 | + * | |
| 163 | + * @param string $step_name Expected Setup Wizard step identifier. | |
| 164 | + * | |
| 165 | + * @return bool True when the unfinished wizard explicitly requested the step. | |
| 166 | + */ | |
| 167 | +function wpbc_setup_wizard_page__is_active_step( $step_name ) { | |
| 168 | + | |
| 169 | + $step_name = sanitize_key( (string) $step_name ); | |
| 170 | + | |
| 171 | + if ( '' === $step_name || ! wpbc_setup_wizard_page__is_in_progress() ) { | |
| 172 | + return false; | |
| 173 | + } | |
| 174 | + | |
| 175 | + return wpbc_setup_wizard_page__has_explicit_step_context( $step_name ); | |
| 176 | +} | |
| 58 | 177 | |
| 59 | 178 | /** |
| 60 | 179 | * Is user can access Wizard Setup page ? |
| 61 | 180 | * @return bool |