| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | * Holds the settings class for the section. |
| 47 | 47 | * |
| 48 | 48 | * @since 1.9.6 |
| 49 | 49 | * |
| 50 | - * @var false|ConvertKit_Settings|ConvertKit_ContactForm7_Settings|ConvertKit_Wishlist_Settings|ConvertKit_Settings_Restrict_Content | |
| 50 | + * @var false|ConvertKit_Settings|ConvertKit_ContactForm7_Settings|ConvertKit_Wishlist_Settings|ConvertKit_Settings_Restrict_Content|ConvertKit_Settings_Broadcasts|ConvertKit_Forminator_Settings | |
| 51 | 51 | */ |
| 52 | 52 | public $settings; |
| 53 | 53 | |
| 54 | 54 | /** |
| @@ -60,8 +60,18 @@ | ||
| 60 | 60 | */ |
| 61 | 61 | public $is_beta = false; |
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | + * Holds whether the save button should be disabled e.g. there are no | |
| 65 | + * settings on screen to save. | |
| 66 | + * | |
| 67 | + * @since 2.4.9 | |
| 68 | + * | |
| 69 | + * @var bool | |
| 70 | + */ | |
| 71 | + public $save_disabled = false; | |
| 72 | + | |
| 73 | + /** | |
| 64 | 74 | * Constructor |
| 65 | 75 | */ |
| 66 | 76 | public function __construct() { |
| 67 | 77 | |
| @@ -75,8 +85,39 @@ | ||
| 75 | 85 | |
| 76 | 86 | } |
| 77 | 87 | |
| 78 | 88 | /** |
| 89 | + * Helper method to determine if we're viewing the current settings screen. | |
| 90 | + * | |
| 91 | + * @since 2.5.0 | |
| 92 | + * | |
| 93 | + * @param string $tab Current settings tab (general|tools|restrict-content|broadcasts). | |
| 94 | + * @return bool | |
| 95 | + */ | |
| 96 | + public function on_settings_screen( $tab ) { | |
| 97 | + | |
| 98 | + // phpcs:disable WordPress.Security.NonceVerification | |
| 99 | + | |
| 100 | + // Bail if we're not on the settings screen. | |
| 101 | + if ( ! array_key_exists( 'page', $_REQUEST ) ) { | |
| 102 | + return false; | |
| 103 | + } | |
| 104 | + if ( sanitize_text_field( $_REQUEST['page'] ) !== '_wp_convertkit_settings' ) { | |
| 105 | + return false; | |
| 106 | + } | |
| 107 | + | |
| 108 | + // Define current settings tab. | |
| 109 | + // General screen won't always be loaded with a `tab` parameter. | |
| 110 | + $current_tab = ( array_key_exists( 'tab', $_REQUEST ) ? sanitize_text_field( $_REQUEST['tab'] ) : 'general' ); | |
| 111 | + | |
| 112 | + // Return whether the request is for the current settings tab. | |
| 113 | + return ( $current_tab === $tab ); | |
| 114 | + | |
| 115 | + // phpcs:enable | |
| 116 | + | |
| 117 | + } | |
| 118 | + | |
| 119 | + /** | |
| 79 | 120 | * Register settings section. |
| 80 | 121 | */ |
| 81 | 122 | public function register_section() { |
| 82 | 123 | |
| @@ -112,8 +153,45 @@ | ||
| 112 | 153 | */ |
| 113 | 154 | abstract public function documentation_url(); |
| 114 | 155 | |
| 115 | 156 | /** |
| 157 | + * Outputs success and/or error notices if required. | |
| 158 | + * | |
| 159 | + * @since 2.0.0 | |
| 160 | + */ | |
| 161 | + public function maybe_output_notices() { | |
| 162 | + | |
| 163 | + // Define notices that might be displayed as a notification. | |
| 164 | + $notices = array(); | |
| 165 | + | |
| 166 | + /** | |
| 167 | + * Register success and error notices for settings screens. | |
| 168 | + * | |
| 169 | + * @since 2.5.1 | |
| 170 | + * | |
| 171 | + * @param array $notices Regsitered success and error notices. | |
| 172 | + * @return array | |
| 173 | + */ | |
| 174 | + $notices = apply_filters( 'convertkit_settings_base_register_notices', $notices ); | |
| 175 | + | |
| 176 | + // Output the verbose error description if supplied (e.g. OAuth). | |
| 177 | + if ( isset( $_REQUEST['error_description'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 178 | + $this->output_error( sanitize_text_field( $_REQUEST['error_description'] ) ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 179 | + } | |
| 180 | + | |
| 181 | + // Output error notification if defined. | |
| 182 | + if ( isset( $_REQUEST['error'] ) && array_key_exists( sanitize_text_field( $_REQUEST['error'] ), $notices ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 183 | + $this->output_error( $notices[ sanitize_text_field( $_REQUEST['error'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 184 | + } | |
| 185 | + | |
| 186 | + // Output success notification if defined. | |
| 187 | + if ( isset( $_REQUEST['success'] ) && array_key_exists( sanitize_text_field( $_REQUEST['success'] ), $notices ) ) { // phpcs:ignore WordPress.Security.NonceVerification | |
| 188 | + $this->output_success( $notices[ sanitize_text_field( $_REQUEST['success'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification | |
| 189 | + } | |
| 190 | + | |
| 191 | + } | |
| 192 | + | |
| 193 | + /** | |
| 116 | 194 | * Renders the section |
| 117 | 195 | */ |
| 118 | 196 | public function render() { |
| 119 | 197 | |
| @@ -129,9 +207,11 @@ | ||
| 129 | 207 | do_settings_sections( $this->settings_key ); |
| 130 | 208 | |
| 131 | 209 | settings_fields( $this->settings_key ); |
| 132 | 210 | |
| 133 | - submit_button(); | |
| 211 | + if ( ! $this->save_disabled ) { | |
| 212 | + submit_button(); | |
| 213 | + } | |
| 134 | 214 | |
| 135 | 215 | $this->render_container_end(); |
| 136 | 216 | |
| 137 | 217 | /** |
| @@ -173,8 +253,103 @@ | ||
| 173 | 253 | |
| 174 | 254 | } |
| 175 | 255 | |
| 176 | 256 | /** |
| 257 | + * Redirects to the settings screen. | |
| 258 | + * | |
| 259 | + * @since 2.2.9 | |
| 260 | + */ | |
| 261 | + public function redirect() { | |
| 262 | + | |
| 263 | + wp_safe_redirect( | |
| 264 | + add_query_arg( | |
| 265 | + array( | |
| 266 | + 'page' => '_wp_convertkit_settings', | |
| 267 | + 'tab' => $this->name, | |
| 268 | + ), | |
| 269 | + 'options-general.php' | |
| 270 | + ) | |
| 271 | + ); | |
| 272 | + exit(); | |
| 273 | + | |
| 274 | + } | |
| 275 | + | |
| 276 | + /** | |
| 277 | + * Redirects to the settings screen with an error notice key. | |
| 278 | + * | |
| 279 | + * The function maybe_output_notices() will then output the translated error notice | |
| 280 | + * based on the supplied key. | |
| 281 | + * | |
| 282 | + * @since 2.5.1 | |
| 283 | + * | |
| 284 | + * @param string $error The error notice key, registered using `convertkit_settings_base_register_notices`. | |
| 285 | + */ | |
| 286 | + public function redirect_with_error_notice( $error ) { | |
| 287 | + | |
| 288 | + wp_safe_redirect( | |
| 289 | + add_query_arg( | |
| 290 | + array( | |
| 291 | + 'page' => '_wp_convertkit_settings', | |
| 292 | + 'tab' => $this->name, | |
| 293 | + 'error' => $error, | |
| 294 | + ), | |
| 295 | + 'options-general.php' | |
| 296 | + ) | |
| 297 | + ); | |
| 298 | + exit(); | |
| 299 | + | |
| 300 | + } | |
| 301 | + | |
| 302 | + /** | |
| 303 | + * Redirects to the settings screen with the verbose error description. | |
| 304 | + * | |
| 305 | + * @since 2.5.1 | |
| 306 | + * | |
| 307 | + * @param string $error_description The error description. | |
| 308 | + */ | |
| 309 | + public function redirect_with_error_description( $error_description ) { | |
| 310 | + | |
| 311 | + wp_safe_redirect( | |
| 312 | + add_query_arg( | |
| 313 | + array( | |
| 314 | + 'page' => '_wp_convertkit_settings', | |
| 315 | + 'tab' => $this->name, | |
| 316 | + 'error_description' => $error_description, | |
| 317 | + ), | |
| 318 | + 'options-general.php' | |
| 319 | + ) | |
| 320 | + ); | |
| 321 | + exit(); | |
| 322 | + | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** | |
| 326 | + * Redirects to the settings screen with a success notice key. | |
| 327 | + * | |
| 328 | + * The function maybe_output_notices() will then output the translated success notice | |
| 329 | + * based on the supplied key. | |
| 330 | + * | |
| 331 | + * @since 2.5.1 | |
| 332 | + * | |
| 333 | + * @param string $success The success notice key, registered using `convertkit_settings_base_register_notices`. | |
| 334 | + */ | |
| 335 | + public function redirect_with_success_notice( $success ) { | |
| 336 | + | |
| 337 | + wp_safe_redirect( | |
| 338 | + add_query_arg( | |
| 339 | + array( | |
| 340 | + 'page' => '_wp_convertkit_settings', | |
| 341 | + 'tab' => $this->name, | |
| 342 | + 'success' => $success, | |
| 343 | + ), | |
| 344 | + 'options-general.php' | |
| 345 | + ) | |
| 346 | + ); | |
| 347 | + exit(); | |
| 348 | + | |
| 349 | + } | |
| 350 | + | |
| 351 | + /** | |
| 177 | 352 | * Outputs the given success message in an inline notice. |
| 178 | 353 | * |
| 179 | 354 | * @since 2.0.0 |
| 180 | 355 | * |
| @@ -261,8 +436,60 @@ | ||
| 261 | 436 | |
| 262 | 437 | } |
| 263 | 438 | |
| 264 | 439 | /** |
| 440 | + * Returns a textarea field. | |
| 441 | + * | |
| 442 | + * @since 2.3.5 | |
| 443 | + * | |
| 444 | + * @param string $name Name. | |
| 445 | + * @param string $value Value. | |
| 446 | + * @param bool|string|array $description Description (false|string|array). | |
| 447 | + * @param bool|array $css_classes CSS Classes (false|array). | |
| 448 | + * @return string HTML Field | |
| 449 | + */ | |
| 450 | + public function get_textarea_field( $name, $value = '', $description = false, $css_classes = false ) { | |
| 451 | + | |
| 452 | + $html = sprintf( | |
| 453 | + '<textarea class="%s" id="%s" name="%s[%s]">%s</textarea>', | |
| 454 | + ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ), | |
| 455 | + $name, | |
| 456 | + $this->settings_key, | |
| 457 | + $name, | |
| 458 | + $value | |
| 459 | + ); | |
| 460 | + | |
| 461 | + return $html . $this->get_description( $description ); | |
| 462 | + | |
| 463 | + } | |
| 464 | + | |
| 465 | + /** | |
| 466 | + * Returns a date field. | |
| 467 | + * | |
| 468 | + * @since 2.2.8 | |
| 469 | + * | |
| 470 | + * @param string $name Name. | |
| 471 | + * @param string $value Value. | |
| 472 | + * @param bool|string|array $description Description (false|string|array). | |
| 473 | + * @param bool|array $css_classes CSS Classes (false|array). | |
| 474 | + * @return string HTML Field | |
| 475 | + */ | |
| 476 | + public function get_date_field( $name, $value = '', $description = false, $css_classes = false ) { | |
| 477 | + | |
| 478 | + $html = sprintf( | |
| 479 | + '<input type="date" class="%s" id="%s" name="%s[%s]" value="%s" />', | |
| 480 | + ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ), | |
| 481 | + $name, | |
| 482 | + $this->settings_key, | |
| 483 | + $name, | |
| 484 | + $value | |
| 485 | + ); | |
| 486 | + | |
| 487 | + return $html . $this->get_description( $description ); | |
| 488 | + | |
| 489 | + } | |
| 490 | + | |
| 491 | + /** | |
| 265 | 492 | * Returns a select dropdown field. |
| 266 | 493 | * |
| 267 | 494 | * @since 1.9.6 |
| 268 | 495 | * |
| @@ -318,11 +545,12 @@ | ||
| 318 | 545 | * @param string $value Value. |
| 319 | 546 | * @param bool $checked Should checkbox be checked/ticked. |
| 320 | 547 | * @param bool|string $label Label. |
| 321 | 548 | * @param bool|string|array $description Description. |
| 549 | + * @param bool|array $css_classes CSS class(es). | |
| 322 | 550 | * @return string HTML Checkbox |
| 323 | 551 | */ |
| 324 | - public function get_checkbox_field( $name, $value, $checked = false, $label = '', $description = '' ) { | |
| 552 | + public function get_checkbox_field( $name, $value, $checked = false, $label = '', $description = false, $css_classes = false ) { | |
| 325 | 553 | |
| 326 | 554 | $html = ''; |
| 327 | 555 | |
| 328 | 556 | if ( $label ) { |
| @@ -332,12 +560,13 @@ | ||
| 332 | 560 | ); |
| 333 | 561 | } |
| 334 | 562 | |
| 335 | 563 | $html .= sprintf( |
| 336 | - '<input type="checkbox" id="%s" name="%s[%s]" value="%s" %s />', | |
| 564 | + '<input type="checkbox" id="%s" name="%s[%s]" class="%s" value="%s" %s />', | |
| 337 | 565 | $name, |
| 338 | 566 | $this->settings_key, |
| 339 | 567 | $name, |
| 568 | + ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ), | |
| 340 | 569 | $value, |
| 341 | 570 | ( $checked ? ' checked' : '' ) |
| 342 | 571 | ); |
| 343 | 572 | |
| @@ -347,8 +576,14 @@ | ||
| 347 | 576 | $label |
| 348 | 577 | ); |
| 349 | 578 | } |
| 350 | 579 | |
| 580 | + // If no description exists, just return the field. | |
| 581 | + if ( empty( $description ) ) { | |
| 582 | + return $html; | |
| 583 | + } | |
| 584 | + | |
| 585 | + // Return field with description appended to it. | |
| 351 | 586 | return $html . $this->get_description( $description ); |
| 352 | 587 | |
| 353 | 588 | } |
| 354 | 589 | |
| @@ -359,9 +594,9 @@ | ||
| 359 | 594 | * |
| 360 | 595 | * @param bool|string|array $description Description. |
| 361 | 596 | * @return string HTML Description |
| 362 | 597 | */ |
| 363 | - private function get_description( $description ) { | |
| 598 | + public function get_description( $description ) { | |
| 364 | 599 | |
| 365 | 600 | // Return blank string if no description specified. |
| 366 | 601 | if ( ! $description ) { |
| 367 | 602 | return ''; |
| @@ -372,9 +607,9 @@ | ||
| 372 | 607 | return '<p class="description">' . $description . '</p>'; |
| 373 | 608 | } |
| 374 | 609 | |
| 375 | 610 | // Return description lines in a paragraph, using breaklines for each description entry in the array. |
| 376 | - return '<p class="description">' . implode( '<br />', $description ); | |
| 611 | + return '<p class="description">' . implode( '<br />', $description ) . '</p>'; | |
| 377 | 612 | |
| 378 | 613 | } |
| 379 | 614 | |
| 380 | 615 | /** |
| @@ -381,15 +616,15 @@ | ||
| 381 | 616 | * Converts the given key/value array pairs into a HTML attribute="value" string. |
| 382 | 617 | * |
| 383 | 618 | * @since 1.9.8.5 |
| 384 | 619 | * |
| 385 | - * @param array $array Attributes. | |
| 386 | - * @return string HTML attributes string | |
| 620 | + * @param array $attributes_array Attributes. | |
| 621 | + * @return string HTML attributes string | |
| 387 | 622 | */ |
| 388 | - private function array_to_attributes( $array ) { | |
| 623 | + private function array_to_attributes( $attributes_array ) { | |
| 389 | 624 | |
| 390 | 625 | $attributes = ''; |
| 391 | - foreach ( $array as $key => $value ) { | |
| 626 | + foreach ( $attributes_array as $key => $value ) { | |
| 392 | 627 | $attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" '; |
| 393 | 628 | } |
| 394 | 629 | |
| 395 | 630 | return trim( $attributes ); |
| @@ -405,18 +640,20 @@ | ||
| 405 | 640 | * @return array Sanitized Settings with Defaults |
| 406 | 641 | */ |
| 407 | 642 | public function sanitize_settings( $settings ) { |
| 408 | 643 | |
| 409 | - // If a Form or Landing Page was specified, request a review. | |
| 410 | - // This can safely be called multiple times, as the review request | |
| 411 | - // class will ensure once a review request is dismissed by the user, | |
| 412 | - // it is never displayed again. | |
| 413 | - if ( ( isset( $settings['page_form'] ) && $settings['page_form'] ) || | |
| 414 | - ( isset( $settings['post_form'] ) && $settings['post_form'] ) ) { | |
| 415 | - WP_ConvertKit()->get_class( 'review_request' )->request_review(); | |
| 416 | - } | |
| 644 | + // Merge settings with defaults. | |
| 645 | + $updated_settings = wp_parse_args( $settings, $this->settings->get_defaults() ); | |
| 417 | 646 | |
| 418 | - return wp_parse_args( $settings, $this->settings->get_defaults() ); | |
| 647 | + /** | |
| 648 | + * Performs actions prior to settings being saved. | |
| 649 | + * | |
| 650 | + * @since 2.2.8 | |
| 651 | + */ | |
| 652 | + do_action( 'convertkit_settings_base_sanitize_settings', $this->name, $updated_settings ); | |
| 653 | + | |
| 654 | + // Return settings to be saved. | |
| 655 | + return $updated_settings; | |
| 419 | 656 | |
| 420 | 657 | } |
| 421 | 658 | |
| 422 | 659 | } |