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