| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin form view. |
| 4 |
* |
| 5 |
* This is responsible for rendering the output of forms in the WordPress dashboard. |
| 6 |
* |
| 7 |
* @package Charitable/Forms/Charitable_Admin_Form_View |
| 8 |
* @author Eric Daams |
| 9 |
* @copyright Copyright (c) 2019, Studio 164a |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 1.5.0 |
| 12 |
* @version 1.5.9 |
| 13 |
*/ |
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Admin_Form_View' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Admin_Form_View. |
| 22 |
* |
| 23 |
* @since 1.5.0 |
| 24 |
*/ |
| 25 |
class Charitable_Admin_Form_View implements Charitable_Form_View_Interface { |
| 26 |
|
| 27 |
/** |
| 28 |
* The Form we are responsible for rendering. |
| 29 |
* |
| 30 |
* @since 1.5.0 |
| 31 |
* |
| 32 |
* @var Charitable_Form |
| 33 |
*/ |
| 34 |
protected $form; |
| 35 |
|
| 36 |
/** |
| 37 |
* Current WP_Post object. |
| 38 |
* |
| 39 |
* @since 1.5.0 |
| 40 |
* |
| 41 |
* @var WP_Post |
| 42 |
*/ |
| 43 |
protected $post; |
| 44 |
|
| 45 |
/** |
| 46 |
* Custom keys for the current post. |
| 47 |
* |
| 48 |
* @since 1.5.0 |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
protected $post_custom_keys; |
| 53 |
|
| 54 |
/** |
| 55 |
* Current tabindex. |
| 56 |
* |
| 57 |
* @since 1.5.0 |
| 58 |
* |
| 59 |
* @var int |
| 60 |
*/ |
| 61 |
protected $tabindex; |
| 62 |
|
| 63 |
/** |
| 64 |
* Set up view instance. |
| 65 |
* |
| 66 |
* @since 1.5.0 |
| 67 |
* |
| 68 |
* @param Charitable_Form $form Form object. |
| 69 |
*/ |
| 70 |
public function __construct( Charitable_Form $form ) { |
| 71 |
$this->form = $form; |
| 72 |
$this->tabindex = 0; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Render a form. |
| 77 |
* |
| 78 |
* @since 1.5.0 |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function render() { |
| 83 |
$this->render_hidden_fields(); |
| 84 |
$this->render_fields(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Render a form's hidden fields. |
| 89 |
* |
| 90 |
* @since 1.5.0 |
| 91 |
* |
| 92 |
* @return boolean True if any fields were rendered. False otherwise. |
| 93 |
*/ |
| 94 |
public function render_hidden_fields() { |
| 95 |
$fields = $this->form->get_hidden_fields(); |
| 96 |
|
| 97 |
if ( ! is_array( $fields ) || empty( $fields ) ) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
array_walk( $fields, array( $this, 'render_hidden_field' ) ); |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Render notices before the form. |
| 108 |
* |
| 109 |
* @since 1.5.0 |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function render_notices() { |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Render all of a form's fields. |
| 118 |
* |
| 119 |
* @since 1.5.0 |
| 120 |
* |
| 121 |
* @param array $fields Optional. A set of fields to display. If not set, |
| 122 |
* we will look for fields in the form's `get_fields()` |
| 123 |
* method (if it has one). |
| 124 |
* @return boolean True if any fields were rendered. False otherwsie. |
| 125 |
*/ |
| 126 |
public function render_fields( $fields = array() ) { |
| 127 |
if ( empty( $fields ) ) { |
| 128 |
$fields = $this->form->get_fields(); |
| 129 |
|
| 130 |
/* If still missing fields, return false and throw an error. */ |
| 131 |
if ( empty( $fields ) ) { |
| 132 |
charitable_get_deprecated()->doing_it_wrong( |
| 133 |
__METHOD__, |
| 134 |
__( 'There are no fields to render.', 'charitable' ), |
| 135 |
'1.5.0' |
| 136 |
); |
| 137 |
return false; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
array_walk( $fields, array( $this, 'render_field' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Render a hidden field. |
| 146 |
* |
| 147 |
* @since 1.5.0 |
| 148 |
* |
| 149 |
* @param string $value Field value. |
| 150 |
* @param string $key Field key. |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function render_hidden_field( $value, $key ) { |
| 154 |
printf( '<input type="hidden" name="%s" value="%s" />', esc_attr( $key ), esc_attr( $value ) ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Render a specific form field. |
| 159 |
* |
| 160 |
* @since 1.5.0 |
| 161 |
* |
| 162 |
* @param array $field Field definition. |
| 163 |
* @param string $key Field key. |
| 164 |
* @param array $args Unused. Mixed array of arguments. |
| 165 |
* @return boolean False if the field was not rendered. True otherwise. |
| 166 |
*/ |
| 167 |
public function render_field( $field, $key, $args = array() ) { |
| 168 |
$field['form_view'] = $this; |
| 169 |
$field['view'] = $this->get_field_view( $field ); |
| 170 |
$field['type'] = $this->get_field_type( $field ); |
| 171 |
$field['key'] = $this->get_field_key( $field, $key ); |
| 172 |
$field['id'] = $this->get_field_id( $field ); |
| 173 |
$field['wrapper_id'] = 'charitable-' . $field['id'] . '-wrap'; |
| 174 |
$field['wrapper_class'] = $this->get_field_class( $field ); |
| 175 |
$field['tabindex'] = array_key_exists( 'tabindex', $field ) ? (int) $field['tabindex'] : $this->tabindex; |
| 176 |
|
| 177 |
if ( 'checkbox' == $field['type'] ) { |
| 178 |
$field['checked'] = $this->is_field_checked( $field ); |
| 179 |
} else { |
| 180 |
$field['value'] = $this->get_field_value( $field ); |
| 181 |
} |
| 182 |
|
| 183 |
/* Increment the tabindex. */ |
| 184 |
$this->tabindex = $field['tabindex'] + 1; |
| 185 |
|
| 186 |
return charitable_admin_view( $field['view'], $field ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Checks whether a field has all required arguments. |
| 191 |
* |
| 192 |
* @since 1.5.0 |
| 193 |
* |
| 194 |
* @param array $view_args The view args. |
| 195 |
* @return boolean |
| 196 |
*/ |
| 197 |
public function field_has_required_args( $view_args ) { |
| 198 |
$required = array( 'view', 'key', 'id', 'wrapper_id', 'type' ); |
| 199 |
$missing = count( array_diff( $required, array_keys( $view_args ) ) ); |
| 200 |
|
| 201 |
if ( $missing > 0 ) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
$type = $view_args['type']; |
| 206 |
|
| 207 |
if ( 'fieldset' == $type ) { |
| 208 |
return array_key_exists( 'fields', $view_args ) && is_array( $view_args['fields'] ) && count( $view_args['fields'] ); |
| 209 |
} |
| 210 |
|
| 211 |
if ( $this->field_needs_options( $type ) ) { |
| 212 |
return array_key_exists( 'options', $view_args ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( 'checkbox' == $type && ! array_key_exists( 'checked', $view_args ) ) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
return array_key_exists( 'value', $view_args ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Return the field view based on a field 'type'. |
| 224 |
* |
| 225 |
* @since 1.5.0 |
| 226 |
* |
| 227 |
* @param array $field Field definition. |
| 228 |
* @return string |
| 229 |
*/ |
| 230 |
protected function get_field_view( $field ) { |
| 231 |
if ( array_key_exists( 'view', $field ) ) { |
| 232 |
return $field['view']; |
| 233 |
} |
| 234 |
|
| 235 |
if ( $this->use_default_field_template( $field['type'] ) ) { |
| 236 |
return 'metaboxes/field-types/default'; |
| 237 |
} |
| 238 |
|
| 239 |
switch ( $field['type'] ) { |
| 240 |
case 'charitable-fieldset': |
| 241 |
return 'metaboxes/field-types/fieldset'; |
| 242 |
|
| 243 |
default: |
| 244 |
return 'metaboxes/field-types/' . $field['type']; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Return the field type. |
| 250 |
* |
| 251 |
* @since 1.5.0 |
| 252 |
* |
| 253 |
* @param array $field Field definition. |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
protected function get_field_type( $field ) { |
| 257 |
if ( array_key_exists( 'type', $field ) ) { |
| 258 |
return $field['type']; |
| 259 |
} |
| 260 |
|
| 261 |
return str_replace( 'metaboxes/field-types/', '', $field['view'] ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Return the field wrapper class. |
| 266 |
* |
| 267 |
* @since 1.5.7 |
| 268 |
* |
| 269 |
* @param array $field Field definition. |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
protected function get_field_class( $field ) { |
| 273 |
$classes = array( |
| 274 |
'charitable-metabox-wrap', |
| 275 |
'charitable-' . $this->get_field_type( $field ) . '-wrap', |
| 276 |
); |
| 277 |
|
| 278 |
if ( array_key_exists( 'wrapper_class', $field ) ) { |
| 279 |
$classes = array_merge( $classes, $field['wrapper_class'] ); |
| 280 |
} |
| 281 |
|
| 282 |
return implode( ' ', $classes ); |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Return the key for a particular field. |
| 288 |
* |
| 289 |
* @since 1.5.0 |
| 290 |
* |
| 291 |
* @param array $field Field definition. |
| 292 |
* @param string $default The key of the form field. This is the fallback option. |
| 293 |
* @return string|void |
| 294 |
*/ |
| 295 |
protected function get_field_key( $field, $default ) { |
| 296 |
foreach ( array( 'key', 'meta_key' ) as $key ) { |
| 297 |
if ( array_key_exists( $key, $field ) ) { |
| 298 |
return $field[ $key ]; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
return $default; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Return the ID for a particular field. |
| 307 |
* |
| 308 |
* @since 1.5.3 |
| 309 |
* |
| 310 |
* @param array $field Field definition. |
| 311 |
* @return string |
| 312 |
*/ |
| 313 |
protected function get_field_id( $field ) { |
| 314 |
if ( array_key_exists( 'id', $field ) ) { |
| 315 |
return $field['id']; |
| 316 |
} |
| 317 |
|
| 318 |
return str_replace( '_', '-', trim( preg_replace( '![^a-z0-9]+!i', '-', $field['key'] ), '-' ) ); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Return the current value of a particular field. |
| 323 |
* |
| 324 |
* @since 1.5.0 |
| 325 |
* |
| 326 |
* @param array $field Field definition. |
| 327 |
* @return string |
| 328 |
*/ |
| 329 |
protected function get_field_value( $field ) { |
| 330 |
if ( array_key_exists( 'value', $field ) ) { |
| 331 |
return $field['value']; |
| 332 |
} |
| 333 |
|
| 334 |
return $this->get_current_value( $field ); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Check whether the field is checked. |
| 339 |
* |
| 340 |
* @since 1.5.0 |
| 341 |
* |
| 342 |
* @param array $field Field definition. |
| 343 |
* @return boolean |
| 344 |
*/ |
| 345 |
protected function is_field_checked( $field ) { |
| 346 |
if ( array_key_exists( 'checked', $field ) ) { |
| 347 |
return $field['checked']; |
| 348 |
} |
| 349 |
|
| 350 |
$value = $this->get_current_value( $field ); |
| 351 |
|
| 352 |
if ( array_key_exists( 'value', $field ) ) { |
| 353 |
return $field['value'] == $value; |
| 354 |
} |
| 355 |
|
| 356 |
return false != $value; |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Fetches the current value for a field, ignoring the 'value' setting of the field. |
| 361 |
* |
| 362 |
* @since 1.5.0 |
| 363 |
* |
| 364 |
* @param array $field Field definition. |
| 365 |
* @return mixed |
| 366 |
*/ |
| 367 |
protected function get_current_value( $field ) { |
| 368 |
global $post; |
| 369 |
|
| 370 |
if ( empty( $field['key'] ) ) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
$default = array_key_exists( 'default', $field ) ? $field['default'] : ''; |
| 375 |
$value = get_post_meta( $post->ID, $field['key'], true ); |
| 376 |
|
| 377 |
return $value ? $value : $default; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Return the custom keys for the current post. |
| 382 |
* |
| 383 |
* @since 1.5.0 |
| 384 |
* |
| 385 |
* @return array |
| 386 |
*/ |
| 387 |
protected function get_post_custom_keys() { |
| 388 |
if ( ! is_a( $this->post, 'WP_Post' ) ) { |
| 389 |
return array(); |
| 390 |
} |
| 391 |
|
| 392 |
if ( ! isset( $this->post_custom_keys ) ) { |
| 393 |
$this->post_custom_keys = get_post_custom_keys( $this->post->ID ); |
| 394 |
|
| 395 |
if ( ! is_array( $this->post_custom_keys ) ) { |
| 396 |
$this->post_custom_keys = array(); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
return $this->post_custom_keys; |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Whether the given field type can use the default field template. |
| 405 |
* |
| 406 |
* @since 1.5.0 |
| 407 |
* |
| 408 |
* @param string $field_type Type of field. |
| 409 |
* @return boolean |
| 410 |
*/ |
| 411 |
protected function use_default_field_template( $field_type ) { |
| 412 |
/** |
| 413 |
* Filter the list of field types that use the default template. |
| 414 |
* |
| 415 |
* @since 1.0.0 |
| 416 |
* |
| 417 |
* @param string[] $types Field types. |
| 418 |
*/ |
| 419 |
$default_field_types = apply_filters( |
| 420 |
'charitable_default_template_field_types', |
| 421 |
array( |
| 422 |
'text', |
| 423 |
'email', |
| 424 |
'password', |
| 425 |
'date', |
| 426 |
) |
| 427 |
); |
| 428 |
|
| 429 |
return in_array( $field_type, $default_field_types ); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Whether a field needs an array of options to be set. |
| 434 |
* |
| 435 |
* @since 1.5.0 |
| 436 |
* |
| 437 |
* @param string $field_type The type of field. |
| 438 |
* @return boolean |
| 439 |
*/ |
| 440 |
protected function field_needs_options( $field_type ) { |
| 441 |
return in_array( $field_type, array( 'select', 'multi-checkbox', 'radio' ) ); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
endif; |
| 446 |
|