Advertisement
Guest User

meta_box.php

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