PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.2.7
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.2.7
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.2.7, at admin/section/class-convertkit-settings-base.php

423 lines 9.1 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
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 * Outputs the given success message in an inline notice.
178 *
179 * @since 2.0.0
180 *
181 * @param string $success_message Success Message.
182 */
183 public function output_success( $success_message ) {
184
185 ?>
186 <div class="notice notice-success is-dismissible">
187 <p>
188 <?php echo esc_attr( $success_message ); ?>
189 </p>
190 </div>
191 <?php
192
193 }
194
195 /**
196 * Outputs the given error message in an inline notice.
197 *
198 * @since 1.9.6
199 *
200 * @param string $error_message Error Message.
201 */
202 public function output_error( $error_message ) {
203
204 ?>
205 <div class="notice notice-error is-dismissible">
206 <p>
207 <?php echo esc_attr( $error_message ); ?>
208 </p>
209 </div>
210 <?php
211
212 }
213
214 /**
215 * Returns a masked value.
216 *
217 * @since 1.9.6
218 *
219 * @param string $value Value.
220 * @param bool|string $description Description.
221 * @return string Masked Value
222 */
223 public function get_masked_value( $value, $description = false ) {
224
225 $html = sprintf(
226 '<code>%s</code>',
227 str_repeat( '*', strlen( $value ) - 4 ) . substr( $value, - 4 )
228 );
229
230 if ( $description ) {
231 $html .= $this->get_description( $description );
232 }
233
234 return $html;
235
236 }
237
238 /**
239 * Returns a text field.
240 *
241 * @since 1.9.6
242 *
243 * @param string $name Name.
244 * @param string $value Value.
245 * @param bool|string|array $description Description (false|string|array).
246 * @param bool|array $css_classes CSS Classes (false|array).
247 * @return string HTML Field
248 */
249 public function get_text_field( $name, $value = '', $description = false, $css_classes = false ) {
250
251 $html = sprintf(
252 '<input type="text" class="%s" id="%s" name="%s[%s]" value="%s" />',
253 ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ),
254 $name,
255 $this->settings_key,
256 $name,
257 $value
258 );
259
260 return $html . $this->get_description( $description );
261
262 }
263
264 /**
265 * Returns a select dropdown field.
266 *
267 * @since 1.9.6
268 *
269 * @param string $name Name.
270 * @param string $value Value.
271 * @param array $options Options / Choices.
272 * @param bool|string $description Description.
273 * @param bool|array $css_classes <select> CSS class(es).
274 * @param bool|array $attributes <select> attributes.
275 * @return string HTML Select Field
276 */
277 public function get_select_field( $name, $value = '', $options = array(), $description = false, $css_classes = false, $attributes = false ) {
278
279 // Build opening <select> tag.
280 $html = sprintf(
281 '<select id="%s" name="%s[%s]" class="%s" size="1" %s>',
282 $this->settings_key . '_' . $name,
283 $this->settings_key,
284 $name,
285 ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ),
286 ( is_array( $attributes ) ? $this->array_to_attributes( $attributes ) : '' )
287 );
288
289 // Build <option> tags.
290 foreach ( $options as $option => $label ) {
291 $html .= sprintf(
292 '<option value="%s"%s>%s</option>',
293 $option,
294 selected( $value, $option, false ),
295 $label
296 );
297 }
298
299 // Close <select>.
300 $html .= '</select>';
301
302 // If no description exists, just return the select field.
303 if ( empty( $description ) ) {
304 return $html;
305 }
306
307 // Return select field with description appended to it.
308 return $html . $this->get_description( $description );
309
310 }
311
312 /**
313 * Returns a checkbox field.
314 *
315 * @since 1.9.6
316 *
317 * @param string $name Name.
318 * @param string $value Value.
319 * @param bool $checked Should checkbox be checked/ticked.
320 * @param bool|string $label Label.
321 * @param bool|string|array $description Description.
322 * @return string HTML Checkbox
323 */
324 public function get_checkbox_field( $name, $value, $checked = false, $label = '', $description = '' ) {
325
326 $html = '';
327
328 if ( $label ) {
329 $html .= sprintf(
330 '<label for="%s">',
331 $name
332 );
333 }
334
335 $html .= sprintf(
336 '<input type="checkbox" id="%s" name="%s[%s]" value="%s" %s />',
337 $name,
338 $this->settings_key,
339 $name,
340 $value,
341 ( $checked ? ' checked' : '' )
342 );
343
344 if ( $label ) {
345 $html .= sprintf(
346 '%s</label>',
347 $label
348 );
349 }
350
351 return $html . $this->get_description( $description );
352
353 }
354
355 /**
356 * Returns the given text wrapped in a paragraph with the description class.
357 *
358 * @since 1.9.6
359 *
360 * @param bool|string|array $description Description.
361 * @return string HTML Description
362 */
363 private function get_description( $description ) {
364
365 // Return blank string if no description specified.
366 if ( ! $description ) {
367 return '';
368 }
369
370 // Return description in paragraph if a string.
371 if ( ! is_array( $description ) ) {
372 return '<p class="description">' . $description . '</p>';
373 }
374
375 // Return description lines in a paragraph, using breaklines for each description entry in the array.
376 return '<p class="description">' . implode( '<br />', $description );
377
378 }
379
380 /**
381 * Converts the given key/value array pairs into a HTML attribute="value" string.
382 *
383 * @since 1.9.8.5
384 *
385 * @param array $array Attributes.
386 * @return string HTML attributes string
387 */
388 private function array_to_attributes( $array ) {
389
390 $attributes = '';
391 foreach ( $array as $key => $value ) {
392 $attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
393 }
394
395 return trim( $attributes );
396
397 }
398
399 /**
400 * Sanitizes the settings prior to being saved.
401 *
402 * @since 1.9.6
403 *
404 * @param array $settings Submitted Settings Fields.
405 * @return array Sanitized Settings with Defaults
406 */
407 public function sanitize_settings( $settings ) {
408
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 }
417
418 return wp_parse_args( $settings, $this->settings->get_defaults() );
419
420 }
421
422 }
423