| 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 |
/** |
| 204 |
* Process submitted form data for the given setup wizard name and current step. |
| 205 |
* |
| 206 |
* @since 1.9.8.4 |
| 207 |
* |
| 208 |
* @param int $step Current step number. |
| 209 |
*/ |
| 210 |
do_action( 'convertkit_admin_setup_wizard_process_form_' . $this->page_name, $this->step ); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Populates the class variables with key information, covering: |
| 216 |
* - current step in the setup process |
| 217 |
* - previous, current and next step URLs. |
| 218 |
* |
| 219 |
* @since 1.9.8.4 |
| 220 |
*/ |
| 221 |
private function define_step_urls() { |
| 222 |
|
| 223 |
// Define the current step URL. |
| 224 |
$this->current_step_url = add_query_arg( |
| 225 |
array( |
| 226 |
'page' => $this->page_name, |
| 227 |
'convertkit-modal' => $this->is_modal(), |
| 228 |
'step' => $this->step, |
| 229 |
), |
| 230 |
admin_url( 'options.php' ) |
| 231 |
); |
| 232 |
|
| 233 |
// Define the previous step URL if we're not on the first or last step. |
| 234 |
if ( $this->step > 1 && $this->step < count( $this->steps ) ) { |
| 235 |
$this->previous_step_url = add_query_arg( |
| 236 |
array( |
| 237 |
'page' => $this->page_name, |
| 238 |
'convertkit-modal' => $this->is_modal(), |
| 239 |
'step' => ( $this->step - 1 ), |
| 240 |
), |
| 241 |
admin_url( 'options.php' ) |
| 242 |
); |
| 243 |
} |
| 244 |
|
| 245 |
// Define the next step URL if we're not on the last page. |
| 246 |
if ( $this->step < count( $this->steps ) ) { |
| 247 |
$this->next_step_url = add_query_arg( |
| 248 |
array( |
| 249 |
'page' => $this->page_name, |
| 250 |
'convertkit-modal' => $this->is_modal(), |
| 251 |
'step' => ( $this->step + 1 ), |
| 252 |
), |
| 253 |
admin_url( 'options.php' ) |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Load any data into class variables for the given setup wizard name and current step. |
| 261 |
* |
| 262 |
* @since 1.9.8.4 |
| 263 |
*/ |
| 264 |
private function load_screen_data() { |
| 265 |
|
| 266 |
/** |
| 267 |
* Load any data into class variables for the given setup wizard name and current step. |
| 268 |
* |
| 269 |
* @since 1.9.8.4 |
| 270 |
* |
| 271 |
* @param int $step Current step number. |
| 272 |
*/ |
| 273 |
do_action( 'convertkit_admin_setup_wizard_load_screen_data_' . $this->page_name, $this->step ); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Enqueue CSS when viewing the Setup screen. |
| 279 |
* |
| 280 |
* @since 1.9.8.4 |
| 281 |
*/ |
| 282 |
public function enqueue_scripts() { |
| 283 |
|
| 284 |
// Enqueue Select2 JS. |
| 285 |
convertkit_select2_enqueue_scripts(); |
| 286 |
|
| 287 |
// Enqueue JS. |
| 288 |
wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 289 |
wp_enqueue_script( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/setup-wizard.js', array(), CONVERTKIT_PLUGIN_VERSION, true ); |
| 290 |
|
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Enqueue CSS when viewing the setup screen. |
| 295 |
* |
| 296 |
* @since 1.9.8.4 |
| 297 |
*/ |
| 298 |
public function enqueue_styles() { |
| 299 |
|
| 300 |
// Enqueue WordPress default styles. |
| 301 |
wp_enqueue_style( 'common' ); |
| 302 |
wp_enqueue_style( 'buttons' ); |
| 303 |
wp_enqueue_style( 'forms' ); |
| 304 |
|
| 305 |
// Enqueue Select2 CSS. |
| 306 |
convertkit_select2_enqueue_styles(); |
| 307 |
|
| 308 |
// Enqueue styles for the setup wizard. |
| 309 |
wp_enqueue_style( 'convertkit-admin-setup-wizard', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/setup-wizard.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
/** |
| 314 |
* Outputs the <head> and opening <body> tag for the standalone setup screen |
| 315 |
* |
| 316 |
* @since 1.9.8.4 |
| 317 |
*/ |
| 318 |
private function output_header() { |
| 319 |
|
| 320 |
// Remove scripts. |
| 321 |
remove_all_actions( 'admin_notices' ); |
| 322 |
remove_all_actions( 'all_admin_notices' ); |
| 323 |
|
| 324 |
// Enqueue scripts. |
| 325 |
do_action( 'admin_enqueue_scripts' ); |
| 326 |
|
| 327 |
// Load header view. |
| 328 |
include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/header.php'; |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Outputs the HTML for the <body> section for the standalone setup screen |
| 334 |
* and defines any form option data that might be needed. |
| 335 |
* |
| 336 |
* @since 1.9.8.4 |
| 337 |
*/ |
| 338 |
private function output_content() { |
| 339 |
|
| 340 |
// Load content view. |
| 341 |
include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/' . $this->page_name . '/content-' . $this->step . '.php'; |
| 342 |
|
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Outputs the closing </body> and </html> tags, and runs some WordPress actions, for the standalone setup screen |
| 347 |
* |
| 348 |
* @since 1.9.8.4 |
| 349 |
*/ |
| 350 |
private function output_footer() { |
| 351 |
|
| 352 |
do_action( 'admin_print_footer_scripts' ); |
| 353 |
|
| 354 |
// Load footer view. |
| 355 |
include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/footer.php'; |
| 356 |
|
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Whether this wizard is served in a modal window. |
| 361 |
* |
| 362 |
* @since 2.2.6 |
| 363 |
* |
| 364 |
* @return bool |
| 365 |
*/ |
| 366 |
public function is_modal() { |
| 367 |
|
| 368 |
return $this->is_modal; |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Outputs HTML to close the current window, due to it being opened |
| 374 |
* by window.open(). |
| 375 |
* |
| 376 |
* @since 2.2.6 |
| 377 |
*/ |
| 378 |
public function maybe_close_modal() { |
| 379 |
|
| 380 |
// Sanity check we requested a modal. |
| 381 |
if ( ! $this->is_modal() ) { |
| 382 |
return; |
| 383 |
} |
| 384 |
|
| 385 |
// Load HTML to close the modal. |
| 386 |
include_once CONVERTKIT_PLUGIN_PATH . '/views/backend/setup-wizard/close-modal.php'; |
| 387 |
exit; |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Determines if the request is for the setup screen |
| 393 |
* |
| 394 |
* @since 1.9.8.4 |
| 395 |
* |
| 396 |
* @return bool Is setup screen request |
| 397 |
*/ |
| 398 |
public function is_setup_request() { |
| 399 |
|
| 400 |
// Don't load if this is an AJAX call. |
| 401 |
if ( wp_doing_ajax() || wp_doing_cron() ) { |
| 402 |
return false; |
| 403 |
} |
| 404 |
|
| 405 |
// Bail if we're not on the setup screen. |
| 406 |
if ( ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 407 |
return false; |
| 408 |
} |
| 409 |
if ( sanitize_text_field( wp_unslash( $_GET['page'] ) ) !== $this->page_name ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
return true; |
| 414 |
|
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Determines if the user has access to the setup wizard. |
| 419 |
* |
| 420 |
* @since 1.9.8.4 |
| 421 |
* |
| 422 |
* @return bool Has access |
| 423 |
*/ |
| 424 |
public function user_has_access() { |
| 425 |
|
| 426 |
// Bail if not logged in. |
| 427 |
if ( ! is_user_logged_in() ) { |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
// Bail if the user doesn't have the required capability. |
| 432 |
if ( ! current_user_can( $this->required_capability ) ) { |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
return true; |
| 437 |
|
| 438 |
} |
| 439 |
|
| 440 |
} |
| 441 |
|