PluginProbe
Posts Table with Search & Sort / 1.3.5
Posts Table with Search & Sort v1.3.5
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.5, at lib/Admin/Settings_API_Helper.php

324 lines 12.5 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 ?>
161 <fieldset>
162 <legend class="screen-reader-text"><?php echo esc_html( $args['title'] ); ?></legend>
163 <?php
164 foreach ( $args['options'] as $value => $option ) :
165 $current_value = self::get_value( sprintf( '%1$s[%2$s]', $args['id'], $value ), $args['default'][$value] );
166 ?>
167 <label for="<?php echo esc_attr( sprintf( '%1$s-%2$s', $args['id'], $value ) ); ?>">
168 <input
169 id="<?php echo esc_attr( sprintf( '%1$s-%2$s', $args['id'], $value ) ); ?>"
170 name="<?php echo esc_attr( sprintf( '%1$s[%2$s]', $args['id'], $value ) ); ?>"
171 class="<?php echo esc_attr( $args['input_class'] ); ?>"
172 type="checkbox"
173 <?php checked( $current_value ); ?>
174 value="1"
175 <?php self::custom_attributes( $args ); ?>
176 />
177 <?php echo esc_html( $option ); ?>
178 </label>
179 <br>
180 <?php endforeach; ?>
181
182 </fieldset>
183 <?php
184 self::field_description( $args );
185 self::field_tooltip( $args );
186 }
187
188 public static function settings_field_hidden( $args ) {
189 ?>
190 <input type="hidden" name="<?php echo esc_attr( $args['id'] ); ?>" value="<?php echo esc_attr( $args['default'] ); ?>"<?php self::custom_attributes( $args ); ?> />
191 <?php
192 }
193
194 public static function settings_field_color( $args ) {
195 wp_enqueue_script( 'wp-color-picker' );
196 wp_enqueue_style( 'wp-color-picker' );
197
198 $current_value = self::get_value( $args['id'], $args['default'] );
199 ?>
200 <div class="color-field">
201 <input
202 type="text"
203 name="<?php echo esc_attr( $args['id'] ); ?>"
204 id="<?php echo esc_attr( $args['id'] ); ?>"
205 class="color-picker"
206 value="<?php echo esc_attr( $current_value ); ?>" />
207
208 <?php self::field_description( $args ); ?>
209 </div>
210 <?php
211 }
212
213 public static function settings_field_color_size( $args ) {
214 wp_enqueue_script( 'wp-color-picker' );
215 wp_enqueue_style( 'wp-color-picker' );
216
217 $current_value = self::get_value( $args['id'], $args['default'] );
218
219 $color_id = $args['id'] . '[color]';
220 $color_value = isset( $current_value['color'] ) ? $current_value['color'] : '';
221
222 $size_id = $args['id'] . '[size]';
223 $size_value = isset( $current_value['size'] ) ? $current_value['size'] : '';
224 $size_placeholder = ! empty( $args['placeholder'] ) ? $args['placeholder'] : __( 'Size', 'posts-data-table' );
225
226 if ( empty( $args['custom_attributes'] ) ) {
227 $args['custom_attributes'] = [];
228 }
229
230 $args['custom_attributes'] = array_merge( [ 'min' => 0, 'size' => 4 ], $args['custom_attributes'] );
231 $size_attributes = self::get_custom_attributes( $args );
232 ?>
233 <div class="color-size-field">
234 <input
235 type="text"
236 name="<?php echo esc_attr( $color_id ); ?>"
237 id="<?php echo esc_attr( $color_id ); ?>"
238 class="color-picker"
239 value="<?php echo esc_attr( $color_value ); ?>" />
240 <input
241 type="number"
242 name="<?php echo esc_attr( $size_id ); ?>"
243 id="<?php echo esc_attr( $size_id ); ?>"
244 class="color-size"
245 value="<?php echo esc_attr( $size_value ); ?>"
246 placeholder="<?php echo esc_attr( $size_placeholder ); ?>"
247 <?php echo $size_attributes; ?> />
248
249 <?php self::field_description( $args ); ?>
250 </div>
251 <?php
252 }
253
254 public static function settings_field_help_note( $args ) {
255 self::field_description( $args );
256 }
257
258 private static function field_description( $args ) {
259 if ( ! empty( $args['desc'] ) ) {
260 echo '<p class="description">' . $args['desc'] . '</p>';
261 }
262 }
263
264 private static function field_tooltip( $args ) {
265 if ( ! empty( $args['desc_tip'] ) ) {
266 wp_enqueue_script( 'barn2-tiptip' );
267
268 $tip = self::sanitize_tooltip( $args['desc_tip'] );
269
270 echo '<span class="barn2-help-tip" data-tip="' . $tip . '"></span>';
271 }
272 }
273
274 private static function sanitize_tooltip( $content ) {
275 return htmlspecialchars( wp_kses( html_entity_decode( $content ), array(
276 'br' => [],
277 'em' => [],
278 'strong' => [],
279 'small' => [],
280 'span' => [],
281 'ul' => [],
282 'li' => [],
283 'ol' => [],
284 'p' => [],
285 'a' => [],
286 ) ) );
287 }
288
289 private static function custom_attributes( $args ) {
290 echo self::get_custom_attributes( $args );
291 }
292
293 private static function get_custom_attributes( $args ) {
294 if ( empty( $args['custom_attributes'] ) ) {
295 return '';
296 }
297 $custom_atts = $args['custom_attributes'];
298 $result = '';
299
300 foreach ( $custom_atts as $att => $value ) {
301 $result .= sprintf( ' %s="%s"', sanitize_key( $att ), esc_attr( $value ) );
302 }
303
304 return $result;
305 }
306
307 private static function get_value( $option, $default = false ) {
308 $value = '';
309 $matches = array();
310 $subkey_match = preg_match( '/(\w+)\[(\w+)\]/U', $option, $matches );
311
312 if ( $subkey_match && isset( $matches[1], $matches[2] ) ) {
313 $subkey = $matches[2];
314 $parent_option = get_option( $matches[1], array() );
315 $value = isset( $parent_option[$subkey] ) ? $parent_option[$subkey] : $default;
316 } else {
317 $value = get_option( $option, $default );
318 }
319
320 return $value;
321 }
322
323 }
324