| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 2.03.05 |
| 8 |
*/ |
| 9 |
class FrmFieldFactory { |
| 10 |
|
| 11 |
/** |
| 12 |
* Create an instance of an FrmFieldValueSelector object |
| 13 |
* |
| 14 |
* @since 2.03.05 |
| 15 |
* |
| 16 |
* @param int $field_id |
| 17 |
* @param array $args |
| 18 |
* |
| 19 |
* @return FrmFieldValueSelector |
| 20 |
*/ |
| 21 |
public static function create_field_value_selector( $field_id, $args ) { |
| 22 |
$selector = null; |
| 23 |
|
| 24 |
if ( $field_id > 0 ) { |
| 25 |
$selector = apply_filters( 'frm_create_field_value_selector', $selector, $field_id, $args ); |
| 26 |
} |
| 27 |
|
| 28 |
if ( ! is_object( $selector ) ) { |
| 29 |
$selector = new FrmFieldValueSelector( $field_id, $args ); |
| 30 |
} |
| 31 |
|
| 32 |
return $selector; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @since 3.0 |
| 37 |
* |
| 38 |
* @param object|array $field |
| 39 |
*/ |
| 40 |
public static function get_field_factory( $field ) { |
| 41 |
if ( is_object( $field ) ) { |
| 42 |
$field_info = self::get_field_object( $field ); |
| 43 |
} elseif ( isset( $field['id'] ) && $field['id'] ) { |
| 44 |
$field_info = self::get_field_object( $field['id'] ); |
| 45 |
} else { |
| 46 |
$field_info = self::get_field_type( $field['type'], $field ); |
| 47 |
} |
| 48 |
|
| 49 |
return $field_info; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param int|string|object $field |
| 54 |
* |
| 55 |
* @return stdClass |
| 56 |
*/ |
| 57 |
public static function get_field_object( $field ) { |
| 58 |
if ( ! is_object( $field ) ) { |
| 59 |
$field = FrmField::getOne( $field ); |
| 60 |
} |
| 61 |
|
| 62 |
return self::get_field_type( $field->type, $field ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @since 3.0 |
| 67 |
* |
| 68 |
* @param string $field_type |
| 69 |
* @param int|array|object $field |
| 70 |
* |
| 71 |
* @return stdClass |
| 72 |
*/ |
| 73 |
public static function get_field_type( $field_type, $field = 0 ) { |
| 74 |
$class = self::get_field_type_class( $field_type ); |
| 75 |
if ( empty( $class ) ) { |
| 76 |
$field = new FrmFieldDefault( $field, $field_type ); |
| 77 |
} else { |
| 78 |
$field = new $class( $field, $field_type ); |
| 79 |
} |
| 80 |
|
| 81 |
return $field; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @since 3.0 |
| 86 |
* |
| 87 |
* @param string $field_type |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
private static function get_field_type_class( $field_type ) { |
| 92 |
$type_classes = array( |
| 93 |
'text' => 'FrmFieldText', |
| 94 |
'textarea' => 'FrmFieldTextarea', |
| 95 |
'select' => 'FrmFieldSelect', |
| 96 |
'radio' => 'FrmFieldRadio', |
| 97 |
'checkbox' => 'FrmFieldCheckbox', |
| 98 |
'number' => 'FrmFieldNumber', |
| 99 |
'phone' => 'FrmFieldPhone', |
| 100 |
'url' => 'FrmFieldUrl', |
| 101 |
'website' => 'FrmFieldUrl', |
| 102 |
'email' => 'FrmFieldEmail', |
| 103 |
'user_id' => 'FrmFieldUserID', |
| 104 |
'html' => 'FrmFieldHTML', |
| 105 |
'hidden' => 'FrmFieldHidden', |
| 106 |
'captcha' => 'FrmFieldCaptcha', |
| 107 |
'name' => 'FrmFieldName', |
| 108 |
'credit_card' => 'FrmFieldCreditCard', |
| 109 |
); |
| 110 |
|
| 111 |
$class = isset( $type_classes[ $field_type ] ) ? $type_classes[ $field_type ] : ''; |
| 112 |
|
| 113 |
return apply_filters( 'frm_get_field_type_class', $class, $field_type ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @since 3.0 |
| 118 |
*/ |
| 119 |
public static function field_has_html( $type ) { |
| 120 |
$has_html = self::field_has_property( $type, 'has_html' ); |
| 121 |
|
| 122 |
// this hook is here for reverse compatibility since 3.0 |
| 123 |
return apply_filters( 'frm_show_custom_html', $has_html, $type ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @since 3.0 |
| 128 |
* |
| 129 |
* @param string $type |
| 130 |
* @param string $property |
| 131 |
*/ |
| 132 |
public static function field_has_property( $type, $property ) { |
| 133 |
$field = self::get_field_type( $type ); |
| 134 |
|
| 135 |
return $field->{$property}; |
| 136 |
} |
| 137 |
} |
| 138 |
|