| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of WizardPageController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace AliNext_Lite;; |
| 11 |
|
| 12 |
use Pages; |
| 13 |
|
| 14 |
class WizardPageController extends AbstractModernAdminPage |
| 15 |
{ |
| 16 |
public const WIZARD_ACTIVATION_KEY = 'a2wl_show_wizard_on_activation'; |
| 17 |
|
| 18 |
private WizardService $WizardService; |
| 19 |
private AdminMenuService $AdminMenuService; |
| 20 |
|
| 21 |
public function __construct( |
| 22 |
WizardService $WizardService, |
| 23 |
AdminMenuService $AdminMenuService |
| 24 |
) { |
| 25 |
parent::__construct( |
| 26 |
Pages::getLabel(Pages::WIZARD), |
| 27 |
Pages::getLabel(Pages::WIZARD), |
| 28 |
Capability::pluginAccess(), |
| 29 |
Pages::WIZARD |
| 30 |
); |
| 31 |
|
| 32 |
$this->WizardService = $WizardService; |
| 33 |
$this->AdminMenuService = $AdminMenuService; |
| 34 |
|
| 35 |
$this->add_style('a2wl-wizard-style', '/assets/css/pages/wizard.css?=2'); |
| 36 |
|
| 37 |
$AdminMenuService->registerPage($this, AdminMenuService::MENU_TYPE_HIDDEN_PAGE, 30); |
| 38 |
|
| 39 |
$this->showNotification(); |
| 40 |
$this->showWizardOnActivation(); |
| 41 |
} |
| 42 |
|
| 43 |
public function render($params = []): void |
| 44 |
{ |
| 45 |
if (!empty($_POST)) { |
| 46 |
check_admin_referer(self::PAGE_NONCE_ACTION, self::NONCE); |
| 47 |
} |
| 48 |
|
| 49 |
if (!PageGuardHelper::canAccessPage(Pages::WIZARD)) { |
| 50 |
wp_die($this->getErrorTextNoPermissions()); |
| 51 |
} |
| 52 |
|
| 53 |
$errors = []; |
| 54 |
if (isset($_POST['wizard_form'])) { |
| 55 |
$errors = $this->WizardService->handle($_POST); |
| 56 |
$redirect = add_query_arg('setup_wizard', 'success', admin_url('admin.php?page=a2wl_dashboard')); |
| 57 |
wp_redirect($redirect); |
| 58 |
exit; |
| 59 |
} |
| 60 |
|
| 61 |
$model = $this->WizardService->collectModel(); |
| 62 |
$model['errors'] = $errors; |
| 63 |
$model['close_link'] = admin_url('admin.php?page=a2wl_dashboard'); |
| 64 |
|
| 65 |
foreach ($model as $key => $value) { |
| 66 |
$this->model_put($key, $value); |
| 67 |
} |
| 68 |
|
| 69 |
$this->include_view("wizard.php"); |
| 70 |
} |
| 71 |
|
| 72 |
protected function showWizardOnActivation(): void |
| 73 |
{ |
| 74 |
add_action('admin_init', function () { |
| 75 |
if (get_option(self::WIZARD_ACTIVATION_KEY)) { |
| 76 |
delete_option(self::WIZARD_ACTIVATION_KEY); |
| 77 |
|
| 78 |
wp_safe_redirect(admin_url('admin.php?page=a2wl_wizard')); |
| 79 |
exit; |
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
protected function showNotification(): void |
| 85 |
{ |
| 86 |
if (isset($_GET['setup_wizard'])) { |
| 87 |
$wizardAlerts[] = PermanentAlert::build( |
| 88 |
esc_html__('Setup Wizard has applied preferred settings!', 'ali2woo'), |
| 89 |
PermanentAlert::TYPE_SUCCESS |
| 90 |
); |
| 91 |
|
| 92 |
add_filter('a2wl_get_permanent_alerts', function (array $alerts) use ($wizardAlerts) { |
| 93 |
return array_merge($alerts, $wizardAlerts); |
| 94 |
}); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|