← All changes
|
admin/setup-wizard/class-convertkit-admin-setup-wizard-restrict-content.php
+184
-69
2.3.0
→
3.4.3
View file →
| @@ -14,33 +14,33 @@ | ||
| 14 | 14 | */ |
| 15 | 15 | class ConvertKit_Admin_Setup_Wizard_Restrict_Content extends ConvertKit_Admin_Setup_Wizard { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Holds the Post Type to generate Members Content for. | |
| 18 | + * Holds the type of Member's Content to generate (course|download). | |
| 19 | 19 | * |
| 20 | 20 | * @since 2.1.0 |
| 21 | 21 | * |
| 22 | 22 | * @var string |
| 23 | 23 | */ |
| 24 | - public $post_type = 'page'; | |
| 24 | + public $type = 'download'; | |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | - * Holds the type of Member's Content to generate (course|download). | |
| 27 | + * Holds the label for the type of Member's Content to generate. | |
| 28 | 28 | * |
| 29 | 29 | * @since 2.1.0 |
| 30 | 30 | * |
| 31 | 31 | * @var string |
| 32 | 32 | */ |
| 33 | - public $type = 'download'; | |
| 33 | + public $type_label = ''; | |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | - * Holds the label for the type of Member's Content to generate. | |
| 36 | + * Holds the ConvertKit Forms resource class. | |
| 37 | 37 | * |
| 38 | - * @since 2.1.0 | |
| 38 | + * @since 2.8.3 | |
| 39 | 39 | * |
| 40 | - * @var string | |
| 40 | + * @var bool|ConvertKit_Resource_Forms | |
| 41 | 41 | */ |
| 42 | - public $type_label = ''; | |
| 42 | + public $forms = false; | |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Holds the ConvertKit Products resource class. |
| 46 | 46 | * |
| @@ -50,8 +50,17 @@ | ||
| 50 | 50 | */ |
| 51 | 51 | public $products = false; |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | + * Holds the ConvertKit Tags resource class. | |
| 55 | + * | |
| 56 | + * @since 2.3.3 | |
| 57 | + * | |
| 58 | + * @var bool|ConvertKit_Resource_Tags | |
| 59 | + */ | |
| 60 | + public $tags = false; | |
| 61 | + | |
| 62 | + /** | |
| 54 | 63 | * Holds the Pages created by this setup wizard. |
| 55 | 64 | * |
| 56 | 65 | * @since 2.1.0 |
| 57 | 66 | * |
| @@ -77,8 +86,17 @@ | ||
| 77 | 86 | */ |
| 78 | 87 | public $course_url = false; |
| 79 | 88 | |
| 80 | 89 | /** |
| 90 | + * Holds the URL to the current setup wizard screen. | |
| 91 | + * | |
| 92 | + * @since 2.3.3 | |
| 93 | + * | |
| 94 | + * @var bool|string | |
| 95 | + */ | |
| 96 | + public $current_url = false; | |
| 97 | + | |
| 98 | + /** | |
| 81 | 99 | * The required user capability to access the setup wizard. |
| 82 | 100 | * |
| 83 | 101 | * @since 2.1.0 |
| 84 | 102 | * |
| @@ -110,30 +128,44 @@ | ||
| 110 | 128 | * @since 2.1.0 |
| 111 | 129 | */ |
| 112 | 130 | public function __construct() { |
| 113 | 131 | |
| 114 | - // Define details for each step in the setup process. | |
| 115 | - $this->steps = array( | |
| 116 | - 1 => array( | |
| 132 | + // Define the steps for the setup wizard. | |
| 133 | + add_filter( 'convertkit_admin_setup_wizard_steps_convertkit-restrict-content-setup', array( $this, 'define_steps' ) ); | |
| 134 | + | |
| 135 | + add_action( 'convertkit_admin_setup_wizard_process_form_convertkit-restrict-content-setup', array( $this, 'process_form' ) ); | |
| 136 | + add_action( 'convertkit_admin_setup_wizard_load_screen_data_convertkit-restrict-content-setup', array( $this, 'load_screen_data' ) ); | |
| 137 | + | |
| 138 | + // Call parent class constructor. | |
| 139 | + parent::__construct(); | |
| 140 | + | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Define the steps for the setup wizard. | |
| 145 | + * | |
| 146 | + * @since 3.1.8 | |
| 147 | + * | |
| 148 | + * @param array $steps The steps for the setup wizard. | |
| 149 | + * @return array | |
| 150 | + */ | |
| 151 | + public function define_steps( $steps ) { | |
| 152 | + | |
| 153 | + return array( | |
| 154 | + 'start' => array( | |
| 117 | 155 | 'name' => __( 'Setup', 'convertkit' ), |
| 118 | 156 | ), |
| 119 | - 2 => array( | |
| 157 | + 'configuration' => array( | |
| 120 | 158 | 'name' => __( 'Configure', 'convertkit' ), |
| 121 | 159 | 'next_button' => array( |
| 122 | 160 | 'label' => __( 'Submit', 'convertkit' ), |
| 123 | 161 | ), |
| 124 | 162 | ), |
| 125 | - 3 => array( | |
| 163 | + 'finish' => array( | |
| 126 | 164 | 'name' => __( 'Done', 'convertkit' ), |
| 127 | 165 | ), |
| 128 | 166 | ); |
| 129 | 167 | |
| 130 | - add_action( 'convertkit_admin_setup_wizard_process_form_convertkit-restrict-content-setup', array( $this, 'process_form' ) ); | |
| 131 | - add_action( 'convertkit_admin_setup_wizard_load_screen_data_convertkit-restrict-content-setup', array( $this, 'load_screen_data' ) ); | |
| 132 | - | |
| 133 | - // Call parent class constructor. | |
| 134 | - parent::__construct(); | |
| 135 | - | |
| 136 | 168 | } |
| 137 | 169 | |
| 138 | 170 | /** |
| 139 | 171 | * Process posted data from the submitted form. |
| @@ -139,25 +171,38 @@ | ||
| 139 | 171 | * Process posted data from the submitted form. |
| 140 | 172 | * |
| 141 | 173 | * @since 2.1.0 |
| 142 | 174 | * |
| 143 | - * @param int $step Current step. | |
| 175 | + * @param string $step Current step. | |
| 144 | 176 | */ |
| 145 | 177 | public function process_form( $step ) { |
| 146 | 178 | |
| 147 | - // phpcs:disable WordPress.Security.NonceVerification.Missing | |
| 148 | - // Nonce verification has been performed in ConvertKit_Admin_Setup_Wizard:process_form(), prior to calling this function. | |
| 179 | + // Set and authorize the Post Type before processing data. | |
| 180 | + $this->set_post_type(); | |
| 181 | + if ( ! convertkit_user_can_create_published_post_type( $this->post_type ) ) { | |
| 182 | + $this->error = __( 'You are not allowed to create and publish this type of content.', 'convertkit' ); | |
| 183 | + return; | |
| 184 | + } | |
| 149 | 185 | |
| 186 | + // Run security checks. | |
| 187 | + if ( ! isset( $_REQUEST['_wpnonce'] ) ) { | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), $this->page_name ) ) { | |
| 191 | + $this->error = __( 'Invalid nonce specified.', 'convertkit' ); | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + | |
| 150 | 195 | // Depending on the step, process the form data. |
| 151 | 196 | switch ( $step ) { |
| 152 | - case 3: | |
| 197 | + case 'finish': | |
| 153 | 198 | // Sanitize configuration. |
| 154 | 199 | $configuration = array( |
| 155 | - 'type' => sanitize_text_field( stripslashes( $_POST['type'] ) ), | |
| 156 | - 'title' => sanitize_text_field( stripslashes( $_POST['title'] ) ), | |
| 157 | - 'description' => sanitize_textarea_field( stripslashes( $_POST['description'] ) ), | |
| 200 | + 'type' => ( isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : 'download' ), | |
| 201 | + 'title' => ( isset( $_POST['title'] ) ? sanitize_text_field( wp_unslash( $_POST['title'] ) ) : '' ), | |
| 202 | + 'description' => ( isset( $_POST['description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['description'] ) ) : '' ), | |
| 158 | 203 | 'number_of_pages' => ( isset( $_POST['number_of_pages'] ) ? absint( $_POST['number_of_pages'] ) : 0 ), |
| 159 | - 'restrict_content' => sanitize_text_field( stripslashes( $_POST['restrict_content'] ) ), | |
| 204 | + 'restrict_content' => ( isset( $_POST['restrict_content'] ) ? sanitize_text_field( wp_unslash( $_POST['restrict_content'] ) ) : '' ), | |
| 160 | 205 | 'post_type' => $this->post_type, |
| 161 | 206 | ); |
| 162 | 207 | |
| 163 | 208 | // Depending on the type of content selected, create WordPress Page(s) now. |
| @@ -181,9 +226,9 @@ | ||
| 181 | 226 | |
| 182 | 227 | // If here, an error occured as create_download() and create_course() perform a redirect on success. |
| 183 | 228 | // Show an error message if Account Details could not be fetched e.g. API credentials supplied are invalid. |
| 184 | 229 | // Decrement the step. |
| 185 | - $this->step = ( $this->step - 1 ); | |
| 230 | + $this->step = $this->get_step_key_by_number( $this->get_current_step_number() - 1 ); | |
| 186 | 231 | $this->error = $result->get_error_message(); |
| 187 | 232 | return; |
| 188 | 233 | |
| 189 | 234 | } |
| @@ -196,9 +241,9 @@ | ||
| 196 | 241 | * Load any data into class variables for the given setup wizard name and current step. |
| 197 | 242 | * |
| 198 | 243 | * @since 2.1.0 |
| 199 | 244 | * |
| 200 | - * @param int $step Current step. | |
| 245 | + * @param string $step Current step. | |
| 201 | 246 | */ |
| 202 | 247 | public function load_screen_data( $step ) { |
| 203 | 248 | |
| 204 | 249 | // Show an error screen if API credentials have not been specified. |
| @@ -204,26 +249,16 @@ | ||
| 204 | 249 | // Show an error screen if API credentials have not been specified. |
| 205 | 250 | // This shouldn't happen, because the 'Add New Member Content' button is only displayed |
| 206 | 251 | // if valid credentials have been specified. |
| 207 | 252 | $settings = new ConvertKit_Settings(); |
| 208 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 209 | - wp_die( esc_html__( 'Add a valid API Key and Secret in the ConvertKit Plugin\'s settings to get started', 'convertkit' ) ); | |
| 253 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 254 | + wp_die( esc_html__( 'Connect your ConvertKit account in the ConvertKit Plugin\'s settings to get started', 'convertkit' ) ); | |
| 210 | 255 | } |
| 211 | 256 | |
| 212 | - // Bail if the Post Type isn't supported. | |
| 213 | - $this->post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( $_REQUEST['post_type'] ) : 'page'; // phpcs:ignore WordPress.Security.NonceVerification | |
| 214 | - if ( ! in_array( $this->post_type, convertkit_get_supported_restrict_content_post_types(), true ) ) { | |
| 215 | - wp_die( | |
| 216 | - sprintf( | |
| 217 | - /* translators: Post Type */ | |
| 218 | - esc_html__( 'The post type `%s` is not supported for Member Content.', 'convertkit' ), | |
| 219 | - esc_html( $this->post_type ) | |
| 220 | - ), | |
| 221 | - esc_html__( 'WordPress Error', 'convertkit' ), | |
| 222 | - array( | |
| 223 | - 'back_link' => true, | |
| 224 | - ) | |
| 225 | - ); | |
| 257 | + // Set and authorize the Post Type. | |
| 258 | + $this->set_post_type(); | |
| 259 | + if ( ! convertkit_user_can_create_published_post_type( $this->post_type ) ) { | |
| 260 | + wp_die( esc_html__( 'Sorry, you are not allowed to create and publish this type of content.', 'convertkit' ) ); | |
| 226 | 261 | } |
| 227 | 262 | |
| 228 | 263 | // Define Exit URL to take the user back to the WP_List_Table for the Post Type they were viewing. |
| 229 | 264 | $this->exit_url = add_query_arg( |
| @@ -234,11 +269,108 @@ | ||
| 234 | 269 | ); |
| 235 | 270 | |
| 236 | 271 | // Load data depending on the current step. |
| 237 | 272 | switch ( $step ) { |
| 238 | - case 2: | |
| 273 | + case 'start': | |
| 274 | + // Fetch Forms, Products and Tags. | |
| 275 | + $this->forms = new ConvertKit_Resource_Forms( 'restrict_content_wizard' ); | |
| 276 | + $this->products = new ConvertKit_Resource_Products( 'restrict_content_wizard' ); | |
| 277 | + $this->tags = new ConvertKit_Resource_Tags( 'restrict_content_wizard' ); | |
| 278 | + | |
| 279 | + // Refresh Forms, Products and Tags resources, in case the user just created their first Form, Product or Tag | |
| 280 | + // in ConvertKit. | |
| 281 | + $result = $this->forms->refresh(); | |
| 282 | + | |
| 283 | + // Bail if an error occured. | |
| 284 | + if ( is_wp_error( $result ) ) { | |
| 285 | + // Change the next label and make it a link to reload the screen. | |
| 286 | + unset( $this->steps['start']['next_button'] ); | |
| 287 | + $this->current_url = add_query_arg( | |
| 288 | + array( | |
| 289 | + 'page' => $this->page_name, | |
| 290 | + 'ck_post_type' => $this->post_type, | |
| 291 | + 'step' => 'start', | |
| 292 | + ), | |
| 293 | + admin_url( 'options.php' ) | |
| 294 | + ); | |
| 295 | + return; | |
| 296 | + } | |
| 297 | + | |
| 298 | + // Refresh Products. | |
| 299 | + $result = $this->products->refresh(); | |
| 300 | + | |
| 301 | + // Bail if an error occured. | |
| 302 | + if ( is_wp_error( $result ) ) { | |
| 303 | + // Change the next label and make it a link to reload the screen. | |
| 304 | + unset( $this->steps['start']['next_button'] ); | |
| 305 | + $this->current_url = add_query_arg( | |
| 306 | + array( | |
| 307 | + 'page' => $this->page_name, | |
| 308 | + 'ck_post_type' => $this->post_type, | |
| 309 | + 'step' => 'start', | |
| 310 | + ), | |
| 311 | + admin_url( 'options.php' ) | |
| 312 | + ); | |
| 313 | + return; | |
| 314 | + } | |
| 315 | + | |
| 316 | + // Refresh Tags. | |
| 317 | + $result = $this->tags->refresh(); | |
| 318 | + | |
| 319 | + // Bail if an error occured. | |
| 320 | + if ( is_wp_error( $result ) ) { | |
| 321 | + // Change the next label and make it a link to reload the screen. | |
| 322 | + unset( $this->steps['start']['next_button'] ); | |
| 323 | + $this->current_url = add_query_arg( | |
| 324 | + array( | |
| 325 | + 'page' => $this->page_name, | |
| 326 | + 'ck_post_type' => $this->post_type, | |
| 327 | + 'step' => 'start', | |
| 328 | + ), | |
| 329 | + admin_url( 'options.php' ) | |
| 330 | + ); | |
| 331 | + return; | |
| 332 | + } | |
| 333 | + | |
| 334 | + // If no Forms, Products and Tags exist in ConvertKit, change the next button label and make it a link to reload | |
| 335 | + // the screen. | |
| 336 | + if ( ! $this->forms->exist() && ! $this->products->exist() && ! $this->tags->exist() ) { | |
| 337 | + unset( $this->steps['start']['next_button'] ); | |
| 338 | + $this->current_url = add_query_arg( | |
| 339 | + array( | |
| 340 | + 'page' => $this->page_name, | |
| 341 | + 'ck_post_type' => $this->post_type, | |
| 342 | + 'step' => 'start', | |
| 343 | + ), | |
| 344 | + admin_url( 'options.php' ) | |
| 345 | + ); | |
| 346 | + } else { | |
| 347 | + // Define Download and Course button links. | |
| 348 | + $this->download_url = add_query_arg( | |
| 349 | + array( | |
| 350 | + 'type' => 'download', | |
| 351 | + 'ck_post_type' => $this->post_type, | |
| 352 | + ), | |
| 353 | + $this->next_step_url | |
| 354 | + ); | |
| 355 | + | |
| 356 | + $this->course_url = add_query_arg( | |
| 357 | + array( | |
| 358 | + 'type' => 'course', | |
| 359 | + 'ck_post_type' => $this->post_type, | |
| 360 | + ), | |
| 361 | + $this->next_step_url | |
| 362 | + ); | |
| 363 | + } | |
| 364 | + break; | |
| 365 | + | |
| 366 | + case 'configuration': | |
| 239 | 367 | // Define Member Content Type. |
| 240 | - $this->type = sanitize_text_field( $_REQUEST['type'] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 368 | + if ( filter_has_var( INPUT_GET, 'type' ) ) { | |
| 369 | + $this->type = filter_input( INPUT_GET, 'type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); | |
| 370 | + } else { | |
| 371 | + $this->type = 'download'; | |
| 372 | + } | |
| 241 | 373 | |
| 242 | 374 | // Define Label for Title. |
| 243 | 375 | switch ( $this->type ) { |
| 244 | 376 | case 'download': |
| @@ -248,30 +380,13 @@ | ||
| 248 | 380 | $this->type_label = __( 'Course', 'convertkit' ); |
| 249 | 381 | break; |
| 250 | 382 | } |
| 251 | 383 | |
| 252 | - // Fetch Products. | |
| 384 | + // Fetch Forms, Products and Tags. | |
| 385 | + $this->forms = new ConvertKit_Resource_Forms( 'restrict_content_wizard' ); | |
| 253 | 386 | $this->products = new ConvertKit_Resource_Products( 'restrict_content_wizard' ); |
| 387 | + $this->tags = new ConvertKit_Resource_Tags( 'restrict_content_wizard' ); | |
| 254 | 388 | break; |
| 255 | - | |
| 256 | - case 1: | |
| 257 | - // Define Download and Course button links. | |
| 258 | - $this->download_url = add_query_arg( | |
| 259 | - array( | |
| 260 | - 'type' => 'download', | |
| 261 | - 'post_type' => $this->post_type, | |
| 262 | - ), | |
| 263 | - $this->next_step_url | |
| 264 | - ); | |
| 265 | - | |
| 266 | - $this->course_url = add_query_arg( | |
| 267 | - array( | |
| 268 | - 'type' => 'course', | |
| 269 | - 'post_type' => $this->post_type, | |
| 270 | - ), | |
| 271 | - $this->next_step_url | |
| 272 | - ); | |
| 273 | - break; | |
| 274 | 389 | } |
| 275 | 390 | |
| 276 | 391 | } |
| 277 | 392 | |
| @@ -290,9 +405,9 @@ | ||
| 290 | 405 | $page_id = $this->create_page( |
| 291 | 406 | $configuration['title'], |
| 292 | 407 | $configuration['description'], |
| 293 | 408 | $configuration['post_type'], |
| 294 | - __( 'The downloadable content (that is available when the visitor has paid for the ConvertKit product) goes here.', 'convertkit' ), | |
| 409 | + __( 'The downloadable member-only content goes here.', 'convertkit' ), | |
| 295 | 410 | $configuration['restrict_content'] |
| 296 | 411 | ); |
| 297 | 412 | |
| 298 | 413 | // Bail if an error occured. |
| @@ -357,9 +472,9 @@ | ||
| 357 | 472 | sprintf( |
| 358 | 473 | '%s %s %s', |
| 359 | 474 | esc_html__( 'Lesson', 'convertkit' ), |
| 360 | 475 | $i, |
| 361 | - esc_html__( 'content (that is available when the visitor has paid for the ConvertKit product) goes here.', 'convertkit' ) | |
| 476 | + esc_html__( 'member-only content goes here.', 'convertkit' ) | |
| 362 | 477 | ), |
| 363 | 478 | $configuration['restrict_content'], |
| 364 | 479 | $i, |
| 365 | 480 | $configuration['number_of_pages'], |