1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CMB base field type |
4
|
|
|
* |
5
|
|
|
* @since 2.2.2 |
6
|
|
|
* |
7
|
|
|
* @category WordPress_Plugin |
8
|
|
|
* @package CMB2 |
9
|
|
|
* @author CMB2 team |
10
|
|
|
* @license GPL-2.0+ |
11
|
|
|
* @link https://cmb2.io |
12
|
|
|
*/ |
13
|
|
|
abstract class CMB2_Type_Base { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The CMB2_Types object |
17
|
|
|
* |
18
|
|
|
* @var CMB2_Types |
19
|
|
|
*/ |
20
|
|
|
public $types; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Arguments for use in the render method |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public $args; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Rendered output (if 'rendered' argument is set to false) |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $rendered = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor |
38
|
|
|
* |
39
|
|
|
* @since 2.2.2 |
40
|
|
|
* @param CMB2_Types $types |
41
|
|
|
* @param array $args |
42
|
|
|
*/ |
43
|
64 |
|
public function __construct( CMB2_Types $types, $args = array() ) { |
44
|
64 |
|
$this->types = $types; |
45
|
64 |
|
$args['rendered'] = isset( $args['rendered'] ) ? (bool) $args['rendered'] : true; |
46
|
64 |
|
$this->args = $args; |
47
|
64 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Handles rendering this field type. |
51
|
|
|
* |
52
|
|
|
* @since 2.2.2 |
53
|
|
|
* @return string Rendered field type. |
54
|
|
|
*/ |
55
|
|
|
abstract public function render(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Stores the rendered field output. |
59
|
|
|
* |
60
|
|
|
* @since 2.2.2 |
61
|
|
|
* @param string|CMB2_Type_Base $rendered Rendered output. |
62
|
|
|
* @return string|CMB2_Type_Base Rendered output or this object. |
63
|
|
|
*/ |
64
|
57 |
|
public function rendered( $rendered ) { |
65
|
57 |
|
$this->field->register_js_data(); |
|
|
|
|
66
|
|
|
|
67
|
57 |
|
if ( $this->args['rendered'] ) { |
68
|
57 |
|
return is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->rendered = is_a( $rendered, __CLASS__ ) ? $rendered->rendered : $rendered; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the stored rendered field output. |
78
|
|
|
* |
79
|
|
|
* @since 2.2.2 |
80
|
|
|
* @return string Stored rendered output (if 'rendered' argument is set to false). |
81
|
|
|
*/ |
82
|
|
|
public function get_rendered() { |
83
|
|
|
return $this->rendered; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Handles parsing and filtering attributes while preserving any passed in via field config. |
88
|
|
|
* |
89
|
|
|
* @since 1.1.0 |
90
|
|
|
* @param string $element Element for filter |
91
|
|
|
* @param array $type_defaults Type default arguments |
92
|
|
|
* @param array $type_overrides Type override arguments |
93
|
|
|
* @return array Parsed and filtered arguments |
94
|
|
|
*/ |
95
|
57 |
|
public function parse_args( $element, $type_defaults, $type_overrides = array() ) { |
96
|
57 |
|
$args = $this->parse_args_from_overrides( $type_overrides ); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Filter attributes for a field type. |
100
|
|
|
* The dynamic portion of the hook name, $element, refers to the field type. |
101
|
|
|
* |
102
|
|
|
* @since 1.1.0 |
103
|
|
|
* @param array $args The array of attribute arguments. |
104
|
|
|
* @param array $type_defaults The array of default values. |
105
|
|
|
* @param array $field The `CMB2_Field` object. |
106
|
|
|
* @param object $field_type_object This `CMB2_Types` object. |
107
|
|
|
*/ |
108
|
57 |
|
$args = apply_filters( "cmb2_{$element}_attributes", $args, $type_defaults, $this->field, $this->types ); |
|
|
|
|
109
|
|
|
|
110
|
57 |
|
$args = wp_parse_args( $args, $type_defaults ); |
111
|
|
|
|
112
|
57 |
|
if ( ! empty( $args['js_dependencies'] ) ) { |
113
|
14 |
|
$this->field->add_js_dependencies( $args['js_dependencies'] ); |
114
|
|
|
} |
115
|
|
|
|
116
|
57 |
|
return $args; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Handles parsing and filtering attributes while preserving any passed in via field config. |
121
|
|
|
* |
122
|
|
|
* @since 2.2.4 |
123
|
|
|
* @param array $type_overrides Type override arguments |
124
|
|
|
* @return array Parsed arguments |
125
|
|
|
*/ |
126
|
57 |
|
protected function parse_args_from_overrides( $type_overrides = array() ) { |
127
|
57 |
|
$type_overrides = empty( $type_overrides ) ? $this->args : $type_overrides; |
128
|
|
|
|
129
|
57 |
|
if ( true !== $this->field->args( 'disable_hash_data_attribute' ) ) { |
|
|
|
|
130
|
57 |
|
$type_overrides['data-hash'] = $this->field->hash_id(); |
131
|
|
|
} |
132
|
|
|
|
133
|
57 |
|
$field_overrides = $this->field->args( 'attributes' ); |
134
|
|
|
|
135
|
57 |
|
return ! empty( $field_overrides ) |
136
|
3 |
|
? wp_parse_args( $field_overrides, $type_overrides ) |
137
|
57 |
|
: $type_overrides; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Fall back to CMB2_Types methods |
142
|
|
|
* |
143
|
|
|
* @param string $field |
144
|
|
|
* @throws Exception Throws an exception if the field is invalid. |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
59 |
|
public function __call( $name, $arguments ) { |
148
|
|
|
switch ( $name ) { |
149
|
59 |
|
case '_id': |
150
|
59 |
|
case '_name': |
151
|
59 |
|
case '_desc': |
152
|
58 |
|
case '_text': |
153
|
56 |
|
case 'concat_attrs': |
154
|
59 |
|
return call_user_func_array( array( $this->types, $name ), $arguments ); |
155
|
|
|
default: |
156
|
|
|
throw new Exception( sprintf( esc_html__( 'Invalid %1$s method: %2$s', 'cmb2' ), __CLASS__, $name ) ); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Magic getter for our object. |
162
|
|
|
* |
163
|
|
|
* @param string $field |
164
|
|
|
* @throws Exception Throws an exception if the field is invalid. |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
64 |
|
public function __get( $field ) { |
168
|
|
|
switch ( $field ) { |
169
|
64 |
|
case 'field': |
170
|
64 |
|
return $this->types->field; |
171
|
|
|
default: |
172
|
|
|
throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|