| 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 <info@barn2.co.uk> |
| 9 |
* @license GPL-3.0 |
| 10 |
* @copyright Barn2 Media Ltd |
| 11 |
* @version 1.2 |
| 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 |
\add_settings_section( $section, $title, $description_callback, $page ); |
| 20 |
self::add_settings_fields( $settings, $section, $page ); |
| 21 |
} |
| 22 |
|
| 23 |
public static function add_settings_fields( $settings, $section, $page ) { |
| 24 |
if ( ! $settings || ! \is_array( $settings ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
foreach ( $settings as $setting ) { |
| 28 |
if ( ! \is_array( $setting ) || empty( $setting['id'] ) ) { |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
$args = \wp_parse_args( $setting, \array_fill_keys( array( 'id', 'type', 'desc', 'label', 'title', 'class', 'field_class', 'default', 'suffix', 'custom_attributes' ), '' ) ); |
| 33 |
|
| 34 |
$args['input_class'] = $args['class']; |
| 35 |
unset( $args['class'] ); |
| 36 |
|
| 37 |
$args['class'] = $args['field_class']; |
| 38 |
$args['label_for'] = $args['id']; |
| 39 |
|
| 40 |
$setting_callback = array( __CLASS__, 'settings_field_' . $args['type'] ); |
| 41 |
|
| 42 |
if ( \is_callable( $setting_callback ) ) { |
| 43 |
\add_settings_field( $args['id'], $args['title'], $setting_callback, $page, $section, $args ); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static function settings_field_text( $args ) { |
| 49 |
$class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'regular-text'; |
| 50 |
$type = ! empty( $args['type'] ) ? $args['type'] : 'text'; |
| 51 |
?> |
| 52 |
<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 |
| 53 |
if ( ! empty( $args['suffix'] ) ) { |
| 54 |
echo ' ' . \esc_html( $args['suffix'] ); |
| 55 |
} |
| 56 |
self::field_description( $args ); |
| 57 |
} |
| 58 |
|
| 59 |
public static function settings_field_number( $args ) { |
| 60 |
$args['input_class'] = ! empty( $args['input_class'] ) ? $args['input_class'] : 'small-text'; |
| 61 |
$args['type'] = 'number'; |
| 62 |
self::settings_field_text( $args ); |
| 63 |
} |
| 64 |
|
| 65 |
public static function settings_field_textarea( $args ) { |
| 66 |
$class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'large-text'; |
| 67 |
$rows = isset( $args['rows'] ) ? \absint( $args['rows'] ) : 4; |
| 68 |
?> |
| 69 |
<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> |
| 70 |
<?php |
| 71 |
self::field_description( $args ); |
| 72 |
} |
| 73 |
|
| 74 |
public static function settings_field_select( $args ) { |
| 75 |
$current_value = self::get_value( $args['id'], $args['default'] ); |
| 76 |
?> |
| 77 |
<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 ); ?>> |
| 78 |
<?php foreach ( $args['options'] as $value => $option ) : ?> |
| 79 |
<option value="<?php echo \esc_attr( $value ); ?>"<?php \selected( $value, $current_value ); ?>><?php echo \esc_html( $option ); ?></option> |
| 80 |
<?php endforeach; ?> |
| 81 |
</select><?php |
| 82 |
if ( ! empty( $args['suffix'] ) ) { |
| 83 |
echo ' ' . \esc_html( $args['suffix'] ); |
| 84 |
} |
| 85 |
self::field_description( $args ); |
| 86 |
} |
| 87 |
|
| 88 |
public static function settings_field_checkbox( $args ) { |
| 89 |
$current_value = self::get_value( $args['id'], $args['default'] ); |
| 90 |
?> |
| 91 |
<fieldset> |
| 92 |
<legend class="screen-reader-text"><?php echo \esc_html( $args['title'] ); ?></legend> |
| 93 |
<label for="<?php echo \esc_attr( $args['id'] ); ?>"> |
| 94 |
<input id="<?php echo \esc_attr( $args['id'] ); ?>" name="<?php echo \esc_attr( $args['id'] ); ?>" class="<?php echo \esc_attr( $args['input_class'] ); ?>" type="checkbox"<?php \checked( $current_value ); ?> value="1"<?php self::custom_attributes( $args ); ?>/> |
| 95 |
<?php |
| 96 |
if ( ! empty( $args['label'] ) ) { |
| 97 |
echo \esc_html( $args['label'] ); |
| 98 |
} |
| 99 |
?> |
| 100 |
</label> |
| 101 |
</fieldset> |
| 102 |
<?php self::field_description( $args ); ?> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
public static function settings_field_hidden( $args ) { |
| 107 |
?> |
| 108 |
<input type="hidden" name="<?php echo \esc_attr( $args['id'] ); ?>" value="<?php echo \esc_attr( $args['default'] ); ?>" /> |
| 109 |
<?php |
| 110 |
} |
| 111 |
|
| 112 |
private static function field_description( $args ) { |
| 113 |
if ( ! empty( $args['desc'] ) ) { |
| 114 |
echo '<p class="description">' . $args['desc'] . '</p>'; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private static function custom_attributes( $args ) { |
| 119 |
echo self::get_custom_attributes( $args ); |
| 120 |
} |
| 121 |
|
| 122 |
private static function get_custom_attributes( $args ) { |
| 123 |
if ( empty( $args['custom_attributes'] ) ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
$custom_atts = $args['custom_attributes']; |
| 127 |
$result = ''; |
| 128 |
|
| 129 |
foreach ( $custom_atts as $att => $value ) { |
| 130 |
$result .= \sprintf( ' %s="%s"', \sanitize_key( $att ), \esc_attr( $value ) ); |
| 131 |
} |
| 132 |
return $result; |
| 133 |
} |
| 134 |
|
| 135 |
private static function get_value( $option, $default = false ) { |
| 136 |
$value = ''; |
| 137 |
$matches = array(); |
| 138 |
$subkey_match = \preg_match( '/(\w+)\[(\w+)\]/U', $option, $matches ); |
| 139 |
|
| 140 |
if ( $subkey_match && isset( $matches[1], $matches[2] ) ) { |
| 141 |
$subkey = $matches[2]; |
| 142 |
$parent_option = \get_option( $matches[1], array() ); |
| 143 |
$value = isset( $parent_option[$subkey] ) ? $parent_option[$subkey] : $default; |
| 144 |
} else { |
| 145 |
$value = \get_option( $option, $default ); |
| 146 |
} |
| 147 |
|
| 148 |
return $value; |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|