| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file handles rendering the settings fields |
| 4 |
* |
| 5 |
* @since 2.0.0 |
| 6 |
* @package Code_Snippets |
| 7 |
* @subpackage Settings |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Code_Snippets\Settings; |
| 11 |
|
| 12 |
/** |
| 13 |
* Represents a single setting field |
| 14 |
* |
| 15 |
* @property-read string $desc Field description. |
| 16 |
* @property-read string $label Field label. |
| 17 |
* @property-read string $type Field type. |
| 18 |
* @property-read string $name Setting name. |
| 19 |
* |
| 20 |
* @property-read int $min Minimum value (for numerical inputs). |
| 21 |
* @property-read int $max Maximum value(for numerical inputs). |
| 22 |
* @property-read array<string, string> $options List of options for a select or checkboxes field. |
| 23 |
* @property-read callable $render_callback Custom function to use when rendering a callback field. |
| 24 |
* @property-read callable $sanitize_callback Custom function to use when sanitize the setting value. |
| 25 |
* @property-read mixed $default Default setting value. |
| 26 |
* |
| 27 |
* @property-read string $input_name Value of `name` HTML attribute on an input element. |
| 28 |
*/ |
| 29 |
class Setting_Field { |
| 30 |
|
| 31 |
/** |
| 32 |
* Input field identifier. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $field_id; |
| 37 |
|
| 38 |
/** |
| 39 |
* Settings section identifier. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private $section; |
| 44 |
|
| 45 |
/** |
| 46 |
* List of possible arguments. |
| 47 |
* |
| 48 |
* @var array<string, mixed> |
| 49 |
*/ |
| 50 |
private $args = array( |
| 51 |
'desc' => '', |
| 52 |
'label' => '', |
| 53 |
'min' => null, |
| 54 |
'max' => null, |
| 55 |
'options' => [], |
| 56 |
); |
| 57 |
|
| 58 |
/** |
| 59 |
* Class constructor. |
| 60 |
* |
| 61 |
* @param string $section_id Settings section identifier. |
| 62 |
* @param string $field_id Setting field identifier. |
| 63 |
* @param array<string, mixed> $args The setting field attributes. |
| 64 |
*/ |
| 65 |
public function __construct( string $section_id, string $field_id, array $args ) { |
| 66 |
$this->field_id = $field_id; |
| 67 |
$this->section = $section_id; |
| 68 |
$this->args = array_merge( $this->args, $args ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Retrieve a single setting attribute. |
| 73 |
* |
| 74 |
* @param string $argument Attribute name. |
| 75 |
* |
| 76 |
* @return mixed Attribute value. |
| 77 |
*/ |
| 78 |
public function __get( string $argument ) { |
| 79 |
|
| 80 |
if ( 'input_name' === $argument ) { |
| 81 |
return sprintf( 'code_snippets_settings[%s][%s]', $this->section, $this->field_id ); |
| 82 |
} |
| 83 |
|
| 84 |
return $this->args[ $argument ]; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Retrieve the saved value for this setting. |
| 89 |
* |
| 90 |
* @return mixed |
| 91 |
*/ |
| 92 |
private function get_saved_value() { |
| 93 |
return get_setting( $this->section, $this->field_id ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Render the setting field |
| 98 |
*/ |
| 99 |
public function render() { |
| 100 |
$method_name = 'render_' . $this->type . '_field'; |
| 101 |
|
| 102 |
if ( method_exists( $this, $method_name ) ) { |
| 103 |
call_user_func( array( $this, $method_name ) ); |
| 104 |
} else { |
| 105 |
// Error message, not necessary to translate. |
| 106 |
printf( 'Cannot render a %s field.', esc_html( $this->type ) ); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
if ( $this->desc ) { |
| 111 |
echo '<p class="description">', wp_kses_post( $this->desc ), '</p>'; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Render a callback field. |
| 117 |
*/ |
| 118 |
public function render_callback_field() { |
| 119 |
call_user_func( $this->render_callback ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Render a single checkbox field. |
| 124 |
* |
| 125 |
* @param string $input_name Input name. |
| 126 |
* @param string $label Input label. |
| 127 |
* @param boolean $checked Whether the checkbox should be checked. |
| 128 |
*/ |
| 129 |
private static function render_checkbox( string $input_name, string $label, bool $checked ) { |
| 130 |
|
| 131 |
$checkbox = sprintf( |
| 132 |
'<input type="checkbox" name="%s"%s>', |
| 133 |
esc_attr( $input_name ), |
| 134 |
checked( $checked, true, false ) |
| 135 |
); |
| 136 |
|
| 137 |
$kses = [ |
| 138 |
'input' => [ |
| 139 |
'type' => [], |
| 140 |
'name' => [], |
| 141 |
'checked' => [], |
| 142 |
], |
| 143 |
]; |
| 144 |
|
| 145 |
if ( $label ) { |
| 146 |
printf( |
| 147 |
'<label>%s %s</label>', |
| 148 |
wp_kses( $checkbox, $kses ), |
| 149 |
wp_kses_post( $label ) |
| 150 |
); |
| 151 |
} else { |
| 152 |
echo wp_kses( $checkbox, $kses ); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Render a checkbox field for a setting |
| 158 |
* |
| 159 |
* @since 2.0.0 |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function render_checkbox_field() { |
| 164 |
$this->render_checkbox( $this->input_name, $this->label, $this->get_saved_value() ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Render a checkbox field for a setting |
| 169 |
* |
| 170 |
* @since 2.0.0 |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function render_checkboxes_field() { |
| 175 |
$saved_value = $this->get_saved_value(); |
| 176 |
$saved_value = is_array( $saved_value ) ? $saved_value : []; |
| 177 |
|
| 178 |
echo '<fieldset>'; |
| 179 |
printf( '<legend class="screen-reader-text"><span>%s</span></legend>', esc_html( $this->name ) ); |
| 180 |
|
| 181 |
foreach ( $this->options as $option => $label ) { |
| 182 |
$this->render_checkbox( $this->input_name . "[$option]", $label, in_array( $option, $saved_value, true ) ); |
| 183 |
echo '<br>'; |
| 184 |
} |
| 185 |
|
| 186 |
echo '</fieldset>'; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Render a basic text field for an editor setting. |
| 191 |
* |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
private function render_text_field() { |
| 195 |
printf( |
| 196 |
'<input type="text" name="%s" value="%s" class="regular-text">', |
| 197 |
esc_attr( $this->input_name ), |
| 198 |
esc_attr( $this->get_saved_value() ) |
| 199 |
); |
| 200 |
|
| 201 |
if ( $this->label ) { |
| 202 |
echo ' ' . wp_kses_post( $this->label ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Render a number select field for an editor setting |
| 208 |
* |
| 209 |
* @since 2.0.0 |
| 210 |
*/ |
| 211 |
private function render_number_field() { |
| 212 |
printf( |
| 213 |
'<input type="number" name="%s" value="%s"', |
| 214 |
esc_attr( $this->input_name ), |
| 215 |
esc_attr( $this->get_saved_value() ) |
| 216 |
); |
| 217 |
|
| 218 |
if ( is_numeric( $this->min ) ) { |
| 219 |
printf( ' min="%d"', intval( $this->min ) ); |
| 220 |
} |
| 221 |
|
| 222 |
if ( is_numeric( $this->max ) ) { |
| 223 |
printf( ' max="%d"', intval( $this->max ) ); |
| 224 |
} |
| 225 |
|
| 226 |
echo '>'; |
| 227 |
|
| 228 |
if ( $this->label ) { |
| 229 |
echo ' ' . wp_kses_post( $this->label ); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Render a number select field for an editor setting |
| 235 |
* |
| 236 |
* @since 3.0.0 |
| 237 |
*/ |
| 238 |
private function render_select_field() { |
| 239 |
$saved_value = $this->get_saved_value(); |
| 240 |
printf( '<select name="%s">', esc_attr( $this->input_name ) ); |
| 241 |
|
| 242 |
foreach ( $this->options as $option => $option_label ) { |
| 243 |
printf( |
| 244 |
'<option value="%s"%s>%s</option>', |
| 245 |
esc_attr( $option ), |
| 246 |
selected( $option, $saved_value, false ), |
| 247 |
esc_html( $option_label ) |
| 248 |
); |
| 249 |
} |
| 250 |
|
| 251 |
echo '</select>'; |
| 252 |
} |
| 253 |
} |
| 254 |
|