item::get_base()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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