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

412 lines 8.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
51 */
52 public $settings;
53
54 /**
55 * Constructor
56 */
57 public function __construct() {
58
59 // If tab text is not defined, use the title for the tab's text.
60 if ( empty( $this->tab_text ) ) {
61 $this->tab_text = $this->title;
62 }
63
64 // Register the settings section.
65 $this->register_section();
66
67 }
68
69 /**
70 * Register settings section.
71 */
72 public function register_section() {
73
74 add_settings_section(
75 $this->name,
76 $this->title,
77 array( $this, 'print_section_info' ),
78 $this->settings_key
79 );
80
81 $this->register_fields();
82
83 register_setting(
84 $this->settings_key,
85 $this->settings_key,
86 array( $this, 'sanitize_settings' )
87 );
88
89 }
90
91 /**
92 * Register fields for this section
93 */
94 abstract public function register_fields();
95
96 /**
97 * Prints help info for this section
98 */
99 abstract public function print_section_info();
100
101 /**
102 * Returns the URL for the ConvertKit documentation for this setting section.
103 */
104 abstract public function documentation_url();
105
106 /**
107 * Renders the section
108 */
109 public function render() {
110
111 /**
112 * Performs actions prior to rendering the settings form.
113 *
114 * @since 1.9.6
115 */
116 do_action( 'convertkit_settings_base_render_before' );
117
118 $this->render_container_start();
119
120 do_settings_sections( $this->settings_key );
121
122 settings_fields( $this->settings_key );
123
124 submit_button();
125
126 $this->render_container_end();
127
128 /**
129 * Performs actions after rendering of the settings form.
130 *
131 * @since 1.9.6
132 */
133 do_action( 'convertkit_settings_base_render_after' );
134
135 }
136
137 /**
138 * Outputs .metabox-holder and .postbox container div elements,
139 * used before beginning a setting screen's output.
140 *
141 * @since 2.0.0
142 */
143 public function render_container_start() {
144
145 ?>
146 <div class="metabox-holder">
147 <div class="postbox">
148 <?php
149
150 }
151
152 /**
153 * Outputs closing .metabox-holder and .postbox container div elements,
154 * used after finishing a setting screen's output.
155 *
156 * @since 2.0.0
157 */
158 public function render_container_end() {
159
160 ?>
161 </div>
162 </div>
163 <?php
164
165 }
166
167 /**
168 * Outputs the given success message in an inline notice.
169 *
170 * @since 2.0.0
171 *
172 * @param string $success_message Success Message.
173 */
174 public function output_success( $success_message ) {
175
176 ?>
177 <div class="notice notice-success is-dismissible">
178 <p>
179 <?php echo esc_attr( $success_message ); ?>
180 </p>
181 </div>
182 <?php
183
184 }
185
186 /**
187 * Outputs the given error message in an inline notice.
188 *
189 * @since 1.9.6
190 *
191 * @param string $error_message Error Message.
192 */
193 public function output_error( $error_message ) {
194
195 ?>
196 <div class="notice notice-error is-dismissible">
197 <p>
198 <?php echo esc_attr( $error_message ); ?>
199 </p>
200 </div>
201 <?php
202
203 }
204
205 /**
206 * Returns a masked value.
207 *
208 * @since 1.9.6
209 *
210 * @param string $value Value.
211 * @param bool|string $description Description.
212 * @return string Masked Value
213 */
214 public function get_masked_value( $value, $description = false ) {
215
216 $html = sprintf(
217 '<code>%s</code>',
218 str_repeat( '*', strlen( $value ) - 4 ) . substr( $value, - 4 )
219 );
220
221 if ( $description ) {
222 $html .= $this->get_description( $description );
223 }
224
225 return $html;
226
227 }
228
229 /**
230 * Returns a text field.
231 *
232 * @since 1.9.6
233 *
234 * @param string $name Name.
235 * @param string $value Value.
236 * @param bool|string|array $description Description.
237 * @return string HTML Field
238 */
239 public function get_text_field( $name, $value = '', $description = false ) {
240
241 $html = sprintf(
242 '<input type="text" class="regular-text code" id="%s" name="%s[%s]" value="%s" />',
243 $name,
244 $this->settings_key,
245 $name,
246 $value
247 );
248
249 return $html . $this->get_description( $description );
250
251 }
252
253 /**
254 * Returns a select dropdown field.
255 *
256 * @since 1.9.6
257 *
258 * @param string $name Name.
259 * @param string $value Value.
260 * @param array $options Options / Choices.
261 * @param bool|string $description Description.
262 * @param bool|array $css_classes <select> CSS class(es).
263 * @param bool|array $attributes <select> attributes.
264 * @return string HTML Select Field
265 */
266 public function get_select_field( $name, $value = '', $options = array(), $description = false, $css_classes = false, $attributes = false ) {
267
268 // Build opening <select> tag.
269 $html = sprintf(
270 '<select id="%s" name="%s[%s]" class="%s" size="1" %s>',
271 $this->settings_key . '_' . $name,
272 $this->settings_key,
273 $name,
274 ( is_array( $css_classes ) ? implode( ' ', $css_classes ) : '' ),
275 ( is_array( $attributes ) ? $this->array_to_attributes( $attributes ) : '' )
276 );
277
278 // Build <option> tags.
279 foreach ( $options as $option => $label ) {
280 $html .= sprintf(
281 '<option value="%s"%s>%s</option>',
282 $option,
283 selected( $value, $option, false ),
284 $label
285 );
286 }
287
288 // Close <select>.
289 $html .= '</select>';
290
291 // If no description exists, just return the select field.
292 if ( empty( $description ) ) {
293 return $html;
294 }
295
296 // Return select field with description appended to it.
297 return $html . $this->get_description( $description );
298
299 }
300
301 /**
302 * Returns a checkbox field.
303 *
304 * @since 1.9.6
305 *
306 * @param string $name Name.
307 * @param string $value Value.
308 * @param bool $checked Should checkbox be checked/ticked.
309 * @param bool|string $label Label.
310 * @param bool|string $description Description.
311 * @return string HTML Checkbox
312 */
313 public function get_checkbox_field( $name, $value, $checked = false, $label = '', $description = '' ) {
314
315 $html = '';
316
317 if ( $label ) {
318 $html .= sprintf(
319 '<label for="%s">',
320 $name
321 );
322 }
323
324 $html .= sprintf(
325 '<input type="checkbox" id="%s" name="%s[%s]" value="%s" %s />',
326 $name,
327 $this->settings_key,
328 $name,
329 $value,
330 ( $checked ? ' checked' : '' )
331 );
332
333 if ( $label ) {
334 $html .= sprintf(
335 '%s</label>',
336 $label
337 );
338 }
339
340 return $html . $this->get_description( $description );
341
342 }
343
344 /**
345 * Returns the given text wrapped in a paragraph with the description class.
346 *
347 * @since 1.9.6
348 *
349 * @param bool|string|array $description Description.
350 * @return string HTML Description
351 */
352 private function get_description( $description ) {
353
354 // Return blank string if no description specified.
355 if ( ! $description ) {
356 return '';
357 }
358
359 // Return description in paragraph if a string.
360 if ( ! is_array( $description ) ) {
361 return '<p class="description">' . $description . '</p>';
362 }
363
364 // Return description lines in a paragraph, using breaklines for each description entry in the array.
365 return '<p class="description">' . implode( '<br />', $description );
366
367 }
368
369 /**
370 * Converts the given key/value array pairs into a HTML attribute="value" string.
371 *
372 * @since 1.9.8.5
373 *
374 * @param array $array Attributes.
375 * @return string HTML attributes string
376 */
377 private function array_to_attributes( $array ) {
378
379 $attributes = '';
380 foreach ( $array as $key => $value ) {
381 $attributes .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
382 }
383
384 return trim( $attributes );
385
386 }
387
388 /**
389 * Sanitizes the settings prior to being saved.
390 *
391 * @since 1.9.6
392 *
393 * @param array $settings Submitted Settings Fields.
394 * @return array Sanitized Settings with Defaults
395 */
396 public function sanitize_settings( $settings ) {
397
398 // If a Form or Landing Page was specified, request a review.
399 // This can safely be called multiple times, as the review request
400 // class will ensure once a review request is dismissed by the user,
401 // it is never displayed again.
402 if ( ( isset( $settings['page_form'] ) && $settings['page_form'] ) ||
403 ( isset( $settings['post_form'] ) && $settings['post_form'] ) ) {
404 WP_ConvertKit()->get_class( 'review_request' )->request_review();
405 }
406
407 return wp_parse_args( $settings, $this->settings->get_defaults() );
408
409 }
410
411 }
412