Skip to content
78 changes: 69 additions & 9 deletions admin/class-convertkit-admin-setup-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class ConvertKit_Admin_Setup_Wizard {
*
* @since 1.9.8.4
*
* @var int
* @var string
*/
public $step = 1;
public $step = 'start';

/**
* The programmatic name of the setup screen.
Expand Down Expand Up @@ -170,7 +170,7 @@ public function maybe_load_setup_screen() {
}

// Define the step the user is on in the setup process.
$this->step = ( filter_has_var( INPUT_GET, 'step' ) ? absint( filter_input( INPUT_GET, 'step', FILTER_SANITIZE_NUMBER_INT ) ) : 1 );
$this->step = $this->get_current_step();

// Process any posted form data.
$this->process_form();
Expand All @@ -193,6 +193,66 @@ public function maybe_load_setup_screen() {

}

/**
* Returns the current step in the setup process.
*
* @since 3.1.7
*
* @return string Current step.
*/
public function get_current_step() {

$step = ( filter_has_var( INPUT_GET, 'step' ) ? filter_input( INPUT_GET, 'step', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : 'start' );

// Fallback to 'start' if the step is a registered step.
if ( ! array_key_exists( $step, $this->steps ) ) {
$step = 'start';
}

return $step;

}

/**
* Get the number of the current step.
*
* @since 3.1.7
*
* @return int Step number.
*/
public function get_current_step_number() {

return array_search( $this->step, array_keys( $this->steps ), true ) + 1;

}

/**
* Get the step by number.
*
* @since 3.1.7
*
* @param int $number Step number (1 based index).
* @return string Step name/key.
*/
public function get_step_key_by_number( $number ) {

return array_keys( $this->steps )[ $number - 1 ];

}

/**
* Get the total number of steps.
*
* @since 3.1.7
*
* @return int Total steps.
*/
public function get_total_steps() {

return count( $this->steps );

}

/**
* Process submitted form data for the given setup wizard name and current step.
*
Expand All @@ -205,7 +265,7 @@ private function process_form() {
*
* @since 1.9.8.4
*
* @param int $step Current step number.
* @param string $step Current step.
*/
do_action( 'convertkit_admin_setup_wizard_process_form_' . $this->page_name, $this->step );

Expand All @@ -231,24 +291,24 @@ private function define_step_urls() {
);

// Define the previous step URL if we're not on the first or last step.
if ( $this->step > 1 && $this->step < count( $this->steps ) ) {
if ( $this->get_current_step_number() > 1 && $this->get_current_step_number() < $this->get_total_steps() ) {
$this->previous_step_url = add_query_arg(
array(
'page' => $this->page_name,
'convertkit-modal' => $this->is_modal(),
'step' => ( $this->step - 1 ),
'step' => $this->get_step_key_by_number( $this->get_current_step_number() - 1 ),
),
admin_url( 'options.php' )
);
}

// Define the next step URL if we're not on the last page.
if ( $this->step < count( $this->steps ) ) {
if ( $this->get_current_step_number() < $this->get_total_steps() ) {
$this->next_step_url = add_query_arg(
array(
'page' => $this->page_name,
'convertkit-modal' => $this->is_modal(),
'step' => ( $this->step + 1 ),
'step' => $this->get_step_key_by_number( $this->get_current_step_number() + 1 ),
),
admin_url( 'options.php' )
);
Expand All @@ -268,7 +328,7 @@ private function load_screen_data() {
*
* @since 1.9.8.4
*
* @param int $step Current step number.
* @param string $step Current step.
*/
do_action( 'convertkit_admin_setup_wizard_load_screen_data_' . $this->page_name, $this->step );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ public function __construct() {

// Define details for each step in the setup process.
$this->steps = array(
1 => array(
'start' => array(
'name' => __( 'Setup', 'convertkit' ),
'next_button' => array(
'label' => __( 'Create', 'convertkit' ),
),
),
2 => array(
'finish' => array(
'name' => __( 'Done', 'convertkit' ),
),
);
Expand All @@ -110,7 +110,7 @@ public function __construct() {
*
* @since 2.5.5
*
* @param int $step Current step.
* @param string $step Current step.
*/
public function process_form( $step ) {

Expand All @@ -124,7 +124,7 @@ public function process_form( $step ) {
}

// Don't process form data if we're not on the second step.
if ( $step !== 2 ) {
if ( $step !== 'finish' ) {
return;
}

Expand All @@ -144,7 +144,7 @@ public function process_form( $step ) {

// If an error occured creating the Page, go back a step to show the error.
if ( is_wp_error( $this->result ) ) {
$this->step = ( $this->step - 1 );
$this->step = $this->get_step_key_by_number( $this->get_current_step_number() - 1 );
$this->error = $this->result->get_error_message();
}

Expand All @@ -155,7 +155,7 @@ public function process_form( $step ) {
*
* @since 2.5.5
*
* @param int $step Current step.
* @param string $step Current step.
*/
public function load_screen_data( $step ) {

Expand Down Expand Up @@ -198,7 +198,7 @@ public function load_screen_data( $step ) {
);

// Don't load data if not on the first step.
if ( $step !== 1 ) {
if ( $step !== 'start' ) {
return;
}

Expand All @@ -211,12 +211,12 @@ public function load_screen_data( $step ) {
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
// Change the next button label and make it a link to reload the screen.
unset( $this->steps[1]['next_button'] );
unset( $this->steps['start']['next_button'] );
$this->current_url = add_query_arg(
array(
'page' => $this->page_name,
'ck_post_type' => $this->post_type,
'step' => 1,
'step' => 'start',
),
admin_url( 'options.php' )
);
Expand All @@ -226,12 +226,12 @@ public function load_screen_data( $step ) {
// If no Landing Pages exist in ConvertKit, change the next button label and make it a link to reload
// the screen.
if ( ! $this->landing_pages->exist() ) {
unset( $this->steps[1]['next_button'] );
unset( $this->steps['start']['next_button'] );
$this->current_url = add_query_arg(
array(
'page' => $this->page_name,
'ck_post_type' => $this->post_type,
'step' => 1,
'step' => 'start',
),
admin_url( 'options.php' )
);
Expand Down
38 changes: 20 additions & 18 deletions admin/setup-wizard/class-convertkit-admin-setup-wizard-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,20 @@ public function __construct() {

// Define details for each step in the setup process.
$this->steps = array(
1 => array(
'start' => array(
'name' => __( 'Connect', 'convertkit' ),
'next_button' => array(
'label' => __( 'Connect', 'convertkit' ),
'link' => $this->api->get_oauth_url( admin_url( 'options.php?page=convertkit-setup&step=2' ), get_site_url() ),
'link' => $this->api->get_oauth_url( admin_url( 'options.php?page=convertkit-setup&step=configuration' ), get_site_url() ),
),
),
2 => array(
'configuration' => array(
'name' => __( 'Configuration', 'convertkit' ),
'next_button' => array(
'label' => __( 'Finish Setup', 'convertkit' ),
),
),
3 => array(
'finish' => array(
'name' => __( 'Done', 'convertkit' ),
),
);
Expand Down Expand Up @@ -210,11 +210,11 @@ public function process_form( $step ) {

// Depending on the step, process the form data.
switch ( $step ) {
case 2:
case 'configuration':
// If an error occured from OAuth i.e. the user did not authorize, show it now.
if ( array_key_exists( 'error', $_REQUEST ) && array_key_exists( 'error_description', $_REQUEST ) ) {
// Decrement the step.
$this->step = ( $this->step - 1 );
$this->step = 'start';
$this->error = sanitize_text_field( wp_unslash( $_REQUEST['error_description'] ) );
return;
}
Expand All @@ -233,7 +233,7 @@ public function process_form( $step ) {
// Show an error message if we could not fetch the access token.
if ( is_wp_error( $result ) ) {
// Decrement the step.
$this->step = ( $this->step - 1 );
$this->step = 'start';
$this->error = $result->get_error_message();
return;
}
Expand All @@ -248,12 +248,14 @@ public function process_form( $step ) {
);
break;

case 3:
case 'finish':
// Run security checks.
if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $this->page_name ) ) {
// Decrement the step.
$this->step = 'configuration';
$this->error = __( 'Invalid nonce specified.', 'convertkit' );
return;
}
Expand Down Expand Up @@ -284,7 +286,7 @@ public function load_screen_data( $step ) {
// If this wizard is being served in a modal window, change the flow.
if ( $this->is_modal() ) {
switch ( $step ) {
case 1:
case 'start':
// Setup API.
$api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI );

Expand All @@ -304,18 +306,18 @@ function ( $hosts ) {
);

// Redirect to OAuth.
wp_safe_redirect( $api->get_oauth_url( admin_url( 'options.php?page=convertkit-setup&step=2&convertkit-modal=1' ), get_site_url() ) );
wp_safe_redirect( $api->get_oauth_url( admin_url( 'options.php?page=convertkit-setup&step=configuration&convertkit-modal=1' ), get_site_url() ) );
die();

case 2:
case 'configuration':
// Close modal.
$this->maybe_close_modal();
break;
}
}

switch ( $step ) {
case 2:
case 'configuration':
// Re-load settings class now that the Access and Refresh Tokens have been defined.
$this->settings = new ConvertKit_Settings();

Expand All @@ -326,11 +328,11 @@ function ( $hosts ) {
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
// Change the next button label and make it a link to reload the screen.
$this->steps[2]['next_button']['label'] = __( 'I\'ve created a form in Kit', 'convertkit' );
$this->steps[2]['next_button']['link'] = add_query_arg(
$this->steps['configuration']['next_button']['label'] = __( 'I\'ve created a form in Kit', 'convertkit' );
$this->steps['configuration']['next_button']['link'] = add_query_arg(
array(
'page' => $this->page_name,
'step' => 2,
'step' => 'configuration',
),
admin_url( 'options.php' )
);
Expand All @@ -340,11 +342,11 @@ function ( $hosts ) {
// If no Forms exist in ConvertKit, change the next button label and make it a link to reload
// the screen.
if ( ! $this->forms->exist() ) {
$this->steps[2]['next_button']['label'] = __( 'I\'ve created a form in Kit', 'convertkit' );
$this->steps[2]['next_button']['link'] = add_query_arg(
$this->steps['configuration']['next_button']['label'] = __( 'I\'ve created a form in Kit', 'convertkit' );
$this->steps['configuration']['next_button']['link'] = add_query_arg(
array(
'page' => $this->page_name,
'step' => 2,
'step' => 'configuration',
),
admin_url( 'options.php' )
);
Expand Down
Loading