PluginProbe
Posts Table with Search & Sort / 1.3.6
Posts Table with Search & Sort v1.3.6
1.4.13 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2 1.3 1.3.1 1.3.1-v2 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 All 40 releases
posts-data-table / lib / Admin / Settings_API_Helper.php

Settings_API_Helper.php in Posts Table with Search & Sort 1.3.6, at lib/Admin/Settings_API_Helper.php

325 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Barn2\PTS_Lib\Admin;
3
4 use Barn2\PTS_Lib\Registerable,
5 Barn2\PTS_Lib\Conditional,
6 Barn2\PTS_Lib\Plugin\Plugin,
7 Barn2\PTS_Lib\Util,
8 Barn2\PTS_Lib\Admin\Settings_Scripts;
9
10 /**
11 * Helper functions for the WordPress Settings API.
12 *
13 * @package Barn2\barn2-lib
14 * @author Barn2 Plugins <support@barn2.com>
15 * @license GPL-3.0
16 * @copyright Barn2 Media Ltd
17 * @version 1.4
18 */
19 class Settings_API_Helper implements Registerable, Conditional {
20
21 /**
22 * @var Plugin The plugin object.
23 */
24 private $plugin;
25
26 /**
27 * @var Settings_Scripts Responsible for registering any additional settings scripts.
28 */
29 private $scripts;
30
31 public function __construct( Plugin $plugin ) {
32 $this->plugin = $plugin;
33 $this->scripts = new Settings_Scripts( $plugin );
34 }
35
36 public function is_required() {
37 return Util::is_admin();
38 }
39
40 public function register() {
41 $this->scripts->register();
42 }
43
44 public static function add_settings_section( $section, $page, $title, $description_callback, $settings = false ) {
45 if ( ! is_callable( $description_callback ) ) {
46 $description_callback = '__return_false';
47 }
48
49 add_settings_section( $section, $title, $description_callback, $page );
50 self::add_settings_fields( $settings, $section, $page );
51 }
52
53 public static function add_settings_fields( $settings, $section, $page ) {
54 if ( ! $settings || ! is_array( $settings ) ) {
55 return;
56 }
57
58 foreach ( $settings as $setting ) {
59 if ( ! is_array( $setting ) || empty( $setting['id'] ) ) {
60 continue;
61 }
62
63 $args = wp_parse_args( $setting, array_fill_keys( array( 'id', 'type', 'desc', 'label', 'title', 'class', 'field_class', 'default', 'suffix',
64 'custom_attributes' ), '' ) );
65
66 $args['input_class'] = $args['class'];
67 unset( $args['class'] );
68
69 $args['class'] = $args['field_class'];
70 $args['label_for'] = $args['id'];
71
72 $setting_callback = array( __CLASS__, 'settings_field_' . $args['type'] );
73
74 if ( is_callable( $setting_callback ) ) {
75 add_settings_field( $args['id'], $args['title'], $setting_callback, $page, $section, $args );
76 }
77 }
78 }
79
80 public static function settings_field_text( $args ) {
81 $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'regular-text';
82 $type = ! empty( $args['type'] ) ? $args['type'] : 'text';
83 ?>
84 <input id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $class ); ?>" type="<?php echo esc_attr( $type ); ?>" value="<?php echo esc_attr( self::get_value( $args['id'], $args['default'] ) ); ?>"<?php self::custom_attributes( $args ); ?> /><?php
85 if ( ! empty( $args['suffix'] ) ) {
86 echo ' ' . esc_html( $args['suffix'] );
87 }
88 self::field_tooltip( $args );
89 self::field_description( $args );
90 }
91
92 public static function settings_field_number( $args ) {
93 $args['input_class'] = ! empty( $args['input_class'] ) ? $args['input_class'] : 'small-text';
94 $args['type'] = 'number';
95
96 self::field_tooltip( $args );
97 self::settings_field_text( $args );
98 }
99
100 public static function settings_field_textarea( $args ) {
101 $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'large-text';
102 $rows = isset( $args['rows'] ) ? absint( $args['rows'] ) : 4;
103 ?>
104 <textarea id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $class ); ?>" rows="<?php echo esc_attr( $rows ); ?>"<?php self::custom_attributes( $args ); ?>><?php echo esc_textarea( self::get_value( $args['id'], $args['default'] ) ); ?></textarea>
105 <?php
106 self::field_tooltip( $args );
107 self::field_description( $args );
108 }
109
110 public static function settings_field_select( $args ) {
111 $current_value = self::get_value( $args['id'], $args['default'] );
112 ?>
113 <select id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['input_class'] ); ?>"<?php self::custom_attributes( $args ); ?>>
114 <?php foreach ( $args['options'] as $value => $option ) : ?>
115 <option value="<?php echo esc_attr( $value ); ?>"<?php selected( $value, $current_value ); ?>><?php echo esc_html( $option ); ?></option>
116 <?php endforeach; ?>
117 </select>
118 <?php
119 self::field_tooltip( $args );
120
121 if ( ! empty( $args['suffix'] ) ) {
122 echo ' ' . esc_html( $args['suffix'] );
123 }
124
125 self::field_description( $args );
126 }
127
128 public static function settings_field_checkbox( $args ) {
129 $current_value = self::get_value( $args['id'], $args['default'] );
130 ?>
131 <fieldset>
132 <legend class="screen-reader-text"><span><?php echo esc_html( $args['title'] ); ?></span></legend>
133 <label for="<?php echo esc_attr( $args['id'] ); ?>">
134 <input type="checkbox" id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['input_class'] ); ?>"<?php checked( $current_value ); ?> value="1"<?php self::custom_attributes( $args ); ?> />
135 <?php echo esc_html( $args['label'] ); ?>
136 </label>
137 <?php self::field_description( $args ); ?>
138 </fieldset>
139 <?php
140 }
141
142 public static function settings_field_radio( $args ) {
143 $current_value = self::get_value( $args['id'], $args['default'] );
144 ?>
145 <fieldset>
146 <legend class="screen-reader-text"><span><?php echo esc_html( $args['title'] ); ?></span></legend>
147 <?php foreach ( $args['options'] as $value => $label ) : ?>
148 <label>
149 <input type="radio" id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['id'] ); ?>" class="<?php echo esc_attr( $args['input_class'] ); ?>"<?php checked( $value, $current_value ); ?> value="<?php echo esc_attr( $value ); ?>"<?php self::custom_attributes( $args ); ?> />
150 <?php echo esc_html( $label ); ?>
151 </label><br/>
152 <?php endforeach; ?>
153 <?php self::field_description( $args ); ?>
154 </fieldset>
155 <?php
156 self::field_tooltip( $args );
157 }
158
159 public static function settings_field_multicheckbox( $args ) {
160 $current_value = self::get_value( $args['id'], $args['default'] );
161 ?>
162
163 <fieldset>
164 <legend class="screen-reader-text"><?php echo esc_html( $args['title'] ); ?></legend>
165
166 <?php foreach ( $args['options'] as $value => $option ) : ?>
167
168 <label for="<?php echo esc_attr( sprintf( '%1$s-%2$s', $args['id'], $value ) ); ?>">
169
170 <input
171 id="<?php echo esc_attr( sprintf( '%1$s-%2$s', $args['id'], $value ) ); ?>"
172 name="<?php echo esc_attr( sprintf( '%1$s[%2$s]', $args['id'], $value ) ); ?>"
173 class="<?php echo esc_attr( $args['input_class'] ); ?>"
174 type="checkbox"
175 <?php checked( $current_value[ $value ] ); ?>
176 value="1"
177 <?php self::custom_attributes( $args ); ?>
178 />
179 <?php echo esc_html( $option ); ?>
180 </label><br>
181 <?php endforeach; ?>
182 </fieldset>
183
184 <?php
185 self::field_description( $args );
186 self::field_tooltip( $args );
187 }
188
189 public static function settings_field_hidden( $args ) {
190 ?>
191 <input type="hidden" name="<?php echo esc_attr( $args['id'] ); ?>" value="<?php echo esc_attr( $args['default'] ); ?>"<?php self::custom_attributes( $args ); ?> />
192 <?php
193 }
194
195 public static function settings_field_color( $args ) {
196 wp_enqueue_script( 'wp-color-picker' );
197 wp_enqueue_style( 'wp-color-picker' );
198
199 $current_value = self::get_value( $args['id'], $args['default'] );
200 ?>
201 <div class="color-field">
202 <input
203 type="text"
204 name="<?php echo esc_attr( $args['id'] ); ?>"
205 id="<?php echo esc_attr( $args['id'] ); ?>"
206 class="color-picker"
207 value="<?php echo esc_attr( $current_value ); ?>" />
208
209 <?php self::field_description( $args ); ?>
210 </div>
211 <?php
212 }
213
214 public static function settings_field_color_size( $args ) {
215 wp_enqueue_script( 'wp-color-picker' );
216 wp_enqueue_style( 'wp-color-picker' );
217
218 $current_value = self::get_value( $args['id'], $args['default'] );
219
220 $color_id = $args['id'] . '[color]';
221 $color_value = isset( $current_value['color'] ) ? $current_value['color'] : '';
222
223 $size_id = $args['id'] . '[size]';
224 $size_value = isset( $current_value['size'] ) ? $current_value['size'] : '';
225 $size_placeholder = ! empty( $args['placeholder'] ) ? $args['placeholder'] : __( 'Size', 'posts-data-table' );
226
227 if ( empty( $args['custom_attributes'] ) ) {
228 $args['custom_attributes'] = [];
229 }
230
231 $args['custom_attributes'] = array_merge( [ 'min' => 0, 'size' => 4 ], $args['custom_attributes'] );
232 $size_attributes = self::get_custom_attributes( $args );
233 ?>
234 <div class="color-size-field">
235 <input
236 type="text"
237 name="<?php echo esc_attr( $color_id ); ?>"
238 id="<?php echo esc_attr( $color_id ); ?>"
239 class="color-picker"
240 value="<?php echo esc_attr( $color_value ); ?>" />
241 <input
242 type="number"
243 name="<?php echo esc_attr( $size_id ); ?>"
244 id="<?php echo esc_attr( $size_id ); ?>"
245 class="color-size"
246 value="<?php echo esc_attr( $size_value ); ?>"
247 placeholder="<?php echo esc_attr( $size_placeholder ); ?>"
248 <?php echo $size_attributes; ?> />
249
250 <?php self::field_description( $args ); ?>
251 </div>
252 <?php
253 }
254
255 public static function settings_field_help_note( $args ) {
256 self::field_description( $args );
257 }
258
259 private static function field_description( $args ) {
260 if ( ! empty( $args['desc'] ) ) {
261 echo '<p class="description">' . $args['desc'] . '</p>';
262 }
263 }
264
265 private static function field_tooltip( $args ) {
266 if ( ! empty( $args['desc_tip'] ) ) {
267 wp_enqueue_script( 'barn2-tiptip' );
268
269 $tip = self::sanitize_tooltip( $args['desc_tip'] );
270
271 echo '<span class="barn2-help-tip" data-tip="' . $tip . '"></span>';
272 }
273 }
274
275 private static function sanitize_tooltip( $content ) {
276 return htmlspecialchars( wp_kses( html_entity_decode( $content ), array(
277 'br' => [],
278 'em' => [],
279 'strong' => [],
280 'small' => [],
281 'span' => [],
282 'ul' => [],
283 'li' => [],
284 'ol' => [],
285 'p' => [],
286 'a' => [],
287 ) ) );
288 }
289
290 private static function custom_attributes( $args ) {
291 echo self::get_custom_attributes( $args );
292 }
293
294 private static function get_custom_attributes( $args ) {
295 if ( empty( $args['custom_attributes'] ) ) {
296 return '';
297 }
298 $custom_atts = $args['custom_attributes'];
299 $result = '';
300
301 foreach ( $custom_atts as $att => $value ) {
302 $result .= sprintf( ' %s="%s"', sanitize_key( $att ), esc_attr( $value ) );
303 }
304
305 return $result;
306 }
307
308 private static function get_value( $option, $default = false ) {
309 $value = '';
310 $matches = array();
311 $subkey_match = preg_match( '/(\w+)\[(\w+)\]/U', $option, $matches );
312
313 if ( $subkey_match && isset( $matches[1], $matches[2] ) ) {
314 $subkey = $matches[2];
315 $parent_option = get_option( $matches[1], array() );
316 $value = isset( $parent_option[$subkey] ) ? $parent_option[$subkey] : $default;
317 } else {
318 $value = get_option( $option, $default );
319 }
320
321 return $value;
322 }
323
324 }
325