| @@ -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,36 +253,103 @@ | ||
| 173 | 253 | |
| 174 | 254 | } |
| 175 | 255 | |
| 176 | 256 | /** |
| 177 | - * Redirects to the settings screen, with an option success or error message. | |
| 257 | + * Redirects to the settings screen. | |
| 178 | 258 | * |
| 179 | 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. | |
| 180 | 278 | * |
| 181 | - * @param false|string $error The error message key. | |
| 182 | - * @param false|string $success The success message key. | |
| 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`. | |
| 183 | 285 | */ |
| 184 | - public function redirect( $error = false, $success = false ) { | |
| 286 | + public function redirect_with_error_notice( $error ) { | |
| 185 | 287 | |
| 186 | - // Build URL to redirect to, depending on whether a message is included. | |
| 187 | - $args = array( | |
| 188 | - 'page' => '_wp_convertkit_settings', | |
| 189 | - 'tab' => $this->name, | |
| 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 | + ) | |
| 190 | 297 | ); |
| 191 | - if ( $error !== false ) { | |
| 192 | - $args['error'] = $error; | |
| 193 | - } | |
| 194 | - if ( $success !== false ) { | |
| 195 | - $args['success'] = $success; | |
| 196 | - } | |
| 298 | + exit(); | |
| 197 | 299 | |
| 198 | - // Redirect. | |
| 199 | - wp_safe_redirect( add_query_arg( $args, 'options-general.php' ) ); | |
| 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 | + ); | |
| 200 | 321 | exit(); |
| 201 | 322 | |
| 202 | 323 | } |
| 203 | 324 | |
| 204 | 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 | + /** | |
| 205 | 352 | * Outputs the given success message in an inline notice. |
| 206 | 353 | * |
| 207 | 354 | * @since 2.0.0 |
| 208 | 355 | * |
| @@ -289,8 +436,34 @@ | ||
| 289 | 436 | |
| 290 | 437 | } |
| 291 | 438 | |
| 292 | 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 | + /** | |
| 293 | 466 | * Returns a date field. |
| 294 | 467 | * |
| 295 | 468 | * @since 2.2.8 |
| 296 | 469 | * |
| @@ -434,9 +607,9 @@ | ||
| 434 | 607 | return '<p class="description">' . $description . '</p>'; |
| 435 | 608 | } |
| 436 | 609 | |
| 437 | 610 | // Return description lines in a paragraph, using breaklines for each description entry in the array. |
| 438 | - return '<p class="description">' . implode( '<br />', $description ); | |
| 611 | + return '<p class="description">' . implode( '<br />', $description ) . '</p>'; | |
| 439 | 612 | |
| 440 | 613 | } |
| 441 | 614 | |
| 442 | 615 | /** |
| @@ -467,19 +640,10 @@ | ||
| 467 | 640 | * @return array Sanitized Settings with Defaults |
| 468 | 641 | */ |
| 469 | 642 | public function sanitize_settings( $settings ) { |
| 470 | 643 | |
| 471 | - // If a Form or Landing Page was specified, request a review. | |
| 472 | - // This can safely be called multiple times, as the review request | |
| 473 | - // class will ensure once a review request is dismissed by the user, | |
| 474 | - // it is never displayed again. | |
| 475 | - if ( ( isset( $settings['page_form'] ) && $settings['page_form'] ) || | |
| 476 | - ( isset( $settings['post_form'] ) && $settings['post_form'] ) ) { | |
| 477 | - WP_ConvertKit()->get_class( 'review_request' )->request_review(); | |
| 478 | - } | |
| 479 | - | |
| 480 | 644 | // Merge settings with defaults. |
| 481 | - $settings = wp_parse_args( $settings, $this->settings->get_defaults() ); | |
| 645 | + $updated_settings = wp_parse_args( $settings, $this->settings->get_defaults() ); | |
| 482 | 646 | |
| 483 | 647 | /** |
| 484 | 648 | * Performs actions prior to settings being saved. |
| 485 | 649 | * |
| @@ -484,12 +648,12 @@ | ||
| 484 | 648 | * Performs actions prior to settings being saved. |
| 485 | 649 | * |
| 486 | 650 | * @since 2.2.8 |
| 487 | 651 | */ |
| 488 | - do_action( 'convertkit_settings_base_sanitize_settings', $this->name, $settings ); | |
| 652 | + do_action( 'convertkit_settings_base_sanitize_settings', $this->name, $updated_settings ); | |
| 489 | 653 | |
| 490 | 654 | // Return settings to be saved. |
| 491 | - return $settings; | |
| 655 | + return $updated_settings; | |
| 492 | 656 | |
| 493 | 657 | } |
| 494 | 658 | |
| 495 | 659 | } |