PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.4.3
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / admin / section / class-convertkit-settings-base.php

class-convertkit-settings-base.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 2.4.3, at admin/section/class-convertkit-settings-base.php

513 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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|ConvertKit_Forminator_Settings
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 textarea field.
294 *
295 * @since 2.3.5
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_textarea_field( $name, $value = '', $description = false, $css_classes = false ) {
304
305 $html = sprintf(
306 '<textarea class="%s" id="%s" name="%s[%s]">%s</textarea>',
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 date field.
320 *
321 * @since 2.2.8
322 *
323 * @param string $name Name.
324 * @param string $value Value.
325 * @param bool|string|array $description Description (false|string|array).
326 * @param bool|array $css_classes CSS Classes (false|array).
327 * @return string HTML Field
328 */
329 public function get_date_field( $name, $value = '', $description = false, $css_classes = false ) {
330
331 $html = sprintf(
332 '<input type="date" class="%s" id="%s" name="%s[%s]" value="%s" />',
333 ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ),
334 $name,
335 $this->settings_key,
336 $name,
337 $value
338 );
339
340 return $html . $this->get_description( $description );
341
342 }
343
344 /**
345 * Returns a select dropdown field.
346 *
347 * @since 1.9.6
348 *
349 * @param string $name Name.
350 * @param string $value Value.
351 * @param array $options Options / Choices.
352 * @param bool|string $description Description.
353 * @param bool|array $css_classes <select> CSS class(es).
354 * @param bool|array $attributes <select> attributes.
355 * @return string HTML Select Field
356 */
357 public function get_select_field( $name, $value = '', $options = array(), $description = false, $css_classes = false, $attributes = false ) {
358
359 // Build opening <select> tag.
360 $html = sprintf(
361 '<select id="%s" name="%s[%s]" class="%s" size="1" %s>',
362 $this->settings_key . '_' . $name,
363 $this->settings_key,
364 $name,
365 ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ),
366 ( is_array( $attributes ) ? $this->array_to_attributes( $attributes ) : '' )
367 );
368
369 // Build <option> tags.
370 foreach ( $options as $option => $label ) {
371 $html .= sprintf(
372 '<option value="%s"%s>%s</option>',
373 $option,
374 selected( $value, $option, false ),
375 $label
376 );
377 }
378
379 // Close <select>.
380 $html .= '</select>';
381
382 // If no description exists, just return the select field.
383 if ( empty( $description ) ) {
384 return $html;
385 }
386
387 // Return select field with description appended to it.
388 return $html . $this->get_description( $description );
389
390 }
391
392 /**
393 * Returns a checkbox field.
394 *
395 * @since 1.9.6
396 *
397 * @param string $name Name.
398 * @param string $value Value.
399 * @param bool $checked Should checkbox be checked/ticked.
400 * @param bool|string $label Label.
401 * @param bool|string|array $description Description.
402 * @param bool|array $css_classes CSS class(es).
403 * @return string HTML Checkbox
404 */
405 public function get_checkbox_field( $name, $value, $checked = false, $label = '', $description = false, $css_classes = false ) {
406
407 $html = '';
408
409 if ( $label ) {
410 $html .= sprintf(
411 '<label for="%s">',
412 $name
413 );
414 }
415
416 $html .= sprintf(
417 '<input type="checkbox" id="%s" name="%s[%s]" class="%s" value="%s" %s />',
418 $name,
419 $this->settings_key,
420 $name,
421 ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ),
422 $value,
423 ( $checked ? ' checked' : '' )
424 );
425
426 if ( $label ) {
427 $html .= sprintf(
428 '%s</label>',
429 $label
430 );
431 }
432
433 // If no description exists, just return the field.
434 if ( empty( $description ) ) {
435 return $html;
436 }
437
438 // Return field with description appended to it.
439 return $html . $this->get_description( $description );
440
441 }
442
443 /**
444 * Returns the given text wrapped in a paragraph with the description class.
445 *
446 * @since 1.9.6
447 *
448 * @param bool|string|array $description Description.
449 * @return string HTML Description
450 */
451 public function get_description( $description ) {
452
453 // Return blank string if no description specified.
454 if ( ! $description ) {
455 return '';
456 }
457
458 // Return description in paragraph if a string.
459 if ( ! is_array( $description ) ) {
460 return '<p class="description">' . $description . '</p>';
461 }
462
463 // Return description lines in a paragraph, using breaklines for each description entry in the array.
464 return '<p class="description">' . implode( '<br />', $description ) . '</p>';
465
466 }
467
468 /**
469 * Converts the given key/value array pairs into a HTML attribute="value" string.
470 *
471 * @since 1.9.8.5
472 *
473 * @param array $attributes_array Attributes.
474 * @return string HTML attributes string
475 */
476 private function array_to_attributes( $attributes_array ) {
477
478 $attributes = '';
479 foreach ( $attributes_array as $key => $value ) {
480 $attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
481 }
482
483 return trim( $attributes );
484
485 }
486
487 /**
488 * Sanitizes the settings prior to being saved.
489 *
490 * @since 1.9.6
491 *
492 * @param array $settings Submitted Settings Fields.
493 * @return array Sanitized Settings with Defaults
494 */
495 public function sanitize_settings( $settings ) {
496
497 // Merge settings with defaults.
498 $settings = wp_parse_args( $settings, $this->settings->get_defaults() );
499
500 /**
501 * Performs actions prior to settings being saved.
502 *
503 * @since 2.2.8
504 */
505 do_action( 'convertkit_settings_base_sanitize_settings', $this->name, $settings );
506
507 // Return settings to be saved.
508 return $settings;
509
510 }
511
512 }
513