0
|
1 |
<?php if ( ! defined( 'OT_VERSION' ) ) exit( 'No direct script access allowed' ); |
|
2 |
/** |
|
3 |
* OptionTree Meta Box API |
|
4 |
* |
|
5 |
* This class loads all the methods and helpers specific to build a meta box. |
|
6 |
* |
|
7 |
* @package OptionTree |
|
8 |
* @author Derek Herman <derek@valendesigns.com> |
|
9 |
* @copyright Copyright (c) 2013, Derek Herman |
|
10 |
*/ |
|
11 |
if ( ! class_exists( 'OT_Meta_Box' ) ) { |
|
12 |
|
|
13 |
class OT_Meta_Box { |
|
14 |
|
|
15 |
/* variable to store the meta box array */ |
|
16 |
private $meta_box; |
|
17 |
|
|
18 |
/** |
|
19 |
* PHP5 constructor method. |
|
20 |
* |
|
21 |
* This method adds other methods of the class to specific hooks within WordPress. |
|
22 |
* |
|
23 |
* @uses add_action() |
|
24 |
* |
|
25 |
* @return void |
|
26 |
* |
|
27 |
* @access public |
|
28 |
* @since 1.0 |
|
29 |
*/ |
|
30 |
function __construct( $meta_box ) { |
|
31 |
if ( ! is_admin() ) |
|
32 |
return; |
5
|
33 |
|
|
34 |
global $ot_meta_boxes; |
|
35 |
|
|
36 |
if ( ! isset( $ot_meta_boxes ) ) { |
|
37 |
$ot_meta_boxes = array(); |
|
38 |
} |
|
39 |
|
|
40 |
$ot_meta_boxes[] = $meta_box; |
|
41 |
|
0
|
42 |
$this->meta_box = $meta_box; |
5
|
43 |
|
0
|
44 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
5
|
45 |
|
0
|
46 |
add_action( 'save_post', array( $this, 'save_meta_box' ), 1, 2 ); |
5
|
47 |
|
0
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* Adds meta box to any post type |
|
52 |
* |
|
53 |
* @uses add_meta_box() |
|
54 |
* |
|
55 |
* @return void |
|
56 |
* |
|
57 |
* @access public |
|
58 |
* @since 1.0 |
|
59 |
*/ |
|
60 |
function add_meta_boxes() { |
|
61 |
foreach ( (array) $this->meta_box['pages'] as $page ) { |
|
62 |
add_meta_box( $this->meta_box['id'], $this->meta_box['title'], array( $this, 'build_meta_box' ), $page, $this->meta_box['context'], $this->meta_box['priority'], $this->meta_box['fields'] ); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* Meta box view |
|
68 |
* |
|
69 |
* @return string |
|
70 |
* |
|
71 |
* @access public |
|
72 |
* @since 1.0 |
|
73 |
*/ |
|
74 |
function build_meta_box( $post, $metabox ) { |
5
|
75 |
|
0
|
76 |
echo '<div class="ot-metabox-wrapper">'; |
5
|
77 |
|
0
|
78 |
/* Use nonce for verification */ |
|
79 |
echo '<input type="hidden" name="' . $this->meta_box['id'] . '_nonce" value="' . wp_create_nonce( $this->meta_box['id'] ) . '" />'; |
|
80 |
|
|
81 |
/* meta box description */ |
|
82 |
echo isset( $this->meta_box['desc'] ) && ! empty( $this->meta_box['desc'] ) ? '<div class="description" style="padding-top:10px;">' . htmlspecialchars_decode( $this->meta_box['desc'] ) . '</div>' : ''; |
|
83 |
|
|
84 |
/* loop through meta box fields */ |
|
85 |
foreach ( $this->meta_box['fields'] as $field ) { |
|
86 |
|
|
87 |
/* get current post meta data */ |
|
88 |
$field_value = get_post_meta( $post->ID, $field['id'], true ); |
|
89 |
|
|
90 |
/* set standard value */ |
|
91 |
if ( isset( $field['std'] ) ) { |
|
92 |
$field_value = ot_filter_std_value( $field_value, $field['std'] ); |
|
93 |
} |
|
94 |
|
|
95 |
/* build the arguments array */ |
|
96 |
$_args = array( |
|
97 |
'type' => $field['type'], |
|
98 |
'field_id' => $field['id'], |
|
99 |
'field_name' => $field['id'], |
|
100 |
'field_value' => $field_value, |
|
101 |
'field_desc' => isset( $field['desc'] ) ? $field['desc'] : '', |
|
102 |
'field_std' => isset( $field['std'] ) ? $field['std'] : '', |
|
103 |
'field_rows' => isset( $field['rows'] ) && ! empty( $field['rows'] ) ? $field['rows'] : 10, |
|
104 |
'field_post_type' => isset( $field['post_type'] ) && ! empty( $field['post_type'] ) ? $field['post_type'] : 'post', |
|
105 |
'field_taxonomy' => isset( $field['taxonomy'] ) && ! empty( $field['taxonomy'] ) ? $field['taxonomy'] : 'category', |
|
106 |
'field_min_max_step'=> isset( $field['min_max_step'] ) && ! empty( $field['min_max_step'] ) ? $field['min_max_step'] : '0,100,1', |
|
107 |
'field_class' => isset( $field['class'] ) ? $field['class'] : '', |
5
|
108 |
'field_condition' => isset( $field['condition'] ) ? $field['condition'] : '', |
|
109 |
'field_operator' => isset( $field['operator'] ) ? $field['operator'] : 'and', |
0
|
110 |
'field_choices' => isset( $field['choices'] ) ? $field['choices'] : array(), |
|
111 |
'field_settings' => isset( $field['settings'] ) && ! empty( $field['settings'] ) ? $field['settings'] : array(), |
|
112 |
'post_id' => $post->ID, |
|
113 |
'meta' => true |
|
114 |
); |
|
115 |
|
5
|
116 |
$conditions = ''; |
|
117 |
|
|
118 |
/* setup the conditions */ |
|
119 |
if ( isset( $field['condition'] ) && ! empty( $field['condition'] ) ) { |
|
120 |
|
|
121 |
$conditions = ' data-condition="' . $field['condition'] . '"'; |
|
122 |
$conditions.= isset( $field['operator'] ) && in_array( $field['operator'], array( 'and', 'AND', 'or', 'OR' ) ) ? ' data-operator="' . $field['operator'] . '"' : ''; |
|
123 |
|
|
124 |
} |
|
125 |
|
0
|
126 |
/* only allow simple textarea due to DOM issues with wp_editor() */ |
5
|
127 |
if ( apply_filters( 'ot_override_forced_textarea_simple', false, $field['id'] ) == false && $_args['type'] == 'textarea' ) |
0
|
128 |
$_args['type'] = 'textarea-simple'; |
5
|
129 |
|
|
130 |
// Build the setting CSS class |
|
131 |
if ( ! empty( $_args['field_class'] ) ) { |
|
132 |
|
|
133 |
$classes = explode( ' ', $_args['field_class'] ); |
|
134 |
|
|
135 |
foreach( $classes as $key => $value ) { |
|
136 |
|
|
137 |
$classes[$key] = $value . '-wrap'; |
|
138 |
|
|
139 |
} |
|
140 |
|
|
141 |
$class = 'format-settings ' . implode( ' ', $classes ); |
|
142 |
|
|
143 |
} else { |
|
144 |
|
|
145 |
$class = 'format-settings'; |
|
146 |
|
|
147 |
} |
0
|
148 |
|
|
149 |
/* option label */ |
5
|
150 |
echo '<div id="setting_' . $field['id'] . '" class="' . $class . '"' . $conditions . '>'; |
|
151 |
|
|
152 |
echo '<div class="format-setting-wrap">'; |
0
|
153 |
|
5
|
154 |
/* don't show title with textblocks */ |
|
155 |
if ( $_args['type'] != 'textblock' && ! empty( $field['label'] ) ) { |
|
156 |
echo '<div class="format-setting-label">'; |
|
157 |
echo '<label for="' . $field['id'] . '" class="label">' . $field['label'] . '</label>'; |
|
158 |
echo '</div>'; |
|
159 |
} |
0
|
160 |
|
5
|
161 |
/* get the option HTML */ |
|
162 |
echo ot_display_by_type( $_args ); |
|
163 |
|
|
164 |
echo '</div>'; |
0
|
165 |
|
|
166 |
echo '</div>'; |
|
167 |
|
|
168 |
} |
5
|
169 |
|
|
170 |
echo '<div class="clear"></div>'; |
0
|
171 |
|
|
172 |
echo '</div>'; |
|
173 |
|
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* Saves the meta box values |
|
178 |
* |
|
179 |
* @return void |
|
180 |
* |
|
181 |
* @access public |
|
182 |
* @since 1.0 |
|
183 |
*/ |
|
184 |
function save_meta_box( $post_id, $post_object ) { |
|
185 |
global $pagenow; |
|
186 |
|
|
187 |
/* don't save if $_POST is empty */ |
5
|
188 |
if ( empty( $_POST ) || ( isset( $_POST['vc_inline'] ) && $_POST['vc_inline'] == true ) ) |
0
|
189 |
return $post_id; |
|
190 |
|
|
191 |
/* don't save during quick edit */ |
|
192 |
if ( $pagenow == 'admin-ajax.php' ) |
|
193 |
return $post_id; |
|
194 |
|
|
195 |
/* don't save during autosave */ |
|
196 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
|
197 |
return $post_id; |
|
198 |
|
|
199 |
/* don't save if viewing a revision */ |
|
200 |
if ( $post_object->post_type == 'revision' || $pagenow == 'revision.php' ) |
|
201 |
return $post_id; |
|
202 |
|
|
203 |
/* verify nonce */ |
|
204 |
if ( isset( $_POST[ $this->meta_box['id'] . '_nonce'] ) && ! wp_verify_nonce( $_POST[ $this->meta_box['id'] . '_nonce'], $this->meta_box['id'] ) ) |
|
205 |
return $post_id; |
|
206 |
|
|
207 |
/* check permissions */ |
|
208 |
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { |
|
209 |
if ( ! current_user_can( 'edit_page', $post_id ) ) |
|
210 |
return $post_id; |
|
211 |
} else { |
|
212 |
if ( ! current_user_can( 'edit_post', $post_id ) ) |
|
213 |
return $post_id; |
|
214 |
} |
|
215 |
|
|
216 |
foreach ( $this->meta_box['fields'] as $field ) { |
|
217 |
|
|
218 |
$old = get_post_meta( $post_id, $field['id'], true ); |
|
219 |
$new = ''; |
|
220 |
|
|
221 |
/* there is data to validate */ |
|
222 |
if ( isset( $_POST[$field['id']] ) ) { |
|
223 |
|
|
224 |
/* slider and list item */ |
|
225 |
if ( in_array( $field['type'], array( 'list-item', 'slider' ) ) ) { |
|
226 |
|
|
227 |
/* required title setting */ |
|
228 |
$required_setting = array( |
|
229 |
array( |
|
230 |
'id' => 'title', |
|
231 |
'label' => __( 'Title', 'option-tree' ), |
|
232 |
'desc' => '', |
|
233 |
'std' => '', |
|
234 |
'type' => 'text', |
|
235 |
'rows' => '', |
|
236 |
'class' => 'option-tree-setting-title', |
|
237 |
'post_type' => '', |
|
238 |
'choices' => array() |
|
239 |
) |
|
240 |
); |
|
241 |
|
|
242 |
/* get the settings array */ |
|
243 |
$settings = isset( $_POST[$field['id'] . '_settings_array'] ) ? unserialize( ot_decode( $_POST[$field['id'] . '_settings_array'] ) ) : array(); |
|
244 |
|
|
245 |
/* settings are empty for some odd ass reason get the defaults */ |
|
246 |
if ( empty( $settings ) ) { |
|
247 |
$settings = 'slider' == $field['type'] ? |
|
248 |
ot_slider_settings( $field['id'] ) : |
|
249 |
ot_list_item_settings( $field['id'] ); |
|
250 |
} |
|
251 |
|
|
252 |
/* merge the two settings array */ |
|
253 |
$settings = array_merge( $required_setting, $settings ); |
|
254 |
|
|
255 |
foreach( $_POST[$field['id']] as $k => $setting_array ) { |
|
256 |
|
|
257 |
foreach( $settings as $sub_setting ) { |
|
258 |
|
|
259 |
/* verify sub setting has a type & value */ |
|
260 |
if ( isset( $sub_setting['type'] ) && isset( $_POST[$field['id']][$k][$sub_setting['id']] ) ) { |
|
261 |
|
|
262 |
$_POST[$field['id']][$k][$sub_setting['id']] = ot_validate_setting( $_POST[$field['id']][$k][$sub_setting['id']], $sub_setting['type'], $sub_setting['id'] ); |
|
263 |
|
|
264 |
} |
|
265 |
|
|
266 |
} |
|
267 |
|
|
268 |
} |
|
269 |
|
|
270 |
/* set up new data with validated data */ |
|
271 |
$new = $_POST[$field['id']]; |
5
|
272 |
|
|
273 |
} else if ( $field['type'] == 'social-links' ) { |
|
274 |
|
|
275 |
/* get the settings array */ |
|
276 |
$settings = isset( $_POST[$field['id'] . '_settings_array'] ) ? unserialize( ot_decode( $_POST[$field['id'] . '_settings_array'] ) ) : array(); |
|
277 |
|
|
278 |
/* settings are empty get the defaults */ |
|
279 |
if ( empty( $settings ) ) { |
|
280 |
$settings = ot_social_links_settings( $field['id'] ); |
|
281 |
} |
|
282 |
|
|
283 |
foreach( $_POST[$field['id']] as $k => $setting_array ) { |
|
284 |
|
|
285 |
foreach( $settings as $sub_setting ) { |
|
286 |
|
|
287 |
/* verify sub setting has a type & value */ |
|
288 |
if ( isset( $sub_setting['type'] ) && isset( $_POST[$field['id']][$k][$sub_setting['id']] ) ) { |
|
289 |
|
|
290 |
$_POST[$field['id']][$k][$sub_setting['id']] = ot_validate_setting( $_POST[$field['id']][$k][$sub_setting['id']], $sub_setting['type'], $sub_setting['id'] ); |
|
291 |
|
|
292 |
} |
|
293 |
|
|
294 |
} |
|
295 |
|
|
296 |
} |
|
297 |
|
|
298 |
/* set up new data with validated data */ |
|
299 |
$new = $_POST[$field['id']]; |
0
|
300 |
|
|
301 |
} else { |
|
302 |
|
|
303 |
/* run through validattion */ |
|
304 |
$new = ot_validate_setting( $_POST[$field['id']], $field['type'], $field['id'] ); |
|
305 |
|
|
306 |
} |
|
307 |
|
|
308 |
/* insert CSS */ |
|
309 |
if ( $field['type'] == 'css' ) { |
|
310 |
|
|
311 |
/* insert CSS into dynamic.css */ |
|
312 |
if ( '' !== $new ) { |
|
313 |
|
|
314 |
ot_insert_css_with_markers( $field['id'], $new, true ); |
|
315 |
|
|
316 |
/* remove old CSS from dynamic.css */ |
|
317 |
} else { |
|
318 |
|
|
319 |
ot_remove_old_css( $field['id'] ); |
|
320 |
|
|
321 |
} |
|
322 |
|
|
323 |
} |
|
324 |
|
|
325 |
} |
|
326 |
|
5
|
327 |
if ( isset( $new ) && $new !== $old ) { |
0
|
328 |
update_post_meta( $post_id, $field['id'], $new ); |
|
329 |
} else if ( '' == $new && $old ) { |
|
330 |
delete_post_meta( $post_id, $field['id'], $old ); |
|
331 |
} |
|
332 |
} |
|
333 |
|
|
334 |
} |
|
335 |
|
|
336 |
} |
|
337 |
|
|
338 |
} |
|
339 |
|
|
340 |
/** |
|
341 |
* This method instantiates the meta box class & builds the UI. |
|
342 |
* |
|
343 |
* @uses OT_Meta_Box() |
|
344 |
* |
|
345 |
* @param array Array of arguments to create a meta box |
|
346 |
* @return void |
|
347 |
* |
|
348 |
* @access public |
|
349 |
* @since 2.0 |
|
350 |
*/ |
|
351 |
if ( ! function_exists( 'ot_register_meta_box' ) ) { |
|
352 |
|
|
353 |
function ot_register_meta_box( $args ) { |
|
354 |
if ( ! $args ) |
|
355 |
return; |
|
356 |
|
|
357 |
$ot_meta_box = new OT_Meta_Box( $args ); |
|
358 |
} |
|
359 |
|
|
360 |
} |
|
361 |
|
|
362 |
/* End of file ot-meta-box-api.php */ |
|
363 |
/* Location: ./includes/ot-meta-box-api.php */ |