|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* UIX Box |
|
4
|
|
|
* |
|
5
|
|
|
* @package ui |
|
6
|
|
|
* @author David Cramer |
|
7
|
|
|
* @license GPL-2.0+ |
|
8
|
|
|
* @link |
|
9
|
|
|
* @copyright 2016 David Cramer |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace uix\ui; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Unlike metaboxes, the box can be rendered via code and will enqueue assets |
|
16
|
|
|
* on the page where its declared. A box also has save on a submission. Data is |
|
17
|
|
|
* saved as an array structure based on the tree of child objects. |
|
18
|
|
|
* |
|
19
|
|
|
* @package uix\ui |
|
20
|
|
|
* @author David Cramer |
|
21
|
|
|
*/ |
|
22
|
|
|
class box extends panel implements \uix\data\save, \uix\data\load { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The type of object |
|
26
|
|
|
* |
|
27
|
|
|
* @since 1.0.0 |
|
28
|
|
|
* @access public |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public $type = 'box'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The wrapper element of the object |
|
35
|
|
|
* |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
* @access public |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
public $element = 'form'; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Sets the controls data |
|
45
|
|
|
* |
|
46
|
|
|
* @since 1.0.0 |
|
47
|
|
|
* @see \uix\uix |
|
48
|
|
|
* @access public |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function init() { |
|
51
|
|
|
// run parents to setup sanitization filters |
|
52
|
1 |
|
$data = uix()->request_vars( 'post' ); |
|
53
|
1 |
|
if ( isset( $data[ 'uixNonce_' . $this->id() ] ) && wp_verify_nonce( $data[ 'uixNonce_' . $this->id() ], $this->id() ) ) { |
|
54
|
1 |
|
$this->save_data(); |
|
55
|
|
|
} else { |
|
56
|
|
|
// load data normally |
|
57
|
1 |
|
$this->set_data( array( $this->slug => $this->load_data() ) ); |
|
58
|
|
|
} |
|
59
|
|
|
// set the wrapper element based on static or not |
|
60
|
1 |
|
if ( ! empty( $this->struct['static'] ) ) { |
|
61
|
|
|
$this->element = 'div'; |
|
62
|
|
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* save data to database |
|
67
|
|
|
* |
|
68
|
|
|
* @since 1.0.0 |
|
69
|
|
|
* @access public |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function save_data() { |
|
72
|
|
|
$data = $this->get_data(); |
|
73
|
1 |
|
|
|
74
|
|
|
return update_option( $this->store_key(), $data ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* get the objects data store key |
|
79
|
|
|
* |
|
80
|
|
|
* @since 1.0.0 |
|
81
|
|
|
* @access public |
|
82
|
|
|
* @return string $store_key the defined option name for this UIX object |
|
83
|
1 |
|
*/ |
|
84
|
1 |
|
public function store_key() { |
|
85
|
|
|
if ( ! empty( $this->struct['store_key'] ) ) { |
|
86
|
|
|
return $this->struct['store_key']; |
|
87
|
|
|
} |
|
88
|
1 |
|
|
|
89
|
|
|
return sanitize_key( $this->slug ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get Data from all controls of this section |
|
94
|
|
|
* |
|
95
|
|
|
* @since 1.0.0 |
|
96
|
|
|
* @see \uix\load |
|
97
|
|
|
* @return array Array of sections data structured by the controls |
|
98
|
2 |
|
*/ |
|
99
|
|
|
public function get_data() { |
|
100
|
2 |
|
|
|
101
|
2 |
|
if ( empty( $this->data ) ) { |
|
102
|
2 |
|
$data = parent::get_data(); |
|
103
|
2 |
|
if ( ! empty( $data[ $this->slug ] ) ) { |
|
104
|
|
|
$this->data = $data[ $this->slug ]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
2 |
|
|
|
108
|
|
|
return $this->data; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get data |
|
113
|
|
|
* |
|
114
|
|
|
* @since 1.0.0 |
|
115
|
|
|
* @access public |
|
116
|
|
|
* @return mixed $data Requested data of the object |
|
117
|
1 |
|
*/ |
|
118
|
1 |
|
public function load_data() { |
|
119
|
|
|
$data = get_option( $this->store_key() ); |
|
120
|
|
|
if ( empty( $data ) ) { |
|
121
|
|
|
$data = $this->get_data(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $data; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
11 |
|
* set metabox styles |
|
129
|
|
|
* |
|
130
|
11 |
|
* @since 1.0.0 |
|
131
|
11 |
|
* @see \uix\ui\uix |
|
132
|
|
|
* @access public |
|
133
|
|
|
*/ |
|
134
|
11 |
|
public function set_assets() { |
|
135
|
11 |
|
$this->set_ajax_assets(); |
|
136
|
|
|
parent::set_assets(); |
|
137
|
|
|
} |
|
138
|
11 |
|
|
|
139
|
|
|
/** |
|
140
|
11 |
|
* Sets the wrappers attributes |
|
141
|
11 |
|
* |
|
142
|
|
|
* @since 1.0.0 |
|
143
|
|
|
* @access public |
|
144
|
|
|
*/ |
|
145
|
|
|
public function set_attributes() { |
|
146
|
|
|
|
|
147
|
|
|
$action = uix()->request_vars( 'server' ); |
|
148
|
|
|
$this->attributes += array( |
|
149
|
3 |
|
'enctype' => 'multipart/form-data', |
|
150
|
|
|
'method' => 'POST', |
|
151
|
3 |
|
'class' => [ 'uix-ajax', 'uix-' . $this->type ], |
|
152
|
|
|
'data-uix' => $this->slug, |
|
153
|
3 |
|
'action' => $action['REQUEST_URI'], |
|
154
|
3 |
|
); |
|
155
|
3 |
|
$this->attributes['class'][] = 'uix-' . $this->type; |
|
156
|
3 |
|
if ( empty( $this->struct['static'] ) ) { |
|
157
|
3 |
|
$this->attributes['class'][] = 'uix-ajax'; |
|
158
|
|
|
} |
|
159
|
3 |
|
|
|
160
|
|
|
parent::set_attributes(); |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Render the main structure based on save or not |
|
166
|
|
|
* |
|
167
|
3 |
|
* @since 1.0.0 |
|
168
|
|
|
* @access public |
|
169
|
3 |
|
* @return string HTML of rendered page |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function render() { |
|
172
|
|
|
$output = null; |
|
173
|
|
|
|
|
174
|
|
|
$output .= '<' . esc_attr( $this->element ) . ' ' . $this->build_attributes() . '>'; |
|
175
|
|
|
$output .= $this->render_header(); |
|
176
|
|
|
$output .= parent::render(); |
|
177
|
|
|
$output .= $this->render_footer(); |
|
178
|
|
|
$output .= wp_nonce_field( $this->id(), 'uixNonce_' . $this->id(), true, false ); |
|
179
|
|
|
$output .= '</' . esc_attr( $this->element ) . '>'; |
|
180
|
3 |
|
|
|
181
|
3 |
|
return $output; |
|
182
|
|
|
} |
|
183
|
3 |
|
|
|
184
|
3 |
|
/** |
|
185
|
3 |
|
* Render the header if set. |
|
186
|
3 |
|
* |
|
187
|
3 |
|
* @since 1.0.0 |
|
188
|
3 |
|
* @access public |
|
189
|
|
|
* @return string HTML of rendered page |
|
190
|
3 |
|
*/ |
|
191
|
|
|
public function render_header() { |
|
192
|
|
|
|
|
193
|
|
|
return $this->render_child_type( 'header' ); |
|
194
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Render a child of type footer. |
|
199
|
|
|
* |
|
200
|
3 |
|
* @since 3.0.0 |
|
201
|
|
|
* @access public |
|
202
|
3 |
|
* @return string HTML of rendered page |
|
203
|
|
|
*/ |
|
204
|
|
|
public function render_footer() { |
|
205
|
|
|
|
|
206
|
|
|
return $this->render_child_type( 'footer' ); |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Render a child type. |
|
212
|
|
|
* |
|
213
|
3 |
|
* @since 3.0.0 |
|
214
|
|
|
* @access public |
|
215
|
3 |
|
* |
|
216
|
|
|
* @param string $type The type of child to render. |
|
217
|
|
|
* |
|
218
|
|
|
* @return string HTML of rendered page |
|
219
|
|
|
*/ |
|
220
|
|
|
public function render_child_type( $type ) { |
|
221
|
|
|
|
|
222
|
|
|
$output = null; |
|
223
|
|
|
if ( ! empty( $this->child ) ) { |
|
224
|
|
|
foreach ( $this->child as $child ) { |
|
225
|
|
|
if ( $type === $child->type ) { |
|
226
|
|
|
$output .= $child->render(); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
3 |
|
} |
|
230
|
|
|
|
|
231
|
3 |
|
return $output; |
|
232
|
3 |
|
|
|
233
|
3 |
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|