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
|
60 |
|
public function __construct( CMB2_Types $types, $args = array() ) { |
44
|
60 |
|
$this->types = $types; |
45
|
60 |
|
$args['rendered'] = isset( $args['rendered'] ) ? (bool) $args['rendered'] : true; |
46
|
60 |
|
$this->args = $args; |
47
|
60 |
|
} |
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
|
53 |
|
public function rendered( $rendered ) { |
65
|
53 |
|
$this->field->register_js_data(); |
|
|
|
|
66
|
53 |
|
|
67
|
|
|
if ( $this->args['rendered'] ) { |
68
|
|
|
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_args Type override arguments |
93
|
53 |
|
* @return array Parsed and filtered arguments |
94
|
53 |
|
*/ |
95
|
|
|
public function parse_args( $element, $type_defaults, $type_args = array() ) { |
96
|
53 |
|
$type_args = empty( $type_args ) ? $this->args : $type_args; |
97
|
|
|
|
98
|
53 |
|
if ( true !== $this->field->args( 'disable_hash_data_attribute' ) ) { |
|
|
|
|
99
|
53 |
|
$type_args['data-hash'] = $this->field->hash_id(); |
100
|
53 |
|
} |
101
|
|
|
|
102
|
|
|
$field_overrides = $this->field->args( 'attributes' ); |
103
|
|
|
|
104
|
|
|
$args = ! empty( $field_overrides ) |
105
|
|
|
? wp_parse_args( $field_overrides, $type_args ) |
106
|
|
|
: $type_args; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Filter attributes for a field type. |
110
|
|
|
* The dynamic portion of the hook name, $element, refers to the field type. |
111
|
|
|
* |
112
|
53 |
|
* @since 1.1.0 |
113
|
|
|
* @param array $args The array of attribute arguments. |
114
|
53 |
|
* @param array $type_defaults The array of default values. |
115
|
|
|
* @param array $field The `CMB2_Field` object. |
116
|
|
|
* @param object $field_type_object This `CMB2_Types` object. |
117
|
|
|
*/ |
118
|
|
|
$args = apply_filters( "cmb2_{$element}_attributes", $args, $type_defaults, $this->field, $this->types ); |
119
|
|
|
|
120
|
|
|
return wp_parse_args( $args, $type_defaults ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
55 |
|
* Fall back to CMB2_Types methods |
125
|
|
|
* |
126
|
55 |
|
* @param string $field |
127
|
55 |
|
* @throws Exception Throws an exception if the field is invalid. |
128
|
55 |
|
* @return mixed |
129
|
55 |
|
*/ |
130
|
55 |
|
public function __call( $name, $arguments ) { |
131
|
55 |
|
switch ( $name ) { |
132
|
|
|
case '_id': |
133
|
|
|
case '_name': |
134
|
|
|
case '_desc': |
135
|
|
|
case '_text': |
136
|
|
|
case 'concat_attrs': |
137
|
|
|
return call_user_func_array( array( $this->types, $name ), $arguments ); |
138
|
|
|
default: |
139
|
|
|
throw new Exception( sprintf( esc_html__( 'Invalid %1$s method: %2$s', 'cmb2' ), __CLASS__, $name ) ); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
60 |
|
* Magic getter for our object. |
145
|
|
|
* |
146
|
60 |
|
* @param string $field |
147
|
60 |
|
* @throws Exception Throws an exception if the field is invalid. |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function __get( $field ) { |
151
|
|
|
switch ( $field ) { |
152
|
|
|
case 'field': |
153
|
|
|
return $this->types->field; |
154
|
|
|
default: |
155
|
|
|
throw new Exception( sprintf( esc_html__( 'Invalid %1$s property: %2$s', 'cmb2' ), __CLASS__, $field ) ); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|