' . $field['desc'] . '' : null; $place = isset( $field['place'] ) ? $field['place'] : null; $size = isset( $field['size'] ) ? $field['size'] : null; $post_type = isset( $field['post_type'] ) ? $field['post_type'] : null; $options = isset( $field['options'] ) ? $field['options'] : null; $settings = isset( $field['settings'] ) ? $field['settings'] : null; $repeatable_fields = isset( $field['repeatable_fields'] ) ? $field['repeatable_fields'] : null; // the id and name for each field $id = $name = isset( $field['id'] ) ? $field['id'] : null; if ( $repeatable ) { $name = $repeatable[0] . '[' . $repeatable[1] . '][' . $id .']'; $id = $repeatable[0] . '_' . $repeatable[1] . '_' . $id; } switch( $type ) { // basic case 'text': case 'tel': case 'email': default: echo ' ' . $desc; break; case 'url': echo '
' . $desc; break; case 'number': echo '
' . $desc; break; // textarea case 'textarea': echo '
' . $desc; break; // editor case 'editor': echo wp_editor( $meta, $id, $settings ) . '
' . $desc; break; // checkbox case 'checkbox': echo ' '; break; // select, chosen case 'select': case 'chosen': echo '
' . $desc; break; // radio case 'radio': echo '' . $desc; break; // checkbox_group case 'checkbox_group': echo '' . $desc; break; // color case 'color': $meta = $meta ? $meta : '#'; echo '
' . $desc; echo '
'; break; // post_select, post_chosen case 'post_select': case 'post_list': case 'post_chosen': echo '  Manage ' . $post_type_object->label ) . '
' . $desc; break; // post_checkboxes case 'post_checkboxes': $posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1 ) ); echo ' ' . $desc , '  Manage ' . $post_type_object->label ) . ''; break; // post_drop_sort case 'post_drop_sort': //areas $post_type_object = get_post_type_object( $post_type ); echo '

' . $desc . '  Manage ' . $post_type_object->label ) . '

'; foreach ( $areas as $area ) { echo ' '; } echo '
'; // source $exclude = null; if ( !empty( $meta ) ) { $exclude = implode( ',', $meta ); // because each ID is in a unique key $exclude = explode( ',', $exclude ); // put all the ID's back into a single array } $posts = get_posts( array( 'post_type' => $post_type, 'posts_per_page' => -1, 'post__not_in' => $exclude ) ); echo ''; break; // tax_select case 'tax_select': echo '  Manage ' . $taxonomy->label . '
' . $desc; break; // tax_checkboxes case 'tax_checkboxes': $terms = get_terms( $id, 'get=all' ); $post_terms = wp_get_object_terms( get_the_ID(), $id ); $taxonomy = get_taxonomy( $id ); $checked = $post_terms ? $taxonomy->hierarchical ? $post_terms[0]->term_id : $post_terms[0]->slug : null; foreach ( $terms as $term ) { $term_value = $taxonomy->hierarchical ? $term->term_id : $term->slug; echo '
'; } echo '' . $field['desc'] . ' Manage ' . $taxonomy->label . ''; break; // date case 'date': echo '
' . $desc; break; // slider case 'slider': $value = $meta != '' ? intval( $meta ) : '0'; echo '

' . $desc; break; // image case 'image': $image = ADF_PLUGIN_DIR . '/images/image.png'; echo '
'; if ( $meta ) { $image = wp_get_attachment_image_src( intval( $meta ), 'medium' ); $image = $image[0]; } echo ' Choose Image  Remove Image

' . $desc; break; // file case 'file': $iconClass = 'meta_box_file'; if ( $meta ) $iconClass .= ' checked'; echo '
' . esc_url( $meta ) . ' Choose File  Remove File

' . $desc; break; // repeatable case 'repeatable': echo ''; $i = 0; // create an empty array if ( $meta == '' || $meta == array() ) { $keys = wp_list_pluck( $repeatable_fields, 'id' ); $meta = array ( array_fill_keys( $keys, null ) ); } $meta = array_values( $meta ); foreach( $meta as $row ) { echo ''; foreach ( $repeatable_fields as $repeatable_field ) { if ( ! array_key_exists( $repeatable_field['id'], $meta[$i] ) ) $meta[$i][$repeatable_field['id']] = null; echo ''; } // end each field echo ''; $i++; } // end each row echo ''; echo ' '; echo '
'; echo adf_custom_meta_box_field( $repeatable_field, $meta[$i][$repeatable_field['id']], array( $id, $i ) ); echo 'Remove
Add New Row
' . $desc; break; } //end switch } /** * Finds any item in any level of an array * * @param string $needle field type to look for * @param array $haystack an array to search the type in * * @return bool whether or not the type is in the provided array */ if (! function_exists('adf_meta_box_find_field_type')) { function adf_meta_box_find_field_type( $needle, $haystack ) { foreach ( $haystack as $h ) if ( isset( $h['type'] ) && $h['type'] == 'repeatable' ) return adf_meta_box_find_field_type( $needle, $h['repeatable_fields'] ); elseif ( ( isset( $h['type'] ) && $h['type'] == $needle ) || ( isset( $h['repeatable_type'] ) && $h['repeatable_type'] == $needle ) ) return true; return false; } } /** * Find repeatable * * This function does almost the same exact thing that the above function * does, except we're exclusively looking for the repeatable field. The * reason is that we need a way to look for other fields nested within a * repeatable, but also need a way to stop at repeatable being true. * Hopefully I'll find a better way to do this later. * * @param string $needle field type to look for * @param array $haystack an array to search the type in * * @return bool whether or not the type is in the provided array */ if (! function_exists('adf_meta_box_find_repeatable')) { function adf_meta_box_find_repeatable( $needle = 'repeatable', $haystack ) { foreach ( $haystack as $h ) if ( isset( $h['type'] ) && $h['type'] == $needle ) return true; return false; } } /** * sanitize boolean inputs */ if (! function_exists('adf_meta_box_santitize_boolean')) { function adf_meta_box_santitize_boolean( $string ) { if ( ! isset( $string ) || $string != 1 || $string != true ) return false; else return true; } } /** * outputs properly sanitized data * * @param string $string the string to run through a validation function * @param string $function the validation function * * @return a validated string */ if (! function_exists('adf_meta_box_sanitize')) { function adf_meta_box_sanitize( $string, $function = 'sanitize_text_field' ) { switch ( $function ) { case 'intval': return intval( $string ); case 'absint': return absint( $string ); case 'wp_kses_post': return wp_kses_post( $string ); case 'wp_kses_data': return wp_kses_data( $string ); case 'esc_url_raw': return esc_url_raw( $string ); case 'is_email': return is_email( $string ); case 'sanitize_title': return sanitize_title( $string ); case 'santitize_boolean': return santitize_boolean( $string ); case 'sanitize_text_field': default: return sanitize_text_field( $string ); } } } /** * Map a multideminsional array * * @param string $func the function to map * @param array $meta a multidimensional array * @param array $sanitizer a matching multidimensional array of sanitizers * * @return array new array, fully mapped with the provided arrays */ if (! function_exists('adf_meta_box_array_map_r')) { function adf_meta_box_array_map_r( $func, $meta, $sanitizer ) { $newMeta = array(); $meta = array_values( $meta ); foreach( $meta as $key => $array ) { if ( $array == '' ) continue; /** * some values are stored as array, we only want multidimensional ones */ if ( ! is_array( $array ) ) { return array_map( $func, $meta, (array)$sanitizer ); break; } /** * the sanitizer will have all of the fields, but the item may only * have valeus for a few, remove the ones we don't have from the santizer */ $keys = array_keys( $array ); $newSanitizer = $sanitizer; if ( is_array( $sanitizer ) ) { foreach( $newSanitizer as $sanitizerKey => $value ) if ( ! in_array( $sanitizerKey, $keys ) ) unset( $newSanitizer[$sanitizerKey] ); } /** * run the function as deep as the array goes */ foreach( $array as $arrayKey => $arrayValue ) if ( is_array( $arrayValue ) ) $array[$arrayKey] = adf_meta_box_array_map_r( $func, $arrayValue, $newSanitizer[$arrayKey] ); $array = array_map( $func, $array, $newSanitizer ); $newMeta[$key] = array_combine( $keys, array_values( $array ) ); } return $newMeta; } } /** * takes in a few peices of data and creates a custom meta box * * @param string $id meta box id * @param string $title title * @param array $fields array of each field the box should include * @param string|array $page post type to add meta box to */ class Custom_Adf_Add_Meta_Box { var $id; var $title; var $fields; var $page; public function __construct( $id, $title, $fields, $page ) { $this->id = $id; $this->title = $title; $this->fields = $fields; $this->page = $page; if( ! is_array( $this->page ) ) $this->page = array( $this->page ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); add_action( 'admin_head', array( $this, 'admin_head' ) ); add_action( 'add_meta_boxes', array( $this, 'add_box' ) ); add_action( 'save_post', array( $this, 'save_box' )); } /** * enqueue necessary scripts and styles */ function admin_enqueue_scripts() { global $pagenow; if ( in_array( $pagenow, array( 'post-new.php', 'post.php' ) ) && in_array( get_post_type(), $this->page ) ) { // js $deps = array( 'jquery' ); if ( adf_meta_box_find_field_type( 'date', $this->fields ) ) $deps[] = 'jquery-ui-datepicker'; if ( adf_meta_box_find_field_type( 'slider', $this->fields ) ) $deps[] = 'jquery-ui-slider'; if ( adf_meta_box_find_field_type( 'color', $this->fields ) ) $deps[] = 'farbtastic'; if ( in_array( true, array( adf_meta_box_find_field_type( 'chosen', $this->fields ), adf_meta_box_find_field_type( 'post_chosen', $this->fields ) ) ) ) { wp_register_script( 'chosen', ADF_PLUGIN_DIR . '/js/chosen.js', array( 'jquery' ) ); $deps[] = 'chosen'; wp_enqueue_style( 'chosen', ADF_PLUGIN_DIR . '/css/chosen.css' ); } if ( in_array( true, array( adf_meta_box_find_field_type( 'date', $this->fields ), adf_meta_box_find_field_type( 'slider', $this->fields ), adf_meta_box_find_field_type( 'color', $this->fields ), adf_meta_box_find_field_type( 'chosen', $this->fields ), adf_meta_box_find_field_type( 'post_chosen', $this->fields ), adf_meta_box_find_repeatable( 'repeatable', $this->fields ), adf_meta_box_find_field_type( 'image', $this->fields ), adf_meta_box_find_field_type( 'file', $this->fields ) ) ) ) //wp_enqueue_script( 'meta_box', ADF_PLUGIN_DIR . '/js/scripts.js', $deps ); // css $deps = array(); wp_register_style( 'jqueryui', ADF_PLUGIN_DIR . '/css/jqueryui.css' ); if ( adf_meta_box_find_field_type( 'date', $this->fields ) || adf_meta_box_find_field_type( 'slider', $this->fields ) ) $deps[] = 'jqueryui'; if ( adf_meta_box_find_field_type( 'color', $this->fields ) ) $deps[] = 'farbtastic'; //wp_enqueue_style( 'meta_box', ADF_PLUGIN_DIR . '/css/meta_box.css', $deps ); } } /** * adds scripts to the head for special fields with extra js requirements */ function admin_head() { if ( in_array( get_post_type(), $this->page ) && ( adf_meta_box_find_field_type( 'date', $this->fields ) || adf_meta_box_find_field_type( 'slider', $this->fields ) ) ) { echo ''; } } /** * adds the meta box for every post type in $page */ function add_box() { foreach ( $this->page as $page ) { add_meta_box( $this->id, $this->title, array( $this, 'meta_box_callback' ), $page, 'normal', 'high' ); } } /** * outputs the meta box */ function meta_box_callback() { // Use nonce for verification wp_nonce_field( 'custom_meta_box_nonce_action', 'custom_meta_box_nonce_field' ); // Begin the field table and loop echo ''; foreach ( $this->fields as $field) { if ( $field['type'] == 'section' ) { echo ''; } else { if($field['label'] !== " "){ echo ''; } echo ''; } } // end foreach echo '

' . $field['label'] . '

'; $meta = get_post_meta( get_the_ID(), $field['id'], true); echo adf_custom_meta_box_field( $field, $meta ); echo '
'; // end table } /** * saves the captured data */ function save_box( $post_id ) { $post_type = get_post_type(); // verify nonce if ( ! isset( $_POST['custom_meta_box_nonce_field'] ) ) return $post_id; if ( ! ( in_array( $post_type, $this->page ) || wp_verify_nonce( $_POST['custom_meta_box_nonce_field'], 'custom_meta_box_nonce_action' ) ) ) return $post_id; // check autosave if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $post_id; // check permissions if ( ! current_user_can( 'edit_page', $post_id ) ) return $post_id; // loop through fields and save the data foreach ( $this->fields as $field ) { if( $field['type'] == 'section' ) { $sanitizer = null; continue; } if( in_array( $field['type'], array( 'tax_select', 'tax_checkboxes' ) ) ) { // save taxonomies if ( isset( $_POST[$field['id']] ) ) { $term = $_POST[$field['id']]; wp_set_object_terms( $post_id, $term, $field['id'] ); } } else { // save the rest $new = false; $old = get_post_meta( $post_id, $field['id'], true ); if ( isset( $_POST[$field['id']] ) ) $new = $_POST[$field['id']]; if ( isset( $new ) && '' == $new && $old ) { delete_post_meta( $post_id, $field['id'], $old ); } elseif ( isset( $new ) && $new != $old ) { $sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field'; if ( is_array( $new ) ) $new = adf_meta_box_array_map_r( 'adf_meta_box_sanitize', $new, $sanitizer ); else $new = adf_meta_box_sanitize( $new, $sanitizer ); update_post_meta( $post_id, $field['id'], $new ); } } } // end foreach } } $prefix = 'adf_'; $fields = array( array( // Repeatable & Sortable Text inputs 'label' => 'Field1 Title', //