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

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