Advertisement
Guest User

Metabox

a guest
Oct 31st, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.95 KB | None | 0 0
  1. <?php
  2.  
  3. // metaboxes directory constant
  4. define( 'CUSTOM_METABOXES_DIR', get_template_directory_uri() . '/plugins/metaboxes' );
  5.  
  6. function in_array_of_terms($array, $hierarchical, $term_id, $slug) {
  7. $item = null;
  8. foreach($array as $term) {
  9. if ($hierarchical && $term_id == $term->term_id) {
  10. $item = $term->term_id;
  11. break;
  12. } else if (!$hierarchical && $slug == $term->slug) {
  13. $item = $term->slug;
  14. break;
  15. }
  16. }
  17. return $item;
  18. }
  19.  
  20. /**
  21. * recives data about a form field and spits out the proper html
  22. *
  23. * @param array $field array with various bits of information about the field
  24. * @param string|int|bool|array $meta the saved data for this field
  25. * @param array $repeatable if is this for a repeatable field, contains parant id and the current integar
  26. *
  27. * @return string html for the field
  28. */
  29. function custom_meta_box_field( $field, $meta = null, $repeatable = null ) {
  30. if ( ! ( $field || is_array( $field ) ) )
  31. return;
  32.  
  33. // get field data
  34. $type = isset( $field['type'] ) ? $field['type'] : null;
  35. $label = isset( $field['label'] ) ? $field['label'] : null;
  36. $desc = isset( $field['desc'] ) ? '<span class="description">' . $field['desc'] . '</span>' : null;
  37. $place = isset( $field['place'] ) ? $field['place'] : null;
  38. $size = isset( $field['size'] ) ? $field['size'] : null;
  39. $post_type = isset( $field['post_type'] ) ? $field['post_type'] : null;
  40. $options = isset( $field['options'] ) ? $field['options'] : null;
  41. $settings = isset( $field['settings'] ) ? $field['settings'] : null;
  42. $repeatable_fields = isset( $field['repeatable_fields'] ) ? $field['repeatable_fields'] : null;
  43.  
  44. // the id and name for each field
  45. $id = $name = isset( $field['id'] ) ? $field['id'] : null;
  46. if ( $repeatable ) {
  47. $name = $repeatable[0] . '[' . $repeatable[1] . '][' . $id .']';
  48. $id = $repeatable[0] . '_' . $repeatable[1] . '_' . $id;
  49. }
  50. switch( $type ) {
  51. // basic
  52. case 'text':
  53. case 'tel':
  54. case 'email':
  55. default:
  56. echo '<input type="' . $type . '" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . esc_attr( $meta ) . '" class="regular-text" size="30" />
  57. <br />' . $desc;
  58. break;
  59. case 'url':
  60. echo '<input type="' . $type . '" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . esc_url( $meta ) . '" class="regular-text" size="30" />
  61. <br />' . $desc;
  62. break;
  63. case 'number':
  64. echo '<input type="' . $type . '" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . intval( $meta ) . '" class="regular-text" size="30" />
  65. <br />' . $desc;
  66. break;
  67. // textarea
  68. case 'textarea':
  69. echo '<textarea name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" cols="60" rows="4">' . esc_textarea( $meta ) . '</textarea>
  70. <br />' . $desc;
  71. break;
  72. // editor
  73. case 'editor':
  74. echo wp_editor( $meta, $id, $settings ) . '<br />' . $desc;
  75. break;
  76. // checkbox
  77. case 'checkbox':
  78. echo '<input type="checkbox" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" ' . checked( $meta, true, false ) . ' value="1" />
  79. <label for="' . esc_attr( $id ) . '">' . $desc . '</label>';
  80. break;
  81. // select, chosen
  82. case 'select':
  83. case 'chosen':
  84. echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '"' , $type == 'chosen' ? ' class="chosen"' : '' , isset( $multiple ) && $multiple == true ? ' multiple="multiple"' : '' , '>
  85. <option value="">Select One</option>'; // Select One
  86. foreach ( $options as $option )
  87. echo '<option' . selected( $meta, $option['value'], false ) . ' value="' . $option['value'] . '">' . $option['label'] . '</option>';
  88. echo '</select><br />' . $desc;
  89. break;
  90. // radio
  91. case 'radio':
  92. echo '<ul class="meta_box_items">';
  93. foreach ( $options as $option )
  94. echo '<li><input type="radio" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '-' . $option['value'] . '" value="' . $option['value'] . '" ' . checked( $meta, $option['value'], false ) . ' />
  95. <label for="' . esc_attr( $id ) . '-' . $option['value'] . '">' . $option['label'] . '</label></li>';
  96. echo '</ul>' . $desc;
  97. break;
  98. // checkbox_group
  99. case 'checkbox_group':
  100. echo '<ul class="meta_box_items">';
  101. foreach ( $options as $option )
  102. echo '<li><input type="checkbox" value="' . $option['value'] . '" name="' . esc_attr( $name ) . '[]" id="' . esc_attr( $id ) . '-' . $option['value'] . '"' , is_array( $meta ) && in_array( $option['value'], $meta ) ? ' checked="checked"' : '' , ' />
  103. <label for="' . esc_attr( $id ) . '-' . $option['value'] . '">' . $option['label'] . '</label></li>';
  104. echo '</ul>' . $desc;
  105. break;
  106. // color
  107. case 'color':
  108. $meta = $meta ? $meta : '#';
  109. echo '<input type="text" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . $meta . '" size="10" />
  110. <br />' . $desc;
  111. echo '<div id="colorpicker-' . esc_attr( $id ) . '"></div>
  112. <script type="text/javascript">
  113. jQuery(function(jQuery) {
  114. jQuery("#colorpicker-' . esc_attr( $id ) . '").hide();
  115. jQuery("#colorpicker-' . esc_attr( $id ) . '").farbtastic("#' . esc_attr( $id ) . '");
  116. jQuery("#' . esc_attr( $id ) . '").bind("blur", function() { jQuery("#colorpicker-' . esc_attr( $id ) . '").slideToggle(); } );
  117. jQuery("#' . esc_attr( $id ) . '").bind("focus", function() { jQuery("#colorpicker-' . esc_attr( $id ) . '").slideToggle(); } );
  118. });
  119. </script>';
  120. break;
  121. // post_select, post_chosen
  122. case 'post_select':
  123. case 'post_list':
  124. case 'post_chosen':
  125. echo '<select data-placeholder="Select One" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '"' , $type == 'post_chosen' ? ' class="chosen"' : '' , isset( $multiple ) && $multiple == true ? ' multiple="multiple"' : '' , '>
  126. <option value=""></option>'; // Select One
  127. $posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'suppress_filters' => 0 ) );
  128. foreach ( $posts as $item )
  129. echo '<option value="' . $item->ID . '"' . selected( $item->ID, $meta, false ) . '>' . $item->post_title . '</option>';
  130.  
  131. $post_type_object = get_post_type_object( $post_type[0] );
  132.  
  133.  
  134. echo '</select> &nbsp;<span class="description"><a href="' . admin_url( 'edit.php?post_type=' . $post_type[0] . '">Manage ' . $post_type_object->labels->name ) . '</a></span><br />' . $desc;
  135. break;
  136. // post_checkboxes
  137. case 'post_checkboxes':
  138. $posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1, 'suppress_filters' => 0 ) );
  139. echo '<ul class="meta_box_items">';
  140. foreach ( $posts as $item )
  141. echo '<li><input type="checkbox" value="' . $item->ID . '" name="' . esc_attr( $name ) . '[]" id="' . esc_attr( $id ) . '-' . $item->ID . '"' , is_array( $meta ) && in_array( $item->ID, $meta ) ? ' checked="checked"' : '' , ' />
  142. <label for="' . esc_attr( $id ) . '-' . $item->ID . '">' . $item->post_title . '</label></li>';
  143.  
  144. $post_type_object = get_post_type_object( $post_type[0] );
  145. echo '</ul> ' . $desc , ' &nbsp;<span class="description"><a href="' . admin_url( 'edit.php?post_type=' . $post_type[0] . '">Manage ' . $post_type_object->labels->name ) . '</a></span>';
  146. break;
  147. // post_drop_sort
  148. case 'post_drop_sort':
  149. //areas
  150. $post_type_object = get_post_type_object( $post_type[0] );
  151. echo '<p>' . $desc . ' &nbsp;<span class="description"><a href="' . admin_url( 'edit.php?post_type=' . $post_type[0] . '">Manage ' . $post_type_object->labels->name ) . '</a></span></p><div class="post_drop_sort_areas">';
  152. foreach ( $areas as $area ) {
  153. echo '<ul id="area-' . $area['id'] . '" class="sort_list">
  154. <li class="post_drop_sort_area_name">' . $area['label'] . '</li>';
  155. if ( is_array( $meta ) ) {
  156. $items = explode( ',', $meta[$area['id']] );
  157. foreach ( $items as $item ) {
  158. $output = $display == 'thumbnail' ? get_the_post_thumbnail( $item, array( 204, 30 ) ) : get_the_title( $item );
  159. echo '<li id="' . $item . '">' . $output . '</li>';
  160. }
  161. }
  162. echo '</ul>
  163. <input type="hidden" name="' . esc_attr( $name ) . '[' . $area['id'] . ']"
  164. class="store-area-' . $area['id'] . '"
  165. value="' , $meta ? $meta[$area['id']] : '' , '" />';
  166. }
  167. echo '</div>';
  168. // source
  169. $exclude = null;
  170. if ( !empty( $meta ) ) {
  171. $exclude = implode( ',', $meta ); // because each ID is in a unique key
  172. $exclude = explode( ',', $exclude ); // put all the ID's back into a single array
  173. }
  174. $posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1, 'post__not_in' => $exclude, 'suppress_filters' => 0 ) );
  175. echo '<ul class="post_drop_sort_source sort_list">
  176. <li class="post_drop_sort_area_name">Available ' . $label . '</li>';
  177. foreach ( $posts as $item ) {
  178. $output = $display == 'thumbnail' ? get_the_post_thumbnail( $item->ID, array( 204, 30 ) ) : get_the_title( $item->ID );
  179. echo '<li id="' . $item->ID . '">' . $output . '</li>';
  180. }
  181. echo '</ul>';
  182. break;
  183. // tax_select
  184. case 'tax_select':
  185. echo '<select name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '"><option value="">Select One</option>'; // Select One
  186. $tax_id = $id;
  187. if ($repeatable)
  188. {
  189. // we are inside repeatable so get rid of tagged on text
  190. $tax_id = str_replace($repeatable[0] . '_' . $repeatable[1] . '_', '', $id);
  191. }
  192. $terms = get_terms( $tax_id, 'get=all&suppress_filters=0' );
  193. $post_terms = wp_get_object_terms( get_the_ID(), $tax_id );
  194. $taxonomy = get_taxonomy( $tax_id );
  195. // $selected = $post_terms ? $taxonomy->hierarchical ? $post_terms[0]->term_id : $post_terms[0]->slug : null;
  196. foreach ( $terms as $term ) {
  197. $selected = in_array_of_terms($post_terms, $taxonomy->hierarchical, $term->term_id, $term->slug);
  198. $term_value = $taxonomy->hierarchical ? $term->term_id : $term->slug;
  199. echo '<option value="' . $term_value . '"' . selected( $selected, $term_value, false ) . '>' . $term->name . '</option>';
  200. }
  201. echo '</select> &nbsp;<span class="description"><a href="'.get_bloginfo( 'url' ) . '/wp-admin/edit-tags.php?taxonomy=' . $tax_id . '">Manage ' . $taxonomy->label . '</a></span>
  202. <br />' . $desc;
  203. break;
  204. // tax_checkboxes
  205. case 'tax_checkboxes':
  206. $tax_id = $id;
  207. if ($repeatable)
  208. {
  209. // we are inside repeatable so get rid of tagged on text
  210. $tax_id = str_replace($repeatable[0] . '_' . $repeatable[1] . '_', '', $id);
  211. }
  212. $terms = get_terms( $tax_id, 'get=all&suppress_filters=0' );
  213. $post_terms = wp_get_object_terms( get_the_ID(), $tax_id );
  214. $taxonomy = get_taxonomy( $tax_id );
  215. //var_dump($post_terms);
  216. // $checked = $post_terms ? $taxonomy->hierarchical ? $post_terms[0]->term_id : $post_terms[0]->slug : null;
  217. foreach ( $terms as $term ) {
  218. $checked = in_array_of_terms($post_terms, $taxonomy->hierarchical, $term->term_id, $term->slug);
  219. $checked = ($post_terms && $checked) ? $taxonomy->hierarchical ? $term->term_id : $term->slug : null;
  220. $term_value = $taxonomy->hierarchical ? $term->term_id : $term->slug;
  221.  
  222. echo '<input type="checkbox" value="' . $term_value . '" name="' . $id . '[]" id="term-' . $term_value . '"' . checked( $checked, $term_value, false ) . ' /> <label for="term-' . $term_value . '">' . $term->name . '</label><br />';
  223. }
  224. echo '<span class="description">' . (isset($field['desc']) ? $field['desc'] : '') . ' <a href="'.get_bloginfo( 'url' ) . '/wp-admin/edit-tags.php?taxonomy=' . $tax_id . '">Manage ' . $taxonomy->label . '</a></span>';
  225. break;
  226. // date
  227. case 'date':
  228. echo '<input type="text" class="datepicker" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . $meta . '" size="30" />
  229. <br />' . $desc;
  230. break;
  231. // slider
  232. case 'slider':
  233. $value = $meta != '' ? intval( $meta ) : '0';
  234. echo '<div id="' . esc_attr( $id ) . '-slider"></div>
  235. <input type="text" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . $value . '" size="5" />
  236. <br />' . $desc;
  237. break;
  238. // image
  239. case 'image':
  240. $image = CUSTOM_METABOXES_DIR . '/images/image.png';
  241. echo '<div class="meta_box_image"><span class="meta_box_default_image" style="display:none">' . $image . '</span>';
  242. if ( $meta ) {
  243. $image = wp_get_attachment_image_src( intval( $meta ), 'medium' );
  244. $image = $image[0];
  245. }
  246. echo '<input name="' . esc_attr( $name ) . '" type="hidden" class="meta_box_upload_image" value="' . intval( $meta ) . '" />
  247. <img src="' . esc_attr( $image ) . '" class="meta_box_preview_image" alt="" />
  248. <a href="#" class="meta_box_upload_image_button button" rel="' . get_the_ID() . '">Choose Image</a>
  249. <small>&nbsp;<a href="#" class="meta_box_clear_image_button">Remove Image</a></small></div>
  250. <br clear="all" />' . $desc;
  251. break;
  252. // file
  253. case 'file':
  254. $iconClass = 'meta_box_file';
  255. if ( $meta ) $iconClass .= ' checked';
  256. echo '<div class="meta_box_file_stuff"><input name="' . esc_attr( $name ) . '" type="hidden" class="meta_box_upload_file" value="' . esc_url( $meta ) . '" />
  257. <span class="' . $iconClass . '"></span>
  258. <span class="meta_box_filename">' . esc_url( $meta ) . '</span>
  259. <a href="#" class="meta_box_upload_image_button button" rel="' . get_the_ID() . '">Choose File</a>
  260. <small>&nbsp;<a href="#" class="meta_box_clear_file_button">Remove File</a></small></div>
  261. <br clear="all" />' . $desc;
  262. break;
  263. // repeatable
  264. case 'repeatable':
  265. echo '<table id="' . esc_attr( $id ) . '-repeatable" class="meta_box_repeatable" cellspacing="0">
  266. <thead>
  267. <tr>
  268. <th><span class="sort_label"></span></th>
  269. <th>Fields</th>
  270. <th><a class="meta_box_repeatable_add" href="#"></a></th>
  271. </tr>
  272. </thead>
  273. <tbody>';
  274. $i = 0;
  275. // create an empty array
  276. if ( $meta == '' || $meta == array() ) {
  277. $keys = wp_list_pluck( $repeatable_fields, 'id' );
  278. $meta = array ( array_fill_keys( $keys, null ) );
  279. }
  280. $meta = array_values( $meta );
  281. foreach( $meta as $row ) {
  282. echo '<tr>
  283. <td><span class="sort hndle"></span></td><td>';
  284. foreach ( $repeatable_fields as $repeatable_field ) {
  285. if ( ! array_key_exists( $repeatable_field['id'], $meta[$i] ) )
  286. $meta[$i][$repeatable_field['id']] = null;
  287. echo '<label>' . $repeatable_field['label'] . '</label><p>';
  288. echo custom_meta_box_field( $repeatable_field, $meta[$i][$repeatable_field['id']], array( $id, $i ) );
  289. echo '</p>';
  290. } // end each field
  291. echo '</td><td><a class="meta_box_repeatable_remove" href="#"></a></td></tr>';
  292. $i++;
  293. } // end each row
  294. echo '</tbody>';
  295. echo '
  296. <tfoot>
  297. <tr>
  298. <th><span class="sort_label"></span></th>
  299. <th>Fields</th>
  300. <th><a class="meta_box_repeatable_add" href="#"></a></th>
  301. </tr>
  302. </tfoot>';
  303. echo '</table>
  304. ' . $desc;
  305. break;
  306. } //end switch
  307.  
  308. }
  309.  
  310.  
  311. /**
  312. * Finds any item in any level of an array
  313. *
  314. * @param string $needle field type to look for
  315. * @param array $haystack an array to search the type in
  316. *
  317. * @return bool whether or not the type is in the provided array
  318. */
  319. function meta_box_find_field_type( $needle, $haystack ) {
  320. foreach ( $haystack as $h )
  321. if ( isset( $h['type'] ) && $h['type'] == 'repeatable' )
  322. return meta_box_find_field_type( $needle, $h['repeatable_fields'] );
  323. elseif ( ( isset( $h['type'] ) && $h['type'] == $needle ) || ( isset( $h['repeatable_type'] ) && $h['repeatable_type'] == $needle ) )
  324. return true;
  325. return false;
  326. }
  327.  
  328. /**
  329. * Find repeatable
  330. *
  331. * This function does almost the same exact thing that the above function
  332. * does, except we're exclusively looking for the repeatable field. The
  333. * reason is that we need a way to look for other fields nested within a
  334. * repeatable, but also need a way to stop at repeatable being true.
  335. * Hopefully I'll find a better way to do this later.
  336. *
  337. * @param string $needle field type to look for
  338. * @param array $haystack an array to search the type in
  339. *
  340. * @return bool whether or not the type is in the provided array
  341. */
  342. function meta_box_find_repeatable( $needle = 'repeatable', $haystack ) {
  343. foreach ( $haystack as $h )
  344. if ( isset( $h['type'] ) && $h['type'] == $needle )
  345. return true;
  346. return false;
  347. }
  348.  
  349. /**
  350. * sanitize boolean inputs
  351. */
  352. function meta_box_santitize_boolean( $string ) {
  353. if ( ! isset( $string ) || $string != 1 || $string != true )
  354. return false;
  355. else
  356. return true;
  357. }
  358.  
  359. /**
  360. * outputs properly sanitized data
  361. *
  362. * @param string $string the string to run through a validation function
  363. * @param string $function the validation function
  364. *
  365. * @return a validated string
  366. */
  367. function meta_box_sanitize( $string, $function = 'sanitize_text_field' ) {
  368. $allowed_html = array(
  369. 'a' => array(
  370. 'href' => true,
  371. 'title' => true,
  372. ),
  373. 'abbr' => array(
  374. 'title' => true,
  375. ),
  376. 'acronym' => array(
  377. 'title' => true,
  378. ),
  379. 'b' => array(),
  380. 'blockquote' => array(
  381. 'cite' => true,
  382. ),
  383. 'cite' => array(),
  384. 'code' => array(),
  385. 'del' => array(
  386. 'datetime' => true,
  387. ),
  388. 'em' => array(),
  389. 'i' => array(),
  390. 'q' => array(
  391. 'cite' => true,
  392. ),
  393. 'strike' => array(),
  394. 'strong' => array(),
  395. 'iframe' => array(
  396. 'width' => true,
  397. 'height' => true,
  398. 'frameborder' => true,
  399. 'scrolling' => true,
  400. 'marginheight' => true,
  401. 'marginwidth' => true,
  402. 'src' => true
  403. ),
  404. 'script' => array(
  405. 'src' => true
  406. ),
  407. 'p' => array(),
  408. 'br' => array(),
  409. 'hr' => array(),
  410. 'div' => array('class' => true),
  411. 'span' => array('class' => true),
  412. 'img' => array(
  413. 'src' => true,
  414. 'alt' => true,
  415. 'class' => true,
  416. 'width' => true,
  417. 'height' => true
  418. ),
  419. 'table' => array(
  420. 'class' => true,
  421. 'id' => true,
  422. ),
  423. 'tr' => array(
  424. 'class' => true,
  425. 'id' => true,
  426. ),
  427. 'td' => array(
  428. 'class' => true,
  429. 'id' => true,
  430. ),
  431. 'tbody' => array()
  432. );
  433.  
  434. switch ( $function ) {
  435. case 'intval':
  436. return intval( $string );
  437. case 'absint':
  438. return absint( $string );
  439. case 'wp_kses_post':
  440. return wp_kses( $string, $allowed_html );
  441. case 'wp_kses_data':
  442. return wp_kses_data( $string );
  443. case 'esc_url_raw':
  444. return esc_url_raw( $string );
  445. case 'is_email':
  446. return is_email( $string );
  447. case 'sanitize_title':
  448. return sanitize_title( $string );
  449. case 'santitize_boolean':
  450. return santitize_boolean( $string );
  451. case 'sanitize_text_field':
  452. default:
  453. return wp_kses( $string, $allowed_html );
  454. }
  455. }
  456.  
  457. /**
  458. * Map a multideminsional array
  459. *
  460. * @param string $func the function to map
  461. * @param array $meta a multidimensional array
  462. * @param array $sanitizer a matching multidimensional array of sanitizers
  463. *
  464. * @return array new array, fully mapped with the provided arrays
  465. */
  466. function meta_box_array_map_r( $func, $meta, $sanitizer ) {
  467.  
  468. $newMeta = array();
  469. $meta = array_values( $meta );
  470.  
  471. foreach( $meta as $key => $array ) {
  472. if ( $array == '' )
  473. continue;
  474. /**
  475. * some values are stored as array, we only want multidimensional ones
  476. */
  477. if ( ! is_array( $array ) ) {
  478. return array_map( $func, $meta, (array)$sanitizer );
  479. break;
  480. }
  481. /**
  482. * the sanitizer will have all of the fields, but the item may only
  483. * have valeus for a few, remove the ones we don't have from the santizer
  484. */
  485. $keys = array_keys( $array );
  486. $newSanitizer = $sanitizer;
  487. if ( is_array( $sanitizer ) ) {
  488. foreach( $newSanitizer as $sanitizerKey => $value )
  489. if ( ! in_array( $sanitizerKey, $keys ) )
  490. unset( $newSanitizer[$sanitizerKey] );
  491. }
  492. /**
  493. * run the function as deep as the array goes
  494. */
  495. foreach( $array as $arrayKey => $arrayValue )
  496. if ( is_array( $arrayValue ) )
  497. $array[$arrayKey] = meta_box_array_map_r( $func, $arrayValue, $newSanitizer[$arrayKey] );
  498.  
  499. $array = array_map( $func, $array, $newSanitizer );
  500. $newMeta[$key] = array_combine( $keys, array_values( $array ) );
  501. }
  502. return $newMeta;
  503. }
  504.  
  505. /**
  506. * takes in a few peices of data and creates a custom meta box
  507. *
  508. * @param string $id meta box id
  509. * @param string $title title
  510. * @param array $fields array of each field the box should include
  511. * @param string|array $page post type to add meta box to
  512. */
  513. class Custom_Add_Meta_Box {
  514.  
  515. var $id;
  516. var $title;
  517. var $fields;
  518. var $page;
  519.  
  520. public function __construct( $id, $title, $fields, $page ) {
  521. $this->id = $id;
  522. $this->title = $title;
  523. $this->fields = $fields;
  524. $this->page = $page;
  525.  
  526. if( ! is_array( $this->page ) )
  527. $this->page = array( $this->page );
  528.  
  529. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  530. add_action( 'admin_head', array( $this, 'admin_head' ) );
  531. add_action( 'add_meta_boxes', array( $this, 'add_box' ) );
  532. add_action( 'save_post', array( $this, 'save_box' ));
  533. }
  534.  
  535. /**
  536. * enqueue necessary scripts and styles
  537. */
  538. function admin_enqueue_scripts() {
  539. global $pagenow;
  540. if ( in_array( $pagenow, array( 'post-new.php', 'post.php' ) ) && in_array( get_post_type(), $this->page ) ) {
  541. // js
  542. $deps = array( 'jquery' );
  543. if ( meta_box_find_field_type( 'date', $this->fields ) )
  544. $deps[] = 'jquery-ui-datepicker';
  545. if ( meta_box_find_field_type( 'slider', $this->fields ) )
  546. $deps[] = 'jquery-ui-slider';
  547. if ( meta_box_find_field_type( 'color', $this->fields ) )
  548. $deps[] = 'farbtastic';
  549. if ( in_array( true, array(
  550. meta_box_find_field_type( 'chosen', $this->fields ),
  551. meta_box_find_field_type( 'post_chosen', $this->fields )
  552. ) ) ) {
  553. wp_register_script( 'chosen', CUSTOM_METABOXES_DIR . '/js/chosen.js', array( 'jquery' ) );
  554. $deps[] = 'chosen';
  555. wp_enqueue_style( 'chosen', CUSTOM_METABOXES_DIR . '/css/chosen.css' );
  556. }
  557. if ( in_array( true, array(
  558. meta_box_find_field_type( 'date', $this->fields ),
  559. meta_box_find_field_type( 'slider', $this->fields ),
  560. meta_box_find_field_type( 'color', $this->fields ),
  561. meta_box_find_field_type( 'chosen', $this->fields ),
  562. meta_box_find_field_type( 'post_chosen', $this->fields ),
  563. meta_box_find_repeatable( 'repeatable', $this->fields ),
  564. meta_box_find_field_type( 'image', $this->fields ),
  565. meta_box_find_field_type( 'file', $this->fields )
  566. ) ) )
  567. wp_enqueue_script( 'meta_box', CUSTOM_METABOXES_DIR . '/js/scripts.js', $deps );
  568.  
  569. // css
  570. $deps = array();
  571. wp_register_style( 'jqueryui', CUSTOM_METABOXES_DIR . '/css/jqueryui.css' );
  572. if ( meta_box_find_field_type( 'date', $this->fields ) || meta_box_find_field_type( 'slider', $this->fields ) )
  573. $deps[] = 'jqueryui';
  574. if ( meta_box_find_field_type( 'color', $this->fields ) )
  575. $deps[] = 'farbtastic';
  576. wp_enqueue_style( 'meta_box', CUSTOM_METABOXES_DIR . '/css/meta_box.css', $deps );
  577. }
  578. }
  579.  
  580. /**
  581. * adds scripts to the head for special fields with extra js requirements
  582. */
  583. function admin_head() {
  584.  
  585. if ( in_array( get_post_type(), $this->page ) && ( meta_box_find_field_type( 'date', $this->fields ) || meta_box_find_field_type( 'slider', $this->fields ) ) ) {
  586.  
  587. echo '<script type="text/javascript">
  588. jQuery(function( $) {';
  589.  
  590. foreach ( $this->fields as $field ) {
  591. switch( $field['type'] ) {
  592. // date
  593. case 'date' :
  594. echo '$("#' . $field['id'] . '").datepicker({
  595. dateFormat: \'yy-mm-dd\'
  596. });';
  597. break;
  598. // slider
  599. case 'slider' :
  600. $value = get_post_meta( get_the_ID(), $field['id'], true );
  601. if ( $value == '' )
  602. $value = $field['min'];
  603. echo '
  604. $( "#' . $field['id'] . '-slider" ).slider({
  605. value: ' . $value . ',
  606. min: ' . $field['min'] . ',
  607. max: ' . $field['max'] . ',
  608. step: ' . $field['step'] . ',
  609. slide: function( event, ui ) {
  610. $( "#' . $field['id'] . '" ).val( ui.value );
  611. }
  612. });';
  613. break;
  614. }
  615. }
  616.  
  617. echo '});
  618. </script>';
  619.  
  620. }
  621. }
  622.  
  623. /**
  624. * adds the meta box for every post type in $page
  625. */
  626. function add_box() {
  627. foreach ( $this->page as $page ) {
  628. add_meta_box( $this->id, $this->title, array( $this, 'meta_box_callback' ), $page, 'normal', 'high' );
  629. }
  630. }
  631.  
  632. /**
  633. * outputs the meta box
  634. */
  635. function meta_box_callback() {
  636. // Use nonce for verification
  637. wp_nonce_field( 'custom_meta_box_nonce_action', 'custom_meta_box_nonce_field' );
  638.  
  639. // Begin the field table and loop
  640. echo '<table class="form-table meta_box">';
  641. foreach ( $this->fields as $field) {
  642. if ( $field['type'] == 'section' ) {
  643. echo '<tr>
  644. <td colspan="2">
  645. <h2>' . $field['label'] . '</h2>
  646. </td>
  647. </tr>';
  648. }
  649. else {
  650. echo '<tr>
  651. <th style="width:20%"><label for="' . $field['id'] . '">' . $field['label'] . '</label></th>
  652. <td>';
  653.  
  654. $meta = get_post_meta( get_the_ID(), $field['id'], true);
  655. echo custom_meta_box_field( $field, $meta );
  656.  
  657. echo '<td>
  658. </tr>';
  659. }
  660. } // end foreach
  661. echo '</table>'; // end table
  662. }
  663.  
  664. /**
  665. * saves the captured data
  666. */
  667. function save_box( $post_id ) {
  668. $post_type = get_post_type();
  669.  
  670. // verify nonce
  671. if ( ! isset( $_POST['custom_meta_box_nonce_field'] ) )
  672. return $post_id;
  673. if ( ! ( in_array( $post_type, $this->page ) || wp_verify_nonce( $_POST['custom_meta_box_nonce_field'], 'custom_meta_box_nonce_action' ) ) )
  674. return $post_id;
  675. // check autosave
  676. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  677. return $post_id;
  678. // check permissions
  679. if ( ! current_user_can( 'edit_page', $post_id ) )
  680. return $post_id;
  681.  
  682. // loop through fields and save the data
  683. foreach ( $this->fields as $field ) {
  684. if( $field['type'] == 'section' ) {
  685. $sanitizer = null;
  686. continue;
  687. }
  688. if( in_array( $field['type'], array( 'tax_select', 'tax_checkboxes' ) ) ) {
  689. // save taxonomies
  690. if ($field['type'] == 'tax_select') {
  691. if ( isset( $_POST[$field['id']] ) ) {
  692. $term = $_POST[$field['id']];
  693. wp_set_object_terms( $post_id, $term, $field['id'] );
  694. }
  695. } else if ($field['type'] == 'tax_checkboxes') {
  696. if ( isset( $_POST[$field['id']] ) ) {
  697. $term = $_POST[$field['id']];
  698. } else {
  699. $term = array();
  700. }
  701. wp_set_object_terms( $post_id, $term, $field['id'] );
  702. }
  703. }
  704. else {
  705. // save the rest
  706. $new = false;
  707. $old = get_post_meta( $post_id, $field['id'], true );
  708. if ( isset( $_POST[$field['id']] ) )
  709. $new = $_POST[$field['id']];
  710. if ( isset( $new ) && '' == $new && $old ) {
  711. delete_post_meta( $post_id, $field['id'], $old );
  712. } elseif ( isset( $new ) && $new != $old ) {
  713. $sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field';
  714. if ( is_array( $new ) )
  715. $new = meta_box_array_map_r( 'meta_box_sanitize', $new, $sanitizer );
  716. else
  717. $new = meta_box_sanitize( $new, $sanitizer );
  718. update_post_meta( $post_id, $field['id'], $new );
  719. }
  720. }
  721. } // end foreach
  722. }
  723.  
  724. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement