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

226 lines 9.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 /**
5 * Helper functions for the WordPress Settings API.
6 *
7 * @package Barn2\barn2-lib
8 * @author Barn2 Plugins <support@barn2.co.uk>
9 * @license GPL-3.0
10 * @copyright Barn2 Media Ltd
11 * @version 1.3.3
12 */
13 class Settings_API_Helper {
14
15 public static function add_settings_section( $section, $page, $title, $description_callback, $settings = false ) {
16 if ( ! is_callable( $description_callback ) ) {
17 $description_callback = '__return_false';
18 }
19
20 add_settings_section( $section, $title, $description_callback, $page );
21 self::add_settings_fields( $settings, $section, $page );
22 }
23
24 public static function add_settings_fields( $settings, $section, $page ) {
25 if ( ! $settings || ! is_array( $settings ) ) {
26 return;
27 }
28
29 foreach ( $settings as $setting ) {
30 if ( ! is_array( $setting ) || empty( $setting['id'] ) ) {
31 continue;
32 }
33
34 $args = wp_parse_args( $setting, array_fill_keys( array( 'id', 'type', 'desc', 'label', 'title', 'class', 'field_class', 'default', 'suffix',
35 'custom_attributes' ), '' ) );
36
37 $args['input_class'] = $args['class'];
38 unset( $args['class'] );
39
40 $args['class'] = $args['field_class'];
41 $args['label_for'] = $args['id'];
42
43 $setting_callback = array( __CLASS__, 'settings_field_' . $args['type'] );
44
45 if ( is_callable( $setting_callback ) ) {
46 add_settings_field( $args['id'], $args['title'], $setting_callback, $page, $section, $args );
47 }
48 }
49 }
50
51 public static function settings_field_text( $args ) {
52 $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'regular-text';
53 $type = ! empty( $args['type'] ) ? $args['type'] : 'text';
54 ?>
55 <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
56 if ( ! empty( $args['suffix'] ) ) {
57 echo ' ' . esc_html( $args['suffix'] );
58 }
59 self::field_description( $args );
60 }
61
62 public static function settings_field_number( $args ) {
63 $args['input_class'] = ! empty( $args['input_class'] ) ? $args['input_class'] : 'small-text';
64 $args['type'] = 'number';
65 self::settings_field_text( $args );
66 }
67
68 public static function settings_field_textarea( $args ) {
69 $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'large-text';
70 $rows = isset( $args['rows'] ) ? absint( $args['rows'] ) : 4;
71 ?>
72 <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>
73 <?php
74 self::field_description( $args );
75 }
76
77 public static function settings_field_select( $args ) {
78 $current_value = self::get_value( $args['id'], $args['default'] );
79 ?>
80 <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 ); ?>>
81 <?php foreach ( $args['options'] as $value => $option ) : ?>
82 <option value="<?php echo esc_attr( $value ); ?>"<?php selected( $value, $current_value ); ?>><?php echo esc_html( $option ); ?></option>
83 <?php endforeach; ?>
84 </select><?php
85 if ( ! empty( $args['suffix'] ) ) {
86 echo ' ' . esc_html( $args['suffix'] );
87 }
88 self::field_description( $args );
89 }
90
91 public static function settings_field_checkbox( $args ) {
92 $current_value = self::get_value( $args['id'], $args['default'] );
93 ?>
94 <fieldset>
95 <legend class="screen-reader-text"><span><?php echo esc_html( $args['title'] ); ?></span></legend>
96 <label for="<?php echo esc_attr( $args['id'] ); ?>">
97 <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 ); ?> />
98 <?php echo esc_html( $args['label'] ); ?>
99 </label>
100 <?php self::field_description( $args ); ?>
101 </fieldset>
102 <?php
103 }
104
105 public static function settings_field_radio( $args ) {
106 $current_value = self::get_value( $args['id'], $args['default'] );
107 ?>
108 <fieldset>
109 <legend class="screen-reader-text"><span><?php echo esc_html( $args['title'] ); ?></span></legend>
110 <?php foreach ( $args['options'] as $value => $label ) : ?>
111 <label>
112 <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 ); ?> />
113 <?php echo esc_html( $label ); ?>
114 </label><br/>
115 <?php endforeach; ?>
116 <?php self::field_description( $args ); ?>
117 </fieldset>
118 <?php
119 }
120
121 public static function settings_field_hidden( $args ) {
122 ?>
123 <input type="hidden" name="<?php echo esc_attr( $args['id'] ); ?>" value="<?php echo esc_attr( $args['default'] ); ?>"<?php self::custom_attributes( $args ); ?> />
124 <?php
125 }
126
127 public static function settings_field_color( $args ) {
128 $current_value = self::get_value( $args['id'], $args['default'] );
129 ?>
130 <div class="color-field">
131 <input
132 type="text"
133 name="<?php echo esc_attr( $args['id'] ); ?>"
134 id="<?php echo esc_attr( $args['id'] ); ?>"
135 class="color-picker"
136 value="<?php echo esc_attr( $current_value ); ?>" />
137
138 <?php self::field_description( $args ); ?>
139 </div>
140 <?php
141 }
142
143 public static function settings_field_color_size( $args ) {
144 $current_value = self::get_value( $args['id'], $args['default'] );
145
146 $color_id = $args['id'] . '[color]';
147 $color_value = isset( $current_value['color'] ) ? $current_value['color'] : '';
148
149 $size_id = $args['id'] . '[size]';
150 $size_value = isset( $current_value['size'] ) ? $current_value['size'] : '';
151 $size_placeholder = ! empty( $args['placeholder'] ) ? $args['placeholder'] : __( 'Size', 'posts-data-table' );
152
153 if ( empty( $args['custom_attributes'] ) ) {
154 $args['custom_attributes'] = [];
155 }
156
157 $args['custom_attributes'] = array_merge( [ 'min' => 0, 'size' => 4 ], $args['custom_attributes'] );
158 $size_attributes = self::get_custom_attributes( $args );
159 ?>
160 <div class="color-size-field">
161 <input
162 type="text"
163 name="<?php echo esc_attr( $color_id ); ?>"
164 id="<?php echo esc_attr( $color_id ); ?>"
165 class="color-picker"
166 value="<?php echo esc_attr( $color_value ); ?>" />
167 <input
168 type="number"
169 name="<?php echo esc_attr( $size_id ); ?>"
170 id="<?php echo esc_attr( $size_id ); ?>"
171 class="color-size"
172 value="<?php echo esc_attr( $size_value ); ?>"
173 placeholder="<?php echo esc_attr( $size_placeholder ); ?>"
174 <?php echo $size_attributes; ?> />
175
176 <?php self::field_description( $args ); ?>
177 </div>
178 <?php
179 }
180
181 public static function settings_field_help_note( $args ) {
182 self::field_description( $args );
183 }
184
185 private static function field_description( $args ) {
186 if ( ! empty( $args['desc'] ) ) {
187 echo '<p class="description">' . $args['desc'] . '</p>';
188 }
189 }
190
191 private static function custom_attributes( $args ) {
192 echo self::get_custom_attributes( $args );
193 }
194
195 private static function get_custom_attributes( $args ) {
196 if ( empty( $args['custom_attributes'] ) ) {
197 return '';
198 }
199 $custom_atts = $args['custom_attributes'];
200 $result = '';
201
202 foreach ( $custom_atts as $att => $value ) {
203 $result .= sprintf( ' %s="%s"', sanitize_key( $att ), esc_attr( $value ) );
204 }
205
206 return $result;
207 }
208
209 private static function get_value( $option, $default = false ) {
210 $value = '';
211 $matches = array();
212 $subkey_match = preg_match( '/(\w+)\[(\w+)\]/U', $option, $matches );
213
214 if ( $subkey_match && isset( $matches[1], $matches[2] ) ) {
215 $subkey = $matches[2];
216 $parent_option = get_option( $matches[1], array() );
217 $value = isset( $parent_option[$subkey] ) ? $parent_option[$subkey] : $default;
218 } else {
219 $value = get_option( $option, $default );
220 }
221
222 return $value;
223 }
224
225 }
226