1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UIX Control |
4
|
|
|
* |
5
|
|
|
* @package controls |
6
|
|
|
* @author David Cramer |
7
|
|
|
* @license GPL-2.0+ |
8
|
|
|
* @link |
9
|
|
|
* @copyright 2016 David Cramer |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace uix\ui\control; |
13
|
|
|
|
14
|
|
|
use uix\ui\modal; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Modal based config items |
18
|
|
|
* |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class item extends \uix\ui\control { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The type of object |
25
|
|
|
* |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
* @access public |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $type = 'item'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The templates to render in the footer |
34
|
|
|
* |
35
|
|
|
* @since 1.0.0 |
36
|
|
|
* @access public |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $templates = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Autoload Children - Checks structure for nested structures |
43
|
|
|
* |
44
|
|
|
* @since 1.0.0 |
45
|
|
|
* @access public |
46
|
|
|
*/ |
47
|
1 |
|
public function setup() { |
48
|
|
|
|
49
|
1 |
|
$this->struct['modal'] = $this->get_base(); |
50
|
|
|
|
51
|
1 |
|
if ( ! empty( $this->struct['config'] ) ) { |
52
|
|
|
$this->struct['modal']['config'] = array_merge( $this->struct['modal']['config'], $this->struct['config'] ); |
53
|
|
|
if ( ! empty( $this->struct['config']['add_text'] ) ) { |
54
|
|
|
$this->struct['modal']['config']['footer']['control']['add_item']['label'] = $this->struct['config']['add_text']; |
55
|
|
|
} |
56
|
|
|
if ( ! empty( $this->struct['config']['update_text'] ) ) { |
57
|
|
|
$this->struct['modal']['config']['footer']['control']['update_item']['label'] = $this->struct['config']['update_text']; |
58
|
|
|
} |
59
|
|
|
unset( $this->struct['config'] ); // remove so no clashing. |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
$this->struct['modal']['config']['view'] = $this->struct['modal']['config']['template']; |
63
|
1 |
|
unset( $this->struct['modal']['config']['template'] ); |
64
|
|
|
|
65
|
1 |
|
add_action( 'uix_control_item_submit_' . $this->slug, $this->struct['modal']['config']['callback'], 100 ); |
66
|
|
|
|
67
|
1 |
|
parent::setup(); |
68
|
|
|
|
69
|
|
|
// set data for templates. |
70
|
1 |
|
if ( ! $this->child['config']->is_submitted() ) { |
71
|
1 |
|
$data = $this->child['config']->get_data(); |
72
|
1 |
|
$this->child['config']->struct['attributes']['data-default'] = json_encode( $data ); |
73
|
1 |
|
$data_template = $this->drill_in( $data ); |
74
|
|
|
|
75
|
1 |
|
$this->child['config']->set_data( $data_template ); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Builds the handlebars based structure for template render |
82
|
|
|
* |
83
|
|
|
* @param array $array the data structure to drill into. |
84
|
|
|
* @param string $tag , the final tag to replace the data with. |
85
|
|
|
* |
86
|
|
|
* @since 1.0.0 |
87
|
|
|
* @access public |
88
|
|
|
* @return array array of the data structure |
89
|
|
|
*/ |
90
|
1 |
|
public function drill_in( $array, $tag = '{{json @root' ) { |
91
|
|
|
|
92
|
1 |
|
$back = array(); |
93
|
1 |
|
foreach ( $array as $key => $value ) { |
94
|
1 |
|
if ( is_array( $value ) && ! empty( $value ) ) { |
95
|
1 |
|
$back[ $key ] = $this->drill_in( $value, $tag . '.' . $key ); |
96
|
|
|
} else { |
97
|
1 |
|
$back[ $key ] = $tag . '.' . $key . '}}'; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
return $back; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sends the items json code back to the browser |
106
|
|
|
* |
107
|
|
|
* @since 1.0.0 |
108
|
|
|
* @access public |
109
|
|
|
*/ |
110
|
|
|
public function send_item_json() { |
111
|
|
|
// send to browser. |
112
|
|
|
wp_send_json_success( $this->child['config']->get_value() ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* All objects loaded - application method for finishing off loading objects |
117
|
|
|
* |
118
|
|
|
* @since 1.0.0 |
119
|
|
|
* @access public |
120
|
|
|
*/ |
121
|
|
|
public function init() { |
122
|
|
|
$this->handle_submit(); |
123
|
|
|
parent::init(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Handles the new item create submission |
128
|
|
|
* |
129
|
|
|
* @since 1.0.0 |
130
|
|
|
* @access public |
131
|
|
|
*/ |
132
|
|
|
public function handle_submit() { |
133
|
|
|
if ( isset( $this->child['config'] ) && $this->child['config']->is_submitted() ) { |
134
|
|
|
$data = $this->child['config']->get_value(); |
135
|
|
|
if ( isset( $data['config_foot'] ) ) { |
136
|
|
|
unset( $data['config_foot'] ); // default footer has no values. override to include values in it. |
137
|
|
|
} |
138
|
|
|
do_action( 'uix_control_item_submit_' . $this->slug, $data, $this ); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Define core UIX scripts - override to register core ( common scripts for |
144
|
|
|
* uix type ) |
145
|
|
|
* |
146
|
|
|
* @since 1.0.0 |
147
|
|
|
* @access public |
148
|
|
|
*/ |
149
|
1 |
|
public function set_assets() { |
150
|
|
|
|
151
|
1 |
|
$this->assets['style']['item'] = $this->url . 'assets/css/item' . UIX_ASSET_DEBUG . '.css'; |
152
|
1 |
|
$this->assets['script']['handlebars'] = array( |
153
|
1 |
|
'src' => $this->url . 'assets/js/handlebars-latest' . UIX_ASSET_DEBUG . '.js', |
154
|
|
|
); |
155
|
1 |
|
$this->assets['script']['baldrick-handlebars'] = array( |
156
|
1 |
|
'src' => $this->url . 'assets/js/handlebars.baldrick' . UIX_ASSET_DEBUG . '.js', |
157
|
|
|
'deps' => array( 'baldrick' ), |
158
|
|
|
); |
159
|
1 |
|
$this->assets['script']['item'] = array( |
160
|
1 |
|
'src' => $this->url . 'assets/js/item' . UIX_ASSET_DEBUG . '.js', |
161
|
|
|
); |
162
|
1 |
|
parent::set_assets(); |
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Gets the attributes for the control. |
167
|
|
|
* |
168
|
|
|
* @since 1.0.0 |
169
|
|
|
* @access public |
170
|
|
|
*/ |
171
|
1 |
|
public function set_attributes() { |
172
|
|
|
|
173
|
1 |
|
parent::set_attributes(); |
174
|
1 |
|
$this->attributes['class'] = 'hidden'; |
175
|
1 |
|
$this->attributes['value'] = '{{json this}}'; |
176
|
|
|
|
177
|
1 |
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Render the Control |
181
|
|
|
* |
182
|
|
|
* @since 1.0.0 |
183
|
|
|
* @see \uix\ui\uix |
184
|
|
|
* @access public |
185
|
|
|
* @return string HTML of rendered control |
186
|
|
|
*/ |
187
|
1 |
|
public function render() { |
188
|
1 |
|
add_action( 'admin_footer', array( $this, 'render_footer_script' ) ); |
189
|
1 |
|
add_action( 'wp_footer', array( $this, 'render_footer_script' ) ); |
190
|
1 |
|
$output = null; |
191
|
1 |
|
$this->templates .= $this->item_template(); |
192
|
1 |
|
$output .= '<div id="' . esc_attr( $this->id() ) . '" data-color="' . esc_attr( $this->base_color() ) . '" data-for="' . esc_attr( $this->id() ) . '-control" class="processing uix-control uix-control-' . esc_attr( $this->type ) . ' ' . esc_attr( $this->id() ) . '">'; |
193
|
1 |
|
$output .= '</div>'; |
194
|
1 |
|
$output .= $this->child['config']->render(); |
195
|
1 |
|
$output .= $this->input(); |
196
|
|
|
|
197
|
|
|
|
198
|
1 |
|
return $output; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Render the modal template |
203
|
|
|
* |
204
|
|
|
* @since 1.0.0 |
205
|
|
|
* @see \uix\ui\uix |
206
|
|
|
* @access public |
207
|
|
|
* @return string HTML of modals templates |
208
|
|
|
*/ |
209
|
1 |
|
public function item_template() { |
210
|
1 |
|
$output = '<script type="text/html" id="' . esc_attr( $this->id() ) . '-tmpl">'; |
211
|
1 |
|
$output .= '<div class="uix-item">'; |
212
|
1 |
|
if ( false !== strpos( $this->child['config']->struct['view'], ABSPATH ) ) { |
213
|
|
|
ob_start(); |
214
|
|
|
include $this->child['config']->struct['view']; |
215
|
|
|
$output .= ob_get_clean(); |
216
|
|
|
} else { |
217
|
1 |
|
$output .= $this->child['config']->struct['view']; |
218
|
|
|
} |
219
|
1 |
|
$output .= '</div>'; |
220
|
1 |
|
$output .= '</script>'; |
221
|
|
|
|
222
|
1 |
|
return $output; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Returns the main input field for rendering |
227
|
|
|
* |
228
|
|
|
* @since 1.0.0 |
229
|
|
|
* @see \uix\ui\uix |
230
|
|
|
* @access public |
231
|
|
|
* @return string Input field HTML striung |
232
|
|
|
*/ |
233
|
1 |
|
public function input() { |
234
|
1 |
|
return '<input type="hidden" value="' . esc_attr( $this->get_value() ) . '" ' . $this->build_attributes() . '>'; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Returns the label for the control |
239
|
|
|
* |
240
|
|
|
* @since 1.0.0 |
241
|
|
|
* @access public |
242
|
|
|
* @return string label of control |
243
|
|
|
*/ |
244
|
|
|
public function label() { |
245
|
|
|
$output = null; |
246
|
|
|
if ( isset( $this->struct['label'] ) ) { |
247
|
|
|
$output .= '<label for="' . esc_attr( $this->id() ) . '-control" class="uix-add-row"><span class="uix-control-label">' . esc_html( $this->struct['label'] ) . '</span></label>'; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $output; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Render the script footer template |
255
|
|
|
* |
256
|
|
|
* @since 1.0.0 |
257
|
|
|
* @see \uix\ui\uix |
258
|
|
|
* @access public |
259
|
|
|
*/ |
260
|
|
|
public function render_footer_script() { |
261
|
|
|
$output = null; |
262
|
|
|
if ( ! empty( $this->templates ) ) { |
263
|
|
|
$output .= $this->templates; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
echo $output; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Sets styling colors |
271
|
|
|
* |
272
|
|
|
* @since 1.0.0 |
273
|
|
|
* @access protected |
274
|
|
|
*/ |
275
|
|
|
protected function set_active_styles() { |
276
|
|
|
|
277
|
|
|
$style = '.' . $this->id() . ' .dashicons.dashicons-plus-alt{ color: ' . $this->base_color() . ' !important;}'; |
278
|
|
|
$style .= '.' . $this->id() . ' .column-handle{background-color: ' . $this->base_color() . ' !important;}'; |
279
|
|
|
$style .= '.' . $this->id() . ' .uix-component-toolbar{background-color: ' . $this->base_color() . ' !important;}'; |
280
|
|
|
$style .= '.' . $this->id() . '.processing:after {background: url(' . $this->url . 'assets/svg/loading.php?base_color=' . urlencode( str_replace( '#', '', $this->base_color() ) ) . ') no-repeat center center;}'; |
281
|
|
|
|
282
|
|
|
uix_share()->set_active_styles( $style ); |
283
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Gets the base struct for an item. |
288
|
|
|
* |
289
|
|
|
* @since 1.0.0 |
290
|
|
|
* @access protected |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
1 |
|
private function get_base() { |
294
|
|
|
return array( |
295
|
1 |
|
'config' => array( |
296
|
1 |
|
'label' => __( 'Add Item', 'uix' ), |
297
|
1 |
|
'description' => __( 'Setup Item', 'uix' ), |
298
|
1 |
|
'callback' => array( $this, 'send_item_json' ), |
299
|
|
|
'attributes' => array( |
300
|
|
|
'class' => 'page-title-action', |
301
|
|
|
'data-content' => 'uix_item_control_modal', |
302
|
|
|
), |
303
|
1 |
|
'height' => 540, |
304
|
1 |
|
'width' => 380, |
305
|
|
|
'config' => array( |
306
|
1 |
|
'target' => 'uix_item_control_modal_handler', |
307
|
1 |
|
'control' => $this->id(), |
308
|
|
|
), |
309
|
1 |
|
'template' => '{{json this}} <button type="button" class="uix-item-edit button button-small">' . esc_html__( 'Edit', 'uix' ) . '</button> | <button type="button" class="uix-item-remove button button-small">' . esc_html__( 'Remove', 'uix' ) . '</button>', |
310
|
|
|
'footer' => array( |
311
|
1 |
|
'id' => $this->slug . '_foot', |
312
|
|
|
'control' => array( |
313
|
|
|
'add_item' => array( |
314
|
1 |
|
'label' => __( 'Add', 'uix' ), |
315
|
1 |
|
'type' => 'button', |
316
|
|
|
'attributes' => array( |
317
|
|
|
'type' => 'submit', |
318
|
|
|
'data-state' => 'add', |
319
|
|
|
), |
320
|
|
|
), |
321
|
|
|
'update_item' => array( |
322
|
1 |
|
'label' => __( 'Update', 'uix' ), |
323
|
1 |
|
'type' => 'button', |
324
|
|
|
'attributes' => array( |
325
|
|
|
'type' => 'submit', |
326
|
|
|
'data-state' => 'update', |
327
|
|
|
), |
328
|
|
|
), |
329
|
|
|
), |
330
|
|
|
), |
331
|
|
|
), |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|