array( 'name' => __( 'Setup', 'convertkit' ), 'next_button' => array( 'label' => __( 'Create', 'convertkit' ), ), ), 'finish' => array( 'name' => __( 'Done', 'convertkit' ), ), ); } /** * Process posted data from the submitted form. * * @since 2.5.5 * * @param string $step Current step. */ public function process_form( $step ) { // Set and authorize the Post Type before processing data. $this->set_post_type(); if ( ! convertkit_user_can_create_published_post_type( $this->post_type ) ) { $this->error = __( 'You are not allowed to create and publish this type of content.', 'convertkit' ); return; } // Run security checks. if ( ! isset( $_REQUEST['_wpnonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $this->page_name ) ) { $this->error = __( 'Invalid nonce specified.', 'convertkit' ); return; } // Don't process form data if we're not on the second step. if ( $step !== 'finish' ) { return; } // Sanitize configuration. $configuration = array( 'landing_page' => ( isset( $_POST['landing_page'] ) ? sanitize_text_field( wp_unslash( $_POST['landing_page'] ) ) : '' ), 'post_name' => ( isset( $_POST['post_name'] ) ? sanitize_text_field( wp_unslash( $_POST['post_name'] ) ) : '' ), 'post_type' => $this->post_type, ); // Create Page. $this->result = $this->create_page_displaying_landing_page( $configuration['post_name'], $configuration['landing_page'], $configuration['post_type'] ); // If an error occured creating the Page, go back a step to show the error. if ( is_wp_error( $this->result ) ) { $this->step = $this->get_step_key_by_number( $this->get_current_step_number() - 1 ); $this->error = $this->result->get_error_message(); } } /** * Load any data into class variables for the given setup wizard name and current step. * * @since 2.5.5 * * @param string $step Current step. */ public function load_screen_data( $step ) { // Show an error screen if API credentials have not been specified. // This shouldn't happen, because the 'Add New Member Content' button is only displayed // if valid credentials have been specified. $settings = new ConvertKit_Settings(); if ( ! $settings->has_access_and_refresh_token() ) { wp_die( esc_html__( 'Connect your Kit account in the Kit Plugin\'s settings to get started', 'convertkit' ) ); } // Set and authorize the Post Type. $this->set_post_type(); if ( ! convertkit_user_can_create_published_post_type( $this->post_type ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to create and publish this type of content.', 'convertkit' ) ); } // Define Exit URL to take the user back to the WP_List_Table for the Post Type they were viewing. $this->exit_url = add_query_arg( array( 'post_type' => $this->post_type, ), admin_url( 'edit.php' ) ); // Don't load data if not on the first step. if ( $step !== 'start' ) { return; } // Fetch Landing Pages. $this->landing_pages = new ConvertKit_Resource_Landing_Pages( 'landing_page_wizard' ); // Refresh Landing Page resources, in case the user just created their first Landing Page in Kit. $result = $this->landing_pages->refresh(); // 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['start']['next_button'] ); $this->current_url = add_query_arg( array( 'page' => $this->page_name, 'ck_post_type' => $this->post_type, 'step' => 'start', ), admin_url( 'options.php' ) ); return; } // 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['start']['next_button'] ); $this->current_url = add_query_arg( array( 'page' => $this->page_name, 'ck_post_type' => $this->post_type, 'step' => 'start', ), admin_url( 'options.php' ) ); } } /** * Creates a WordPress Page for the given title, set to display the given landing page. * * @since 2.5.5 * * @param string $post_name Post Name / Permalink. * @param string $landing_page Landing Page ID or URL. * @param string $post_type Post Type. * @return WP_Error|int Error or Page ID */ private function create_page_displaying_landing_page( $post_name, $landing_page, $post_type ) { // Create Page. $page_id = wp_insert_post( array( 'post_type' => $post_type, 'post_name' => sanitize_title_with_dashes( $post_name ), 'post_title' => $post_name, 'post_status' => 'publish', 'post_author' => get_current_user_id(), ), true ); // Bail if an error occured. if ( is_wp_error( $page_id ) ) { return $page_id; } // Define Page's settings. WP_ConvertKit()->get_class( 'admin_post' )->save_post_settings( $page_id, array( 'form' => '0', // Don't display a Form. 'landing_page' => $landing_page, ) ); // Return. return $page_id; } }