| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * ConvertKit Settings Base class. | |
| 3 | + * ConvertKit Settings class | |
| 4 | 4 | * |
| 5 | 5 | * @package ConvertKit |
| 6 | 6 | * @author ConvertKit |
| 7 | 7 | */ |
| @@ -6,16 +6,20 @@ | ||
| 6 | 6 | * @author ConvertKit |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * ConvertKit Settings class | |
| 11 | - * | |
| 12 | - * @package ConvertKit | |
| 13 | - * @author ConvertKit | |
| 10 | + * Class ConvertKit_Settings_Base | |
| 14 | 11 | */ |
| 15 | 12 | abstract class ConvertKit_Settings_Base { |
| 16 | 13 | |
| 17 | 14 | /** |
| 15 | + * Setting | |
| 16 | + * | |
| 17 | + * @var bool | |
| 18 | + */ | |
| 19 | + public $is_registerable = true; | |
| 20 | + | |
| 21 | + /** | |
| 18 | 22 | * Section name |
| 19 | 23 | * |
| 20 | 24 | * @var string |
| 21 | 25 | */ |
| @@ -35,9 +39,9 @@ | ||
| 35 | 39 | */ |
| 36 | 40 | public $tab_text; |
| 37 | 41 | |
| 38 | 42 | /** |
| 39 | - * Options table key | |
| 43 | + * Database key | |
| 40 | 44 | * |
| 41 | 45 | * @var string |
| 42 | 46 | */ |
| 43 | 47 | public $settings_key; |
| @@ -42,44 +46,43 @@ | ||
| 42 | 46 | */ |
| 43 | 47 | public $settings_key; |
| 44 | 48 | |
| 45 | 49 | /** |
| 46 | - * Holds the settings class for the section. | |
| 50 | + * API instance | |
| 47 | 51 | * |
| 48 | - * @since 1.9.6 | |
| 49 | - * | |
| 50 | - * @var false|ConvertKit_Settings|ConvertKit_ContactForm7_Settings|ConvertKit_Wishlist_Settings|ConvertKit_Settings_Restrict_Content|ConvertKit_Settings_Broadcasts|ConvertKit_Forminator_Settings | |
| 52 | + * @var ConvertKitAPI | |
| 51 | 53 | */ |
| 52 | - public $settings; | |
| 54 | + public $api; | |
| 53 | 55 | |
| 54 | 56 | /** |
| 55 | - * Holds whether this settings section is for beta functionality. | |
| 57 | + * Options array | |
| 56 | 58 | * |
| 57 | - * @since 2.1.0 | |
| 58 | - * | |
| 59 | - * @var bool | |
| 59 | + * @var mixed|void | |
| 60 | 60 | */ |
| 61 | - public $is_beta = false; | |
| 61 | + public $options; | |
| 62 | 62 | |
| 63 | 63 | /** |
| 64 | 64 | * Constructor |
| 65 | 65 | */ |
| 66 | 66 | public function __construct() { |
| 67 | + global $convertkit_settings; | |
| 67 | 68 | |
| 68 | - // If tab text is not defined, use the title for the tab's text. | |
| 69 | + $this->api = $convertkit_settings->api; | |
| 70 | + $this->options = get_option( $this->settings_key ); | |
| 69 | 71 | if ( empty( $this->tab_text ) ) { |
| 70 | 72 | $this->tab_text = $this->title; |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | - // Register the settings section. | |
| 74 | 75 | $this->register_section(); |
| 75 | - | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | - * Register settings section. | |
| 79 | + * Register settings section | |
| 80 | 80 | */ |
| 81 | 81 | public function register_section() { |
| 82 | + if ( false === get_option( $this->settings_key ) ) { | |
| 83 | + add_option( $this->settings_key ); | |
| 84 | + } | |
| 82 | 85 | |
| 83 | 86 | add_settings_section( |
| 84 | 87 | $this->name, |
| 85 | 88 | $this->title, |
| @@ -93,403 +96,25 @@ | ||
| 93 | 96 | $this->settings_key, |
| 94 | 97 | $this->settings_key, |
| 95 | 98 | array( $this, 'sanitize_settings' ) |
| 96 | 99 | ); |
| 97 | - | |
| 98 | 100 | } |
| 99 | 101 | |
| 100 | 102 | /** |
| 101 | - * Register fields for this section | |
| 102 | - */ | |
| 103 | - abstract public function register_fields(); | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * Prints help info for this section | |
| 107 | - */ | |
| 108 | - abstract public function print_section_info(); | |
| 109 | - | |
| 110 | - /** | |
| 111 | - * Returns the URL for the ConvertKit documentation for this setting section. | |
| 112 | - */ | |
| 113 | - abstract public function documentation_url(); | |
| 114 | - | |
| 115 | - /** | |
| 116 | 103 | * Renders the section |
| 117 | 104 | */ |
| 118 | 105 | public function render() { |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Performs actions prior to rendering the settings form. | |
| 122 | - * | |
| 123 | - * @since 1.9.6 | |
| 124 | - */ | |
| 125 | - do_action( 'convertkit_settings_base_render_before' ); | |
| 126 | - | |
| 127 | - $this->render_container_start(); | |
| 128 | - | |
| 129 | 106 | do_settings_sections( $this->settings_key ); |
| 130 | - | |
| 131 | 107 | settings_fields( $this->settings_key ); |
| 132 | - | |
| 133 | 108 | submit_button(); |
| 134 | - | |
| 135 | - $this->render_container_end(); | |
| 136 | - | |
| 137 | - /** | |
| 138 | - * Performs actions after rendering of the settings form. | |
| 139 | - * | |
| 140 | - * @since 1.9.6 | |
| 141 | - */ | |
| 142 | - do_action( 'convertkit_settings_base_render_after' ); | |
| 143 | - | |
| 144 | 109 | } |
| 145 | 110 | |
| 146 | 111 | /** |
| 147 | - * Outputs .metabox-holder and .postbox container div elements, | |
| 148 | - * used before beginning a setting screen's output. | |
| 149 | - * | |
| 150 | - * @since 2.0.0 | |
| 112 | + * Register settings fields | |
| 151 | 113 | */ |
| 152 | - public function render_container_start() { | |
| 114 | + abstract public function register_fields(); | |
| 153 | 115 | |
| 154 | - ?> | |
| 155 | - <div class="metabox-holder"> | |
| 156 | - <div class="postbox <?php echo sanitize_html_class( $this->is_beta ? 'convertkit-beta' : '' ); ?>"> | |
| 157 | - <?php | |
| 158 | - | |
| 159 | - } | |
| 160 | - | |
| 161 | 116 | /** |
| 162 | - * Outputs closing .metabox-holder and .postbox container div elements, | |
| 163 | - * used after finishing a setting screen's output. | |
| 164 | - * | |
| 165 | - * @since 2.0.0 | |
| 117 | + * Prints help info for this section | |
| 166 | 118 | */ |
| 167 | - public function render_container_end() { | |
| 168 | - | |
| 169 | - ?> | |
| 170 | - </div> | |
| 171 | - </div> | |
| 172 | - <?php | |
| 173 | - | |
| 174 | - } | |
| 175 | - | |
| 176 | - /** | |
| 177 | - * Redirects to the settings screen, with an option success or error message. | |
| 178 | - * | |
| 179 | - * @since 2.2.9 | |
| 180 | - * | |
| 181 | - * @param false|string $error The error message key. | |
| 182 | - * @param false|string $success The success message key. | |
| 183 | - */ | |
| 184 | - public function redirect( $error = false, $success = false ) { | |
| 185 | - | |
| 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, | |
| 190 | - ); | |
| 191 | - if ( $error !== false ) { | |
| 192 | - $args['error'] = $error; | |
| 193 | - } | |
| 194 | - if ( $success !== false ) { | |
| 195 | - $args['success'] = $success; | |
| 196 | - } | |
| 197 | - | |
| 198 | - // Redirect. | |
| 199 | - wp_safe_redirect( add_query_arg( $args, 'options-general.php' ) ); | |
| 200 | - exit(); | |
| 201 | - | |
| 202 | - } | |
| 203 | - | |
| 204 | - /** | |
| 205 | - * Outputs the given success message in an inline notice. | |
| 206 | - * | |
| 207 | - * @since 2.0.0 | |
| 208 | - * | |
| 209 | - * @param string $success_message Success Message. | |
| 210 | - */ | |
| 211 | - public function output_success( $success_message ) { | |
| 212 | - | |
| 213 | - ?> | |
| 214 | - <div class="notice notice-success is-dismissible"> | |
| 215 | - <p> | |
| 216 | - <?php echo esc_attr( $success_message ); ?> | |
| 217 | - </p> | |
| 218 | - </div> | |
| 219 | - <?php | |
| 220 | - | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * Outputs the given error message in an inline notice. | |
| 225 | - * | |
| 226 | - * @since 1.9.6 | |
| 227 | - * | |
| 228 | - * @param string $error_message Error Message. | |
| 229 | - */ | |
| 230 | - public function output_error( $error_message ) { | |
| 231 | - | |
| 232 | - ?> | |
| 233 | - <div class="notice notice-error is-dismissible"> | |
| 234 | - <p> | |
| 235 | - <?php echo esc_attr( $error_message ); ?> | |
| 236 | - </p> | |
| 237 | - </div> | |
| 238 | - <?php | |
| 239 | - | |
| 240 | - } | |
| 241 | - | |
| 242 | - /** | |
| 243 | - * Returns a masked value. | |
| 244 | - * | |
| 245 | - * @since 1.9.6 | |
| 246 | - * | |
| 247 | - * @param string $value Value. | |
| 248 | - * @param bool|string $description Description. | |
| 249 | - * @return string Masked Value | |
| 250 | - */ | |
| 251 | - public function get_masked_value( $value, $description = false ) { | |
| 252 | - | |
| 253 | - $html = sprintf( | |
| 254 | - '<code>%s</code>', | |
| 255 | - str_repeat( '*', strlen( $value ) - 4 ) . substr( $value, - 4 ) | |
| 256 | - ); | |
| 257 | - | |
| 258 | - if ( $description ) { | |
| 259 | - $html .= $this->get_description( $description ); | |
| 260 | - } | |
| 261 | - | |
| 262 | - return $html; | |
| 263 | - | |
| 264 | - } | |
| 265 | - | |
| 266 | - /** | |
| 267 | - * Returns a text field. | |
| 268 | - * | |
| 269 | - * @since 1.9.6 | |
| 270 | - * | |
| 271 | - * @param string $name Name. | |
| 272 | - * @param string $value Value. | |
| 273 | - * @param bool|string|array $description Description (false|string|array). | |
| 274 | - * @param bool|array $css_classes CSS Classes (false|array). | |
| 275 | - * @return string HTML Field | |
| 276 | - */ | |
| 277 | - public function get_text_field( $name, $value = '', $description = false, $css_classes = false ) { | |
| 278 | - | |
| 279 | - $html = sprintf( | |
| 280 | - '<input type="text" class="%s" id="%s" name="%s[%s]" value="%s" />', | |
| 281 | - ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ), | |
| 282 | - $name, | |
| 283 | - $this->settings_key, | |
| 284 | - $name, | |
| 285 | - $value | |
| 286 | - ); | |
| 287 | - | |
| 288 | - return $html . $this->get_description( $description ); | |
| 289 | - | |
| 290 | - } | |
| 291 | - | |
| 292 | - /** | |
| 293 | - * Returns a date field. | |
| 294 | - * | |
| 295 | - * @since 2.2.8 | |
| 296 | - * | |
| 297 | - * @param string $name Name. | |
| 298 | - * @param string $value Value. | |
| 299 | - * @param bool|string|array $description Description (false|string|array). | |
| 300 | - * @param bool|array $css_classes CSS Classes (false|array). | |
| 301 | - * @return string HTML Field | |
| 302 | - */ | |
| 303 | - public function get_date_field( $name, $value = '', $description = false, $css_classes = false ) { | |
| 304 | - | |
| 305 | - $html = sprintf( | |
| 306 | - '<input type="date" class="%s" id="%s" name="%s[%s]" value="%s" />', | |
| 307 | - ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ), | |
| 308 | - $name, | |
| 309 | - $this->settings_key, | |
| 310 | - $name, | |
| 311 | - $value | |
| 312 | - ); | |
| 313 | - | |
| 314 | - return $html . $this->get_description( $description ); | |
| 315 | - | |
| 316 | - } | |
| 317 | - | |
| 318 | - /** | |
| 319 | - * Returns a select dropdown field. | |
| 320 | - * | |
| 321 | - * @since 1.9.6 | |
| 322 | - * | |
| 323 | - * @param string $name Name. | |
| 324 | - * @param string $value Value. | |
| 325 | - * @param array $options Options / Choices. | |
| 326 | - * @param bool|string $description Description. | |
| 327 | - * @param bool|array $css_classes <select> CSS class(es). | |
| 328 | - * @param bool|array $attributes <select> attributes. | |
| 329 | - * @return string HTML Select Field | |
| 330 | - */ | |
| 331 | - public function get_select_field( $name, $value = '', $options = array(), $description = false, $css_classes = false, $attributes = false ) { | |
| 332 | - | |
| 333 | - // Build opening <select> tag. | |
| 334 | - $html = sprintf( | |
| 335 | - '<select id="%s" name="%s[%s]" class="%s" size="1" %s>', | |
| 336 | - $this->settings_key . '_' . $name, | |
| 337 | - $this->settings_key, | |
| 338 | - $name, | |
| 339 | - ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ), | |
| 340 | - ( is_array( $attributes ) ? $this->array_to_attributes( $attributes ) : '' ) | |
| 341 | - ); | |
| 342 | - | |
| 343 | - // Build <option> tags. | |
| 344 | - foreach ( $options as $option => $label ) { | |
| 345 | - $html .= sprintf( | |
| 346 | - '<option value="%s"%s>%s</option>', | |
| 347 | - $option, | |
| 348 | - selected( $value, $option, false ), | |
| 349 | - $label | |
| 350 | - ); | |
| 351 | - } | |
| 352 | - | |
| 353 | - // Close <select>. | |
| 354 | - $html .= '</select>'; | |
| 355 | - | |
| 356 | - // If no description exists, just return the select field. | |
| 357 | - if ( empty( $description ) ) { | |
| 358 | - return $html; | |
| 359 | - } | |
| 360 | - | |
| 361 | - // Return select field with description appended to it. | |
| 362 | - return $html . $this->get_description( $description ); | |
| 363 | - | |
| 364 | - } | |
| 365 | - | |
| 366 | - /** | |
| 367 | - * Returns a checkbox field. | |
| 368 | - * | |
| 369 | - * @since 1.9.6 | |
| 370 | - * | |
| 371 | - * @param string $name Name. | |
| 372 | - * @param string $value Value. | |
| 373 | - * @param bool $checked Should checkbox be checked/ticked. | |
| 374 | - * @param bool|string $label Label. | |
| 375 | - * @param bool|string|array $description Description. | |
| 376 | - * @param bool|array $css_classes CSS class(es). | |
| 377 | - * @return string HTML Checkbox | |
| 378 | - */ | |
| 379 | - public function get_checkbox_field( $name, $value, $checked = false, $label = '', $description = false, $css_classes = false ) { | |
| 380 | - | |
| 381 | - $html = ''; | |
| 382 | - | |
| 383 | - if ( $label ) { | |
| 384 | - $html .= sprintf( | |
| 385 | - '<label for="%s">', | |
| 386 | - $name | |
| 387 | - ); | |
| 388 | - } | |
| 389 | - | |
| 390 | - $html .= sprintf( | |
| 391 | - '<input type="checkbox" id="%s" name="%s[%s]" class="%s" value="%s" %s />', | |
| 392 | - $name, | |
| 393 | - $this->settings_key, | |
| 394 | - $name, | |
| 395 | - ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ), | |
| 396 | - $value, | |
| 397 | - ( $checked ? ' checked' : '' ) | |
| 398 | - ); | |
| 399 | - | |
| 400 | - if ( $label ) { | |
| 401 | - $html .= sprintf( | |
| 402 | - '%s</label>', | |
| 403 | - $label | |
| 404 | - ); | |
| 405 | - } | |
| 406 | - | |
| 407 | - // If no description exists, just return the field. | |
| 408 | - if ( empty( $description ) ) { | |
| 409 | - return $html; | |
| 410 | - } | |
| 411 | - | |
| 412 | - // Return field with description appended to it. | |
| 413 | - return $html . $this->get_description( $description ); | |
| 414 | - | |
| 415 | - } | |
| 416 | - | |
| 417 | - /** | |
| 418 | - * Returns the given text wrapped in a paragraph with the description class. | |
| 419 | - * | |
| 420 | - * @since 1.9.6 | |
| 421 | - * | |
| 422 | - * @param bool|string|array $description Description. | |
| 423 | - * @return string HTML Description | |
| 424 | - */ | |
| 425 | - public function get_description( $description ) { | |
| 426 | - | |
| 427 | - // Return blank string if no description specified. | |
| 428 | - if ( ! $description ) { | |
| 429 | - return ''; | |
| 430 | - } | |
| 431 | - | |
| 432 | - // Return description in paragraph if a string. | |
| 433 | - if ( ! is_array( $description ) ) { | |
| 434 | - return '<p class="description">' . $description . '</p>'; | |
| 435 | - } | |
| 436 | - | |
| 437 | - // Return description lines in a paragraph, using breaklines for each description entry in the array. | |
| 438 | - return '<p class="description">' . implode( '<br />', $description ); | |
| 439 | - | |
| 440 | - } | |
| 441 | - | |
| 442 | - /** | |
| 443 | - * Converts the given key/value array pairs into a HTML attribute="value" string. | |
| 444 | - * | |
| 445 | - * @since 1.9.8.5 | |
| 446 | - * | |
| 447 | - * @param array $attributes_array Attributes. | |
| 448 | - * @return string HTML attributes string | |
| 449 | - */ | |
| 450 | - private function array_to_attributes( $attributes_array ) { | |
| 451 | - | |
| 452 | - $attributes = ''; | |
| 453 | - foreach ( $attributes_array as $key => $value ) { | |
| 454 | - $attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" '; | |
| 455 | - } | |
| 456 | - | |
| 457 | - return trim( $attributes ); | |
| 458 | - | |
| 459 | - } | |
| 460 | - | |
| 461 | - /** | |
| 462 | - * Sanitizes the settings prior to being saved. | |
| 463 | - * | |
| 464 | - * @since 1.9.6 | |
| 465 | - * | |
| 466 | - * @param array $settings Submitted Settings Fields. | |
| 467 | - * @return array Sanitized Settings with Defaults | |
| 468 | - */ | |
| 469 | - public function sanitize_settings( $settings ) { | |
| 470 | - | |
| 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 | - // Merge settings with defaults. | |
| 481 | - $settings = wp_parse_args( $settings, $this->settings->get_defaults() ); | |
| 482 | - | |
| 483 | - /** | |
| 484 | - * Performs actions prior to settings being saved. | |
| 485 | - * | |
| 486 | - * @since 2.2.8 | |
| 487 | - */ | |
| 488 | - do_action( 'convertkit_settings_base_sanitize_settings', $this->name, $settings ); | |
| 489 | - | |
| 490 | - // Return settings to be saved. | |
| 491 | - return $settings; | |
| 492 | - | |
| 493 | - } | |
| 494 | - | |
| 119 | + abstract public function print_section_info(); | |
| 495 | 120 | } |