Completed
Push — develop ( 3f50dc...934856 )
by David
01:57
created

repeat::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
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
				$this->instance = $instance_id;
157
				$has_data       = array_filter( $instance );
158
				if ( empty( $has_data ) ) {
159
					continue;
160
				}
161
				if ( ! isset( $this->struct['active'] ) ) {
162
					$this->struct['active'] = 'true';
163
				}
164
165
				$output .= $this->render_repeatable();
166
167
			}
168
		}
169
170
		return $output;
171
	}
172
173
	/**
174
	 * Sets the data for all children
175
	 *
176
	 * @since  1.0.0
177
	 * @access public
178
	 */
179
	public function get_data() {
180
181
		$data           = [
182
			$this->slug => [],
183
		];
184
		$this->instance = 0;
185
		$has_data       = true;
186
		while ( true === $has_data ) {
187
			$this_data = $this->get_child_data();
188
			$this_data = array_filter( $this_data );
189
			if ( ! empty( $this_data ) ) {
190
				$data[ $this->slug ][] = $this_data;
191
			} else {
192
				$has_data = false;
193
			}
194
			$this->instance ++;
195
		}
196
197
		return $data;
198
	}
199
200
	/**
201
	 * Render the internal section
202
	 *
203
	 * @since  1.0.0
204
	 * @access public
205
	 *
206
	 * @param bool $reset Flag to indicate reset repeatbles.
207
	 *
208
	 * @return string|null HTML of rendered object
209
	 */
210
	public function render_repeatable( $reset = false ) {
211
212
		$output = '<div class="uix-repeat" >';
213
		$output .= $this->render_template();
214
		if ( ! empty( $this->child ) ) {
215
			$output .= $this->render_children( $reset );
216
		}
217
218
		$output .= '<button type="button" class="button button-small uix-remover"><span class="dashicons dashicons-no"></span></button> </div>';
219
220
		return $output;
221
222
	}
223
224
	/**
225
	 * Render the child objects
226
	 *
227
	 * @since  1.0.0
228
	 * @access public
229
	 *
230
	 * @param bool $reset Flag to reset the instances of child repeatables.
231
	 *
232
	 * @return string|null
233
	 */
234
	public function render_children( $reset = false ) {
235
		$output = null;
236
		foreach ( $this->child as $child ) {
237
			if ( true === $reset && 'repeat' === $child->type ) {
238
				$child->instances = 0;
239
				$child->instance  = 0;
240
			}
241
			if ( 'repeat' === $child->type && $reset === false ) {
242
				$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...
243
			}
244
			$output .= $child->render();
245
		}
246
247
		return $output;
248
	}
249
250
	/**
251
	 * Render the add more button and template
252
	 *
253
	 * @since  1.0.0
254
	 * @access public
255
	 * @return string|null HTML of rendered object
256
	 */
257
	public function render_repeatable_more() {
258
259
		$label = __( 'Add Another', 'uix' );
260
261
		if ( ! empty( $this->struct['label'] ) ) {
262
			$label = $this->struct['label'];
263
		}
264
265
		$this->instance  = '{{_inst_}}';
266
		$this->templates = $this->render_repeatable( true );
267
		$this->instance  = 0;
268
		$output          = '<div class="repeatable-footer"><button type="button" class="button" data-uix-repeat="' . esc_attr( $this->id() ) . '">' . esc_html( $label ) . '</button></div>';
269
270
		return $output;
271
272
	}
273
274
	/**
275
	 * Render the script footer template
276
	 *
277
	 * @since  1.0.0
278
	 * @see    \uix\ui\uix
279
	 * @access public
280
	 */
281
	public function render_repeatable_script() {
282
		$output = null;
283
		if ( ! empty( $this->templates ) ) {
284
			$output .= '<script type="text/html" id="' . esc_attr( $this->id() ) . '-tmpl">';
285
			$output .= $this->templates;
286
			$output .= '</script>';
287
		}
288
289
		echo $output;
290
	}
291
292
	/**
293
	 * Enqueues specific tabs assets for the active pages
294
	 *
295
	 * @since  1.0.0
296
	 * @access protected
297
	 */
298
	protected function set_active_styles() {
299
300
		parent::set_active_styles();
301
		$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); };';
302
		uix_share()->set_active_styles( $style );
303
	}
304
305
}
306