PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.5
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.5
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 2.3.3 All 194 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.2.5, at admin/class-convertkit-admin-setup-wizard.php

410 lines 9.4 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 * The URL to take the user to when they click the Exit link.
69 *
70 * @since 1.9.8.4
71 *
72 * @var bool|string
73 */
74 public $exit_url = false;
75
76 /**
77 * Holds the URL for the current step in the setup process.
78 *
79 * @since 1.9.8.4
80 *
81 * @var bool|string
82 */
83 public $current_step_url = false;
84
85 /**
86 * Holds the URL to the next step in the setup process.
87 *
88 * @since 1.9.8.4
89 *
90 * @var bool|string
91 */
92 public $next_step_url = false;
93
94 /**
95 * Holds the URL to the previous step in the setup process.
96 *
97 * @since 1.9.8.4
98 *
99 * @var bool|string
100 */
101 public $previous_step_url = false;
102
103 /**
104 * Registers action and filter hooks.
105 *
106 * @since 1.9.8.4
107 */
108 public function __construct() {
109
110 // Bail if no page name is defined.
111 if ( $this->page_name === false ) {
112 return;
113 }
114
115 // Define actions to register the setup screen.
116 add_action( 'admin_menu', array( $this, 'register_screen' ) );
117 add_action( 'admin_head', array( $this, 'hide_screen_from_menu' ) );
118 add_action( 'admin_init', array( $this, 'maybe_load_setup_screen' ) );
119
120 }
121
122 /**
123 * Register the setup screen in WordPress' Dashboard, so that index.php?page={$this->page_name}
124 * does not 404 when in the WordPress Admin interface.
125 *
126 * @since 1.9.8.4
127 */
128 public function register_screen() {
129
130 add_dashboard_page( '', '', 'edit_posts', $this->page_name, '__return_false' );
131
132 }
133
134 /**
135 * Hides the menu registered when register_screen() above is called, otherwise
136 * we would have a blank submenu entry below the Dashboard menu.
137 *
138 * @since 1.9.8.4
139 */
140 public function hide_screen_from_menu() {
141
142 remove_submenu_page( 'index.php', $this->page_name );
143
144 }
145
146 /**
147 * Loads the setup screen if the request URL is for this class
148 *
149 * @since 1.9.8.4
150 */
151 public function maybe_load_setup_screen() {
152
153 // Bail if this isn't a request for the setup screen.
154 if ( ! $this->is_setup_request() ) {
155 return;
156 }
157
158 // Redirect back to the Dashboard if the user doesn't have the required capability to access this setup wizard.
159 if ( ! $this->user_has_access() ) {
160 wp_safe_redirect( admin_url( 'index.php' ) );
161 exit;
162 }
163
164 // Define current screen, so that calls to get_current_screen() tell Plugins which screen is loaded.
165 set_current_screen( $this->page_name );
166
167 // Define the step the user is on in the setup process.
168 $this->step = ( isset( $_REQUEST['step'] ) ? absint( $_REQUEST['step'] ) : 1 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
169
170 // Process any posted form data.
171 $this->process_form();
172
173 // Define current, previous and next step URLs.
174 $this->define_step_urls();
175
176 // Load any data for the current screen.
177 $this->load_screen_data();
178
179 // Load scripts and styles.
180 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
181 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
182
183 // Output custom HTML for the setup screen.
184 $this->output_header();
185 $this->output_content();
186 $this->output_footer();
187 exit;
188
189 }
190
191 /**
192 * Process submitted form data for the given setup wizard name and current step.
193 *
194 * @since 1.9.8.4
195 */
196 private function process_form() {
197
198 // Run security checks.
199 if ( ! isset( $_POST['_wpnonce'] ) ) {
200 return;
201 }
202 if ( ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), $this->page_name ) ) {
203 $this->error = __( 'Invalid nonce specified.', 'convertkit' );
204 return;
205 }
206
207 /**
208 * Process submitted form data for the given setup wizard name and current step.
209 *
210 * @since 1.9.8.4
211 *
212 * @param int $step Current step number.
213 */
214 do_action( 'convertkit_admin_setup_wizard_process_form_' . $this->page_name, $this->step );
215
216 }
217
218 /**
219 * Populates the class variables with key information, covering:
220 * - current step in the setup process
221 * - previous, current and next step URLs.
222 *
223 * @since 1.9.8.4
224 */
225 private function define_step_urls() {
226
227 // Define the current step URL.
228 $this->current_step_url = add_query_arg(
229 array(
230 'page' => $this->page_name,
231 'step' => $this->step,
232 ),
233 admin_url( 'index.php' )
234 );
235
236 // Define the previous step URL if we're not on the first or last step.
237 if ( $this->step > 1 && $this->step < count( $this->steps ) ) {
238 $this->previous_step_url = add_query_arg(
239 array(
240 'page' => $this->page_name,
241 'step' => ( $this->step - 1 ),
242 ),
243 admin_url( 'index.php' )
244 );
245 }
246
247 // Define the next step URL if we're not on the last page.
248 if ( $this->step < count( $this->steps ) ) {
249 $this->next_step_url = add_query_arg(
250 array(
251 'page' => $this->page_name,
252 'step' => ( $this->step + 1 ),
253 ),
254 admin_url( 'index.php' )
255 );
256 }
257
258 }
259
260 /**
261 * Load any data into class variables for the given setup wizard name and current step.
262 *
263 * @since 1.9.8.4
264 */
265 private function load_screen_data() {
266
267 /**
268 * Load any data into class variables for the given setup wizard name and current step.
269 *
270 * @since 1.9.8.4
271 *
272 * @param int $step Current step number.
273 */
274 do_action( 'convertkit_admin_setup_wizard_load_screen_data_' . $this->page_name, $this->step );
275
276 }
277
278 /**
279 * Enqueue CSS when viewing the Setup screen.
280 *
281 * @since 1.9.8.4
282 */
283 public function enqueue_scripts() {
284
285 // Enqueue Select2 JS.
286 convertkit_select2_enqueue_scripts();
287
288 // Enqueue JS.
289 wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
290 wp_enqueue_script( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/setup-wizard.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
291
292 }
293
294 /**
295 * Enqueue CSS when viewing the setup screen.
296 *
297 * @since 1.9.8.4
298 */
299 public function enqueue_styles() {
300
301 // Enqueue WordPress default styles.
302 wp_enqueue_style( 'common' );
303 wp_enqueue_style( 'buttons' );
304 wp_enqueue_style( 'forms' );
305
306 // Enqueue Select2 CSS.
307 convertkit_select2_enqueue_styles();
308
309 // Enqueue styles for the setup wizard.
310 wp_enqueue_style( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/setup-wizard.css', array(), CONVERTKIT_PLUGIN_VERSION );
311
312 }
313
314 /**
315 * Outputs the <head> and opening <body> tag for the standalone setup screen
316 *
317 * @since 1.9.8.4
318 */
319 private function output_header() {
320
321 // Remove scripts.
322 remove_all_actions( 'admin_notices' );
323 remove_all_actions( 'all_admin_notices' );
324
325 // Enqueue scripts.
326 do_action( 'admin_enqueue_scripts' );
327
328 // Load header view.
329 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/header.php';
330
331 }
332
333 /**
334 * Outputs the HTML for the <body> section for the standalone setup screen
335 * and defines any form option data that might be needed.
336 *
337 * @since 1.9.8.4
338 */
339 private function output_content() {
340
341 // Load content view.
342 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/' . $this->page_name . '/content-' . $this->step . '.php';
343
344 }
345
346 /**
347 * Outputs the closing </body> and </html> tags, and runs some WordPress actions, for the standalone setup screen
348 *
349 * @since 1.9.8.4
350 */
351 private function output_footer() {
352
353 do_action( 'admin_print_footer_scripts' );
354
355 // Load footer view.
356 include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/footer.php';
357
358 }
359
360 /**
361 * Determines if the request is for the setup screen
362 *
363 * @since 1.9.8.4
364 *
365 * @return bool Is setup screen request
366 */
367 public function is_setup_request() {
368
369 // Don't load if this is an AJAX call.
370 if ( wp_doing_ajax() || wp_doing_cron() ) {
371 return false;
372 }
373
374 // Bail if we're not on the setup screen.
375 if ( ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
376 return false;
377 }
378 if ( sanitize_text_field( $_GET['page'] ) !== $this->page_name ) { // phpcs:ignore WordPress.Security.NonceVerification
379 return false;
380 }
381
382 return true;
383
384 }
385
386 /**
387 * Determines if the user has access to the setup wizard.
388 *
389 * @since 1.9.8.4
390 *
391 * @return bool Has access
392 */
393 public function user_has_access() {
394
395 // Bail if not logged in.
396 if ( ! is_user_logged_in() ) {
397 return false;
398 }
399
400 // Bail if the user doesn't have the required capability.
401 if ( ! current_user_can( $this->required_capability ) ) {
402 return false;
403 }
404
405 return true;
406
407 }
408
409 }
410