box::render_footer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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
73 1
		return update_option( $this->store_key(), $this->get_data() );
74
	}
75
76
	/**
77
	 * get the objects data store key
78
	 *
79
	 * @since  1.0.0
80
	 * @access public
81
	 * @return string $store_key the defined option name for this UIX object
82
	 */
83 1
	public function store_key() {
84 1
		if ( ! empty( $this->struct['store_key'] ) ) {
85
			return $this->struct['store_key'];
86
		}
87
88 1
		return sanitize_key( $this->slug );
89
	}
90
91
	/**
92
	 * Get Data from all controls of this section
93
	 *
94
	 * @since 1.0.0
95
	 * @see   \uix\load
96
	 * @return array Array of sections data structured by the controls
97
	 */
98 2
	public function get_data() {
99
100 2
		if ( empty( $this->data ) ) {
101 2
			$data = parent::get_data();
102 2
			if ( ! empty( $data[ $this->slug ] ) ) {
103 2
				$this->data = $data[ $this->slug ];
104
			}
105
		}
106
107 2
		return $this->data;
108
	}
109
110
	/**
111
	 * Get data
112
	 *
113
	 * @since  1.0.0
114
	 * @access public
115
	 * @return mixed $data Requested data of the object
116
	 */
117 1
	public function load_data() {
118 1
		return get_option( $this->store_key(), $this->get_data() );
119
	}
120
121
	/**
122
	 * set metabox styles
123
	 *
124
	 * @since  1.0.0
125
	 * @see    \uix\ui\uix
126
	 * @access public
127
	 */
128 11 View Code Duplication
	public function set_assets() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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