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