Completed
Push — develop ( face5f...9d2a1d )
by David
02:00
created

repeat::render_instance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 13
ccs 0
cts 4
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * UIX repeat
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
 * A repetable container for repeatable areas.
16
 *
17
 * @since 1.0.0
18
 * @see   \uix\uix
19
 */
20
class repeat extends panel {
21
22
	/**
23
	 * The type of object
24
	 *
25
	 * @since  1.0.0
26
	 * @access public
27
	 * @var      string
28
	 */
29
	public $type = 'repeat';
30
31
	/**
32
	 * The instance of this object
33
	 *
34
	 * @since  1.0.0
35
	 * @access public
36
	 * @var      int|string
37
	 */
38
	public $instance = 0;
39
40
	/**
41
	 * total instances of this object
42
	 *
43
	 * @since  1.0.0
44
	 * @access public
45
	 * @var      int|string
46
	 */
47
	public $instances = 0;
48
49
	/**
50
	 * The templates to render in the footer
51
	 *
52
	 * @since  1.0.0
53
	 * @access public
54
	 * @var      string
55
	 */
56
	public $templates = null;
57
58
	/**
59
	 * Button Label
60
	 *
61
	 * @since  1.0.0
62
	 * @access public
63
	 * @var      string
64
	 */
65
	public $button_label;
66
67
	/**
68
	 * Define core repeat styles ans scripts
69
	 *
70
	 * @since  1.0.0
71
	 * @access public
72
	 */
73
	public function set_assets() {
74
75
		$this->assets['script'][ $this->type ] = $this->url . 'assets/js/' . $this->type . UIX_ASSET_DEBUG . '.js';
76
		$this->assets['style'][ $this->type ]  = $this->url . 'assets/css/' . $this->type . UIX_ASSET_DEBUG . '.css';
77
78
		parent::set_assets();
79
80
	}
81
82
	/**
83
	 * Sets the data for all children
84
	 *
85
	 * @since  1.0.0
86
	 * @access public
87
	 */
88
	public function set_data( $data ) {
89
90
		$this->instance = 0;
91
		foreach ( (array) $data[ $this->slug ] as $instance => $instance_data ) {
92
			foreach ( $this->child as $child ) {
93
				if ( method_exists( $child, 'set_data' ) ) {
94
					$child->set_data( $instance_data );
95
				}
96
			}
97
			$this->instance ++;
98
		}
99
		$this->instances = $this->instance;
100
		$this->instance  = 0;
101
102
	}
103
104
	/**
105
	 * Render the complete section
106
	 *
107
	 * @since  1.0.0
108
	 * @access public
109
	 * @return string|null HTML of rendered repeatable
110
	 */
111
	public function render() {
112
113
		add_action( 'admin_footer', [
114
			$this,
115
			'render_repeatable_script',
116
		] );
117
		add_action( 'wp_footer', [ $this, 'render_repeatable_script' ] );
118
119
		$output = '<div data-uix-template="' . esc_attr( $this->id() ) . '" ' . $this->build_attributes() . '>';
120
		$output .= $this->render_instances();
121
		$output .= '</div>';
122
123
		$output .= $this->render_repeatable_more();
124
125
		return $output;
126
	}
127
128
	/**
129
	 * Repeatable instance object id.
130
	 *
131
	 * @since  1.0.0
132
	 * @access public
133
	 * @return string The object ID
134
	 */
135
	public function id() {
136
137
		return parent::id() . '-' . $this->instance;
138
	}
139
140
	/**
141
	 * Render each instance from data
142
	 *
143
	 * @since  1.0.0
144
	 * @access private
145
	 * @return string|null HTML of rendered instances
146
	 */
147
	private function render_instances() {
148
		$data           = $this->get_data();
149
		$output         = null;
150
		$this->instance = 0;
151
		if ( ! empty( $data[ $this->slug ] ) ) {
152
153
			$data = array_filter( $data[ $this->slug ] );
154
155
			foreach ( (array) $data as $instance_id => $instance ) {
156
				$output .= $this->render_instance( $instance_id, $instance );
157
			}
158
		}
159
160
		return $output;
161
	}
162
163
	/**
164
	 * Render a single instance.
165
	 *
166
	 * @since  1.0.0
167
	 *
168
	 * @param int   $instance_id The current instance to render.
169
	 * @param array $instance The data for the instance to check.
170
	 *
171
	 * @access public
172
	 *
173
	 * @return string The rendered html of the instance.
174
	 */
175
	public function render_instance( $instance_id, $instance ) {
176
		$this->instance = $instance_id;
177
		$has_data       = array_filter( $instance );
178
		if ( ! empty( $has_data ) ) {
179
			if ( ! isset( $this->struct['active'] ) ) {
180
				$this->struct['active'] = 'true';
181
			}
182
183
			return $this->render_repeatable();
184
		}
185
186
		return '';
187
	}
188
189
	/**
190
	 * Sets the data for all children
191
	 *
192
	 * @since  1.0.0
193
	 * @access public
194
	 */
195
	public function get_data() {
196
197
		$data           = [
198
			$this->slug => [],
199
		];
200
		$this->instance = 0;
201
		$has_data       = true;
202
		while ( true === $has_data ) {
203
			$this_data = $this->get_child_data();
204
			$this_data = array_filter( $this_data );
205
			if ( ! empty( $this_data ) ) {
206
				$data[ $this->slug ][] = $this_data;
207
			} else {
208
				$has_data = false;
209
			}
210
			$this->instance ++;
211
		}
212
213
		return $data;
214
	}
215
216
	/**
217
	 * Render the internal section
218
	 *
219
	 * @since  1.0.0
220
	 * @access public
221
	 *
222
	 * @param bool $reset Flag to indicate reset repeatbles.
223
	 *
224
	 * @return string|null HTML of rendered object
225
	 */
226
	public function render_repeatable( $reset = false ) {
227
228
		$output = '<div class="uix-repeat" >';
229
		$output .= $this->render_template();
230
		if ( ! empty( $this->child ) ) {
231
			$output .= $this->render_children( $reset );
232
		}
233
234
		$output .= '<button type="button" class="button button-small uix-remover"><span class="dashicons dashicons-no"></span></button> </div>';
235
236
		return $output;
237
238
	}
239
240
	/**
241
	 * Render the child objects
242
	 *
243
	 * @since  1.0.0
244
	 * @access public
245
	 *
246
	 * @param bool $reset Flag to reset the instances of child repeatables.
247
	 *
248
	 * @return string|null
249
	 */
250
	public function render_children( $reset = false ) {
251
		$output = null;
252
		foreach ( $this->child as $child ) {
253
			if ( true === $reset && 'repeat' === $child->type ) {
254
				$child->instances = 0;
255
				$child->instance  = 0;
256
			}
257
			if ( 'repeat' === $child->type && $reset === false ) {
258
				$id = 1;
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
259
			}
260
			$output .= $child->render();
261
		}
262
263
		return $output;
264
	}
265
266
	/**
267
	 * Render the add more button and template
268
	 *
269
	 * @since  1.0.0
270
	 * @access public
271
	 * @return string|null HTML of rendered object
272
	 */
273
	public function render_repeatable_more() {
274
275
		$label = __( 'Add Another', 'uix' );
276
277
		if ( ! empty( $this->struct['label'] ) ) {
278
			$label = $this->struct['label'];
279
		}
280
281
		$this->instance  = '{{_inst_}}';
282
		$this->templates = $this->render_repeatable( true );
283
		$this->instance  = 0;
284
		$output          = '<div class="repeatable-footer"><button type="button" class="button" data-uix-repeat="' . esc_attr( $this->id() ) . '">' . esc_html( $label ) . '</button></div>';
285
286
		return $output;
287
288
	}
289
290
	/**
291
	 * Render the script footer template
292
	 *
293
	 * @since  1.0.0
294
	 * @see    \uix\ui\uix
295
	 * @access public
296
	 */
297
	public function render_repeatable_script() {
298
		$output = null;
299
		if ( ! empty( $this->templates ) ) {
300
			$output .= '<script type="text/html" id="' . esc_attr( $this->id() ) . '-tmpl">';
301
			$output .= $this->templates;
302
			$output .= '</script>';
303
		}
304
305
		echo $output;
306
	}
307
308
	/**
309
	 * Enqueues specific tabs assets for the active pages
310
	 *
311
	 * @since  1.0.0
312
	 * @access protected
313
	 */
314
	protected function set_active_styles() {
315
316
		parent::set_active_styles();
317
		$style = '#' . $this->id() . ' .uix-repeat{ box-shadow: 1px 0 0 ' . $this->base_color() . ' inset, -37px 0 0 #f5f5f5 inset, -38px 0 0 #ddd inset, 0 2px 3px rgba(0, 0, 0, 0.05); };';
318
		uix_share()->set_active_styles( $style );
319
	}
320
321
}
322