Completed
Push — develop ( 35270b...3f50dc )
by David
01:42
created

metabox::get_data()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 12
c 1
b 1
f 0
nc 4
nop 0
dl 0
loc 20
ccs 3
cts 3
cp 1
crap 5
rs 8.8571
1
<?php
2
/**
3
 * UIX Metaboxes
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
 * Metabox class for adding metaboxes to post types in the post editor
16
 *
17
 * @package uix\ui
18
 * @author  David Cramer
19
 */
20
class metabox 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 = 'metabox';
30
31
	/**
32
	 * Holds the current post object
33
	 *
34
	 * @since  1.0.0
35
	 * @access public
36
	 * @var      WP_Post
37
	 */
38
	public $post = null;
39
40
	/**
41
	 * Status of the metabox to determin if assets should be loaded
42
	 *
43
	 * @since  1.0.0
44
	 * @access public
45
	 * @var      bool
46
	 */
47
	public $is_active = false;
48
49
	/**
50
	 * Setup submission data
51
	 *
52
	 * @since  1.0.0
53
	 * @access public
54
	 */
55 1
	public function setup() {
56
		// do parent
57 1
		parent::setup();
58 1
		if ( ! isset( $this->struct['screen'] ) ) {
59 1
			$this->struct['screen'] = ( $this->parent ? $this->parent->slug : null );
60
		}
61 1
	}
62
63
	/**
64
	 * set metabox styles
65
	 *
66
	 * @since  1.0.0
67
	 * @see    \uix\ui\uix
68
	 * @access public
69
	 */
70 1
	public function set_assets() {
71
72 1
		$this->assets['style']['metabox']   = $this->url . 'assets/css/metabox' . UIX_ASSET_DEBUG . '.css';
73 1
		$this->assets['script']['baldrick'] = [
74 1
			'src'  => $this->url . 'assets/js/jquery.baldrick' . UIX_ASSET_DEBUG . '.js',
75
			'deps' => [ 'jquery' ],
76
		];
77 1
		parent::set_assets();
78 1
	}
79
80
	/**
81
	 * Checks the screen object to determin if the metabox should load assets
82
	 *
83
	 * @since  1.0.0
84
	 * @access public
85
	 * @uses   "current_screen" hook
86
	 *
87
	 * @param screen $screen The current screen object;
88
	 */
89 1
	public function set_active_status( $screen ) {
90
91 1
		if ( 'post' == $screen->base && ( null === $this->struct['screen'] || in_array( $screen->id, (array) $this->struct['screen'] ) ) ) {
92 1
			$this->is_active = true;
93
		}
94
95 1
	}
96
97
	/**
98
	 * Add metaboxes to screen
99
	 *
100
	 * @since  1.0.0
101
	 * @access public
102
	 * @uses   "add_meta_boxes" hook
103
	 */
104 1
	public function add_metaboxes() {
105
106
		// metabox defaults
107
		$defaults = [
108 1
			'context'  => 'advanced',
109
			'priority' => 'default',
110
		];
111
112 1
		$metabox = array_merge( $defaults, $this->struct );
113
114 1
		add_meta_box(
115 1
			'metabox-' . $this->id(),
116 1
			$metabox['name'],
117 1
			[ $this, 'create_metabox' ],
118 1
			$metabox['screen'],
119 1
			$metabox['context'],
120 1
			$metabox['priority']
121
		);
122
123 1
	}
124
125
	/**
126
	 * Callback for the `add_meta_box` that sets the metabox data and renders it
127
	 *
128
	 * @since  1.0.0
129
	 * @uses   "add_meta_box" function
130
	 * @access public
131
	 *
132
	 * @param wp_post $post Current post for the metabox
133
	 */
134 1
	public function create_metabox( $post ) {
135
136 1
		$this->post = $post;
137 1
		$data       = get_post_meta( $post->ID, $this->slug, true );
138 1
		if ( empty( $data ) ) {
139
			$data = [
140
				$this->slug => [],
141
			];
142
		}
143 1
		$this->set_data( $data );
144 1
		echo $this->render();
145
146 1
	}
147
148
	/**
149
	 * Render the Metabox
150
	 *
151
	 * @since  1.0.0
152
	 * @access public
153
	 * @return string HTML of rendered metabox
154
	 */
155 1
	public function render() {
156
		// render fields setup
157 1
		return parent::render();
158
	}
159
160
	/**
161
	 * Saves a metabox data
162
	 *
163
	 * @uses   "save_post" hook
164
	 * @since  1.0.0
165
	 * @access public
166
	 *
167
	 * @param int     $post_id ID of the current post being saved
168
	 * @param wp_post $post    Current post being saved
169
	 */
170 1
	public function save_meta( $post_id, $post ) {
171
172 1
		$this->post = $post;
173 1
		$data       = $this->get_data();
174
175 1
		if ( ! $this->is_active() || empty( $data ) ) {
176
			return;
177
		}
178
179
		// save compiled data
180 1
		update_post_meta( $post_id, $this->slug, $data );
181 1
		$data = call_user_func_array( 'array_merge', $data );
182
183 1
		foreach ( $data as $meta_key => $meta_value ) {
184
185 1
			$this->save_meta_data( $meta_key, $meta_value );
186
		}
187
188 1
	}
189
190
	/**
191
	 * Determin which metaboxes are used for the current screen and set them
192
	 * active
193
	 *
194
	 * @since  1.0.0
195
	 * @access public
196
	 */
197 1
	public function is_active() {
198 1
		return $this->is_active;
199
	}
200
201
	/**
202
	 * Save the meta data for the post
203
	 *
204
	 * @since  1.0.0
205
	 * @access private
206
	 *
207
	 * @param string $slug slug of the meta_key
208
	 * @param mixed  $data Data to be saved
209
	 */
210 1
	private function save_meta_data( $slug, $data ) {
211
212 1
		$prev = get_post_meta( $this->post->ID, $slug, true );
213
214 1
		if ( null === $data && $prev ) {
215
			delete_post_meta( $this->post->ID, $slug );
216 1
		} elseif ( $data !== $prev ) {
217 1
			update_post_meta( $this->post->ID, $slug, $data );
218
		}
219
220 1
	}
221
222
	/**
223
	 * setup actions and hooks to add metaboxes and save metadata
224
	 *
225
	 * @since  1.0.0
226
	 * @access protected
227
	 */
228 1
	protected function actions() {
229
230
		// run parent to keep init and enqueuing assets
231 1
		parent::actions();
232
		// set screen activation
233 1
		add_action( 'current_screen', [ $this, 'set_active_status' ], 25 );
234
		// add metaboxes
235 1
		add_action( 'add_meta_boxes', [ $this, 'add_metaboxes' ], 25 );
236
		// save metabox
237 1
		add_action( 'save_post', [ $this, 'save_meta' ], 10, 2 );
238
239 1
	}
240
241
	/**
242
	 * Enqueues specific tabs assets for the active pages
243
	 *
244
	 * @since  1.0.0
245
	 * @access protected
246
	 */
247 1
	protected function set_active_styles() {
248
249 1
		$style = '#' . $this->id() . '.uix-top-tabs > .uix-panel-tabs > li[aria-selected="true"] a,';
250 1
		$style .= '#side-sortables #' . $this->id() . ' > .uix-panel-tabs > li[aria-selected="true"] a {';
251 1
		$style .= 'box-shadow: 0 3px 0 ' . $this->base_color() . ' inset; }';
252
253 1
		$style .= '#' . $this->id() . ' > .uix-panel-tabs > li[aria-selected="true"] a {';
254 1
		$style .= 'box-shadow: 3px 0 0 ' . $this->base_color() . ' inset;}';
255
256 1
		$style .= $this->chromeless();
257
258 1
		uix_share()->set_active_styles( $style );
259
260 1
	}
261
262
	/**
263
	 * Writes script required to make a metabox `chromeless`
264
	 *
265
	 * @since  1.0.0
266
	 * @access protected
267
	 */
268 1
	protected function chromeless() {
269 1
		$style = null;
270 1
		if ( ! empty( $this->struct['chromeless'] ) ) {
271 1
			$style .= '#metabox-' . $this->id() . '{background: transparent none repeat scroll 0 0;border: 0 none;';
272 1
			$style .= 'box-shadow: none;margin: 0 0 20px;padding: 0;}';
273 1
			$style .= '#metabox-' . $this->id() . ' .handlediv.button-link,';
274 1
			$style .= '#metabox-' . $this->id() . ' .hndle {display: none;}';
275 1
			$style .= '#metabox-' . $this->id() . ' > .inside {padding: 0;}';
276
		}
277
278 1
		return $style;
279
	}
280
281
}
282