| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Settings Base class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* ConvertKit Settings class |
| 11 |
* |
| 12 |
* @package ConvertKit |
| 13 |
* @author ConvertKit |
| 14 |
*/ |
| 15 |
abstract class ConvertKit_Settings_Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* Section name |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $name; |
| 23 |
|
| 24 |
/** |
| 25 |
* Section title |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $title; |
| 30 |
|
| 31 |
/** |
| 32 |
* Section tab text |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public $tab_text; |
| 37 |
|
| 38 |
/** |
| 39 |
* Options table key |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public $settings_key; |
| 44 |
|
| 45 |
/** |
| 46 |
* Holds the settings class for the section. |
| 47 |
* |
| 48 |
* @since 1.9.6 |
| 49 |
* |
| 50 |
* @var false|ConvertKit_Settings|ConvertKit_ContactForm7_Settings|ConvertKit_Wishlist_Settings|ConvertKit_Settings_Restrict_Content|ConvertKit_Settings_Broadcasts |
| 51 |
*/ |
| 52 |
public $settings; |
| 53 |
|
| 54 |
/** |
| 55 |
* Holds whether this settings section is for beta functionality. |
| 56 |
* |
| 57 |
* @since 2.1.0 |
| 58 |
* |
| 59 |
* @var bool |
| 60 |
*/ |
| 61 |
public $is_beta = false; |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructor |
| 65 |
*/ |
| 66 |
public function __construct() { |
| 67 |
|
| 68 |
// If tab text is not defined, use the title for the tab's text. |
| 69 |
if ( empty( $this->tab_text ) ) { |
| 70 |
$this->tab_text = $this->title; |
| 71 |
} |
| 72 |
|
| 73 |
// Register the settings section. |
| 74 |
$this->register_section(); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register settings section. |
| 80 |
*/ |
| 81 |
public function register_section() { |
| 82 |
|
| 83 |
add_settings_section( |
| 84 |
$this->name, |
| 85 |
$this->title, |
| 86 |
array( $this, 'print_section_info' ), |
| 87 |
$this->settings_key |
| 88 |
); |
| 89 |
|
| 90 |
$this->register_fields(); |
| 91 |
|
| 92 |
register_setting( |
| 93 |
$this->settings_key, |
| 94 |
$this->settings_key, |
| 95 |
array( $this, 'sanitize_settings' ) |
| 96 |
); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 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 |
* Renders the section |
| 117 |
*/ |
| 118 |
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 |
do_settings_sections( $this->settings_key ); |
| 130 |
|
| 131 |
settings_fields( $this->settings_key ); |
| 132 |
|
| 133 |
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 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Outputs .metabox-holder and .postbox container div elements, |
| 148 |
* used before beginning a setting screen's output. |
| 149 |
* |
| 150 |
* @since 2.0.0 |
| 151 |
*/ |
| 152 |
public function render_container_start() { |
| 153 |
|
| 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 |
/** |
| 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 |
| 166 |
*/ |
| 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 |
|
| 495 |
} |
| 496 |
|