SetupWizard.php
5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace AIOSEO\Plugin\Common\Standalone;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class that holds our setup wizard.
*
* @since 4.0.0
*/
class SetupWizard {
/**
* Class constructor.
*
* @since 4.0.0
*/
public function __construct() {
if ( ! is_admin() || wp_doing_cron() || wp_doing_ajax() ) {
return;
}
add_action( 'admin_menu', [ $this, 'addDashboardPage' ] );
add_action( 'admin_head', [ $this, 'hideDashboardPageFromMenu' ] );
add_action( 'admin_init', [ $this, 'maybeLoadOnboardingWizard' ] );
add_action( 'admin_init', [ $this, 'redirect' ], 9999 );
}
/**
* Onboarding Wizard redirect.
*
* This function checks if a new install or update has just occurred. If so,
* then we redirect the user to the appropriate page.
*
* @since 4.0.0
*
* @return void
*/
public function redirect() {
// Check if we should consider redirection.
if ( ! aioseo()->core->cache->get( 'activation_redirect' ) ) {
return;
}
// If we are redirecting, clear the transient so it only happens once.
aioseo()->core->cache->delete( 'activation_redirect' );
// Check option to disable welcome redirect.
if ( get_option( 'aioseo_activation_redirect', false ) ) {
return;
}
// Only do this for single site installs.
if ( isset( $_GET['activate-multi'] ) || is_network_admin() ) {
return;
}
wp_safe_redirect( admin_url( 'index.php?page=aioseo-setup-wizard' ) );
exit;
}
/**
* Adds a dashboard page for our setup wizard.
*
* @since 4.0.0
*
* @return void
*/
public function addDashboardPage() {
add_dashboard_page( '', '', aioseo()->admin->getPageRequiredCapability( 'aioseo-setup-wizard' ), 'aioseo-setup-wizard', '' );
}
/**
* Hide the dashboard page from the menu.
*
* @since 4.1.5
*
* @return void
*/
public function hideDashboardPageFromMenu() {
remove_submenu_page( 'index.php', 'aioseo-setup-wizard' );
}
/**
* Checks to see if we should load the setup wizard.
*
* @since 4.0.0
*
* @return void
*/
public function maybeLoadOnboardingWizard() {
// Don't load the interface if doing an ajax call.
if ( wp_doing_ajax() || wp_doing_cron() ) {
return;
}
// Check for wizard-specific parameter
// Allow plugins to disable the setup wizard
// Check if current user is allowed to save settings.
if (
! isset( $_GET['page'] ) ||
'aioseo-setup-wizard' !== wp_unslash( $_GET['page'] ) || // phpcs:ignore HM.Security.ValidatedSanitizedInput.InputNotSanitized
! current_user_can( aioseo()->admin->getPageRequiredCapability( 'aioseo-setup-wizard' ) )
) {
return;
}
set_current_screen();
// Remove an action in the Gutenberg plugin ( not core Gutenberg ) which throws an error.
remove_action( 'admin_print_styles', 'gutenberg_block_editor_admin_print_styles' );
// If we are redirecting, clear the transient so it only happens once.
aioseo()->core->cache->delete( 'activation_redirect' );
$this->loadOnboardingWizard();
}
/**
* Load the Onboarding Wizard template.
*
* @since 4.0.0
*
* @return void
*/
private function loadOnboardingWizard() {
$this->enqueueScripts();
$this->setupWizardHeader();
$this->setupWizardContent();
$this->setupWizardFooter();
exit;
}
/**
* Enqueue's scripts for the setup wizard.
*
* @since 4.0.0
*
* @return void
*/
public function enqueueScripts() {
// We don't want any plugin adding notices to our screens. Let's clear them out here.
remove_all_actions( 'admin_notices' );
remove_all_actions( 'all_admin_notices' );
aioseo()->core->assets->load( 'src/vue/standalone/setup-wizard/main.js', [], aioseo()->helpers->getVueData( 'setup-wizard' ) );
aioseo()->main->enqueueTranslations();
wp_enqueue_style( 'common' );
wp_enqueue_media();
}
/**
* Outputs the simplified header used for the Onboarding Wizard.
*
* @since 4.0.0
*
* @return void
*/
public function setupWizardHeader() {
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta name="viewport" content="width=device-width"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>
<?php
// Translators: 1 - The plugin name ("All in One SEO").
echo sprintf( esc_html__( '%1$s › Onboarding Wizard', 'all-in-one-seo-pack' ), esc_html( AIOSEO_PLUGIN_SHORT_NAME ) );
?>
</title>
<?php do_action( 'admin_print_scripts' ); ?>
<?php do_action( 'admin_print_styles' ); ?>
<?php do_action( 'admin_head' ); ?>
</head>
<body class="aioseo-setup-wizard">
<?php
}
/**
* Outputs the content of the current step.
*
* @since 4.0.0
*
* @return void
*/
public function setupWizardContent() {
echo '<div id="aioseo-app">';
aioseo()->templates->getTemplate( 'admin/settings-page.php' );
echo '</div>';
}
/**
* Outputs the simplified footer used for the Onboarding Wizard.
*
* @since 4.0.0
*
* @return void
*/
public function setupWizardFooter() {
?>
<?php
wp_print_scripts( 'aioseo-vendors' );
wp_print_scripts( 'aioseo-common' );
wp_print_scripts( 'aioseo-setup-wizard-script' );
do_action( 'admin_footer', '' );
do_action( 'admin_print_footer_scripts' );
// do_action( 'customize_controls_print_footer_scripts' );
?>
</body>
</html>
<?php
}
/**
* Check whether or not the Setup Wizard is completed.
*
* @since 4.2.0
*
* @return boolean Whether or not the Setup Wizard is completed.
*/
public function isCompleted() {
$wizard = (string) aioseo()->internalOptions->internal->wizard;
$wizard = json_decode( $wizard );
if ( ! $wizard ) {
return false;
}
$totalStageCount = count( $wizard->stages );
$currentStageCount = array_search( $wizard->currentStage, $wizard->stages, true );
// If not found, let's assume it's completed.
if ( false === $currentStageCount ) {
return true;
}
return $currentStageCount + 1 === $totalStageCount;
}
}