Advertisement
bowenac

meta box

Oct 19th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 34.97 KB | None | 0 0
  1. <?php
  2. /*
  3. Script Name:    Custom Metaboxes and Fields
  4. Contributors:   Andrew Norcross (@norcross / andrewnorcross.com)
  5.                 Jared Atchison (@jaredatch / jaredatchison.com)
  6.                 Bill Erickson (@billerickson / billerickson.net)
  7.                 Justin Sternberg (@jtsternberg / dsgnwrks.pro)
  8. Description:    This will create metaboxes with custom fields that will blow your mind.
  9. Version:        0.9.4
  10. */
  11.  
  12. /**
  13.  * Released under the GPL license
  14.  * http://www.opensource.org/licenses/gpl-license.php
  15.  *
  16.  * This is an add-on for WordPress
  17.  * http://wordpress.org/
  18.  *
  19.  * **********************************************************************
  20.  * This program is free software; you can redistribute it and/or modify
  21.  * it under the terms of the GNU General Public License as published by
  22.  * the Free Software Foundation; either version 2 of the License, or
  23.  * (at your option) any later version.
  24.  *
  25.  * This program is distributed in the hope that it will be useful,
  26.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28.  * GNU General Public License for more details.
  29.  * **********************************************************************
  30.  */
  31.  
  32. /************************************************************************
  33.         You should not edit the code below or things might explode!
  34. *************************************************************************/
  35.  
  36. $meta_boxes = array();
  37. $meta_boxes = apply_filters ( 'cmb_meta_boxes' , $meta_boxes );
  38. foreach ( $meta_boxes as $meta_box ) {
  39.     $my_box = new cmb_Meta_Box( $meta_box );
  40. }
  41.  
  42. /**
  43.  * Validate value of meta fields
  44.  * Define ALL validation methods inside this class and use the names of these
  45.  * methods in the definition of meta boxes (key 'validate_func' of each field)
  46.  */
  47. class cmb_Meta_Box_Validate {
  48.     function check_text( $text ) {
  49.         if ($text != 'hello') {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54. }
  55.  
  56. /**
  57.  * Defines the url to which is used to load local resources.
  58.  * This may need to be filtered for local Window installations.
  59.  * If resources do not load, please check the wiki for details.
  60.  */
  61. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  62.        //winblows
  63.     define( 'CMB_META_BOX_URL', trailingslashit( str_replace( DIRECTORY_SEPARATOR, '/', str_replace( str_replace( '/', DIRECTORY_SEPARATOR, WP_CONTENT_DIR ), WP_CONTENT_URL, dirname(__FILE__) ) ) ) );
  64.  
  65. } else {
  66.   define('CMB_META_BOX_URL', apply_filters('cmb_meta_box_url',
  67.     trailingslashit(str_replace(
  68.       array(WP_CONTENT_DIR, WP_PLUGIN_DIR),
  69.       array(WP_CONTENT_URL, WP_PLUGIN_URL),
  70.       dirname( __FILE__ )
  71.     ))
  72.   ));
  73. }
  74.  
  75. /**
  76.  * Create meta boxes
  77.  */
  78. class cmb_Meta_Box {
  79.     protected $_meta_box;
  80.  
  81.     function __construct( $meta_box ) {
  82.         if ( !is_admin() ) return;
  83.  
  84.         $this->_meta_box = $meta_box;
  85.  
  86.         $upload = false;
  87.         foreach ( $meta_box['fields'] as $field ) {
  88.             if ( $field['type'] == 'file' || $field['type'] == 'file_list' ) {
  89.                 $upload = true;
  90.                 break;
  91.             }
  92.         }
  93.  
  94.         global $pagenow;
  95.         if ( $upload && in_array( $pagenow, array( 'page.php', 'page-new.php', 'post.php', 'post-new.php' ) ) ) {
  96.             add_action( 'admin_head', array( &$this, 'add_post_enctype' ) );
  97.         }
  98.  
  99.         add_action( 'admin_menu', array( &$this, 'add' ) );
  100.         add_action( 'save_post', array( &$this, 'save' ), 10, 2 );
  101.  
  102.         add_filter( 'cmb_show_on', array( &$this, 'add_for_id' ), 10, 2 );
  103.         add_filter( 'cmb_show_on', array( &$this, 'add_for_page_template' ), 10, 2 );
  104.     }
  105.  
  106.     function add_post_enctype() {
  107.         echo '
  108.         <script type="text/javascript">
  109.         jQuery(document).ready(function(){
  110.             jQuery("#post").attr("enctype", "multipart/form-data");
  111.             jQuery("#post").attr("encoding", "multipart/form-data");
  112.         });
  113.         </script>';
  114.     }
  115.  
  116.     // Add metaboxes
  117.     function add() {
  118.         $this->_meta_box['context'] = empty($this->_meta_box['context']) ? 'normal' : $this->_meta_box['context'];
  119.         $this->_meta_box['priority'] = empty($this->_meta_box['priority']) ? 'high' : $this->_meta_box['priority'];
  120.         $this->_meta_box['show_on'] = empty( $this->_meta_box['show_on'] ) ? array('key' => false, 'value' => false) : $this->_meta_box['show_on'];
  121.  
  122.         foreach ( $this->_meta_box['pages'] as $page ) {
  123.             if( apply_filters( 'cmb_show_on', true, $this->_meta_box ) )
  124.                 add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']) ;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Show On Filters
  130.      * Use the 'cmb_show_on' filter to further refine the conditions under which a metabox is displayed.
  131.      * Below you can limit it by ID and page template
  132.      */
  133.  
  134.     // Add for ID
  135.     function add_for_id( $display, $meta_box ) {
  136.         if ( 'id' !== $meta_box['show_on']['key'] )
  137.             return $display;
  138.  
  139.         // If we're showing it based on ID, get the current ID
  140.         if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
  141.         elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
  142.         if( !isset( $post_id ) )
  143.             return false;
  144.  
  145.         // If value isn't an array, turn it into one
  146.         $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
  147.  
  148.         // If current page id is in the included array, display the metabox
  149.  
  150.         if ( in_array( $post_id, $meta_box['show_on']['value'] ) )
  151.             return true;
  152.         else
  153.             return false;
  154.     }
  155.  
  156.     // Add for Page Template
  157.     function add_for_page_template( $display, $meta_box ) {
  158.         if( 'page-template' !== $meta_box['show_on']['key'] )
  159.             return $display;
  160.  
  161.         // Get the current ID
  162.         if( isset( $_GET['post'] ) ) $post_id = $_GET['post'];
  163.         elseif( isset( $_POST['post_ID'] ) ) $post_id = $_POST['post_ID'];
  164.         if( !( isset( $post_id ) || is_page() ) ) return false;
  165.  
  166.         // Get current template
  167.         $current_template = get_post_meta( $post_id, '_wp_page_template', true );
  168.  
  169.         // If value isn't an array, turn it into one
  170.         $meta_box['show_on']['value'] = !is_array( $meta_box['show_on']['value'] ) ? array( $meta_box['show_on']['value'] ) : $meta_box['show_on']['value'];
  171.  
  172.         // See if there's a match
  173.         if( in_array( $current_template, $meta_box['show_on']['value'] ) )
  174.             return true;
  175.         else
  176.             return false;
  177.     }
  178.  
  179.     // Show fields
  180.     function show() {
  181.  
  182.         global $post;
  183.  
  184.         // Use nonce for verification
  185.         echo '<input type="hidden" name="wp_meta_box_nonce" value="', wp_create_nonce( basename(__FILE__) ), '" />';
  186. foreach ( $this->_meta_box['fields'] as $field ) {
  187.         switch ( $field['name'] ) {
  188.         case 'Test Select':
  189.             if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  190.             echo '<div class="page-select-element-list-wrapper combobox">';
  191.             echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  192.             foreach ($field['options'] as $option) {
  193.                 echo '<option>'.$option['name'].'</option>';
  194.             }
  195.             echo '</select>';
  196.             echo '</div>';
  197.             echo '<input type="button" value="Add item" class="page-add-item-button" id="page-add-item-button">';
  198.             echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  199.             // Start of items to clone 
  200.             break;
  201.         }
  202.     }
  203.  
  204.  
  205.         // Start items to clone wrapper
  206.         echo '<div id="page-element-lists" class="page-element-lists">';
  207.         $value = get_post_meta( $post->ID, 'page-selected-elements', true );
  208.         echo $value;
  209.         $mydata = ( $_POST['page-selected-elements'] );
  210.         echo $mydata;
  211.  
  212.         foreach ( $this->_meta_box['fields'] as $field ) {
  213.             // Set up blank or default values for empty ones
  214.             if ( !isset( $field['name'] ) ) $field['name'] = '';
  215.             if ( !isset( $field['desc'] ) ) $field['desc'] = '';
  216.             $field['std'] = apply_filters( 'cmb_std_filter', ( isset( $field['std'] ) ? $field['std'] : '' ), $field );
  217.             if ( 'file' == $field['type'] && !isset( $field['allow'] ) ) $field['allow'] = array( 'url', 'attachment' );
  218.             if ( 'file' == $field['type'] && !isset( $field['save_id'] ) )  $field['save_id']  = false;
  219.             if ( 'multicheck' == $field['type'] ) $field['multiple'] = true;
  220.  
  221.             $meta = get_post_meta( $post->ID, $field['id'], 'multicheck' != $field['type'] /* If multicheck this can be multiple values */ );
  222.  
  223.             switch ( $field['name'] ) {
  224.             // My custom fields
  225.                 case 'Divider':
  226.                     echo '
  227.                     <div id="page-element" class="page-element element1-1" rel="'.$field['name'].'">
  228.                     <div class="page-element-item">
  229.                         <div class="left-side">
  230.                             <input type="button" class="increase-size" value="+"/>
  231.                             <input type="button" class="decrease-size" value="-"/>
  232.                             <span class="element-description">'.$field['name'].'</span>
  233.                         </div>
  234.                         <input type="hidden" name="" value="Tab" class="page-option-item-type" id="page-option-item-type">
  235.                         <input type="hidden" name="" value="element1-4" class="page-option-item-size" id="page-option-item-size">
  236.                         <div class="right-side">
  237.                             <span class="element-size-text" id="element-size-text">'.$field['size'].'</span>
  238.                             <div class="change-element-property">
  239.                                         <a title="Edit"><div class="edit-element" id="page-element-edit-box" rel="edit-box"></div></a>
  240.                                         <a title="Delete"><div id="delete-element" class="remove-element"></div></a>
  241.                             </div>
  242.                         </div>
  243.                     </div>
  244.                     <div class="page-element-edit" id="my_modal">
  245.                     <p>'.$field['name'].'</p>
  246.                     <input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '"/>
  247.                     </div>
  248.                     </div>';
  249.                     break;
  250.                 case 'Quote':
  251.                     echo '
  252.                     <div id="page-element" class="page-element element1-4" rel="'.$field['name'].'">
  253.                     <div class="page-element-item">
  254.                         <div class="left-side">
  255.                             <input type="button" class="increase-size" value="+"/>
  256.                             <input type="button" class="decrease-size" value="-"/>
  257.                             <span class="element-description">'.$field['name'].'</span>
  258.                         </div>
  259.                         <input type="hidden" name="" value="Tab" class="page-option-item-type" id="page-option-item-type">
  260.                         <input type="hidden" name="" value="element1-4" class="page-option-item-size" id="page-option-item-size">
  261.                         <div class="right-side">
  262.                             <span class="element-size-text" id="element-size-text">'.$field['size'].'</span>
  263.                             <div class="change-element-property">
  264.                                         <a title="Edit"><div class="edit-element" id="page-element-edit-box" rel="edit-box"></div></a>
  265.                                         <a title="Delete"><div id="delete-element" class="remove-element"></div></a>
  266.                             </div>
  267.                         </div>
  268.                     </div>
  269.                     <div class="page-element-edit" id="my_modal">
  270.                     <p>'.$field['name'].'</p>
  271.                     <input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '"/>
  272.                     </div>
  273.                     </div>';
  274.                     break;
  275.                 case 'Column':
  276.                     echo '
  277.                     <div id="page-element" class="page-element element1-4" rel="'.$field['name'].'">
  278.                     <div class="page-element-item">
  279.                         <div class="left-side">
  280.                             <input type="button" class="increase-size" value="+"/>
  281.                             <input type="button" class="decrease-size" value="-"/>
  282.                             <span class="element-description">'.$field['name'].'</span>
  283.                         </div>
  284.                         <input type="hidden" name="" value="Tab" class="page-option-item-type" id="page-option-item-type">
  285.                         <input type="hidden" name="" value="element1-4" class="page-option-item-size" id="page-option-item-size">
  286.                         <div class="right-side">
  287.                             <span class="element-size-text" id="element-size-text">'.$field['size'].'</span>
  288.                             <div class="change-element-property">
  289.                                         <a title="Edit"><div class="edit-element" id="page-element-edit-box" rel="edit-box"></div></a>
  290.                                         <a title="Delete"><div id="delete-element" class="remove-element"></div></a>
  291.                             </div>
  292.                         </div>
  293.                     </div>
  294.                     <div class="page-element-edit" id="my_modal">
  295.                     <p>'.$field['name'].'</p>
  296.                     <input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '"/>
  297.                     </div>
  298.                     </div>';
  299.                     break;
  300. // End My custom fields            
  301.                 case 'text':
  302.                     echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" />','<p class="cmb_metabox_description">', $field['desc'], '</p>';
  303.                     break;
  304.                 case 'text_small':
  305.                     echo '<input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  306.                     break;
  307.                 case 'text_medium':
  308.                     echo '<input class="cmb_text_medium" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  309.                     break;
  310.                 case 'text_date':
  311.                     echo '<input class="cmb_text_small cmb_datepicker" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  312.                     break;
  313.                 case 'text_date_timestamp':
  314.                     echo '<input class="cmb_text_small cmb_datepicker" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? date( 'm\/d\/Y', $meta ) : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  315.                     break;
  316.  
  317.                 case 'text_datetime_timestamp':
  318.                     echo '<input class="cmb_text_small cmb_datepicker" type="text" name="', $field['id'], '[date]" id="', $field['id'], '_date" value="', '' !== $meta ? date( 'm\/d\/Y', $meta ) : $field['std'], '" />';
  319.                     echo '<input class="cmb_timepicker text_time" type="text" name="', $field['id'], '[time]" id="', $field['id'], '_time" value="', '' !== $meta ? date( 'h:i A', $meta ) : $field['std'], '" /><span class="cmb_metabox_description" >', $field['desc'], '</span>';
  320.                     break;
  321.                 case 'text_time':
  322.                     echo '<input class="cmb_timepicker text_time" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  323.                     break;
  324.                 case 'text_money':
  325.                     echo ! empty( $field['before'] ) ? '' : '$', ' <input class="cmb_text_money" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  326.                     break;
  327.                 case 'colorpicker':
  328.                     $meta = '' !== $meta ? $meta : $field['std'];
  329.                     $hex_color = '(([a-fA-F0-9]){3}){1,2}$';
  330.                     if ( preg_match( '/^' . $hex_color . '/i', $meta ) ) // Value is just 123abc, so prepend #.
  331.                         $meta = '#' . $meta;
  332.                     elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta ) ) // Value doesn't match #123abc, so sanitize to just #.
  333.                         $meta = "#";
  334.                     echo '<input class="cmb_colorpicker cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta, '" /><span class="cmb_metabox_description">', $field['desc'], '</span>';
  335.                     break;
  336.                 case 'textarea':
  337.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="10">', '' !== $meta ? $meta : $field['std'], '</textarea>','<p class="cmb_metabox_description">', $field['desc'], '</p>';
  338.                     break;
  339.                 case 'textarea_small':
  340.  
  341.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4">', '' !== $meta ? $meta : $field['std'], '</textarea>','<p class="cmb_metabox_description">', $field['desc'], '</p>';
  342.                     break;
  343.                 case 'textarea_code':
  344.                     echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="10" class="cmb_textarea_code">', '' !== $meta ? $meta : $field['std'], '</textarea>','<p class="cmb_metabox_description">', $field['desc'], '</p>';
  345.                     break;
  346.                 case 'select':
  347.                     if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  348.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  349.                     foreach ($field['options'] as $option) {
  350.                         echo '<option value="', $option['value'], '"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
  351.                     }
  352.                     echo '</select>';
  353.                     echo '<input type="button" value="Add item" class="page-add-item-button" id="page-add-item-button">';
  354.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  355.                     break;
  356.                 case 'radio_inline':
  357.                     if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  358.                     echo '<div class="cmb_radio_inline">';
  359.                     $i = 1;
  360.                     foreach ($field['options'] as $option) {
  361.                         echo '<div class="cmb_radio_inline_option"><input type="radio" name="', $field['id'], '" id="', $field['id'], $i, '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /><label for="', $field['id'], $i, '">', $option['name'], '</label></div>';
  362.                         $i++;
  363.                     }
  364.                     echo '</div>';
  365.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  366.                     break;
  367.                 case 'radio':
  368.                     if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  369.                     echo '<ul>';
  370.                     $i = 1;
  371.                     foreach ($field['options'] as $option) {
  372.                         echo '<li><input type="radio" name="', $field['id'], '" id="', $field['id'], $i,'" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /><label for="', $field['id'], $i, '">', $option['name'].'</label></li>';
  373.                         $i++;
  374.                     }
  375.                     echo '</ul>';
  376.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  377.                     break;
  378.                 case 'checkbox':
  379.                     echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  380.                     echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
  381.                     break;
  382.                 case 'multicheck':
  383.                     echo '<ul>';
  384.                     $i = 1;
  385.                     foreach ( $field['options'] as $value => $name ) {
  386.                         // Append `[]` to the name to get multiple values
  387.                         // Use in_array() to check whether the current option should be checked
  388.                         echo '<li><input type="checkbox" name="', $field['id'], '[]" id="', $field['id'], $i, '" value="', $value, '"', in_array( $value, $meta ) ? ' checked="checked"' : '', ' /><label for="', $field['id'], $i, '">', $name, '</label></li>';
  389.                         $i++;
  390.                     }
  391.                     echo '</ul>';
  392.                     echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
  393.                     break;
  394.                 case 'title':
  395.                     echo '<h5 class="cmb_metabox_title">', $field['name'], '</h5>';
  396.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  397.                     break;
  398.                 case 'wysiwyg':
  399.                     wp_editor( $meta ? $meta : $field['std'], $field['id'], isset( $field['options'] ) ? $field['options'] : array() );
  400.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  401.                     break;
  402.                 case 'taxonomy_select':
  403.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  404.                     $names= wp_get_object_terms( $post->ID, $field['taxonomy'] );
  405.                     $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
  406.                     foreach ( $terms as $term ) {
  407.                         if (!is_wp_error( $names ) && !empty( $names ) && !strcmp( $term->slug, $names[0]->slug ) ) {
  408.                             echo '<option value="' . $term->slug . '" selected>' . $term->name . '</option>';
  409.                         } else {
  410.                             echo '<option value="' . $term->slug . '  ' , $meta == $term->slug ? $meta : ' ' ,'  ">' . $term->name . '</option>';
  411.                         }
  412.                     }
  413.                     echo '</select>';
  414.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  415.                     break;
  416.                 case 'taxonomy_radio':
  417.                     $names= wp_get_object_terms( $post->ID, $field['taxonomy'] );
  418.                     $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
  419.                     echo '<ul>';
  420.                     foreach ( $terms as $term ) {
  421.                         if ( !is_wp_error( $names ) && !empty( $names ) && !strcmp( $term->slug, $names[0]->slug ) ) {
  422.                             echo '<li><input type="radio" name="', $field['id'], '" value="'. $term->slug . '" checked>' . $term->name . '</li>';
  423.                         } else {
  424.                             echo '<li><input type="radio" name="', $field['id'], '" value="' . $term->slug . '  ' , $meta == $term->slug ? $meta : ' ' ,'  ">' . $term->name .'</li>';
  425.                         }
  426.                     }
  427.                     echo '</ul>';
  428.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  429.                     break;
  430.                 case 'taxonomy_multicheck':
  431.                     echo '<ul>';
  432.                     $names = wp_get_object_terms( $post->ID, $field['taxonomy'] );
  433.                     $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
  434.                     foreach ($terms as $term) {
  435.                         echo '<li><input type="checkbox" name="', $field['id'], '[]" id="', $field['id'], '" value="', $term->name , '"';
  436.                         foreach ($names as $name) {
  437.                             if ( $term->slug == $name->slug ){ echo ' checked="checked" ';};
  438.                         }
  439.                         echo' /><label>', $term->name , '</label></li>';
  440.                     }
  441.                     echo '</ul>';
  442.                     echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
  443.                 break;
  444.                 case 'file_list':
  445.                     echo '<input class="cmb_upload_file" type="text" size="36" name="', $field['id'], '" value="" />';
  446.                     echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
  447.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  448.                         $args = array(
  449.                                 'post_type' => 'attachment',
  450.                                 'numberposts' => null,
  451.                                 'post_status' => null,
  452.                                 'post_parent' => $post->ID
  453.                             );
  454.                             $attachments = get_posts($args);
  455.                             if ($attachments) {
  456.                                 echo '<ul class="attach_list">';
  457.                                 foreach ($attachments as $attachment) {
  458.                                     echo '<li>'.wp_get_attachment_link($attachment->ID, 'thumbnail', 0, 0, 'Download');
  459.                                     echo '<span>';
  460.                                     echo apply_filters('the_title', '&nbsp;'.$attachment->post_title);
  461.                                     echo '</span></li>';
  462.                                 }
  463.                                 echo '</ul>';
  464.                             }
  465.                         break;
  466.                 case 'file':
  467.                     $input_type_url = "hidden";
  468.                     if ( 'url' == $field['allow'] || ( is_array( $field['allow'] ) && in_array( 'url', $field['allow'] ) ) )
  469.                         $input_type_url="text";
  470.                     echo '<input class="cmb_upload_file" type="' . $input_type_url . '" size="45" id="', $field['id'], '" name="', $field['id'], '" value="', $meta, '" />';
  471.                     echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
  472.                     echo '<input class="cmb_upload_file_id" type="hidden" id="', $field['id'], '_id" name="', $field['id'], '_id" value="', get_post_meta( $post->ID, $field['id'] . "_id",true), '" />';
  473.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  474.                     echo '<div id="', $field['id'], '_status" class="cmb_media_status">';
  475.                         if ( ! empty( $meta ) ) {
  476.  
  477.                             $parsed = @parse_url( $meta, PHP_URL_PATH );
  478.                             $file_ext = $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
  479.                             $valid = (array) apply_filters( 'cmb_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif', 'ico', 'icon' ) );
  480.  
  481.                             if ( $file_ext && in_array( $file_ext, $valid ) ) {
  482.                                 echo '<div class="img_status">';
  483.                                 echo '<img src="', $meta, '" alt="" />';
  484.                                 echo '<a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove Image</a>';
  485.                                 echo '</div>';
  486.                             } else {
  487.                                 $parts = explode( '/', $meta );
  488.                                 for( $i = 0; $i < count( $parts ); ++$i ) {
  489.                                     $title = $parts[$i];
  490.                                 }
  491.                                 echo 'File: <strong>', $title, '</strong>&nbsp;&nbsp;&nbsp; (<a href="', $meta, '" target="_blank" rel="external">Download</a> / <a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove</a>)';
  492.                             }
  493.                         }
  494.                     echo '</div>';
  495.                 break;
  496.                 case 'oembed':
  497.                     echo '<input class="cmb_oembed" type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" />','<p class="cmb_metabox_description">', $field['desc'], '</p>';
  498.                     echo '<p class="cmb-spinner spinner"></p>';
  499.                     echo '<div id="', $field['id'], '_status" class="cmb_media_status ui-helper-clearfix embed_wrap">';
  500.                         if ( $meta != '' ) {
  501.                             $check_embed = $GLOBALS['wp_embed']->run_shortcode( '[embed]'. esc_url( $meta ) .'[/embed]' );
  502.                             if ( $check_embed ) {
  503.                                 echo '<div class="embed_status">';
  504.                                 echo $check_embed;
  505.                                 echo '<a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove Embed</a>';
  506.                                 echo '</div>';
  507.                             } else {
  508.                                 echo 'URL is not a valid oEmbed URL.';
  509.                             }
  510.                         }
  511.                     echo '</div>';
  512.  
  513.                     break;
  514.  
  515.                 default:
  516.                     do_action('cmb_render_' . $field['type'] , $field, $meta);
  517.  
  518.                    
  519.             }
  520.  
  521.         }
  522.             // Clost clone items wrapper
  523.             echo '</div>';
  524.             echo'<div class="page-methodology">
  525.                 <div class="page-selected-elements-wrapper">
  526.                 <div id="page-selected-elements" name="page-selected-elements" class="page-selected-elements page-no-sidebar ui-sortable">';
  527.                 // Trying to save everything that gets added inside here using js. Then retreive that data once its saving to show on page edit screen.
  528.                 ?>
  529.                 <?php
  530.                 $values = get_post_custom( $post->ID );
  531.                 $selected = isset( $values['added-page-element'] ) ? $values['added-page-element'][0] : '';
  532.                 $recipe_url = isset( $values['added-page-element-two'] ) ? $values['added-page-element-two'][0] : '';
  533.                 echo $selected;
  534.                 ?>
  535.                 <!-- Test this one works -->
  536.                 <input type="text" name="added-page-element-two" id="added-page-element-two" value=<?php echo $recipe_url;?>></input>
  537.                 <?php
  538.                 echo'
  539.                 </div>
  540.                 </div>
  541.                 </div>';
  542.     }
  543.  
  544.     // Save data from metabox
  545.     function save( $post_id, $post )  {
  546.  
  547.         // verify nonce
  548.         if ( ! isset( $_POST['wp_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['wp_meta_box_nonce'], basename(__FILE__) ) ) {
  549.             return $post_id;
  550.         }
  551.  
  552.         // check autosave
  553.         if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  554.             return $post_id;
  555.         }
  556.  
  557.         // check permissions
  558.         if ( 'page' == $_POST['post_type'] ) {
  559.             if ( !current_user_can( 'edit_page', $post_id ) ) {
  560.                 return $post_id;
  561.             }
  562.         } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
  563.             return $post_id;
  564.         }
  565.  
  566.         // get the post types applied to the metabox group
  567.         // and compare it to the post type of the content
  568.         $post_type = $post->post_type;
  569.         $meta_type = $this->_meta_box['pages'];
  570.         $type_comp = in_array($post_type, $meta_type) ? true : false;
  571.  
  572.         foreach ( $this->_meta_box['fields'] as $field ) {
  573.             $name = $field['id'];
  574.  
  575.             if ( ! isset( $field['multiple'] ) )
  576.                 $field['multiple'] = ( 'multicheck' == $field['type'] ) ? true : false;
  577.  
  578.             $old = get_post_meta( $post_id, $name, !$field['multiple'] /* If multicheck this can be multiple values */ );
  579.             $new = isset( $_POST[$field['id']] ) ? $_POST[$field['id']] : null;
  580.  
  581.             if ( $type_comp == true && in_array( $field['type'], array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_multicheck' ) ) )  {
  582.                 $new = wp_set_object_terms( $post_id, $new, $field['taxonomy'] );
  583.             }
  584.  
  585.             if ( ($field['type'] == 'textarea') || ($field['type'] == 'textarea_small') ) {
  586.                 $new = htmlspecialchars( $new );
  587.             }
  588.  
  589.             if ( ($field['type'] == 'textarea_code') ) {
  590.                 $new = htmlspecialchars_decode( $new );
  591.             }
  592.  
  593.             if ( $type_comp == true && $field['type'] == 'text_date_timestamp' ) {
  594.                 $new = strtotime( $new );
  595.             }
  596.  
  597.             if ( $type_comp == true && $field['type'] == 'text_datetime_timestamp' ) {
  598.                 $string = $new['date'] . ' ' . $new['time'];
  599.                 $new = strtotime( $string );
  600.             }
  601.  
  602.             $new = apply_filters('cmb_validate_' . $field['type'], $new, $post_id, $field);
  603.  
  604.             // validate meta value
  605.             if ( isset( $field['validate_func']) ) {
  606.                 $ok = call_user_func( array( 'cmb_Meta_Box_Validate', $field['validate_func']), $new );
  607.                 if ( $ok === false ) { // pass away when meta value is invalid
  608.                     continue;
  609.                 }
  610.             } elseif ( $field['multiple'] ) {
  611.                 delete_post_meta( $post_id, $name );
  612.                 if ( !empty( $new ) ) {
  613.                     foreach ( $new as $add_new ) {
  614.                         add_post_meta( $post_id, $name, $add_new, false );
  615.                     }
  616.                 }
  617.             } elseif ( '' !== $new && $new != $old  ) {
  618.                 update_post_meta( $post_id, $name, $new );
  619.             } elseif ( '' == $new ) {
  620.                 delete_post_meta( $post_id, $name );
  621.             }
  622.  
  623.             if ( 'file' == $field['type'] ) {
  624.                 $name = $field['id'] . "_id";
  625.                 $old = get_post_meta( $post_id, $name, !$field['multiple'] /* If multicheck this can be multiple values */ );
  626.                 if ( isset( $field['save_id'] ) && $field['save_id'] ) {
  627.                     $new = isset( $_POST[$name] ) ? $_POST[$name] : null;
  628.                 } else {
  629.                     $new = "";
  630.                 }
  631.  
  632.                 if ( $new && $new != $old ) {
  633.                     update_post_meta( $post_id, $name, $new );
  634.                 } elseif ( '' == $new && $old ) {
  635.                     delete_post_meta( $post_id, $name, $old );
  636.                 }
  637.             }
  638.  
  639.         // Update the meta field in the database.
  640.         $doc = new DomDocument();
  641.         $thediv = $doc->getElementById('added-page-element');
  642.         $content =  $thediv->textContent;
  643.  
  644.    
  645.         if( isset( $_POST['added-page-element'] ) )
  646.         update_post_meta( $post_id, 'added-page-element', $content );
  647.  
  648.  
  649.  
  650.         if( isset( $_POST['added-page-element-two'] ) )
  651.         update_post_meta( $post_id, 'added-page-element-two', $_POST['added-page-element-two'] );
  652.         }
  653.  
  654.  
  655.  
  656.  
  657.  
  658.     }
  659.  
  660.  
  661. }
  662.  
  663. /**
  664.  * Adding scripts and styles
  665.  */
  666. function cmb_scripts( $hook ) {
  667.     global $wp_version;
  668.     // only enqueue our scripts/styles on the proper pages
  669.     if ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'page-new.php' || $hook == 'page.php' ) {
  670.         // scripts required for cmb
  671.         $cmb_script_array = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox' );
  672.         // styles required for cmb
  673.         $cmb_style_array = array( 'thickbox' );
  674.         // if we're 3.5 or later, user wp-color-picker
  675.         if ( 3.5 <= $wp_version ) {
  676.             $cmb_script_array[] = 'wp-color-picker';
  677.             $cmb_style_array[] = 'wp-color-picker';
  678.         } else {
  679.             // otherwise use the older 'farbtastic'
  680.             $cmb_script_array[] = 'farbtastic';
  681.             $cmb_style_array[] = 'farbtastic';
  682.         }
  683.         wp_register_script( 'cmb-timepicker', CMB_META_BOX_URL . 'js/jquery.timePicker.min.js' );
  684.         wp_register_script( 'cmb-scripts', CMB_META_BOX_URL . 'js/cmb.js', $cmb_script_array, '0.9.4' );
  685.         wp_localize_script( 'cmb-scripts', 'cmb_ajax_data', array( 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), 'post_id' => get_the_ID() ) );
  686.         wp_enqueue_script( 'cmb-timepicker' );
  687.         wp_enqueue_script( 'cmb-scripts' );
  688.         wp_register_style( 'cmb-styles', CMB_META_BOX_URL . 'style.css', $cmb_style_array );
  689.         wp_enqueue_style( 'cmb-styles' );
  690.     }
  691. }
  692. add_action( 'admin_enqueue_scripts', 'cmb_scripts', 10 );
  693.  
  694. function cmb_editor_footer_scripts() { ?>
  695.     <?php
  696.     if ( isset( $_GET['cmb_force_send'] ) && 'true' == $_GET['cmb_force_send'] ) {
  697.         $label = $_GET['cmb_send_label'];
  698.         if ( empty( $label ) ) $label="Select File";
  699.         ?>
  700.         <script type="text/javascript">
  701.         jQuery(function($) {
  702.             $('td.savesend input').val('<?php echo $label; ?>');
  703.         });
  704.         </script>
  705.         <?php
  706.     }
  707. }
  708. add_action( 'admin_print_footer_scripts', 'cmb_editor_footer_scripts', 99 );
  709.  
  710. // Force 'Insert into Post' button from Media Library
  711. add_filter( 'get_media_item_args', 'cmb_force_send' );
  712. function cmb_force_send( $args ) {
  713.  
  714.     // if the Gallery tab is opened from a custom meta box field, add Insert Into Post button
  715.     if ( isset( $_GET['cmb_force_send'] ) && 'true' == $_GET['cmb_force_send'] )
  716.         $args['send'] = true;
  717.  
  718.     // if the From Computer tab is opened AT ALL, add Insert Into Post button after an image is uploaded
  719.     if ( isset( $_POST['attachment_id'] ) && '' != $_POST["attachment_id"] ) {
  720.  
  721.         $args['send'] = true;
  722.  
  723.         // TO DO: Are there any conditions in which we don't want the Insert Into Post
  724.         // button added? For example, if a post type supports thumbnails, does not support
  725.         // the editor, and does not have any cmb file inputs? If so, here's the first
  726.         // bits of code needed to check all that.
  727.         // $attachment_ancestors = get_post_ancestors( $_POST["attachment_id"] );
  728.         // $attachment_parent_post_type = get_post_type( $attachment_ancestors[0] );
  729.         // $post_type_object = get_post_type_object( $attachment_parent_post_type );
  730.     }
  731.  
  732.     // change the label of the button on the From Computer tab
  733.     if ( isset( $_POST['attachment_id'] ) && '' != $_POST["attachment_id"] ) {
  734.  
  735.         echo '
  736.             <script type="text/javascript">
  737.                 function cmbGetParameterByNameInline(name) {
  738.                     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  739.                     var regexS = "[\\?&]" + name + "=([^&#]*)";
  740.                     var regex = new RegExp(regexS);
  741.                     var results = regex.exec(window.location.href);
  742.                     if(results == null)
  743.                         return "";
  744.                     else
  745.                         return decodeURIComponent(results[1].replace(/\+/g, " "));
  746.                 }
  747.  
  748.                 jQuery(function($) {
  749.                     if (cmbGetParameterByNameInline("cmb_force_send")=="true") {
  750.                         var cmb_send_label = cmbGetParameterByNameInline("cmb_send_label");
  751.                         $("td.savesend input").val(cmb_send_label);
  752.                     }
  753.                 });
  754.             </script>
  755.         ';
  756.     }
  757.  
  758.     return $args;
  759.  
  760. }
  761.  
  762. add_action( 'wp_ajax_cmb_oembed_handler', 'cmb_oembed_ajax_results' );
  763. /**
  764.  * Handles our oEmbed ajax request
  765.  */
  766. function cmb_oembed_ajax_results() {
  767.  
  768.     // verify our nonce
  769.     if ( ! ( isset( $_REQUEST['cmb_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb_ajax_nonce'], 'ajax_nonce' ) ) )
  770.         die();
  771.  
  772.     // sanitize our search string
  773.     $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] );
  774.  
  775.     if ( empty( $oembed_string ) ) {
  776.         $return = '<p class="ui-state-error-text">'. __( 'Please Try Again', 'cmb' ) .'</p>';
  777.         $found = 'not found';
  778.     } else {
  779.  
  780.         global $wp_embed;
  781.  
  782.         $oembed_url = esc_url( $oembed_string );
  783.         // Post ID is needed to check for embeds
  784.         if ( isset( $_REQUEST['post_id'] ) )
  785.             $GLOBALS['post'] = get_post( $_REQUEST['post_id'] );
  786.         // Set width of embed
  787.         $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640';
  788.         // ping WordPress for an embed
  789.         $check_embed = $wp_embed->run_shortcode( '[embed width="'. $embed_width .'"]'. $oembed_url .'[/embed]' );
  790.         // fallback that WordPress creates when no oEmbed was found
  791.         $fallback = $wp_embed->maybe_make_link( $oembed_url );
  792.  
  793.         if ( $check_embed && $check_embed != $fallback ) {
  794.             // Embed data
  795.             $return = '<div class="embed_status">'. $check_embed .'<a href="#" class="cmb_remove_file_button" rel="'. $_REQUEST['field_id'] .'">'. __( 'Remove Embed', 'cmb' ) .'</a></div>';
  796.             // set our response id
  797.             $found = 'found';
  798.  
  799.         } else {
  800.             // error info when no oEmbeds were found
  801.             $return = '<p class="ui-state-error-text">'.sprintf( __( 'No oEmbed Results Found for %s. View more info at', 'cmb' ), $fallback ) .' <a href="http://codex.wordpress.org/Embeds" target="_blank">codex.wordpress.org/Embeds</a>.</p>';
  802.             // set our response id
  803.             $found = 'not found';
  804.         }
  805.     }
  806.  
  807.     // send back our encoded data
  808.     echo json_encode( array( 'result' => $return, 'id' => $found ) );
  809.     die();
  810. }
  811.  
  812. // End. That's it, folks! //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement