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 |
|
|
136
|
|
|
$this->assets['script']['baldrick'] = array( |
137
|
|
|
'src' => $this->url . 'assets/js/jquery.baldrick' . UIX_ASSET_DEBUG . '.js', |
138
|
11 |
|
'deps' => array( 'jquery' ), |
139
|
|
|
); |
140
|
11 |
|
$this->assets['script']['uix-ajax'] = array( |
141
|
11 |
|
'src' => $this->url . 'assets/js/ajax' . UIX_ASSET_DEBUG . '.js', |
142
|
|
|
'deps' => array( 'baldrick' ), |
143
|
|
|
); |
144
|
|
|
$this->assets['style']['uix-ajax'] = $this->url . 'assets/css/ajax' . UIX_ASSET_DEBUG . '.css'; |
145
|
|
|
|
146
|
|
|
parent::set_assets(); |
147
|
|
|
} |
148
|
|
|
|
149
|
3 |
|
/** |
150
|
|
|
* Sets the wrappers attributes |
151
|
3 |
|
* |
152
|
|
|
* @since 1.0.0 |
153
|
3 |
|
* @access public |
154
|
3 |
|
*/ |
155
|
3 |
|
public function set_attributes() { |
156
|
3 |
|
|
157
|
3 |
|
$action = uix()->request_vars( 'server' ); |
158
|
|
|
$attributes = array( |
159
|
3 |
|
'enctype' => 'multipart/form-data', |
160
|
|
|
'method' => 'POST', |
161
|
|
|
'class' => 'uix-ajax uix-' . $this->type, |
162
|
|
|
'data-uix' => $this->slug, |
163
|
|
|
'action' => $action['REQUEST_URI'], |
164
|
|
|
); |
165
|
|
|
if ( ! empty( $this->struct['static'] ) ) { |
166
|
|
|
|
167
|
3 |
|
$attributes = array( |
168
|
|
|
'class' => 'uix-' . $this->type, |
169
|
3 |
|
'data-uix' => $this->slug, |
170
|
|
|
); |
171
|
3 |
|
} |
172
|
|
|
|
173
|
|
|
$this->attributes += $attributes; |
174
|
|
|
|
175
|
|
|
parent::set_attributes(); |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
3 |
|
* Render the main structure based on save or not |
181
|
3 |
|
* |
182
|
|
|
* @since 1.0.0 |
183
|
3 |
|
* @access public |
184
|
3 |
|
* @return string HTML of rendered page |
185
|
3 |
|
*/ |
186
|
3 |
|
public function render() { |
187
|
3 |
|
$output = null; |
188
|
3 |
|
|
189
|
|
|
$output .= '<' . esc_attr( $this->element ) . ' ' . $this->build_attributes() . '>'; |
190
|
3 |
|
$output .= $this->render_header(); |
191
|
|
|
$output .= parent::render(); |
192
|
|
|
$output .= $this->render_footer(); |
193
|
|
|
$output .= wp_nonce_field( $this->id(), 'uixNonce_' . $this->id(), true, false ); |
194
|
|
|
$output .= '</' . esc_attr( $this->element ) . '>'; |
195
|
|
|
|
196
|
|
|
return $output; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
3 |
|
* Render the header if set. |
201
|
|
|
* |
202
|
3 |
|
* @since 1.0.0 |
203
|
|
|
* @access public |
204
|
|
|
* @return string HTML of rendered page |
205
|
|
|
*/ |
206
|
|
|
public function render_header() { |
207
|
|
|
|
208
|
|
|
return $this->render_child_type( 'header' ); |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
3 |
|
* Render a child of type footer. |
214
|
|
|
* |
215
|
3 |
|
* @since 3.0.0 |
216
|
|
|
* @access public |
217
|
|
|
* @return string HTML of rendered page |
218
|
|
|
*/ |
219
|
|
|
public function render_footer() { |
220
|
|
|
|
221
|
|
|
return $this->render_child_type( 'footer' ); |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Render a child type. |
227
|
|
|
* |
228
|
|
|
* @since 3.0.0 |
229
|
3 |
|
* @access public |
230
|
|
|
* |
231
|
3 |
|
* @param string $type The type of child to render. |
232
|
3 |
|
* |
233
|
3 |
|
* @return string HTML of rendered page |
234
|
3 |
|
*/ |
235
|
3 |
|
public function render_child_type( $type ) { |
236
|
|
|
|
237
|
|
|
$output = null; |
238
|
|
|
if ( ! empty( $this->child ) ) { |
239
|
|
|
foreach ( $this->child as $child ) { |
240
|
3 |
|
if ( $type === $child->type ) { |
241
|
|
|
$output .= $child->render(); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $output; |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|