Completed
Push — develop ( 2ff0a5...60f87c )
by David
01:30
created

metabox::get_data()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 2
nop 0
dl 0
loc 16
ccs 2
cts 2
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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
	 * Get the current post ID.
127
	 *
128
	 * @since 3.0.0
129
	 * @see   \uix\load
130
	 * @return int|bool the post ID or false if not exit.
131
	 */
132
	private function get_post_id() {
133
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
134 1
		$post_id = get_queried_object_id();
135
		if ( empty( $post_id ) && is_object( $post ) ) {
136 1
			if ( isset( $post->ID ) ) {
137 1
				$post_id = $post->ID;
138 1
			}
139
		}
140
141
		return $post_id;
142
	}
143 1
144 1
	/**
145
	 * Get Data from all controls of this section
146 1
	 *
147
	 * @since 1.0.0
148
	 * @see   \uix\load
149
	 * @return array|null Array of sections data structured by the controls or
150
	 *                    null if none.
151
	 */
152
	public function get_data() {
153
154
		$post_id = $this->get_post_id();
155 1
156
		//$this->data = get_post_meta( $post_id, $this->slug, true );
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
157 1
		$this->data = [
158
			$this->slug => [],
159
		];
160
		if ( ! empty( $this->child ) ) {
161
			foreach ( $this->child as $child ) {
162
				$this->data[ $this->slug ][ $child->slug ] = get_post_meta( $post_id, $child->slug, true );
163
			}
164
		}
165
166
		return $this->data;
167
	}
168
169
	/**
170 1
	 * Callback for the `add_meta_box` that sets the metabox data and renders it
171
	 *
172 1
	 * @since  1.0.0
173 1
	 * @uses   "add_meta_box" function
174
	 * @access public
175 1
	 *
176
	 * @param wp_post $post Current post for the metabox
177
	 */
178
	public function create_metabox( $post ) {
179
180 1
		$this->post = $post;
181 1
		$data       = $this->get_data();
182
		$this->set_data( $data );
183 1
		echo $this->render();
184
185 1
	}
186
187
	/**
188 1
	 * Render the Metabox
189
	 *
190
	 * @since  1.0.0
191
	 * @access public
192
	 * @return string HTML of rendered metabox
193
	 */
194
	public function render() {
195
		// render fields setup
196
		return parent::render();
197 1
	}
198 1
199
	/**
200
	 * Saves a metabox data
201
	 *
202
	 * @uses   "save_post" hook
203
	 * @since  1.0.0
204
	 * @access public
205
	 *
206
	 * @param int     $post_id ID of the current post being saved
207
	 * @param wp_post $post    Current post being saved
208
	 */
209
	public function save_meta( $post_id, $post ) {
210 1
211
		$this->post = $post;
212 1
		$data       = [
213
			$this->slug => $this->get_child_data(),
214 1
		];
215
216 1
		if ( ! $this->is_active() || empty( $data ) ) {
217 1
			return;
218
		}
219
		// save compiled data.
220 1
		update_post_meta( $post_id, $this->slug, $data );
221
		$data = call_user_func_array( 'array_merge', $data );
222
223
		foreach ( $data as $meta_key => $meta_value ) {
224
225
			$this->save_meta_data( $meta_key, $meta_value );
226
		}
227
228 1
	}
229
230
	/**
231 1
	 * Determin which metaboxes are used for the current screen and set them
232
	 * active
233 1
	 *
234
	 * @since  1.0.0
235 1
	 * @access public
236
	 */
237 1
	public function is_active() {
238
		return $this->is_active;
239 1
	}
240
241
	/**
242
	 * Save the meta data for the post
243
	 *
244
	 * @since  1.0.0
245
	 * @access private
246
	 *
247 1
	 * @param string $slug slug of the meta_key
248
	 * @param mixed  $data Data to be saved
249 1
	 */
250 1
	private function save_meta_data( $slug, $data ) {
251 1
252
		$prev = get_post_meta( $this->post->ID, $slug, true );
253 1
254 1
		if ( null === $data && $prev ) {
255
			delete_post_meta( $this->post->ID, $slug );
256 1
		} elseif ( $data !== $prev ) {
257
			update_post_meta( $this->post->ID, $slug, $data );
258 1
		}
259
260 1
	}
261
262
	/**
263
	 * setup actions and hooks to add metaboxes and save metadata
264
	 *
265
	 * @since  1.0.0
266
	 * @access protected
267
	 */
268 1
	protected function actions() {
269 1
270 1
		// run parent to keep init and enqueuing assets
271 1
		parent::actions();
272 1
		// set screen activation
273 1
		add_action( 'current_screen', [ $this, 'set_active_status' ], 25 );
274 1
		// add metaboxes
275 1
		add_action( 'add_meta_boxes', [ $this, 'add_metaboxes' ], 25 );
276
		// save metabox
277
		add_action( 'save_post', [ $this, 'save_meta' ], 10, 2 );
278 1
279
	}
280
281
	/**
282
	 * Enqueues specific tabs assets for the active pages
283
	 *
284
	 * @since  1.0.0
285
	 * @access protected
286
	 */
287
	protected function set_active_styles() {
288
289
		$style = '#' . $this->id() . '.uix-top-tabs > .uix-panel-tabs > li[aria-selected="true"] a,';
290
		$style .= '#side-sortables #' . $this->id() . ' > .uix-panel-tabs > li[aria-selected="true"] a {';
291
		$style .= 'box-shadow: 0 3px 0 ' . $this->base_color() . ' inset; }';
292
293
		$style .= '#' . $this->id() . ' > .uix-panel-tabs > li[aria-selected="true"] a {';
294
		$style .= 'box-shadow: 3px 0 0 ' . $this->base_color() . ' inset;}';
295
296
		$style .= $this->chromeless();
297
298
		uix_share()->set_active_styles( $style );
299
300
	}
301
302
	/**
303
	 * Writes script required to make a metabox `chromeless`
304
	 *
305
	 * @since  1.0.0
306
	 * @access protected
307
	 */
308
	protected function chromeless() {
309
		$style = null;
310
		if ( ! empty( $this->struct['chromeless'] ) ) {
311
			$style .= '#metabox-' . $this->id() . '{background: transparent none repeat scroll 0 0;border: 0 none;';
312
			$style .= 'box-shadow: none;margin: 0 0 20px;padding: 0;}';
313
			$style .= '#metabox-' . $this->id() . ' .handlediv.button-link,';
314
			$style .= '#metabox-' . $this->id() . ' .hndle {display: none;}';
315
			$style .= '#metabox-' . $this->id() . ' > .inside {padding: 0;}';
316
		}
317
318
		return $style;
319
	}
320
321
}
322