| 1 |
<?php |
| 2 |
/** |
| 3 |
* Setup Wizard Service |
| 4 |
* Handles setup wizard initialization and registration |
| 5 |
* |
| 6 |
* @package Yatra\Services |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Yatra\Services; |
| 11 |
|
| 12 |
use Yatra\Controllers\SetupWizardController; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
|
| 16 |
class SetupWizardService |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Initialize the setup wizard service |
| 20 |
*/ |
| 21 |
public static function init(): void |
| 22 |
{ |
| 23 |
// Only initialize in admin |
| 24 |
if (!is_admin()) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// Initialize the setup wizard controller on plugins_loaded |
| 29 |
add_action('plugins_loaded', [__CLASS__, 'initController']); |
| 30 |
|
| 31 |
// Register activation hook handler |
| 32 |
add_action('admin_init', [__CLASS__, 'handleWizardRedirect']); |
| 33 |
|
| 34 |
// Force redirect to setup wizard if not completed |
| 35 |
add_action('admin_init', [__CLASS__, 'forceWizardRedirect']); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Initialize the setup wizard controller |
| 40 |
*/ |
| 41 |
public static function initController(): void |
| 42 |
{ |
| 43 |
new SetupWizardController(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Handle wizard redirect after plugin activation |
| 48 |
*/ |
| 49 |
public static function handleWizardRedirect(): void |
| 50 |
{ |
| 51 |
SetupWizardController::setup_wizard_redirect(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Force redirect to setup wizard if not completed |
| 56 |
* This implements the legacy logic: if yatra_setup_wizard_ran != '1', redirect to setup wizard |
| 57 |
*/ |
| 58 |
public static function forceWizardRedirect(): void |
| 59 |
{ |
| 60 |
// Only apply to users who can manage options |
| 61 |
if (!current_user_can('manage_options')) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Check if setup wizard is enabled via filter (legacy compatibility) |
| 66 |
if (!apply_filters('yatra_enable_setup_wizard', true)) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// Check if wizard has been completed (yatra_setup_wizard_ran != '1') |
| 71 |
if (get_option('yatra_setup_wizard_ran') !== '1') { |
| 72 |
// Don't redirect if we're already on the setup wizard page |
| 73 |
if (isset($_GET['page']) && $_GET['page'] === 'yatra-setup') { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// Don't redirect on AJAX requests |
| 78 |
if (wp_doing_ajax()) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Force redirect to setup wizard |
| 83 |
wp_safe_redirect(admin_url('admin.php?page=yatra-setup')); |
| 84 |
exit; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Trigger wizard redirect on plugin activation |
| 90 |
* Called from activation hook |
| 91 |
*/ |
| 92 |
public static function triggerWizardOnActivation(): void |
| 93 |
{ |
| 94 |
set_transient('yatra_setup_wizard_redirect', 1, 30); |
| 95 |
} |
| 96 |
} |
| 97 |
|