PluginProbe
Depicter — Popup & Slider Builder / 1.9.2
Depicter — Popup & Slider Builder v1.9.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Dashboard / DashboardSetting.php

DashboardSetting.php in Depicter — Popup & Slider Builder 1.9.2, at app/src/Dashboard/DashboardSetting.php

597 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Dashboard;
3
4 // Exit if accessed directly.
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class DashboardSettings {
10
11 /**
12 * Settings Page title.
13 *
14 * @var string
15 */
16 private $page_title;
17
18 /**
19 * Menu title.
20 *
21 * @var string
22 */
23 private $menu_title;
24
25 /**
26 * Capability to access Settings page.
27 *
28 * @var string
29 */
30 private $capability;
31
32 /**
33 * Menu slug.
34 *
35 * @var string
36 */
37 private $menu_slug;
38
39 /**
40 * Settings form action.
41 *
42 * @var string
43 */
44 private $form_action;
45
46 /**
47 * Option group.
48 *
49 * @var string
50 */
51 private $option_group;
52
53 /**
54 * Constructor
55 */
56 public function __construct() {
57 // parameters.
58 $this->page_title = __( 'Depicter Settings', 'depicter' );
59 $this->menu_title = __( 'Settings', 'depicter' );
60 $this->capability = 'manage_depicter';
61 $this->menu_slug = 'depicter-settings';
62
63 // $this->form_action = 'admin.php?page=depicter-settings';
64 $this->form_action = 'options.php';
65
66 $this->option_group = 'depicter_';
67
68 add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
69 add_action( 'admin_init', array( $this, 'add_settings' ) );
70 }
71
72 /**
73 * Adds a submenu page to the Settings main menu.
74 */
75 public function add_settings_page() {
76 /**
77 * Params for add_options_page
78 *
79 * @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
80 * @param string $menu_title The text to be used for the menu.
81 * @param string $capability The capability required for this menu to be displayed to the user.
82 * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
83 * @param callable $callback Optional. The function to be called to output the content for this page.
84 * @param int $position Optional. The position in the menu order this item should appear.
85 * @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
86 */
87 // add_options_page(
88 // $this->page_title,
89 // $this->menu_title,
90 // $this->capability,
91 // $this->menu_slug,
92 // array( $this, 'settings_page_html' )
93 // );
94
95 add_submenu_page(
96 'depicter-dashboard',
97 $this->page_title,
98 $this->menu_title,
99 $this->capability,
100 $this->menu_slug,
101 [ $this, 'settings_page_html' ] );
102 }
103
104 /**
105 * Compose settings
106 */
107 public function add_settings() {
108
109 // Define Sections ----------------------------------------------------.
110
111 /**
112 * Adds a new section to a settings page.
113 *
114 * Part of the Settings API. Use this to define new settings sections for an admin page.
115 * Show settings sections in your admin page callback function with do_settings_sections().
116 * Add settings fields to your section with add_settings_field().
117 *
118 * The $callback argument should be the name of a function that echoes out any
119 * content you want to show at the top of the settings section before the actual
120 * fields. It can output nothing if you want.
121 *
122 * @param string $id Slug-name to identify the section. Used in the 'id' attribute of tags.
123 * @param string $title Formatted title of the section. Shown as the heading for the section.
124 * @param callable $callback Function that echos out any content at the top of the section (between heading and fields).
125 * @param string $page The slug-name of the settings page on which to show the section. Built-in pages include
126 * 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using
127 * add_options_page();
128 */
129 add_settings_section(
130 'depicter_general_section',
131 __( 'General', 'depicter' ),
132 null,
133 $this->menu_slug
134 );
135
136 add_settings_section(
137 'depicter_performance_section',
138 __( 'Performance', 'depicter' ),
139 null,
140 $this->menu_slug
141 );
142
143 // Input text field ---------------------------------------------------.
144
145 /**
146 * Adds a new field to a section of a settings page.
147 *
148 * Part of the Settings API. Use this to define a settings field that will show
149 * as part of a settings section inside a settings page. The fields are shown using
150 * do_settings_fields() in do_settings_sections().
151 *
152 * The $callback argument should be the name of a function that echoes out the
153 * HTML input tags for this setting field. Use get_option() to retrieve existing
154 * values to show.
155 *
156 * @param string $id Slug-name to identify the field. Used in the 'id' attribute of tags.
157 * @param string $title Formatted title of the field. Shown as the label for the field
158 * during output.
159 * @param callable $callback Function that fills the field with the desired form inputs. The
160 * function should echo its output.
161 * @param string $page The slug-name of the settings page on which to show the section
162 * (general, reading, writing, ...).
163 * @param string $section Optional. The slug-name of the section of the settings page
164 * in which to show the box. Default 'default'.
165 * @param array $args {
166 * Optional. Extra arguments used when outputting the field.
167 *
168 * @type string $label_for When supplied, the setting title will be wrapped
169 * in a `<label>` element, its `for` attribute populated
170 * with this value.
171 * @type string $class CSS Class to be added to the `<tr>` element when the
172 * field is output.
173 *
174 */
175 add_settings_field(
176 'cliowp_sp_input1',
177 __( 'Input1 Label', 'depicter' ),
178 array( $this, 'input1_html' ),
179 $this->menu_slug,
180 'depicter_general_section'
181 );
182
183 /**
184 * Registers a setting and its data.
185 *
186 * @param string $option_group A settings group name. Should correspond to an allowed option key name.
187 * Default allowed option key names include 'general', 'discussion', 'media',
188 * 'reading', 'writing', and 'options'.
189 * @param string $option_name The name of an option to sanitize and save.
190 * @param array $args {
191 * Data used to describe the setting when registered.
192 *
193 * @type string $type The type of data associated with this setting.
194 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
195 * @type string $description A description of the data attached to this setting.
196 * @type callable $sanitize_callback A callback function that sanitizes the option's value.
197 * @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API.
198 * When registering complex settings, this argument may optionally be an
199 * array with a 'schema' key.
200 * @type mixed $default Default value when calling `get_option()`.
201 * }
202 */
203 register_setting(
204 $this->option_group,
205 'cliowp_sp_input1',
206 array(
207 'sanitize_callback' => array( $this, 'sanitize_input1' ),
208 'default' => 'input1 test',
209 )
210 );
211
212 // Date field ---------------------------------------------------------.
213 add_settings_field(
214 'cliowp_sp_date1',
215 __( 'Date1 Label', 'depicter' ),
216 array( $this, 'date1_html' ),
217 $this->menu_slug,
218 'depicter_general_section'
219 );
220
221 register_setting(
222 $this->option_group,
223 'cliowp_sp_date1',
224 array(
225 'sanitize_callback' => 'sanitize_text_field',
226 )
227 );
228
229 // DateTime field -----------------------------------------------------.
230 add_settings_field(
231 'cliowp_sp_datetime1',
232 __( 'Datetime1 Label', 'depicter' ),
233 array( $this, 'datetime1_html' ),
234 $this->menu_slug,
235 'depicter_general_section'
236 );
237
238 register_setting(
239 $this->option_group,
240 'cliowp_sp_datetime1',
241 array(
242 'sanitize_callback' => 'sanitize_text_field',
243 )
244 );
245
246 // Password field -----------------------------------------------------.
247 add_settings_field(
248 'cliowp_sp_password1',
249 __( 'Password1 Label', 'depicter' ),
250 array( $this, 'password1_html' ),
251 $this->menu_slug,
252 'depicter_general_section'
253 );
254
255 register_setting(
256 $this->option_group,
257 'cliowp_sp_password1',
258 array(
259 'sanitize_callback' => array( $this, 'encrypt_password1' ),
260 )
261 );
262
263 // Number field -------------------------------------------------------.
264 add_settings_field(
265 'cliowp_sp_number1',
266 __( 'Number1 Label', 'depicter' ),
267 array( $this, 'number1_html' ),
268 $this->menu_slug,
269 'depicter_general_section',
270 array(
271 'min' => 1,
272 'max' => 10,
273 'step' => 1,
274 )
275 );
276
277 register_setting(
278 $this->option_group,
279 'cliowp_sp_number1',
280 );
281
282 // Select field -------------------------------------------------------.
283 add_settings_field(
284 'cliowp_sp_select1',
285 __( 'Select1 Label', 'depicter' ),
286 array( $this, 'select1_html' ),
287 $this->menu_slug,
288 'depicter_general_section'
289 );
290
291 register_setting(
292 $this->option_group,
293 'cliowp_sp_select1',
294 array(
295 'sanitize_callback' => array( $this, 'sanitize_select1' ),
296 'default' => '1',
297 )
298 );
299
300 // Checkbox field -----------------------------------------------------.
301 add_settings_field(
302 'cliowp_sp_checkbox1',
303 __( 'Checkbox1 Label', 'depicter' ),
304 array( $this, 'checkbox1_html' ),
305 $this->menu_slug,
306 'depicter_general_section'
307 );
308
309 register_setting(
310 $this->option_group,
311 'cliowp_sp_checkbox1',
312 array(
313 'sanitize_callback' => 'sanitize_text_field',
314 'default' => '1',
315 )
316 );
317
318 // MultiSelect field --------------------------------------------------.
319 add_settings_field(
320 'cliowp_sp_multiselect1',
321 __( 'MultiSelect1 Label', 'depicter' ),
322 array( $this, 'multiselect1_html' ),
323 $this->menu_slug,
324 'depicter_performance_section'
325 );
326
327 register_setting(
328 $this->option_group,
329 'cliowp_sp_multiselect1',
330 );
331
332 // Textarea field -----------------------------------------------------.
333 add_settings_field(
334 'cliowp_sp_textarea1',
335 __( 'Textarea1 Label', 'depicter' ),
336 array( $this, 'textarea1_html' ),
337 $this->menu_slug,
338 'depicter_performance_section',
339 array(
340 'rows' => 4,
341 'cols' => 30,
342 )
343 );
344
345 register_setting(
346 $this->option_group,
347 'cliowp_sp_textarea1',
348 array(
349 'sanitize_callback' => 'sanitize_textarea_field',
350 )
351 );
352
353 // Color field --------------------------------------------------------.
354 add_settings_field(
355 'cliowp_sp_color1',
356 __( 'Color1 Label', 'depicter' ),
357 array( $this, 'color1_html' ),
358 $this->menu_slug,
359 'depicter_performance_section'
360 );
361
362 register_setting(
363 $this->option_group,
364 'cliowp_sp_color1',
365 );
366
367 // WYSIWYG editor field -----------------------------------------------.
368 add_settings_field(
369 'cliowp_sp_editor1',
370 __( 'Editor1 Label', 'depicter' ),
371 array( $this, 'editor1_html' ),
372 $this->menu_slug,
373 'depicter_performance_section',
374 );
375
376 register_setting(
377 $this->option_group,
378 'cliowp_sp_editor1',
379 array(
380 'sanitize_callback' => 'wp_kses_post',
381 )
382 );
383
384 }
385
386 /**
387 * Create HTML for input1 field
388 */
389 public function input1_html() { ?>
390 <input type="text" name="cliowp_sp_input1" value="<?php echo esc_attr( get_option( 'cliowp_sp_input1' ) ); ?>">
391 <?php
392 }
393
394 /**
395 * Sanitize input1
396 *
397 * @param string $input The input value.
398 */
399 public function sanitize_input1( $input ) {
400 if ( true === empty( trim( $input ) ) ) {
401 add_settings_error(
402 'cliowp_sp_input1',
403 'cliowp_sp_input1_error',
404 __( 'Input1 cannot be empty', 'depicter' ),
405 );
406 return get_option( 'cliowp_sp_input1' );
407 }
408
409 return sanitize_text_field( $input );
410 }
411
412 /**
413 * Create HTML for date1 field
414 */
415 public function date1_html() {
416 ?>
417 <input type="date" name="cliowp_sp_date1" value="<?php echo esc_attr( get_option( 'cliowp_sp_date1' ) ); ?>">
418 <?php
419 }
420
421 /**
422 * Create HTML for datetime1 field
423 */
424 public function datetime1_html() {
425 ?>
426 <input type="datetime-local" name="cliowp_sp_datetime1" value="<?php echo esc_attr( get_option( 'cliowp_sp_datetime1' ) ); ?>">
427 <?php
428 }
429
430 /**
431 * Create HTML for password1 field
432 *
433 * This is the only field that does not retrieve the value from the database
434 * (because a hash is stored and not that original value).
435 * Check the wp_options table to view what is saved as a hash.
436 */
437 public function password1_html() {
438 ?>
439 <input type="password" name="cliowp_sp_password1" value="">
440 <?php
441 }
442
443 /**
444 * Encrypt password1
445 *
446 * @param string $input The plain password.
447 */
448 public function encrypt_password1( $input ) {
449
450 return wp_hash_password( $input );
451 }
452
453 /**
454 * Create HTML for number1 field
455 *
456 * @param array $args Arguments passed.
457 */
458 public function number1_html( array $args ) {
459 ?>
460 <input type="number" name="cliowp_sp_number1"
461 min="<?php echo esc_html( $args['min'] ); ?>"
462 max="<?php echo esc_html( $args['max'] ); ?>"
463 step="<?php echo esc_html( $args['step'] ); ?>"
464 value="<?php echo esc_attr( get_option( 'cliowp_sp_number1' ) ); ?>">
465 <?php
466 }
467
468 /**
469 * Create HTML for select1 field
470 */
471 public function select1_html() {
472 ?>
473 <select name="cliowp_sp_select1">
474 <option value="1" <?php selected( get_option( 'cliowp_sp_select1' ), '1' ); ?>><?php esc_attr_e( 'Option1', 'depicter' ); ?></option>
475 <option value="2" <?php selected( get_option( 'cliowp_sp_select1' ), '2' ); ?>><?php esc_attr_e( 'Option2', 'depicter' ); ?></option>
476 <option value="3" <?php selected( get_option( 'cliowp_sp_select1' ), '3' ); ?>><?php esc_attr_e( 'Option3', 'depicter' ); ?></option>
477 </select>
478 <?php
479 }
480
481 /**
482 * Sanitize select1
483 *
484 * @param string $input The selected value.
485 */
486 public function sanitize_select1( $input ) {
487 $valid_input = array( '1', '2', '3' );
488 if ( false === in_array( $input, $valid_input, true ) ) {
489 add_settings_error(
490 'cliowp_sp_select1',
491 'cliowp_sp_select1_error',
492 __( 'Invalid option for Select1', 'depicter' ),
493 );
494 return get_option( 'cliowp_sp_select1' );
495 }
496 return $input;
497 }
498
499 /**
500 * Create HTML for checkbox1 field
501 */
502 public function checkbox1_html() {
503 ?>
504 <input type="checkbox" name="cliowp_sp_checkbox1" value="1" <?php checked( get_option( 'cliowp_sp_checkbox1' ), '1' ); ?>>
505 <?php
506 }
507
508 /**
509 * Create HTML for multiselect1 field
510 */
511 public function multiselect1_html() {
512 $selected_values = get_option( 'cliowp_sp_multiselect1' );
513 ?>
514 <select name="cliowp_sp_multiselect1[]" multiple>
515 <option value="1" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '1' ) ); ?>><?php esc_attr_e( 'MultiSelect Option1', 'depicter' ); ?></option>
516 <option value="2" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '2' ) ); ?>><?php esc_attr_e( 'MultiSelect Option2', 'depicter' ); ?></option>
517 <option value="3" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '3' ) ); ?>><?php esc_attr_e( 'MultiSelect Option3', 'depicter' ); ?></option>
518 <option value="4" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '4' ) ); ?>><?php esc_attr_e( 'MultiSelect Option4', 'depicter' ); ?></option>
519 <option value="5" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '5' ) ); ?>><?php esc_attr_e( 'MultiSelect Option5', 'depicter' ); ?></option>
520 </select>
521 <?php
522 }
523
524 /**
525 * Utility function to check if value is selected
526 *
527 * @param array|string $selected_values Array (or empty string) returned by get_option().
528 * @param string $current_value Value to check if it is selected.
529 *
530 * @return string
531 */
532 private function cliowp_multiselected(
533 $selected_values,
534 string $current_value
535 ): string {
536 if ( is_array( $selected_values ) && in_array( $current_value, $selected_values, true ) ) {
537 return 'selected';
538 }
539
540 return '';
541 }
542
543 /**
544 * Create HTML for textarea1 field
545 *
546 * @param array $args Arguments passed.
547 */
548 public function textarea1_html( array $args ) {
549 ?>
550 <textarea
551 name="cliowp_sp_textarea1"
552 rows="<?php echo esc_html( $args['rows'] ); ?>"
553 cols="<?php echo esc_html( $args['cols'] ); ?>"><?php echo esc_attr( get_option( 'cliowp_sp_textarea1' ) ); ?></textarea>
554 <?php
555 }
556
557 /**
558 * Create HTML for color1 field
559 */
560 public function color1_html() {
561 ?>
562 <input type="color" name="cliowp_sp_color1" value="<?php echo esc_attr( get_option( 'cliowp_sp_color1' ) ); ?>">
563 <?php
564 }
565
566 /**
567 * Create HTML for editor1 field
568 */
569 public function editor1_html() {
570 wp_editor(
571 wp_kses_post( get_option( 'cliowp_sp_editor1' ) ),
572 'cliowp_sp_editor1',
573 );
574 }
575
576 /**
577 * Create Settings Page HTML
578 */
579 public function settings_page_html() {
580 ?>
581
582 <div class="wrap">
583 <h1><?php echo esc_attr( $this->page_title ); ?>
584 </h1>
585 <form action="<?php echo esc_attr( $this->form_action ); ?>" method="POST">
586 <?php
587 settings_fields( $this->option_group );
588 do_settings_sections( $this->menu_slug );
589 submit_button();
590 ?>
591 </form>
592 </div>
593
594 <?php
595 }
596 }
597