PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.3
3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 All 195 releases
convertkit / admin / class-convertkit-admin-setup-wizard.php

class-convertkit-admin-setup-wizard.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.4.3, at admin/class-convertkit-admin-setup-wizard.php

450 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Admin Setup Wizard class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Provides a UI for displaying a step by step wizard style screen in the WordPress
11 * Administration.
12 *
13 * To use this class, extend it with your own configuration.
14 *
15 * Refer to the admin/setup-wizard folder for current implementations.
16 *
17 * @package ConvertKit
18 * @author ConvertKit
19 */
20 class ConvertKit_Admin_Setup_Wizard {
21
22 /**
23 * The steps available in this wizard.
24 *
25 * @since 1.9.8.4
26 *
27 * @var array
28 */
29 public $steps = array();
30
31 /**
32 * Holds an error message to display on screen.
33 *
34 * @since 1.9.8.4
35 *
36 * @var bool|string
37 */
38 public $error = false;
39
40 /**
41 * The required user capability to access the setup wizard.
42 *
43 * @since 1.9.8.4
44 *
45 * @var string
46 */
47 public $required_capability = 'activate_plugins';
48
49 /**
50 * The current step in the setup process the user is on.
51 *
52 * @since 1.9.8.4
53 *
54 * @var int
55 */
56 public $step = 1;
57
58 /**
59 * The programmatic name of the setup screen.
60 *
61 * @since 1.9.8.4
62 *
63 * @var bool|string
64 */
65 public $page_name = false;
66
67 /**
68 * Whether the wizard is being served within a modal or
69 * new window.
70 *
71 * @since 2.2.6
72 *
73 * @var bool
74 */
75 public $is_modal = false;
76
77 /**
78 * The URL to take the user to when they click the Exit link.
79 *
80 * @since 1.9.8.4
81 *
82 * @var bool|string
83 */
84 public $exit_url = false;
85
86 /**
87 * Holds the URL for the current step in the setup process.
88 *
89 * @since 1.9.8.4
90 *
91 * @var bool|string
92 */
93 public $current_step_url = false;
94
95 /**
96 * Holds the URL to the next step in the setup process.
97 *
98 * @since 1.9.8.4
99 *
100 * @var bool|string
101 */
102 public $next_step_url = false;
103
104 /**
105 * Holds the URL to the previous step in the setup process.
106 *
107 * @since 1.9.8.4
108 *
109 * @var bool|string
110 */
111 public $previous_step_url = false;
112
113 /**
114 * Registers action and filter hooks.
115 *
116 * @since 1.9.8.4
117 */
118 public function __construct() {
119
120 // Bail if no page name is defined.
121 if ( $this->page_name === false ) {
122 return;
123 }
124
125 // Define actions to register the setup screen.
126 add_action( 'admin_menu', array( $this, 'register_screen' ) );
127 add_action( 'admin_init', array( $this, 'maybe_load_setup_screen' ) );
128
129 }
130
131 /**
132 * Register the wizard screen in WordPress' Dashboard, so that options.php?page={$this->page_name}
133 * does not 404 when in the WordPress Admin interface.
134 *
135 * Ensures the WordPress user has the given required_capability to access this screen.
136 *
137 * @since 1.9.8.4
138 */
139 public function register_screen() {
140
141 add_submenu_page( '', '', '', $this->required_capability, $this->page_name, '__return_false' );
142
143 }
144
145 /**
146 * Loads the setup screen if the request URL is for this class
147 *
148 * @since 1.9.8.4
149 */
150 public function maybe_load_setup_screen() {
151
152 // Bail if this isn't a request for the setup screen.
153 if ( ! $this->is_setup_request() ) {
154 return;
155 }
156
157 // Redirect back to the Dashboard if the user doesn't have the required capability to access this setup wizard.
158 if ( ! $this->user_has_access() ) {
159 wp_safe_redirect( admin_url( 'index.php' ) );
160 exit;
161 }
162
163 // Define current screen, so that calls to get_current_screen() tell Plugins which screen is loaded.
164 set_current_screen( $this->page_name );
165
166 // If the convertkit-modal parameter exists and is 1, set the flag to denote
167 // this wizard is served in a modal.
168 if ( array_key_exists( 'convertkit-modal', $_REQUEST ) && $_REQUEST['convertkit-modal'] === '1' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
169 $this->is_modal = true;
170 }
171
172 // Define the step the user is on in the setup process.
173 $this->step = ( isset( $_REQUEST['step'] ) ? absint( $_REQUEST['step'] ) : 1 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
174
175 // Process any posted form data.
176 $this->process_form();
177
178 // Define current, previous and next step URLs.
179 $this->define_step_urls();
180
181 // Load any data for the current screen.
182 $this->load_screen_data();
183
184 // Load scripts and styles.
185 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
186 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
187
188 // Output custom HTML for the setup screen.
189 $this->output_header();
190 $this->output_content();
191 $this->output_footer();
192 exit;
193
194 }
195
196 /**
197 * Process submitted form data for the given setup wizard name and current step.
198 *
199 * @since 1.9.8.4
200 */
201 private function process_form() {
202
203 // Run security checks.
204 if ( ! isset( $_POST['_wpnonce'] ) ) {
205 return;
206 }
207 if ( ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), $this->page_name ) ) {
208 $this->error = __( 'Invalid nonce specified.', 'convertkit' );
209 return;
210 }
211
212 /**
213 * Process submitted form data for the given setup wizard name and current step.
214 *
215 * @since 1.9.8.4
216 *
217 * @param int $step Current step number.
218 */
219 do_action( 'convertkit_admin_setup_wizard_process_form_' . $this->page_name, $this->step );
220
221 }
222
223 /**
224 * Populates the class variables with key information, covering:
225 * - current step in the setup process
226 * - previous, current and next step URLs.
227 *
228 * @since 1.9.8.4
229 */
230 private function define_step_urls() {
231
232 // Define the current step URL.
233 $this->current_step_url = add_query_arg(
234 array(
235 'page' => $this->page_name,
236 'convertkit-modal' => $this->is_modal(),
237 'step' => $this->step,
238 ),
239 admin_url( 'options.php' )
240 );
241
242 // Define the previous step URL if we're not on the first or last step.
243 if ( $this->step > 1 && $this->step < count( $this->steps ) ) {
244 $this->previous_step_url = add_query_arg(
245 array(
246 'page' => $this->page_name,
247 'convertkit-modal' => $this->is_modal(),
248 'step' => ( $this->step - 1 ),
249 ),
250 admin_url( 'options.php' )
251 );
252 }
253
254 // Define the next step URL if we're not on the last page.
255 if ( $this->step < count( $this->steps ) ) {
256 $this->next_step_url = add_query_arg(
257 array(
258 'page' => $this->page_name,
259 'convertkit-modal' => $this->is_modal(),
260 'step' => ( $this->step + 1 ),
261 ),
262 admin_url( 'options.php' )
263 );
264 }
265
266 }
267
268 /**
269 * Load any data into class variables for the given setup wizard name and current step.
270 *
271 * @since 1.9.8.4
272 */
273 private function load_screen_data() {
274
275 /**
276 * Load any data into class variables for the given setup wizard name and current step.
277 *
278 * @since 1.9.8.4
279 *
280 * @param int $step Current step number.
281 */
282 do_action( 'convertkit_admin_setup_wizard_load_screen_data_' . $this->page_name, $this->step );
283
284 }
285
286 /**
287 * Enqueue CSS when viewing the Setup screen.
288 *
289 * @since 1.9.8.4
290 */
291 public function enqueue_scripts() {
292
293 // Enqueue Select2 JS.
294 convertkit_select2_enqueue_scripts();
295
296 // Enqueue JS.
297 wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
298 wp_enqueue_script( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/setup-wizard.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
299
300 }
301
302 /**
303 * Enqueue CSS when viewing the setup screen.
304 *
305 * @since 1.9.8.4
306 */
307 public function enqueue_styles() {
308
309 // Enqueue WordPress default styles.
310 wp_enqueue_style( 'common' );
311 wp_enqueue_style( 'buttons' );
312 wp_enqueue_style( 'forms' );
313
314 // Enqueue Select2 CSS.
315 convertkit_select2_enqueue_styles();
316
317 // Enqueue styles for the setup wizard.
318 wp_enqueue_style( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/setup-wizard.css', array(), CONVERTKIT_PLUGIN_VERSION );
319
320 }
321
322 /**
323 * Outputs the <head> and opening <body> tag for the standalone setup screen
324 *
325 * @since 1.9.8.4
326 */
327 private function output_header() {
328
329 // Remove scripts.
330 remove_all_actions( 'admin_notices' );
331 remove_all_actions( 'all_admin_notices' );
332
333 // Enqueue scripts.
334 do_action( 'admin_enqueue_scripts' );
335
336 // Load header view.
337 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/header.php';
338
339 }
340
341 /**
342 * Outputs the HTML for the <body> section for the standalone setup screen
343 * and defines any form option data that might be needed.
344 *
345 * @since 1.9.8.4
346 */
347 private function output_content() {
348
349 // Load content view.
350 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/' . $this->page_name . '/content-' . $this->step . '.php';
351
352 }
353
354 /**
355 * Outputs the closing </body> and </html> tags, and runs some WordPress actions, for the standalone setup screen
356 *
357 * @since 1.9.8.4
358 */
359 private function output_footer() {
360
361 do_action( 'admin_print_footer_scripts' );
362
363 // Load footer view.
364 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/footer.php';
365
366 }
367
368 /**
369 * Whether this wizard is served in a modal window.
370 *
371 * @since 2.2.6
372 *
373 * @return bool
374 */
375 public function is_modal() {
376
377 return $this->is_modal;
378
379 }
380
381 /**
382 * Outputs HTML to close the current window, due to it being opened
383 * by window.open().
384 *
385 * @since 2.2.6
386 */
387 public function maybe_close_modal() {
388
389 // Sanity check we requested a modal.
390 if ( ! $this->is_modal() ) {
391 return;
392 }
393
394 // Load HTML to close the modal.
395 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/close-modal.php';
396 exit;
397
398 }
399
400 /**
401 * Determines if the request is for the setup screen
402 *
403 * @since 1.9.8.4
404 *
405 * @return bool Is setup screen request
406 */
407 public function is_setup_request() {
408
409 // Don't load if this is an AJAX call.
410 if ( wp_doing_ajax() || wp_doing_cron() ) {
411 return false;
412 }
413
414 // Bail if we're not on the setup screen.
415 if ( ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
416 return false;
417 }
418 if ( sanitize_text_field( $_GET['page'] ) !== $this->page_name ) { // phpcs:ignore WordPress.Security.NonceVerification
419 return false;
420 }
421
422 return true;
423
424 }
425
426 /**
427 * Determines if the user has access to the setup wizard.
428 *
429 * @since 1.9.8.4
430 *
431 * @return bool Has access
432 */
433 public function user_has_access() {
434
435 // Bail if not logged in.
436 if ( ! is_user_logged_in() ) {
437 return false;
438 }
439
440 // Bail if the user doesn't have the required capability.
441 if ( ! current_user_can( $this->required_capability ) ) {
442 return false;
443 }
444
445 return true;
446
447 }
448
449 }
450