cli
3 days ago
fields
3 days ago
widgets
3 days ago
Pods.php
3 days ago
PodsAPI.php
3 days ago
PodsAdmin.php
3 days ago
PodsArray.php
3 days ago
PodsComponent.php
3 days ago
PodsComponents.php
3 days ago
PodsData.php
3 days ago
PodsField.php
3 days ago
PodsForm.php
3 days ago
PodsI18n.php
3 days ago
PodsInit.php
3 days ago
PodsMeta.php
3 days ago
PodsMigrate.php
3 days ago
PodsRESTFields.php
3 days ago
PodsRESTHandlers.php
3 days ago
PodsTermSplitting.php
3 days ago
PodsUI.php
3 days ago
PodsView.php
3 days ago
PodsForm.php
1816 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @package Pods |
| 5 | */ |
| 6 | class PodsForm { |
| 7 | |
| 8 | /** |
| 9 | * @var PodsForm |
| 10 | */ |
| 11 | protected static $instance = null; |
| 12 | |
| 13 | /** |
| 14 | * @var string |
| 15 | */ |
| 16 | public static $field = null; |
| 17 | |
| 18 | /** |
| 19 | * @var string |
| 20 | */ |
| 21 | public static $field_group = null; |
| 22 | |
| 23 | /** |
| 24 | * @var string |
| 25 | */ |
| 26 | public static $field_type = null; |
| 27 | |
| 28 | /** |
| 29 | * @var array |
| 30 | */ |
| 31 | public static $field_types = array(); |
| 32 | |
| 33 | /** |
| 34 | * @var array |
| 35 | */ |
| 36 | public static $loaded = array(); |
| 37 | |
| 38 | /** |
| 39 | * @var int |
| 40 | */ |
| 41 | public static $form_counter = 0; |
| 42 | |
| 43 | /** |
| 44 | * Singleton handling for a basic pods_form() request |
| 45 | * |
| 46 | * @return \PodsForm |
| 47 | * |
| 48 | * @since 2.3.5 |
| 49 | */ |
| 50 | public static function init() { |
| 51 | |
| 52 | if ( ! is_object( self::$instance ) ) { |
| 53 | self::$instance = new self(); |
| 54 | } |
| 55 | |
| 56 | return self::$instance; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Master handler for all field / form methods |
| 61 | * |
| 62 | * @return \PodsForm |
| 63 | * |
| 64 | * @license http://www.gnu.org/licenses/gpl-2.0.html |
| 65 | * @since 2.0.0 |
| 66 | */ |
| 67 | private function __construct() { |
| 68 | |
| 69 | add_action( 'admin_init', array( $this, 'admin_init' ), 14 ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Prevent clones |
| 74 | * |
| 75 | * @since 2.3.0 |
| 76 | */ |
| 77 | private function __clone() { |
| 78 | // Hulk smash |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Output a field's label |
| 83 | * |
| 84 | * @since 2.0.0 |
| 85 | */ |
| 86 | |
| 87 | /** |
| 88 | * Output a field's label |
| 89 | * |
| 90 | * @param string $name Field name |
| 91 | * @param string $label Label text |
| 92 | * @param string $help Help text |
| 93 | * @param array $options Field options |
| 94 | * |
| 95 | * @return string Label HTML |
| 96 | * |
| 97 | * @since 2.0.0 |
| 98 | */ |
| 99 | public static function label( $name, $label, $help = '', $options = null ) { |
| 100 | |
| 101 | if ( is_array( $label ) || is_object( $label ) ) { |
| 102 | $options = $label; |
| 103 | $label = $options['label']; |
| 104 | |
| 105 | if ( empty( $label ) ) { |
| 106 | $label = ucwords( str_replace( '_', ' ', $name ) ); |
| 107 | } |
| 108 | |
| 109 | $help = $options['help']; |
| 110 | } else { |
| 111 | $options = self::options( null, $options ); |
| 112 | } |
| 113 | |
| 114 | $label = apply_filters( 'pods_form_ui_label_text', $label, $name, $help, $options ); |
| 115 | $help = apply_filters( 'pods_form_ui_label_help', $help, $name, $label, $options ); |
| 116 | |
| 117 | ob_start(); |
| 118 | |
| 119 | $name_clean = self::clean( $name ); |
| 120 | $name_more_clean = self::clean( $name, true ); |
| 121 | |
| 122 | $type = 'label'; |
| 123 | $attributes = array(); |
| 124 | $attributes['class'] = 'pods-form-ui-' . $type . ' pods-form-ui-' . $type . '-' . $name_more_clean; |
| 125 | $attributes['for'] = ( false === strpos( $name_clean, 'pods-form-ui-' ) ? 'pods-form-ui-' : '' ) . $name_clean; |
| 126 | $attributes = self::merge_attributes( $attributes, $name, $type, $options, false ); |
| 127 | |
| 128 | pods_view( PODS_DIR . 'ui/fields/_label.php', compact( array_keys( get_defined_vars() ) ) ); |
| 129 | |
| 130 | $output = ob_get_clean(); |
| 131 | |
| 132 | return apply_filters( "pods_form_ui_{$type}", $output, $name, $label, $help, $attributes, $options ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Output a Field Comment Paragraph |
| 137 | * |
| 138 | * @param string $name Field name |
| 139 | * @param string $message Field comments |
| 140 | * @param array $options Field options |
| 141 | * |
| 142 | * @return string Comment HTML |
| 143 | * |
| 144 | * @since 2.0.0 |
| 145 | */ |
| 146 | public static function comment( $name, $message = null, $options = null ) { |
| 147 | |
| 148 | $options = self::options( null, $options ); |
| 149 | |
| 150 | $name_more_clean = self::clean( $name, true ); |
| 151 | |
| 152 | if ( ! empty( $options['description'] ) ) { |
| 153 | $message = $options['description']; |
| 154 | } elseif ( empty( $message ) ) { |
| 155 | return ''; |
| 156 | } |
| 157 | |
| 158 | $message = apply_filters( 'pods_form_ui_comment_text', $message, $name, $options ); |
| 159 | |
| 160 | ob_start(); |
| 161 | |
| 162 | $type = 'comment'; |
| 163 | $attributes = array(); |
| 164 | $attributes['class'] = 'description pods-form-ui-' . $type . ' pods-form-ui-' . $type . '-' . $name_more_clean; |
| 165 | $attributes = self::merge_attributes( $attributes, $name, $type, $options, false ); |
| 166 | |
| 167 | pods_view( PODS_DIR . 'ui/fields/_comment.php', compact( array_keys( get_defined_vars() ) ) ); |
| 168 | |
| 169 | $output = ob_get_clean(); |
| 170 | |
| 171 | return apply_filters( "pods_form_ui_{$type}", $output, $name, $message, $attributes, $options ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Output a field |
| 176 | * |
| 177 | * @param string $name Field name |
| 178 | * @param mixed $value Field value |
| 179 | * @param string $type Field type |
| 180 | * @param array $options Field options |
| 181 | * @param array $pod Pod data |
| 182 | * @param int $id Item ID |
| 183 | * |
| 184 | * @return string Field HTML |
| 185 | * |
| 186 | * @since 2.0.0 |
| 187 | */ |
| 188 | public static function field( $name, $value, $type = 'text', $options = null, $pod = null, $id = null ) { |
| 189 | |
| 190 | // Take a field array |
| 191 | if ( is_array( $name ) || is_object( $name ) ) { |
| 192 | $options = $name; |
| 193 | |
| 194 | if ( is_object( $type ) ) { |
| 195 | $pod = $type; |
| 196 | $id = $options; |
| 197 | } |
| 198 | |
| 199 | $name = pods_v( 'name', $options ); |
| 200 | $type = pods_v( 'type', $options ); |
| 201 | } |
| 202 | |
| 203 | $options = self::options( $type, $options ); |
| 204 | $options = apply_filters( "pods_form_ui_field_{$type}_options", $options, $value, $name, $pod, $id ); |
| 205 | |
| 206 | if ( null === $value || ( '' === $value && 'boolean' === $type ) || ( ! empty( $pod ) && empty( $id ) ) ) { |
| 207 | $value = self::default_value( $value, $type, $name, $options, $pod, $id ); |
| 208 | } |
| 209 | |
| 210 | // Fix double help qtip when using single checkboxes (boolean type) |
| 211 | if ( 'boolean' === $type ) { |
| 212 | $options['help'] = ''; |
| 213 | } |
| 214 | |
| 215 | if ( false === self::permission( $type, $name, $options, null, $pod, $id ) ) { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | $value = apply_filters( "pods_form_ui_field_{$type}_value", $value, $name, $options, $pod, $id ); |
| 220 | $form_field_type = self::$field_type; |
| 221 | |
| 222 | ob_start(); |
| 223 | |
| 224 | $helper = false; |
| 225 | |
| 226 | /** |
| 227 | * Input helpers are deprecated and not guaranteed to work properly. |
| 228 | * |
| 229 | * They will be entirely removed in Pods 3.0. |
| 230 | * |
| 231 | * @deprecated 2.7.0 |
| 232 | */ |
| 233 | if ( 0 < strlen( pods_v( 'input_helper', $options ) ) ) { |
| 234 | $helper = pods_api()->load_helper( array( 'name' => $options['input_helper'] ) ); |
| 235 | } |
| 236 | |
| 237 | // @todo Move into DFV field method or PodsObject later |
| 238 | if ( ( ! isset( $options['data'] ) || empty( $options['data'] ) ) && is_object( self::$loaded[ $type ] ) && method_exists( self::$loaded[ $type ], 'data' ) ) { |
| 239 | $options['data'] = self::$loaded[ $type ]->data( $name, $value, $options, $pod, $id, true ); |
| 240 | $data = $options['data']; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * pods_form_ui_field_{$type}_override filter leaves too much to be done by developer. |
| 245 | * |
| 246 | * It will be replaced in Pods 3.0 with better documentation. |
| 247 | * |
| 248 | * @deprecated 2.7.0 |
| 249 | */ |
| 250 | if ( true === apply_filters( "pods_form_ui_field_{$type}_override", false, $name, $value, $options, $pod, $id ) ) { |
| 251 | /** |
| 252 | * pods_form_ui_field_{$type} action leaves too much to be done by developer. |
| 253 | * |
| 254 | * It will be replaced in Pods 3.0 with better documentation. |
| 255 | * |
| 256 | * @deprecated 2.7.0 |
| 257 | */ |
| 258 | do_action( "pods_form_ui_field_{$type}", $name, $value, $options, $pod, $id ); |
| 259 | } elseif ( ! empty( $helper ) && 0 < strlen( pods_v( 'code', $helper ) ) && false === strpos( $helper['code'], '$this->' ) && ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) ) { |
| 260 | /** |
| 261 | * Input helpers are deprecated and not guaranteed to work properly. |
| 262 | * |
| 263 | * They will be entirely removed in Pods 3.0. |
| 264 | * |
| 265 | * @deprecated 2.7.0 |
| 266 | */ |
| 267 | eval( '?>' . $helper['code'] ); |
| 268 | } elseif ( method_exists( get_class(), 'field_' . $type ) ) { |
| 269 | // @todo Move these custom field methods into real/faux field classes |
| 270 | echo call_user_func( array( get_class(), 'field_' . $type ), $name, $value, $options ); |
| 271 | } elseif ( is_object( self::$loaded[ $type ] ) && method_exists( self::$loaded[ $type ], 'input' ) ) { |
| 272 | self::$loaded[ $type ]->input( $name, $value, $options, $pod, $id ); |
| 273 | } else { |
| 274 | /** |
| 275 | * pods_form_ui_field_{$type} action leaves too much to be done by developer. |
| 276 | * |
| 277 | * It will be replaced in Pods 3.0 with better documentation. |
| 278 | * |
| 279 | * @deprecated 2.7.0 |
| 280 | */ |
| 281 | do_action( "pods_form_ui_field_{$type}", $name, $value, $options, $pod, $id ); |
| 282 | }//end if |
| 283 | |
| 284 | $output = ob_get_clean(); |
| 285 | |
| 286 | /** |
| 287 | * pods_form_ui_field_{$type} filter will remain supported. |
| 288 | * |
| 289 | * It is not intended for replacing but augmenting input markup. |
| 290 | */ |
| 291 | return apply_filters( "pods_form_ui_field_{$type}", $output, $name, $value, $options, $pod, $id ); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Output field type 'db' |
| 296 | * |
| 297 | * Used for field names and other places where only [a-z0-9_] is accepted |
| 298 | * |
| 299 | * @since 2.0.0 |
| 300 | * |
| 301 | * @param $name |
| 302 | * @param null $value |
| 303 | * @param null $options |
| 304 | * |
| 305 | * @return mixed|void |
| 306 | */ |
| 307 | protected static function field_db( $name, $value = null, $options = null ) { |
| 308 | |
| 309 | $form_field_type = self::$field_type; |
| 310 | |
| 311 | ob_start(); |
| 312 | |
| 313 | pods_view( PODS_DIR . 'ui/fields/_db.php', compact( array_keys( get_defined_vars() ) ) ); |
| 314 | |
| 315 | $output = ob_get_clean(); |
| 316 | |
| 317 | return apply_filters( 'pods_form_ui_field_db', $output, $name, $value, $options ); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Output a hidden field |
| 322 | * |
| 323 | * @param $name |
| 324 | * @param null $value |
| 325 | * @param null $options |
| 326 | * |
| 327 | * @return mixed|void |
| 328 | */ |
| 329 | protected static function field_hidden( $name, $value = null, $options = null ) { |
| 330 | |
| 331 | $form_field_type = self::$field_type; |
| 332 | |
| 333 | ob_start(); |
| 334 | |
| 335 | pods_view( PODS_DIR . 'ui/fields/_hidden.php', compact( array_keys( get_defined_vars() ) ) ); |
| 336 | |
| 337 | $output = ob_get_clean(); |
| 338 | |
| 339 | return apply_filters( 'pods_form_ui_field_hidden', $output, $name, $value, $options ); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns a submit button, with provided text and appropriate class, copied from WP Core for use on the frontend |
| 344 | * |
| 345 | * @see get_submit_button |
| 346 | * |
| 347 | * @param string $text The text of the button (defaults to 'Save Changes') |
| 348 | * @param string $type The type of button. One of: primary, secondary, delete |
| 349 | * @param string $name The HTML name of the submit button. Defaults to "submit". If no id |
| 350 | * attribute is given in $other_attributes below, $name will be used as the |
| 351 | * button's id. |
| 352 | * @param bool $wrap True if the output button should be wrapped in a paragraph tag, |
| 353 | * false otherwise. Defaults to true |
| 354 | * @param array|string $other_attributes Other attributes that should be output with the button, |
| 355 | * mapping attributes to their values, such as array( 'tabindex' => '1' ). |
| 356 | * These attributes will be output as attribute="value", such as |
| 357 | * tabindex="1". |
| 358 | * Defaults to no other attributes. Other attributes can also be provided as |
| 359 | * a |
| 360 | * string such as 'tabindex="1"', though the array format is typically |
| 361 | * cleaner. |
| 362 | * |
| 363 | * @since 2.7.0 |
| 364 | * @return string |
| 365 | */ |
| 366 | public static function submit_button( $text = null, $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = null ) { |
| 367 | |
| 368 | if ( function_exists( 'get_submit_button' ) ) { |
| 369 | return get_submit_button( $text, $type, $name, $wrap, $other_attributes ); |
| 370 | } |
| 371 | |
| 372 | if ( ! is_array( $type ) ) { |
| 373 | $type = explode( ' ', $type ); |
| 374 | } |
| 375 | |
| 376 | $button_shorthand = array( |
| 377 | 'primary', |
| 378 | 'small', |
| 379 | 'large', |
| 380 | ); |
| 381 | |
| 382 | $classes = array( |
| 383 | 'button', |
| 384 | ); |
| 385 | |
| 386 | foreach ( $type as $t ) { |
| 387 | if ( 'secondary' === $t || 'button-secondary' === $t ) { |
| 388 | continue; |
| 389 | } |
| 390 | |
| 391 | $classes[] = in_array( $t, $button_shorthand ) ? 'button-' . $t : $t; |
| 392 | } |
| 393 | |
| 394 | $class = implode( ' ', array_unique( $classes ) ); |
| 395 | |
| 396 | if ( 'delete' === $type ) { |
| 397 | $class = 'button-secondary delete'; |
| 398 | } |
| 399 | |
| 400 | $text = $text ? $text : __( 'Save Changes' ); |
| 401 | |
| 402 | // Default the id attribute to $name unless an id was specifically provided in $other_attributes |
| 403 | $id = $name; |
| 404 | |
| 405 | if ( is_array( $other_attributes ) && isset( $other_attributes['id'] ) ) { |
| 406 | $id = $other_attributes['id']; |
| 407 | unset( $other_attributes['id'] ); |
| 408 | } |
| 409 | |
| 410 | $attributes = ''; |
| 411 | |
| 412 | if ( is_array( $other_attributes ) ) { |
| 413 | foreach ( $other_attributes as $attribute => $value ) { |
| 414 | $attributes .= $attribute . '="' . esc_attr( $value ) . '" '; |
| 415 | // Trailing space is important |
| 416 | } |
| 417 | } elseif ( ! empty( $other_attributes ) ) { |
| 418 | // Attributes provided as a string |
| 419 | $attributes = $other_attributes; |
| 420 | } |
| 421 | |
| 422 | $button = '<input type="submit" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ); |
| 423 | $button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />'; |
| 424 | |
| 425 | if ( $wrap ) { |
| 426 | $button = '<p class="submit">' . $button . '</p>'; |
| 427 | } |
| 428 | |
| 429 | return $button; |
| 430 | |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Output a row (label, field, and comment) |
| 435 | * |
| 436 | * @param string $name Field name |
| 437 | * @param mixed $value Field value |
| 438 | * @param string $type Field type |
| 439 | * @param array $options Field options |
| 440 | * @param array $pod Pod data |
| 441 | * @param int $id Item ID |
| 442 | * |
| 443 | * @return string Row HTML |
| 444 | * |
| 445 | * @since 2.0.0 |
| 446 | */ |
| 447 | public static function row( $name, $value, $type = 'text', $options = null, $pod = null, $id = null ) { |
| 448 | |
| 449 | $options = self::options( null, $options ); |
| 450 | |
| 451 | ob_start(); |
| 452 | |
| 453 | pods_view( PODS_DIR . 'ui/fields/_row.php', compact( array_keys( get_defined_vars() ) ) ); |
| 454 | |
| 455 | $output = ob_get_clean(); |
| 456 | |
| 457 | return apply_filters( 'pods_form_ui_field_row', $output, $name, $value, $options, $pod, $id ); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Output a field's attributes |
| 462 | * |
| 463 | * @since 2.0.0 |
| 464 | * |
| 465 | * @param $attributes |
| 466 | * @param null $name |
| 467 | * @param null $type |
| 468 | * @param null $options |
| 469 | */ |
| 470 | public static function attributes( $attributes, $name = null, $type = null, $options = null ) { |
| 471 | |
| 472 | $attributes = (array) apply_filters( "pods_form_ui_field_{$type}_attributes", $attributes, $name, $options ); |
| 473 | |
| 474 | foreach ( $attributes as $attribute => $value ) { |
| 475 | if ( null === $value ) { |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | echo ' ' . esc_attr( (string) $attribute ) . '="' . esc_attr( (string) $value ) . '"'; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Output a field's data (for use with jQuery) |
| 485 | * |
| 486 | * @since 2.0.0 |
| 487 | * |
| 488 | * @param $data |
| 489 | * @param null $name |
| 490 | * @param null $type |
| 491 | * @param null $options |
| 492 | */ |
| 493 | public static function data( $data, $name = null, $type = null, $options = null ) { |
| 494 | |
| 495 | $data = (array) apply_filters( "pods_form_ui_field_{$type}_data", $data, $name, $options ); |
| 496 | |
| 497 | foreach ( $data as $key => $value ) { |
| 498 | if ( null === $value ) { |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | $key = sanitize_title( $key ); |
| 503 | |
| 504 | if ( is_array( $value ) ) { |
| 505 | $value = implode( ',', $value ); |
| 506 | } |
| 507 | |
| 508 | echo ' data-' . esc_attr( (string) $key ) . '="' . esc_attr( (string) $value ) . '"'; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Merge attributes and handle classes |
| 514 | * |
| 515 | * @since 2.0.0 |
| 516 | * |
| 517 | * @param $attributes |
| 518 | * @param null $name |
| 519 | * @param null $type |
| 520 | * @param null $options |
| 521 | * @param string $classes |
| 522 | * |
| 523 | * @return array |
| 524 | */ |
| 525 | public static function merge_attributes( $attributes, $name = null, $type = null, $options = null, $classes = '' ) { |
| 526 | |
| 527 | $options = (array) $options; |
| 528 | |
| 529 | if ( ! in_array( $type, array( 'label', 'comment' ) ) ) { |
| 530 | $name_clean = self::clean( $name ); |
| 531 | $name_more_clean = self::clean( $name, true ); |
| 532 | $_attributes = array(); |
| 533 | $_attributes['name'] = $name; |
| 534 | $_attributes['data-name-clean'] = $name_more_clean; |
| 535 | |
| 536 | if ( 0 < strlen( pods_v( 'label', $options, '' ) ) ) { |
| 537 | $_attributes['data-label'] = strip_tags( pods_v( 'label', $options ) ); |
| 538 | } |
| 539 | |
| 540 | $_attributes['id'] = 'pods-form-ui-' . $name_clean . ( self::$form_counter > 1 ? '-' . self::$form_counter : '' ); |
| 541 | $_attributes['class'] = 'pods-form-ui-field pods-form-ui-field-type-' . $type . ' pods-form-ui-field-name-' . $name_more_clean; |
| 542 | |
| 543 | if ( isset( $options['dependency'] ) && false !== $options['dependency'] ) { |
| 544 | $_attributes['class'] .= ' pods-dependent-toggle'; |
| 545 | } |
| 546 | |
| 547 | $attributes = array_merge( $_attributes, (array) $attributes ); |
| 548 | |
| 549 | if ( isset( $options['attributes'] ) && is_array( $options['attributes'] ) && ! empty( $options['attributes'] ) ) { |
| 550 | $attributes = array_merge( $attributes, $options['attributes'] ); |
| 551 | } |
| 552 | } elseif ( isset( $options[ $type . '_attributes' ] ) && is_array( $options[ $type . '_attributes' ] ) && ! empty( $options[ $type . '_attributes' ] ) ) { |
| 553 | $attributes = array_merge( $attributes, $options[ $type . '_attributes' ] ); |
| 554 | }//end if |
| 555 | |
| 556 | if ( isset( $options['class'] ) && ! empty( $options['class'] ) ) { |
| 557 | if ( is_array( $options['class'] ) ) { |
| 558 | $options['class'] = implode( ' ', $options['class'] ); |
| 559 | } |
| 560 | |
| 561 | $options['class'] = (string) $options['class']; |
| 562 | if ( isset( $attributes['class'] ) ) { |
| 563 | $attributes['class'] = $attributes['class'] . ' ' . $options['class']; |
| 564 | } else { |
| 565 | $attributes['class'] = $options['class']; |
| 566 | } |
| 567 | |
| 568 | $attributes['class'] = trim( $attributes['class'] ); |
| 569 | } |
| 570 | |
| 571 | if ( ! empty( $classes ) ) { |
| 572 | if ( isset( $attributes['class'] ) ) { |
| 573 | $attributes['class'] = $attributes['class'] . ' ' . $classes; |
| 574 | } else { |
| 575 | $attributes['class'] = $classes; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | $placeholder = trim( pods_v( 'placeholder', $options, pods_v( $type . '_placeholder', $options ) ) ); |
| 580 | |
| 581 | if ( ! empty( $placeholder ) ) { |
| 582 | $attributes['placeholder'] = $placeholder; |
| 583 | } |
| 584 | |
| 585 | if ( 1 === (int) pods_v( 'required', $options, 0 ) ) { |
| 586 | $attributes['class'] .= ' pods-validate pods-validate-required'; |
| 587 | } |
| 588 | |
| 589 | $max_length = (int) pods_v( 'maxlength', $options, pods_v( $type . '_max_length', $options, 0 ) ); |
| 590 | |
| 591 | if ( 0 < $max_length ) { |
| 592 | $attributes['maxlength'] = $max_length; |
| 593 | } |
| 594 | |
| 595 | $attributes = (array) apply_filters( "pods_form_ui_field_{$type}_merge_attributes", $attributes, $name, $options ); |
| 596 | |
| 597 | return $attributes; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Setup options for a field and store them for later use |
| 602 | * |
| 603 | * @param $type |
| 604 | * @param $options |
| 605 | * |
| 606 | * @return array |
| 607 | * |
| 608 | * @static |
| 609 | * |
| 610 | * @since 2.0.0 |
| 611 | */ |
| 612 | public static function options( $type, $options ) { |
| 613 | |
| 614 | $options = (array) $options; |
| 615 | |
| 616 | if ( ! is_object( $options ) && isset( $options['options'] ) ) { |
| 617 | $options_temp = $options['options']; |
| 618 | |
| 619 | unset( $options['options'] ); |
| 620 | |
| 621 | $options = array_merge( $options_temp, $options ); |
| 622 | |
| 623 | $override = array( |
| 624 | 'class', |
| 625 | ); |
| 626 | |
| 627 | foreach ( $override as $check ) { |
| 628 | if ( isset( $options_temp[ $check ] ) ) { |
| 629 | $options[ $check ] = $options_temp[ $check ]; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | $defaults = self::options_setup( $type, $options ); |
| 635 | |
| 636 | $core_defaults = array( |
| 637 | 'id' => 0, |
| 638 | 'label' => '', |
| 639 | 'description' => '', |
| 640 | 'help' => '', |
| 641 | 'default' => null, |
| 642 | 'attributes' => array(), |
| 643 | 'class' => '', |
| 644 | 'grouped' => 0, |
| 645 | ); |
| 646 | |
| 647 | $defaults = array_merge( $core_defaults, $defaults ); |
| 648 | |
| 649 | foreach ( $defaults as $option => $settings ) { |
| 650 | $default = $settings; |
| 651 | |
| 652 | if ( is_array( $settings ) && isset( $settings['default'] ) ) { |
| 653 | $default = $settings['default']; |
| 654 | } |
| 655 | |
| 656 | if ( ! isset( $options[ $option ] ) ) { |
| 657 | $options[ $option ] = $default; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | return $options; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Get options for a field type and setup defaults |
| 666 | * |
| 667 | * @static |
| 668 | * |
| 669 | * @param $type |
| 670 | * |
| 671 | * @param null $options |
| 672 | * |
| 673 | * @return array|null |
| 674 | * @since 2.0.0 |
| 675 | */ |
| 676 | public static function options_setup( $type = null, $options = null ) { |
| 677 | |
| 678 | $core_defaults = array( |
| 679 | 'id' => 0, |
| 680 | 'name' => '', |
| 681 | 'label' => '', |
| 682 | 'description' => '', |
| 683 | 'help' => '', |
| 684 | 'default' => null, |
| 685 | 'attributes' => array(), |
| 686 | 'class' => '', |
| 687 | 'type' => 'text', |
| 688 | 'group' => 0, |
| 689 | 'grouped' => 0, |
| 690 | 'developer_mode' => false, |
| 691 | 'dependency' => false, |
| 692 | 'depends-on' => array(), |
| 693 | 'excludes-on' => array(), |
| 694 | 'options' => array(), |
| 695 | ); |
| 696 | |
| 697 | if ( ! empty( $options ) && is_array( $options ) ) { |
| 698 | $core_defaults = array_merge( $core_defaults, $options ); |
| 699 | } |
| 700 | |
| 701 | if ( null === $type ) { |
| 702 | return $core_defaults; |
| 703 | } else { |
| 704 | self::field_loader( $type ); |
| 705 | } |
| 706 | |
| 707 | $options = apply_filters( "pods_field_{$type}_options", (array) self::$loaded[ $type ]->options(), $type ); |
| 708 | |
| 709 | $first_field = current( $options ); |
| 710 | |
| 711 | if ( ! empty( $options ) && ! isset( $first_field['name'] ) && ! isset( $first_field['label'] ) ) { |
| 712 | $all_options = array(); |
| 713 | |
| 714 | foreach ( $options as $group => $group_options ) { |
| 715 | $all_options = array_merge( $all_options, self::fields_setup( $group_options, $core_defaults ) ); |
| 716 | } |
| 717 | |
| 718 | $options = $all_options; |
| 719 | } else { |
| 720 | $options = self::fields_setup( $options, $core_defaults ); |
| 721 | } |
| 722 | |
| 723 | return $options; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Get Admin options for a field type and setup defaults |
| 728 | * |
| 729 | * @static |
| 730 | * |
| 731 | * @param $type |
| 732 | * |
| 733 | * @return array|null |
| 734 | * |
| 735 | * @since 2.0.0 |
| 736 | */ |
| 737 | public static function ui_options( $type ) { |
| 738 | |
| 739 | $core_defaults = array( |
| 740 | 'id' => 0, |
| 741 | 'name' => '', |
| 742 | 'label' => '', |
| 743 | 'description' => '', |
| 744 | 'help' => '', |
| 745 | 'default' => null, |
| 746 | 'attributes' => array(), |
| 747 | 'class' => '', |
| 748 | 'type' => 'text', |
| 749 | 'group' => 0, |
| 750 | 'grouped' => 0, |
| 751 | 'developer_mode' => false, |
| 752 | 'dependency' => false, |
| 753 | 'depends-on' => array(), |
| 754 | 'excludes-on' => array(), |
| 755 | 'options' => array(), |
| 756 | ); |
| 757 | |
| 758 | self::field_loader( $type ); |
| 759 | |
| 760 | $options = apply_filters( "pods_field_{$type}_ui_options", (array) self::$loaded[ $type ]->ui_options(), $type ); |
| 761 | |
| 762 | $first_field = current( $options ); |
| 763 | |
| 764 | if ( ! empty( $options ) && ! isset( $first_field['name'] ) && ! isset( $first_field['label'] ) ) { |
| 765 | foreach ( $options as $group => $group_options ) { |
| 766 | $options[ $group ] = self::fields_setup( $group_options, $core_defaults ); |
| 767 | } |
| 768 | } else { |
| 769 | $options = self::fields_setup( $options, $core_defaults ); |
| 770 | } |
| 771 | |
| 772 | return $options; |
| 773 | } |
| 774 | |
| 775 | /** |
| 776 | * Get options for a field and setup defaults |
| 777 | * |
| 778 | * @param null $fields |
| 779 | * @param null $core_defaults |
| 780 | * @param bool $single |
| 781 | * |
| 782 | * @return array|null |
| 783 | * |
| 784 | * @static |
| 785 | * @since 2.0.0 |
| 786 | */ |
| 787 | public static function fields_setup( $fields = null, $core_defaults = null, $single = false ) { |
| 788 | |
| 789 | if ( empty( $core_defaults ) ) { |
| 790 | $core_defaults = array( |
| 791 | 'id' => 0, |
| 792 | 'name' => '', |
| 793 | 'label' => '', |
| 794 | 'description' => '', |
| 795 | 'help' => '', |
| 796 | 'default' => null, |
| 797 | 'attributes' => array(), |
| 798 | 'class' => '', |
| 799 | 'type' => 'text', |
| 800 | 'group' => 0, |
| 801 | 'grouped' => 0, |
| 802 | 'developer_mode' => false, |
| 803 | 'dependency' => false, |
| 804 | 'depends-on' => array(), |
| 805 | 'excludes-on' => array(), |
| 806 | 'options' => array(), |
| 807 | ); |
| 808 | } |
| 809 | |
| 810 | if ( $single ) { |
| 811 | $fields = array( $fields ); |
| 812 | } |
| 813 | |
| 814 | foreach ( $fields as $f => $field ) { |
| 815 | $fields[ $f ] = self::field_setup( $field, $core_defaults, pods_v( 'type', $field, 'text' ) ); |
| 816 | |
| 817 | if ( ! $single && strlen( $fields[ $f ]['name'] ) < 1 ) { |
| 818 | $fields[ $f ]['name'] = $f; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | if ( $single ) { |
| 823 | $fields = $fields[0]; |
| 824 | } |
| 825 | |
| 826 | return $fields; |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Get options for a field and setup defaults |
| 831 | * |
| 832 | * @static |
| 833 | * |
| 834 | * @param null $field |
| 835 | * @param null $core_defaults |
| 836 | * @param null $type |
| 837 | * |
| 838 | * @return array|null |
| 839 | * |
| 840 | * @since 2.0.0 |
| 841 | */ |
| 842 | public static function field_setup( $field = null, $core_defaults = null, $type = null ) { |
| 843 | |
| 844 | $options = array(); |
| 845 | |
| 846 | if ( empty( $core_defaults ) ) { |
| 847 | $core_defaults = array( |
| 848 | 'id' => 0, |
| 849 | 'name' => '', |
| 850 | 'label' => '', |
| 851 | 'description' => '', |
| 852 | 'help' => '', |
| 853 | 'default' => null, |
| 854 | 'attributes' => array(), |
| 855 | 'class' => '', |
| 856 | 'type' => 'text', |
| 857 | 'group' => 0, |
| 858 | 'grouped' => 0, |
| 859 | 'developer_mode' => false, |
| 860 | 'dependency' => false, |
| 861 | 'depends-on' => array(), |
| 862 | 'excludes-on' => array(), |
| 863 | 'options' => array(), |
| 864 | ); |
| 865 | |
| 866 | if ( null !== $type ) { |
| 867 | self::field_loader( $type ); |
| 868 | |
| 869 | if ( method_exists( self::$loaded[ $type ], 'options' ) ) { |
| 870 | $options = apply_filters( "pods_field_{$type}_options", (array) self::$loaded[ $type ]->options(), $type ); |
| 871 | } |
| 872 | } |
| 873 | }//end if |
| 874 | |
| 875 | if ( ! is_array( $field ) ) { |
| 876 | $field = array( 'default' => $field ); |
| 877 | } |
| 878 | |
| 879 | if ( isset( $field['group'] ) && is_array( $field['group'] ) ) { |
| 880 | foreach ( $field['group'] as $g => $group_option ) { |
| 881 | $field['group'][ $g ] = array_merge( $core_defaults, $group_option ); |
| 882 | |
| 883 | if ( strlen( $field['group'][ $g ]['name'] ) < 1 ) { |
| 884 | $field['group'][ $g ]['name'] = $g; |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | $field = array_merge( $core_defaults, $field ); |
| 890 | |
| 891 | foreach ( $options as $option => $settings ) { |
| 892 | $v = null; |
| 893 | |
| 894 | if ( isset( $settings['default'] ) ) { |
| 895 | $v = $settings['default']; |
| 896 | } |
| 897 | |
| 898 | if ( ! isset( $field['options'][ $option ] ) ) { |
| 899 | $field['options'][ $option ] = $v; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | return $field; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Setup dependency / exclusion classes |
| 908 | * |
| 909 | * @param array $options array( 'depends-on' => ..., 'excludes-on' => ...) |
| 910 | * @param string $prefix |
| 911 | * |
| 912 | * @return array |
| 913 | * @static |
| 914 | * @since 2.0.0 |
| 915 | */ |
| 916 | public static function dependencies( $options, $prefix = '' ) { |
| 917 | |
| 918 | $options = (array) $options; |
| 919 | $classes = array(); |
| 920 | $data = array(); |
| 921 | $depends_on = array(); |
| 922 | $excludes_on = array(); |
| 923 | $wildcard_on = array(); |
| 924 | |
| 925 | if ( isset( $options['depends-on'] ) ) { |
| 926 | $depends_on = (array) $options['depends-on']; |
| 927 | |
| 928 | if ( ! empty( $depends_on ) ) { |
| 929 | $classes[] = 'pods-depends-on'; |
| 930 | |
| 931 | foreach ( $depends_on as $depends => $on ) { |
| 932 | $classes[] = 'pods-depends-on-' . $prefix . self::clean( $depends, true ); |
| 933 | |
| 934 | if ( ! is_bool( $on ) ) { |
| 935 | $on = (array) $on; |
| 936 | |
| 937 | foreach ( $on as $o ) { |
| 938 | $classes[] = 'pods-depends-on-' . $prefix . self::clean( $depends, true ) . '-' . self::clean( $o, true ); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if ( isset( $options['excludes-on'] ) ) { |
| 946 | $excludes_on = (array) $options['excludes-on']; |
| 947 | |
| 948 | if ( ! empty( $excludes_on ) ) { |
| 949 | $classes[] = 'pods-excludes-on'; |
| 950 | |
| 951 | foreach ( $excludes_on as $excludes => $on ) { |
| 952 | $classes[] = 'pods-excludes-on-' . $prefix . self::clean( $excludes, true ); |
| 953 | |
| 954 | $on = (array) $on; |
| 955 | |
| 956 | foreach ( $on as $o ) { |
| 957 | $classes[] = 'pods-excludes-on-' . $prefix . self::clean( $excludes, true ) . '-' . self::clean( $o, true ); |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | if ( isset( $options['wildcard-on'] ) ) { |
| 964 | $wildcard_on = (array) $options['wildcard-on']; |
| 965 | |
| 966 | if ( ! empty( $wildcard_on ) ) { |
| 967 | $classes[] = 'pods-wildcard-on'; |
| 968 | |
| 969 | // Add the appropriate classes and data attribs per value dependency |
| 970 | foreach ( $wildcard_on as $target => $wildcards ) { |
| 971 | $target = $prefix . self::clean( $target, true ); |
| 972 | $classes[] = 'pods-wildcard-on-' . $target; |
| 973 | $data[ 'pods-wildcard-' . $target ] = $wildcards; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | $classes = implode( ' ', $classes ); |
| 979 | |
| 980 | return array( |
| 981 | 'classes' => $classes, |
| 982 | 'data' => $data, |
| 983 | ); |
| 984 | } |
| 985 | |
| 986 | /** |
| 987 | * Change the value of the field |
| 988 | * |
| 989 | * @param $type |
| 990 | * @param mixed $value |
| 991 | * @param string $name |
| 992 | * @param array $options |
| 993 | * @param array $pod |
| 994 | * @param int $id |
| 995 | * @param array $traverse |
| 996 | * |
| 997 | * @return array|mixed|null|object |
| 998 | * @internal param array $fields |
| 999 | * @since 2.3.0 |
| 1000 | */ |
| 1001 | public static function value( $type, $value = null, $name = null, $options = null, $pod = null, $id = null, $traverse = null ) { |
| 1002 | |
| 1003 | self::field_loader( $type ); |
| 1004 | |
| 1005 | if ( in_array( $type, self::repeatable_field_types() ) && 1 == pods_v( $type . '_repeatable', $options, 0 ) && ! is_array( $value ) ) { |
| 1006 | if ( 0 < strlen( $value ) ) { |
| 1007 | $simple = @json_decode( $value, true ); |
| 1008 | |
| 1009 | if ( is_array( $simple ) ) { |
| 1010 | $value = $simple; |
| 1011 | } else { |
| 1012 | $value = (array) $value; |
| 1013 | } |
| 1014 | } else { |
| 1015 | $value = array(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if ( is_array( $value ) && in_array( $type, self::tableless_field_types(), true ) ) { |
| 1020 | foreach ( $value as &$display_value ) { |
| 1021 | $display_value = call_user_func( array( |
| 1022 | self::$loaded[ $type ], |
| 1023 | 'value', |
| 1024 | ), $display_value, $name, $options, $pod, $id, $traverse ); |
| 1025 | } |
| 1026 | } else { |
| 1027 | $value = call_user_func( array( |
| 1028 | self::$loaded[ $type ], |
| 1029 | 'value', |
| 1030 | ), $value, $name, $options, $pod, $id, $traverse ); |
| 1031 | }//end if |
| 1032 | |
| 1033 | return $value; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Change the way the value of the field is displayed with Pods::get |
| 1038 | * |
| 1039 | * @param $type |
| 1040 | * @param mixed $value |
| 1041 | * @param string $name |
| 1042 | * @param array $options |
| 1043 | * @param array $pod |
| 1044 | * @param int $id |
| 1045 | * @param array $traverse |
| 1046 | * |
| 1047 | * @return array|mixed|null|void |
| 1048 | * @internal param array $fields |
| 1049 | * @since 2.0.0 |
| 1050 | */ |
| 1051 | public static function display( $type, $value = null, $name = null, $options = null, $pod = null, $id = null, $traverse = null ) { |
| 1052 | |
| 1053 | self::field_loader( $type ); |
| 1054 | |
| 1055 | $tableless_field_types = self::tableless_field_types(); |
| 1056 | |
| 1057 | if ( method_exists( self::$loaded[ $type ], 'display_list' ) ) { |
| 1058 | $value = call_user_func_array( |
| 1059 | array( self::$loaded[ $type ], 'display_list' ), array( |
| 1060 | $value, |
| 1061 | $name, |
| 1062 | $options, |
| 1063 | $pod, |
| 1064 | $id, |
| 1065 | $traverse, |
| 1066 | ) |
| 1067 | ); |
| 1068 | } elseif ( method_exists( self::$loaded[ $type ], 'display' ) ) { |
| 1069 | if ( is_array( $value ) && ! in_array( $type, $tableless_field_types ) ) { |
| 1070 | foreach ( $value as $k => $display_value ) { |
| 1071 | $value[ $k ] = call_user_func_array( |
| 1072 | array( self::$loaded[ $type ], 'display' ), array( |
| 1073 | $display_value, |
| 1074 | $name, |
| 1075 | $options, |
| 1076 | $pod, |
| 1077 | $id, |
| 1078 | $traverse, |
| 1079 | ) |
| 1080 | ); |
| 1081 | } |
| 1082 | } else { |
| 1083 | $value = call_user_func_array( |
| 1084 | array( self::$loaded[ $type ], 'display' ), array( |
| 1085 | $value, |
| 1086 | $name, |
| 1087 | $options, |
| 1088 | $pod, |
| 1089 | $id, |
| 1090 | $traverse, |
| 1091 | ) |
| 1092 | ); |
| 1093 | }//end if |
| 1094 | }//end if |
| 1095 | |
| 1096 | $value = apply_filters( "pods_form_display_{$type}", $value, $name, $options, $pod, $id, $traverse ); |
| 1097 | |
| 1098 | return $value; |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * Setup regex for JS / PHP |
| 1103 | * |
| 1104 | * @static |
| 1105 | * |
| 1106 | * @param $type |
| 1107 | * @param $options |
| 1108 | * |
| 1109 | * @return mixed|void |
| 1110 | * @since 2.0.0 |
| 1111 | */ |
| 1112 | public static function regex( $type, $options ) { |
| 1113 | |
| 1114 | self::field_loader( $type ); |
| 1115 | |
| 1116 | $regex = false; |
| 1117 | |
| 1118 | if ( method_exists( self::$loaded[ $type ], 'regex' ) ) { |
| 1119 | $regex = self::$loaded[ $type ]->regex( $options ); |
| 1120 | } |
| 1121 | |
| 1122 | $regex = apply_filters( "pods_field_{$type}_regex", $regex, $options, $type ); |
| 1123 | |
| 1124 | return $regex; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * Setup value preparation for sprintf |
| 1129 | * |
| 1130 | * @static |
| 1131 | * |
| 1132 | * @param $type |
| 1133 | * @param $options |
| 1134 | * |
| 1135 | * @return mixed|void |
| 1136 | * @since 2.0.0 |
| 1137 | */ |
| 1138 | public static function prepare( $type, $options ) { |
| 1139 | |
| 1140 | self::field_loader( $type ); |
| 1141 | |
| 1142 | $prepare = '%s'; |
| 1143 | |
| 1144 | if ( method_exists( self::$loaded[ $type ], 'prepare' ) ) { |
| 1145 | $prepare = self::$loaded[ $type ]->prepare( $options ); |
| 1146 | } |
| 1147 | |
| 1148 | $prepare = apply_filters( "pods_field_{$type}_prepare", $prepare, $options, $type ); |
| 1149 | |
| 1150 | return $prepare; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * Validate a value before it's saved |
| 1155 | * |
| 1156 | * @param string $type |
| 1157 | * @param mixed $value |
| 1158 | * @param string $name |
| 1159 | * @param array $options |
| 1160 | * @param array $fields |
| 1161 | * @param array $pod |
| 1162 | * @param int $id |
| 1163 | * @param array|object $params |
| 1164 | * |
| 1165 | * @static |
| 1166 | * |
| 1167 | * @since 2.0.0 |
| 1168 | * @return bool|mixed|void |
| 1169 | */ |
| 1170 | public static function validate( $type, $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
| 1171 | |
| 1172 | self::field_loader( $type ); |
| 1173 | |
| 1174 | $validate = true; |
| 1175 | |
| 1176 | if ( 1 == pods_v( 'pre_save', $options, 1 ) && method_exists( self::$loaded[ $type ], 'validate' ) ) { |
| 1177 | $validate = self::$loaded[ $type ]->validate( $value, $name, $options, $fields, $pod, $id, $params ); |
| 1178 | } |
| 1179 | |
| 1180 | $validate = apply_filters( "pods_field_{$type}_validate", $validate, $value, $name, $options, $fields, $pod, $id, $type, $params ); |
| 1181 | |
| 1182 | return $validate; |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * Change the value or perform actions after validation but before saving to the DB |
| 1187 | * |
| 1188 | * @param string $type |
| 1189 | * @param mixed $value |
| 1190 | * @param int $id |
| 1191 | * @param string $name |
| 1192 | * @param array $options |
| 1193 | * @param array $fields |
| 1194 | * @param array $pod |
| 1195 | * @param object $params |
| 1196 | * |
| 1197 | * @static |
| 1198 | * |
| 1199 | * @since 2.0.0 |
| 1200 | * @return mixed |
| 1201 | */ |
| 1202 | public static function pre_save( $type, $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 1203 | |
| 1204 | self::field_loader( $type ); |
| 1205 | |
| 1206 | if ( 1 == pods_v( 'field_pre_save', $options, 1 ) && method_exists( self::$loaded[ $type ], 'pre_save' ) ) { |
| 1207 | $value = self::$loaded[ $type ]->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 1208 | } |
| 1209 | |
| 1210 | return $value; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Save the value to the DB |
| 1215 | * |
| 1216 | * @param string $type |
| 1217 | * @param mixed $value |
| 1218 | * @param int $id |
| 1219 | * @param string $name |
| 1220 | * @param array $options |
| 1221 | * @param array $fields |
| 1222 | * @param array $pod |
| 1223 | * @param object $params |
| 1224 | * |
| 1225 | * @static |
| 1226 | * |
| 1227 | * @since 2.3.0 |
| 1228 | * @return null |
| 1229 | */ |
| 1230 | public static function save( $type, $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
| 1231 | |
| 1232 | self::field_loader( $type ); |
| 1233 | |
| 1234 | $saved = null; |
| 1235 | |
| 1236 | if ( 1 == pods_v( 'field_save', $options, 1 ) && method_exists( self::$loaded[ $type ], 'save' ) ) { |
| 1237 | $saved = self::$loaded[ $type ]->save( $value, $id, $name, $options, $fields, $pod, $params ); |
| 1238 | } |
| 1239 | |
| 1240 | return $saved; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * Delete the value from the DB |
| 1245 | * |
| 1246 | * @param string $type |
| 1247 | * @param int $id |
| 1248 | * @param string $name |
| 1249 | * @param array $options |
| 1250 | * @param array $pod |
| 1251 | * |
| 1252 | * @static |
| 1253 | * |
| 1254 | * @since 2.3.0 |
| 1255 | * @return null |
| 1256 | */ |
| 1257 | public static function delete( $type, $id = null, $name = null, $options = null, $pod = null ) { |
| 1258 | |
| 1259 | self::field_loader( $type ); |
| 1260 | |
| 1261 | $deleted = null; |
| 1262 | |
| 1263 | if ( 1 == pods_v( 'field_delete', $options, 1 ) && method_exists( self::$loaded[ $type ], 'delete' ) ) { |
| 1264 | $deleted = self::$loaded[ $type ]->delete( $id, $name, $options, $pod ); |
| 1265 | } |
| 1266 | |
| 1267 | return $deleted; |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Check if a user has permission to be editing a field |
| 1272 | * |
| 1273 | * @param $type |
| 1274 | * @param null $name |
| 1275 | * @param null $options |
| 1276 | * @param null $fields |
| 1277 | * @param null $pod |
| 1278 | * @param null $id |
| 1279 | * @param null $params |
| 1280 | * |
| 1281 | * @static |
| 1282 | * |
| 1283 | * @since 2.0.0 |
| 1284 | * @return bool |
| 1285 | */ |
| 1286 | public static function permission( $type, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
| 1287 | |
| 1288 | $permission = pods_permission( $options ); |
| 1289 | |
| 1290 | $permission = (boolean) apply_filters( 'pods_form_field_permission', $permission, $type, $name, $options, $fields, $pod, $id, $params ); |
| 1291 | |
| 1292 | return $permission; |
| 1293 | } |
| 1294 | |
| 1295 | /** |
| 1296 | * Parse the default the value |
| 1297 | * |
| 1298 | * @since 2.0.0 |
| 1299 | * |
| 1300 | * @param $value |
| 1301 | * @param string $type |
| 1302 | * @param null $name |
| 1303 | * @param null $options |
| 1304 | * @param null $pod |
| 1305 | * @param null $id |
| 1306 | * |
| 1307 | * @return mixed|void |
| 1308 | */ |
| 1309 | public static function default_value( $value, $type = 'text', $name = null, $options = null, $pod = null, $id = null ) { |
| 1310 | |
| 1311 | $default_value = pods_v( 'default_value', $options ); |
| 1312 | |
| 1313 | if ( '' === $default_value || null === $default_value ) { |
| 1314 | $default_value = $value; |
| 1315 | } |
| 1316 | |
| 1317 | $default = pods_v( 'default', $options, $default_value, true ); |
| 1318 | |
| 1319 | if ( is_string( $default ) ) { |
| 1320 | $default_value = str_replace( array( '{@', '}' ), '', trim( $default ) ); |
| 1321 | } |
| 1322 | |
| 1323 | if ( $default != $default_value && 1 == (int) pods_v( 'default_evaluate_tags', $options, 1 ) ) { |
| 1324 | $default = pods_evaluate_tags( $default ); |
| 1325 | } |
| 1326 | |
| 1327 | $default = pods_v( pods_v( 'default_value_parameter', $options ), 'request', $default, true ); |
| 1328 | |
| 1329 | if ( $default != $value ) { |
| 1330 | $value = $default; |
| 1331 | } |
| 1332 | |
| 1333 | if ( is_array( $value ) ) { |
| 1334 | $value = pods_serial_comma( $value ); |
| 1335 | } |
| 1336 | |
| 1337 | return apply_filters( 'pods_form_field_default_value', $value, $default, $type, $options, $pod, $id ); |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * Clean a value for use in class / id |
| 1342 | * |
| 1343 | * @since 2.0.0 |
| 1344 | * |
| 1345 | * @param $input |
| 1346 | * @param bool $noarray |
| 1347 | * @param bool $db_field |
| 1348 | * |
| 1349 | * @return mixed|string |
| 1350 | */ |
| 1351 | public static function clean( $input, $noarray = false, $db_field = false ) { |
| 1352 | |
| 1353 | $output = trim( (string) $input ); |
| 1354 | |
| 1355 | $output = str_replace( '--1', 'podsfixtemp1', $output ); |
| 1356 | $output = str_replace( '__1', 'podsfixtemp2', $output ); |
| 1357 | |
| 1358 | if ( false !== $noarray ) { |
| 1359 | $output = preg_replace( '/\[podsfixtemp\d+\]/', '-', $output ); |
| 1360 | $output = preg_replace( '/\[\d*\]/', '-', $output ); |
| 1361 | } |
| 1362 | |
| 1363 | $output = str_replace( array( '[', ']' ), '-', $output ); |
| 1364 | |
| 1365 | $output = pods_clean_name( $output ); |
| 1366 | |
| 1367 | $output = preg_replace( '/([^a-z0-9\-_])/', '', $output ); |
| 1368 | $output = preg_replace( '/(_){2,}/', '_', $output ); |
| 1369 | $output = preg_replace( '/(-){2,}/', '-', $output ); |
| 1370 | |
| 1371 | if ( true !== $db_field ) { |
| 1372 | $output = str_replace( '_', '-', $output ); |
| 1373 | } |
| 1374 | |
| 1375 | $output = rtrim( $output, '-' ); |
| 1376 | |
| 1377 | $output = str_replace( 'podsfixtemp1', '--1', $output ); |
| 1378 | $output = str_replace( 'podsfixtemp2', '__1', $output ); |
| 1379 | |
| 1380 | return $output; |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * Run admin_init methods for each field type |
| 1385 | * |
| 1386 | * @since 2.3.0 |
| 1387 | */ |
| 1388 | public function admin_init() { |
| 1389 | |
| 1390 | $admin_field_types = pods_transient_get( 'pods_form_admin_init_field_types' ); |
| 1391 | |
| 1392 | if ( empty( $admin_field_types ) ) { |
| 1393 | $admin_field_types = array(); |
| 1394 | |
| 1395 | $field_types = self::field_types(); |
| 1396 | |
| 1397 | foreach ( $field_types as $field_type => $field_type_data ) { |
| 1398 | $has_admin_init = self::field_method( $field_type_data['type'], 'admin_init' ); |
| 1399 | |
| 1400 | if ( false !== $has_admin_init ) { |
| 1401 | $admin_field_types[] = $field_type; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | pods_transient_set( 'pods_form_admin_init_field_types', $admin_field_types ); |
| 1406 | } else { |
| 1407 | foreach ( $admin_field_types as $field_type ) { |
| 1408 | self::field_method( $field_type, 'admin_init' ); |
| 1409 | } |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Autoload a Field Type's class |
| 1415 | * |
| 1416 | * @param string $field_type Field Type indentifier |
| 1417 | * @param string $file The Field Type class file location |
| 1418 | * |
| 1419 | * @return string |
| 1420 | * @access public |
| 1421 | * @static |
| 1422 | * @since 2.0.0 |
| 1423 | */ |
| 1424 | public static function field_loader( $field_type, $file = '' ) { |
| 1425 | |
| 1426 | if ( isset( self::$loaded[ $field_type ] ) ) { |
| 1427 | $class_vars = get_class_vars( get_class( self::$loaded[ $field_type ] ) ); |
| 1428 | // PHP 5.2.x workaround |
| 1429 | self::$field_group = ( isset( $class_vars['group'] ) ? $class_vars['group'] : '' ); |
| 1430 | self::$field_type = $class_vars['type']; |
| 1431 | |
| 1432 | if ( 'Unknown' !== $class_vars['label'] ) { |
| 1433 | return self::$loaded[ $field_type ]; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | include_once PODS_DIR . 'classes/PodsField.php'; |
| 1438 | |
| 1439 | $field_type = self::clean( $field_type, true, true ); |
| 1440 | |
| 1441 | $class_name = ucfirst( $field_type ); |
| 1442 | $class_name = "PodsField_{$class_name}"; |
| 1443 | |
| 1444 | $content_dir = realpath( WP_CONTENT_DIR ); |
| 1445 | $plugins_dir = realpath( WP_PLUGIN_DIR ); |
| 1446 | $muplugins_dir = realpath( WPMU_PLUGIN_DIR ); |
| 1447 | $abspath_dir = realpath( ABSPATH ); |
| 1448 | $pods_dir = realpath( PODS_DIR ); |
| 1449 | |
| 1450 | if ( ! class_exists( $class_name ) ) { |
| 1451 | if ( isset( self::$field_types[ $field_type ] ) && ! empty( self::$field_types[ $field_type ]['file'] ) ) { |
| 1452 | $file = realpath( self::$field_types[ $field_type ]['file'] ); |
| 1453 | } |
| 1454 | |
| 1455 | if ( ! empty( $file ) && 0 === strpos( $file, $abspath_dir ) && file_exists( $file ) ) { |
| 1456 | include_once $file; |
| 1457 | } else { |
| 1458 | $file = str_replace( '../', '', apply_filters( 'pods_form_field_include', PODS_DIR . 'classes/fields/' . basename( $field_type ) . '.php', $field_type ) ); |
| 1459 | $file = realpath( $file ); |
| 1460 | |
| 1461 | if ( file_exists( $file ) && ( 0 === strpos( $file, $pods_dir ) || 0 === strpos( $file, $content_dir ) || 0 === strpos( $file, $plugins_dir ) || 0 === strpos( $file, $muplugins_dir ) || 0 === strpos( $file, $abspath_dir ) ) ) { |
| 1462 | include_once $file; |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | if ( class_exists( $class_name ) ) { |
| 1468 | $class = new $class_name(); |
| 1469 | } else { |
| 1470 | $class = new PodsField(); |
| 1471 | $class_name = 'PodsField'; |
| 1472 | } |
| 1473 | |
| 1474 | $class_vars = get_class_vars( $class_name ); |
| 1475 | // PHP 5.2.x workaround |
| 1476 | self::$field_group = ( isset( $class_vars['group'] ) ? $class_vars['group'] : '' ); |
| 1477 | self::$field_type = $class_vars['type']; |
| 1478 | |
| 1479 | self::$loaded[ $field_type ] =& $class; |
| 1480 | |
| 1481 | return self::$loaded[ $field_type ]; |
| 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * Run a method from a Field Type's class |
| 1486 | * |
| 1487 | * @return mixed |
| 1488 | * @internal param string $field_type Field Type indentifier |
| 1489 | * @internal param string $method Method name |
| 1490 | * @internal param mixed $arg More arguments |
| 1491 | * |
| 1492 | * @access public |
| 1493 | * @static |
| 1494 | * @since 2.0.0 |
| 1495 | */ |
| 1496 | public static function field_method() { |
| 1497 | |
| 1498 | $args = func_get_args(); |
| 1499 | |
| 1500 | if ( empty( $args ) && count( $args ) < 2 ) { |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
| 1504 | $field_type = array_shift( $args ); |
| 1505 | $method = array_shift( $args ); |
| 1506 | |
| 1507 | $class = self::field_loader( $field_type ); |
| 1508 | |
| 1509 | if ( method_exists( $class, $method ) ) { |
| 1510 | return call_user_func_array( array( $class, $method ), $args ); |
| 1511 | } |
| 1512 | |
| 1513 | return false; |
| 1514 | } |
| 1515 | |
| 1516 | /** |
| 1517 | * Add a new Pod field type |
| 1518 | * |
| 1519 | * @param string $type The new field type identifier |
| 1520 | * @param string $file The new field type class file location |
| 1521 | * |
| 1522 | * @return array Field Type data |
| 1523 | * |
| 1524 | * @since 2.3.0 |
| 1525 | */ |
| 1526 | public static function register_field_type( $type, $file = null ) { |
| 1527 | |
| 1528 | $field_type = pods_transient_get( 'pods_field_type_' . $type ); |
| 1529 | |
| 1530 | if ( empty( $field_type ) || $field_type['type'] != $type || $field_type['file'] != $file ) { |
| 1531 | self::field_loader( $type, $file ); |
| 1532 | |
| 1533 | $class_vars = get_class_vars( get_class( self::$loaded[ $type ] ) ); |
| 1534 | // PHP 5.2.x workaround |
| 1535 | self::$field_types[ $type ] = $class_vars; |
| 1536 | self::$field_types[ $type ]['file'] = $file; |
| 1537 | |
| 1538 | pods_transient_set( 'pods_field_type_' . $type, self::$field_types[ $type ] ); |
| 1539 | } else { |
| 1540 | self::$field_types[ $type ] = $field_type; |
| 1541 | } |
| 1542 | |
| 1543 | return self::$field_types[ $type ]; |
| 1544 | } |
| 1545 | |
| 1546 | /** |
| 1547 | * Get a list of all available field types and include |
| 1548 | * |
| 1549 | * @return array Registered Field Types data |
| 1550 | * |
| 1551 | * @since 2.3.0 |
| 1552 | */ |
| 1553 | public static function field_types() { |
| 1554 | |
| 1555 | $field_types = array( |
| 1556 | 'text', |
| 1557 | 'website', |
| 1558 | // 'link', |
| 1559 | 'phone', |
| 1560 | 'email', |
| 1561 | 'password', |
| 1562 | 'paragraph', |
| 1563 | 'wysiwyg', |
| 1564 | 'code', |
| 1565 | 'datetime', |
| 1566 | 'date', |
| 1567 | 'time', |
| 1568 | 'number', |
| 1569 | 'currency', |
| 1570 | 'file', |
| 1571 | 'avatar', |
| 1572 | 'oembed', |
| 1573 | 'pick', |
| 1574 | 'boolean', |
| 1575 | 'color', |
| 1576 | 'slug', |
| 1577 | ); |
| 1578 | |
| 1579 | $field_types = array_merge( $field_types, array_keys( self::$field_types ) ); |
| 1580 | |
| 1581 | $field_types = array_filter( array_unique( $field_types ) ); |
| 1582 | |
| 1583 | $types = apply_filters( 'pods_api_field_types', $field_types ); |
| 1584 | |
| 1585 | $field_types = pods_transient_get( 'pods_field_types' ); |
| 1586 | |
| 1587 | if ( empty( $field_types ) || count( $types ) != count( $field_types ) ) { |
| 1588 | $field_types = array(); |
| 1589 | |
| 1590 | foreach ( $types as $field_type ) { |
| 1591 | $file = null; |
| 1592 | |
| 1593 | if ( isset( self::$field_types[ $field_type ] ) ) { |
| 1594 | $file = self::$field_types[ $field_type ]['file']; |
| 1595 | } |
| 1596 | |
| 1597 | self::field_loader( $field_type, $file ); |
| 1598 | |
| 1599 | if ( ! isset( self::$loaded[ $field_type ] ) || ! is_object( self::$loaded[ $field_type ] ) ) { |
| 1600 | continue; |
| 1601 | } |
| 1602 | |
| 1603 | $class_vars = get_class_vars( get_class( self::$loaded[ $field_type ] ) ); |
| 1604 | // PHP 5.2.x workaround |
| 1605 | $field_types[ $field_type ] = $class_vars; |
| 1606 | $field_types[ $field_type ]['file'] = $file; |
| 1607 | } |
| 1608 | |
| 1609 | self::$field_types = $field_types; |
| 1610 | |
| 1611 | pods_transient_set( 'pods_field_types', self::$field_types ); |
| 1612 | } else { |
| 1613 | self::$field_types = array_merge( $field_types, self::$field_types ); |
| 1614 | }//end if |
| 1615 | |
| 1616 | return self::$field_types; |
| 1617 | } |
| 1618 | |
| 1619 | /** |
| 1620 | * Get list of available tableless field types |
| 1621 | * |
| 1622 | * @return array Tableless field types |
| 1623 | * |
| 1624 | * @since 2.3.0 |
| 1625 | */ |
| 1626 | public static function tableless_field_types() { |
| 1627 | |
| 1628 | static $field_types = null; |
| 1629 | |
| 1630 | if ( null === $field_types ) { |
| 1631 | $field_types = array( |
| 1632 | 'pick', |
| 1633 | 'file', |
| 1634 | 'avatar', |
| 1635 | 'taxonomy', |
| 1636 | 'comment', |
| 1637 | ); |
| 1638 | |
| 1639 | $field_types = apply_filters( 'pods_tableless_field_types', $field_types ); |
| 1640 | } |
| 1641 | |
| 1642 | return $field_types; |
| 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * Get list of available file field types |
| 1647 | * |
| 1648 | * @return array File field types |
| 1649 | * |
| 1650 | * @since 2.3.0 |
| 1651 | */ |
| 1652 | public static function file_field_types() { |
| 1653 | |
| 1654 | static $field_types = null; |
| 1655 | |
| 1656 | if ( null === $field_types ) { |
| 1657 | $field_types = array( 'file', 'avatar' ); |
| 1658 | |
| 1659 | $field_types = apply_filters( 'pods_file_field_types', $field_types ); |
| 1660 | } |
| 1661 | |
| 1662 | return $field_types; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * Get list of available repeatable field types |
| 1667 | * |
| 1668 | * @return array Repeatable field types |
| 1669 | * |
| 1670 | * @since 2.3.0 |
| 1671 | */ |
| 1672 | public static function repeatable_field_types() { |
| 1673 | |
| 1674 | static $field_types = null; |
| 1675 | |
| 1676 | if ( null === $field_types ) { |
| 1677 | $field_types = array( |
| 1678 | 'code', |
| 1679 | 'color', |
| 1680 | 'currency', |
| 1681 | 'date', |
| 1682 | 'datetime', |
| 1683 | 'email', |
| 1684 | 'number', |
| 1685 | 'paragraph', |
| 1686 | 'phone', |
| 1687 | 'text', |
| 1688 | 'time', |
| 1689 | 'website', |
| 1690 | 'wysiwyg', |
| 1691 | ); |
| 1692 | |
| 1693 | $field_types = apply_filters( 'pods_repeatable_field_types', $field_types ); |
| 1694 | } |
| 1695 | |
| 1696 | return $field_types; |
| 1697 | } |
| 1698 | |
| 1699 | /** |
| 1700 | * Get list of available number field types |
| 1701 | * |
| 1702 | * @return array Number field types |
| 1703 | * |
| 1704 | * @since 2.3.0 |
| 1705 | */ |
| 1706 | public static function number_field_types() { |
| 1707 | |
| 1708 | static $field_types = null; |
| 1709 | |
| 1710 | if ( null === $field_types ) { |
| 1711 | $field_types = array( 'currency', 'number' ); |
| 1712 | |
| 1713 | $field_types = apply_filters( 'pods_tableless_field_types', $field_types ); |
| 1714 | } |
| 1715 | |
| 1716 | return $field_types; |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * Get list of available date field types |
| 1721 | * |
| 1722 | * @return array Date field types |
| 1723 | * |
| 1724 | * @since 2.3.0 |
| 1725 | */ |
| 1726 | public static function date_field_types() { |
| 1727 | |
| 1728 | static $field_types = null; |
| 1729 | |
| 1730 | if ( null === $field_types ) { |
| 1731 | $field_types = array( 'date', 'datetime', 'time' ); |
| 1732 | |
| 1733 | $field_types = apply_filters( 'pods_tableless_field_types', $field_types ); |
| 1734 | } |
| 1735 | |
| 1736 | return $field_types; |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Get list of available text field types |
| 1741 | * |
| 1742 | * @return array Text field types |
| 1743 | * |
| 1744 | * @since 2.3.0 |
| 1745 | */ |
| 1746 | public static function text_field_types() { |
| 1747 | |
| 1748 | static $field_types = null; |
| 1749 | |
| 1750 | if ( null === $field_types ) { |
| 1751 | $field_types = array( 'code', 'paragraph', 'slug', 'password', 'text', 'wysiwyg' ); |
| 1752 | |
| 1753 | $field_types = apply_filters( 'pods_text_field_types', $field_types ); |
| 1754 | } |
| 1755 | |
| 1756 | return $field_types; |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * Get list of available text field types |
| 1761 | * |
| 1762 | * @return array Text field types |
| 1763 | * |
| 1764 | * @since 2.3.0 |
| 1765 | */ |
| 1766 | public static function block_field_types() { |
| 1767 | |
| 1768 | static $field_types = null; |
| 1769 | |
| 1770 | if ( null === $field_types ) { |
| 1771 | $field_types = array( 'heading', 'html' ); |
| 1772 | |
| 1773 | /** |
| 1774 | * Returns the available text field types |
| 1775 | * |
| 1776 | * @since unknown |
| 1777 | * |
| 1778 | * @param object $field_types Outputs the field types |
| 1779 | */ |
| 1780 | |
| 1781 | $field_types = apply_filters( 'pods_block_field_types', $field_types ); |
| 1782 | } |
| 1783 | |
| 1784 | return $field_types; |
| 1785 | } |
| 1786 | |
| 1787 | /** |
| 1788 | * Get list of available text field types |
| 1789 | * |
| 1790 | * @return array Text field types |
| 1791 | * |
| 1792 | * @since 2.3.0 |
| 1793 | */ |
| 1794 | public static function simple_tableless_objects() { |
| 1795 | |
| 1796 | static $object_types = null; |
| 1797 | |
| 1798 | if ( null === $object_types ) { |
| 1799 | $object_types = self::field_method( 'pick', 'simple_objects' ); |
| 1800 | } |
| 1801 | |
| 1802 | return $object_types; |
| 1803 | } |
| 1804 | |
| 1805 | /** |
| 1806 | * Render the postbox header in a compatible way. |
| 1807 | * |
| 1808 | * @since 2.7.22 |
| 1809 | * |
| 1810 | * @param string $title Header title. |
| 1811 | */ |
| 1812 | public static function render_postbox_header( $title ) { |
| 1813 | pods_view( PODS_DIR . 'ui/admin/postbox-header.php', compact( array_keys( get_defined_vars() ) ) ); |
| 1814 | } |
| 1815 | } |
| 1816 |