PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.3.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.3.6
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 3.3.6, at admin/class-convertkit-admin-setup-wizard.php

511 lines 11.5 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 string
55 */
56 public $step = 'start';
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 ( filter_has_var( INPUT_GET, 'convertkit-modal' ) && filter_input( INPUT_GET, 'convertkit-modal', FILTER_SANITIZE_NUMBER_INT ) === '1' ) {
169 $this->is_modal = true;
170 }
171
172 /**
173 * Define the steps for the setup wizard.
174 *
175 * @since 3.1.8
176 *
177 * @param array $steps The steps for the setup wizard.
178 * @return array The steps for the setup wizard.
179 */
180 $this->steps = apply_filters( 'convertkit_admin_setup_wizard_steps_' . $this->page_name, $this->steps );
181
182 // Define the step the user is on in the setup process.
183 $this->step = $this->get_current_step();
184
185 // Process any posted form data.
186 $this->process_form();
187
188 // Define current, previous and next step URLs.
189 $this->define_step_urls();
190
191 // Load any data for the current screen.
192 $this->load_screen_data();
193
194 // Load scripts and styles.
195 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
196 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
197
198 // Output custom HTML for the setup screen.
199 $this->output_header();
200 $this->output_content();
201 $this->output_footer();
202 exit;
203
204 }
205
206 /**
207 * Returns the current step in the setup process.
208 *
209 * @since 3.1.7
210 *
211 * @return string Current step.
212 */
213 public function get_current_step() {
214
215 $step = ( filter_has_var( INPUT_GET, 'step' ) ? filter_input( INPUT_GET, 'step', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'start' );
216
217 // Fallback to 'start' if the step is a registered step.
218 if ( ! array_key_exists( $step, $this->steps ) ) {
219 $step = 'start';
220 }
221
222 return $step;
223
224 }
225
226 /**
227 * Get the number of the current step.
228 *
229 * @since 3.1.7
230 *
231 * @return int Step number.
232 */
233 public function get_current_step_number() {
234
235 return array_search( $this->step, array_keys( $this->steps ), true ) + 1;
236
237 }
238
239 /**
240 * Get the step by number.
241 *
242 * @since 3.1.7
243 *
244 * @param int $number Step number (1 based index).
245 * @return string Step name/key.
246 */
247 public function get_step_key_by_number( $number ) {
248
249 return array_keys( $this->steps )[ $number - 1 ];
250
251 }
252
253 /**
254 * Get the total number of steps.
255 *
256 * @since 3.1.7
257 *
258 * @return int Total steps.
259 */
260 public function get_total_steps() {
261
262 return count( $this->steps );
263
264 }
265
266 /**
267 * Process submitted form data for the given setup wizard name and current step.
268 *
269 * @since 1.9.8.4
270 */
271 private function process_form() {
272
273 /**
274 * Process submitted form data for the given setup wizard name and current step.
275 *
276 * @since 1.9.8.4
277 *
278 * @param string $step Current step.
279 */
280 do_action( 'convertkit_admin_setup_wizard_process_form_' . $this->page_name, $this->step );
281
282 }
283
284 /**
285 * Populates the class variables with key information, covering:
286 * - current step in the setup process
287 * - previous, current and next step URLs.
288 *
289 * @since 1.9.8.4
290 */
291 private function define_step_urls() {
292
293 // Define the current step URL.
294 $this->current_step_url = add_query_arg(
295 array(
296 'page' => $this->page_name,
297 'convertkit-modal' => $this->is_modal(),
298 'step' => $this->step,
299 ),
300 admin_url( 'options.php' )
301 );
302
303 // Define the previous step URL if we're not on the first or last step.
304 if ( $this->get_current_step_number() > 1 && $this->get_current_step_number() < $this->get_total_steps() ) {
305 $this->previous_step_url = add_query_arg(
306 array(
307 'page' => $this->page_name,
308 'convertkit-modal' => $this->is_modal(),
309 'step' => $this->get_step_key_by_number( $this->get_current_step_number() - 1 ),
310 ),
311 admin_url( 'options.php' )
312 );
313 }
314
315 // Define the next step URL if we're not on the last page.
316 if ( $this->get_current_step_number() < $this->get_total_steps() ) {
317 $this->next_step_url = add_query_arg(
318 array(
319 'page' => $this->page_name,
320 'convertkit-modal' => $this->is_modal(),
321 'step' => $this->get_step_key_by_number( $this->get_current_step_number() + 1 ),
322 ),
323 admin_url( 'options.php' )
324 );
325 }
326
327 }
328
329 /**
330 * Load any data into class variables for the given setup wizard name and current step.
331 *
332 * @since 1.9.8.4
333 */
334 private function load_screen_data() {
335
336 /**
337 * Load any data into class variables for the given setup wizard name and current step.
338 *
339 * @since 1.9.8.4
340 *
341 * @param string $step Current step.
342 */
343 do_action( 'convertkit_admin_setup_wizard_load_screen_data_' . $this->page_name, $this->step );
344
345 }
346
347 /**
348 * Enqueue CSS when viewing the Setup screen.
349 *
350 * @since 1.9.8.4
351 */
352 public function enqueue_scripts() {
353
354 // Enqueue Select2 JS.
355 convertkit_select2_enqueue_scripts();
356
357 // Enqueue JS.
358 wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
359 wp_enqueue_script( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/setup-wizard.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
360
361 }
362
363 /**
364 * Enqueue CSS when viewing the setup screen.
365 *
366 * @since 1.9.8.4
367 */
368 public function enqueue_styles() {
369
370 // Enqueue WordPress default styles.
371 wp_enqueue_style( 'common' );
372 wp_enqueue_style( 'buttons' );
373 wp_enqueue_style( 'forms' );
374
375 // Enqueue Select2 CSS.
376 convertkit_select2_enqueue_styles();
377
378 // Enqueue styles for the setup wizard.
379 wp_enqueue_style( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/setup-wizard.css', array(), CONVERTKIT_PLUGIN_VERSION );
380
381 }
382
383 /**
384 * Outputs the <head> and opening <body> tag for the standalone setup screen
385 *
386 * @since 1.9.8.4
387 */
388 private function output_header() {
389
390 // Remove scripts.
391 remove_all_actions( 'admin_notices' );
392 remove_all_actions( 'all_admin_notices' );
393
394 // Enqueue scripts.
395 do_action( 'admin_enqueue_scripts' );
396
397 // Load header view.
398 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/header.php';
399
400 }
401
402 /**
403 * Outputs the HTML for the <body> section for the standalone setup screen
404 * and defines any form option data that might be needed.
405 *
406 * @since 1.9.8.4
407 */
408 private function output_content() {
409
410 // Load content view.
411 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/' . $this->page_name . '/content-' . $this->step . '.php';
412
413 }
414
415 /**
416 * Outputs the closing </body> and </html> tags, and runs some WordPress actions, for the standalone setup screen
417 *
418 * @since 1.9.8.4
419 */
420 private function output_footer() {
421
422 do_action( 'admin_print_footer_scripts' );
423
424 // Load footer view.
425 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/footer.php';
426
427 }
428
429 /**
430 * Whether this wizard is served in a modal window.
431 *
432 * @since 2.2.6
433 *
434 * @return bool
435 */
436 public function is_modal() {
437
438 return $this->is_modal;
439
440 }
441
442 /**
443 * Outputs HTML to close the current window, due to it being opened
444 * by window.open().
445 *
446 * @since 2.2.6
447 */
448 public function maybe_close_modal() {
449
450 // Sanity check we requested a modal.
451 if ( ! $this->is_modal() ) {
452 return;
453 }
454
455 // Load HTML to close the modal.
456 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/close-modal.php';
457 exit;
458
459 }
460
461 /**
462 * Determines if the request is for the setup screen
463 *
464 * @since 1.9.8.4
465 *
466 * @return bool Is setup screen request
467 */
468 public function is_setup_request() {
469
470 // Don't load if this is an AJAX call.
471 if ( wp_doing_ajax() || wp_doing_cron() ) {
472 return false;
473 }
474
475 // Bail if we're not on the setup screen.
476 if ( ! filter_has_var( INPUT_GET, 'page' ) ) {
477 return false;
478 }
479 if ( filter_input( INPUT_GET, 'page', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) !== $this->page_name ) {
480 return false;
481 }
482
483 return true;
484
485 }
486
487 /**
488 * Determines if the user has access to the setup wizard.
489 *
490 * @since 1.9.8.4
491 *
492 * @return bool Has access
493 */
494 public function user_has_access() {
495
496 // Bail if not logged in.
497 if ( ! is_user_logged_in() ) {
498 return false;
499 }
500
501 // Bail if the user doesn't have the required capability.
502 if ( ! current_user_can( $this->required_capability ) ) {
503 return false;
504 }
505
506 return true;
507
508 }
509
510 }
511