| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Setup Wizard for Landing Pages class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Provides a UI for setting up a new WordPress Page that displays a ConvertKit Landing Page. |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
class ConvertKit_Admin_Setup_Wizard_Landing_Page extends ConvertKit_Admin_Setup_Wizard { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds the ConvertKit Products resource class. |
| 19 |
* |
| 20 |
* @since 2.5.5 |
| 21 |
* |
| 22 |
* @var bool|ConvertKit_Resource_Landing_Pages |
| 23 |
*/ |
| 24 |
public $landing_pages = false; |
| 25 |
|
| 26 |
/** |
| 27 |
* Holds the result of creating a WordPress Page. |
| 28 |
* |
| 29 |
* @since 2.5.5 |
| 30 |
* |
| 31 |
* @var int|WP_Error |
| 32 |
*/ |
| 33 |
public $result; |
| 34 |
|
| 35 |
/** |
| 36 |
* Holds the URL to the current setup wizard screen. |
| 37 |
* |
| 38 |
* @since 2.5.5 |
| 39 |
* |
| 40 |
* @var bool|string |
| 41 |
*/ |
| 42 |
public $current_url = false; |
| 43 |
|
| 44 |
/** |
| 45 |
* The required user capability to access the setup wizard. |
| 46 |
* |
| 47 |
* @since 2.5.5 |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public $required_capability = 'edit_posts'; |
| 52 |
|
| 53 |
/** |
| 54 |
* The programmatic name for this wizard. |
| 55 |
* |
| 56 |
* @since 2.5.5 |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
public $page_name = 'convertkit-landing-page-setup'; |
| 61 |
|
| 62 |
/** |
| 63 |
* The URL to take the user to when they click the Exit link. |
| 64 |
* |
| 65 |
* @since 2.5.5 |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
public $exit_url = 'edit.php?post_type=page'; |
| 70 |
|
| 71 |
/** |
| 72 |
* Registers action and filter hooks. |
| 73 |
* |
| 74 |
* @since 2.5.5 |
| 75 |
*/ |
| 76 |
public function __construct() { |
| 77 |
|
| 78 |
// Define the steps for the setup wizard. |
| 79 |
add_filter( 'convertkit_admin_setup_wizard_steps_convertkit-landing-page-setup', array( $this, 'define_steps' ) ); |
| 80 |
|
| 81 |
add_action( 'convertkit_admin_setup_wizard_process_form_convertkit-landing-page-setup', array( $this, 'process_form' ) ); |
| 82 |
add_action( 'convertkit_admin_setup_wizard_load_screen_data_convertkit-landing-page-setup', array( $this, 'load_screen_data' ) ); |
| 83 |
|
| 84 |
// Call parent class constructor. |
| 85 |
parent::__construct(); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Define the steps for the setup wizard. |
| 91 |
* |
| 92 |
* @since 3.1.8 |
| 93 |
* |
| 94 |
* @param array $steps The steps for the setup wizard. |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function define_steps( $steps ) { |
| 98 |
|
| 99 |
return array( |
| 100 |
'start' => array( |
| 101 |
'name' => __( 'Setup', 'convertkit' ), |
| 102 |
'next_button' => array( |
| 103 |
'label' => __( 'Create', 'convertkit' ), |
| 104 |
), |
| 105 |
), |
| 106 |
'finish' => array( |
| 107 |
'name' => __( 'Done', 'convertkit' ), |
| 108 |
), |
| 109 |
); |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Process posted data from the submitted form. |
| 115 |
* |
| 116 |
* @since 2.5.5 |
| 117 |
* |
| 118 |
* @param string $step Current step. |
| 119 |
*/ |
| 120 |
public function process_form( $step ) { |
| 121 |
|
| 122 |
// Set and authorize the Post Type before processing data. |
| 123 |
$this->set_post_type(); |
| 124 |
if ( ! convertkit_user_can_create_published_post_type( $this->post_type ) ) { |
| 125 |
$this->error = __( 'You are not allowed to create and publish this type of content.', 'convertkit' ); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// Run security checks. |
| 130 |
if ( ! isset( $_REQUEST['_wpnonce'] ) ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $this->page_name ) ) { |
| 134 |
$this->error = __( 'Invalid nonce specified.', 'convertkit' ); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
// Don't process form data if we're not on the second step. |
| 139 |
if ( $step !== 'finish' ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// Sanitize configuration. |
| 144 |
$configuration = array( |
| 145 |
'landing_page' => ( isset( $_POST['landing_page'] ) ? sanitize_text_field( wp_unslash( $_POST['landing_page'] ) ) : '' ), |
| 146 |
'post_name' => ( isset( $_POST['post_name'] ) ? sanitize_text_field( wp_unslash( $_POST['post_name'] ) ) : '' ), |
| 147 |
'post_type' => $this->post_type, |
| 148 |
); |
| 149 |
|
| 150 |
// Create Page. |
| 151 |
$this->result = $this->create_page_displaying_landing_page( |
| 152 |
$configuration['post_name'], |
| 153 |
$configuration['landing_page'], |
| 154 |
$configuration['post_type'] |
| 155 |
); |
| 156 |
|
| 157 |
// If an error occured creating the Page, go back a step to show the error. |
| 158 |
if ( is_wp_error( $this->result ) ) { |
| 159 |
$this->step = $this->get_step_key_by_number( $this->get_current_step_number() - 1 ); |
| 160 |
$this->error = $this->result->get_error_message(); |
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Load any data into class variables for the given setup wizard name and current step. |
| 167 |
* |
| 168 |
* @since 2.5.5 |
| 169 |
* |
| 170 |
* @param string $step Current step. |
| 171 |
*/ |
| 172 |
public function load_screen_data( $step ) { |
| 173 |
|
| 174 |
// Show an error screen if API credentials have not been specified. |
| 175 |
// This shouldn't happen, because the 'Add New Member Content' button is only displayed |
| 176 |
// if valid credentials have been specified. |
| 177 |
$settings = new ConvertKit_Settings(); |
| 178 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 179 |
wp_die( esc_html__( 'Connect your Kit account in the Kit Plugin\'s settings to get started', 'convertkit' ) ); |
| 180 |
} |
| 181 |
|
| 182 |
// Set and authorize the Post Type. |
| 183 |
$this->set_post_type(); |
| 184 |
if ( ! convertkit_user_can_create_published_post_type( $this->post_type ) ) { |
| 185 |
wp_die( esc_html__( 'Sorry, you are not allowed to create and publish this type of content.', 'convertkit' ) ); |
| 186 |
} |
| 187 |
|
| 188 |
// Define Exit URL to take the user back to the WP_List_Table for the Post Type they were viewing. |
| 189 |
$this->exit_url = add_query_arg( |
| 190 |
array( |
| 191 |
'post_type' => $this->post_type, |
| 192 |
), |
| 193 |
admin_url( 'edit.php' ) |
| 194 |
); |
| 195 |
|
| 196 |
// Don't load data if not on the first step. |
| 197 |
if ( $step !== 'start' ) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
// Fetch Landing Pages. |
| 202 |
$this->landing_pages = new ConvertKit_Resource_Landing_Pages( 'landing_page_wizard' ); |
| 203 |
|
| 204 |
// Refresh Landing Page resources, in case the user just created their first Landing Page in Kit. |
| 205 |
$result = $this->landing_pages->refresh(); |
| 206 |
|
| 207 |
// Bail if an error occured. |
| 208 |
if ( is_wp_error( $result ) ) { |
| 209 |
// Change the next button label and make it a link to reload the screen. |
| 210 |
unset( $this->steps['start']['next_button'] ); |
| 211 |
$this->current_url = add_query_arg( |
| 212 |
array( |
| 213 |
'page' => $this->page_name, |
| 214 |
'ck_post_type' => $this->post_type, |
| 215 |
'step' => 'start', |
| 216 |
), |
| 217 |
admin_url( 'options.php' ) |
| 218 |
); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
// If no Landing Pages exist in ConvertKit, change the next button label and make it a link to reload |
| 223 |
// the screen. |
| 224 |
if ( ! $this->landing_pages->exist() ) { |
| 225 |
unset( $this->steps['start']['next_button'] ); |
| 226 |
$this->current_url = add_query_arg( |
| 227 |
array( |
| 228 |
'page' => $this->page_name, |
| 229 |
'ck_post_type' => $this->post_type, |
| 230 |
'step' => 'start', |
| 231 |
), |
| 232 |
admin_url( 'options.php' ) |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Creates a WordPress Page for the given title, set to display the given landing page. |
| 240 |
* |
| 241 |
* @since 2.5.5 |
| 242 |
* |
| 243 |
* @param string $post_name Post Name / Permalink. |
| 244 |
* @param string $landing_page Landing Page ID or URL. |
| 245 |
* @param string $post_type Post Type. |
| 246 |
* @return WP_Error|int Error or Page ID |
| 247 |
*/ |
| 248 |
private function create_page_displaying_landing_page( $post_name, $landing_page, $post_type ) { |
| 249 |
|
| 250 |
// Create Page. |
| 251 |
$page_id = wp_insert_post( |
| 252 |
array( |
| 253 |
'post_type' => $post_type, |
| 254 |
'post_name' => sanitize_title_with_dashes( $post_name ), |
| 255 |
'post_title' => $post_name, |
| 256 |
'post_status' => 'publish', |
| 257 |
'post_author' => get_current_user_id(), |
| 258 |
), |
| 259 |
true |
| 260 |
); |
| 261 |
|
| 262 |
// Bail if an error occured. |
| 263 |
if ( is_wp_error( $page_id ) ) { |
| 264 |
return $page_id; |
| 265 |
} |
| 266 |
|
| 267 |
// Define Page's settings. |
| 268 |
WP_ConvertKit()->get_class( 'admin_post' )->save_post_settings( |
| 269 |
$page_id, |
| 270 |
array( |
| 271 |
'form' => '0', // Don't display a Form. |
| 272 |
'landing_page' => $landing_page, |
| 273 |
) |
| 274 |
); |
| 275 |
|
| 276 |
// Return. |
| 277 |
return $page_id; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|