Advertisement
bowenac

Untitled

Oct 20th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.80 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.         foreach ( $this->_meta_box['fields'] as $field ) {
  208.             // Set up blank or default values for empty ones
  209.             if ( !isset( $field['name'] ) ) $field['name'] = '';
  210.             if ( !isset( $field['desc'] ) ) $field['desc'] = '';
  211.             $field['std'] = apply_filters( 'cmb_std_filter', ( isset( $field['std'] ) ? $field['std'] : '' ), $field );
  212.             if ( 'file' == $field['type'] && !isset( $field['allow'] ) ) $field['allow'] = array( 'url', 'attachment' );
  213.             if ( 'file' == $field['type'] && !isset( $field['save_id'] ) )  $field['save_id']  = false;
  214.             if ( 'multicheck' == $field['type'] ) $field['multiple'] = true;
  215.  
  216.             $meta = get_post_meta( $post->ID, $field['id'], 'multicheck' != $field['type'] /* If multicheck this can be multiple values */ );
  217.  
  218.             switch ( $field['name'] ) {
  219.             // My custom fields
  220.                 case 'Divider':
  221.                     echo '
  222.                     <div id="page-element" class="page-element element1-1" rel="'.$field['name'].'">
  223.                     <div class="page-element-item">
  224.                         <div class="left-side">
  225.                             <input type="button" class="increase-size" value="+"/>
  226.                             <input type="button" class="decrease-size" value="-"/>
  227.                             <span class="element-description">'.$field['name'].'</span>
  228.                         </div>
  229.                         <div class="right-side">
  230.                             <span class="element-size-text" id="element-size-text">'.$field['size'].'</span>
  231.                             <div class="change-element-property">
  232.                                         <input type="button" class="edit-element" id="page-element-edit-box" rel="edit-box" value="edit"></input>
  233.                                         <input type="button" id="delete-element" class="remove-element" value="Remove"></input>
  234.                             </div>
  235.                         </div>
  236.                     </div>
  237.                     <div class="page-element-edit" id="my_modal">
  238.                     <p>'.$field['name'].'</p>
  239.                     <input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '"/>
  240.                     </div>
  241.                     </div>';
  242.                     break;
  243.                 case 'Quote':
  244.                     echo '
  245.                     <div id="page-element" class="page-element element1-4" rel="'.$field['name'].'">
  246.                     <div class="page-element-item">
  247.                         <div class="left-side">
  248.                             <input type="button" class="increase-size" value="+"/>
  249.                             <input type="button" class="decrease-size" value="-"/>
  250.                             <span class="element-description">'.$field['name'].'</span>
  251.                         </div>
  252.                         <div class="right-side">
  253.                             <span class="element-size-text" id="element-size-text">'.$field['size'].'</span>
  254.                             <div class="change-element-property">
  255.                                         <input type="button" class="edit-element" id="page-element-edit-box" rel="edit-box" value="edit"></input>
  256.                                         <input type="button" id="delete-element" class="remove-element" value="Remove"></input>
  257.                             </div>
  258.                         </div>
  259.                     </div>
  260.                     <div class="page-element-edit" id="my_modal">
  261.                     <p>'.$field['name'].'</p>
  262.                     <input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '"/>
  263.                     </div>
  264.                     </div>';
  265.                     break;
  266.                 case 'Column':
  267.                     echo '
  268.                     <div id="page-element" class="page-element element1-4" rel="'.$field['name'].'">
  269.                     <div class="page-element-item">
  270.                         <div class="left-side">
  271.                             <input type="button" class="increase-size" value="+"/>
  272.                             <input type="button" class="decrease-size" value="-"/>
  273.                             <span class="element-description">'.$field['name'].'</span>
  274.                         </div>
  275.                         <div class="right-side">
  276.                             <span class="element-size-text" id="element-size-text">'.$field['size'].'</span>
  277.                             <div class="change-element-property">
  278.                                         <input type="button" class="edit-element" id="page-element-edit-box" rel="edit-box" value="edit"></input>
  279.                                         <input type="button" id="delete-element" class="remove-element" value="Remove"></input>
  280.                             </div>
  281.                         </div>
  282.                     </div>
  283.                     <div class="page-element-edit" id="my_modal">
  284.                     <p>'.$field['name'].'</p>
  285.                     <input class="cmb_text_small" type="text" name="', $field['id'], '" id="', $field['id'], '"/>
  286.                     </div>
  287.                     </div>';
  288.                     break;
  289. // End My custom fields            
  290.                 case 'text':
  291.                     echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', '' !== $meta ? $meta : $field['std'], '" />','<p class="cmb_metabox_description">', $field['desc'], '</p>';
  292.                     break;
  293.                 case 'text_small':
  294.                     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>';
  295.                     break;
  296.                 case 'text_medium':
  297.                     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>';
  298.                     break;
  299.                 case 'text_date':
  300.                     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>';
  301.                     break;
  302.                 case 'text_date_timestamp':
  303.                     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>';
  304.                     break;
  305.  
  306.                 case 'text_datetime_timestamp':
  307.                     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'], '" />';
  308.                     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>';
  309.                     break;
  310.                 case 'text_time':
  311.                     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>';
  312.                     break;
  313.                 case 'text_money':
  314.                     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>';
  315.                     break;
  316.                 case 'colorpicker':
  317.                     $meta = '' !== $meta ? $meta : $field['std'];
  318.                     $hex_color = '(([a-fA-F0-9]){3}){1,2}$';
  319.                     if ( preg_match( '/^' . $hex_color . '/i', $meta ) ) // Value is just 123abc, so prepend #.
  320.                         $meta = '#' . $meta;
  321.                     elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta ) ) // Value doesn't match #123abc, so sanitize to just #.
  322.                         $meta = "#";
  323.                     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>';
  324.                     break;
  325.                 case 'textarea':
  326.                     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>';
  327.                     break;
  328.                 case 'textarea_small':
  329.                     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>';
  330.                     break;
  331.                 case 'textarea_code':
  332.                     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>';
  333.                     break;
  334.                 case 'select':
  335.                     if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  336.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  337.                     foreach ($field['options'] as $option) {
  338.                         echo '<option value="', $option['value'], '"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
  339.                     }
  340.                     echo '</select>';
  341.                     echo '<input type="button" value="Add item" class="page-add-item-button" id="page-add-item-button">';
  342.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  343.                     break;
  344.                 case 'radio_inline':
  345.                     if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  346.                     echo '<div class="cmb_radio_inline">';
  347.                     $i = 1;
  348.                     foreach ($field['options'] as $option) {
  349.                         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>';
  350.                         $i++;
  351.                     }
  352.                     echo '</div>';
  353.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  354.                     break;
  355.                 case 'radio':
  356.                     if( empty( $meta ) && !empty( $field['std'] ) ) $meta = $field['std'];
  357.                     echo '<ul>';
  358.                     $i = 1;
  359.                     foreach ($field['options'] as $option) {
  360.                         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>';
  361.                         $i++;
  362.                     }
  363.                     echo '</ul>';
  364.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  365.                     break;
  366.                 case 'checkbox':
  367.                     echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  368.                     echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
  369.                     break;
  370.                 case 'multicheck':
  371.                     echo '<ul>';
  372.                     $i = 1;
  373.                     foreach ( $field['options'] as $value => $name ) {
  374.                         // Append `[]` to the name to get multiple values
  375.                         // Use in_array() to check whether the current option should be checked
  376.                         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>';
  377.                         $i++;
  378.                     }
  379.                     echo '</ul>';
  380.                     echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
  381.                     break;
  382.                 case 'title':
  383.                     echo '<h5 class="cmb_metabox_title">', $field['name'], '</h5>';
  384.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  385.                     break;
  386.                 case 'wysiwyg':
  387.                     wp_editor( $meta ? $meta : $field['std'], $field['id'], isset( $field['options'] ) ? $field['options'] : array() );
  388.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  389.                     break;
  390.                 case 'taxonomy_select':
  391.                     echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  392.                     $names= wp_get_object_terms( $post->ID, $field['taxonomy'] );
  393.                     $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
  394.                     foreach ( $terms as $term ) {
  395.                         if (!is_wp_error( $names ) && !empty( $names ) && !strcmp( $term->slug, $names[0]->slug ) ) {
  396.                             echo '<option value="' . $term->slug . '" selected>' . $term->name . '</option>';
  397.                         } else {
  398.                             echo '<option value="' . $term->slug . '  ' , $meta == $term->slug ? $meta : ' ' ,'  ">' . $term->name . '</option>';
  399.                         }
  400.                     }
  401.                     echo '</select>';
  402.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  403.                     break;
  404.                 case 'taxonomy_radio':
  405.                     $names= wp_get_object_terms( $post->ID, $field['taxonomy'] );
  406.                     $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
  407.                     echo '<ul>';
  408.                     foreach ( $terms as $term ) {
  409.                         if ( !is_wp_error( $names ) && !empty( $names ) && !strcmp( $term->slug, $names[0]->slug ) ) {
  410.                             echo '<li><input type="radio" name="', $field['id'], '" value="'. $term->slug . '" checked>' . $term->name . '</li>';
  411.                         } else {
  412.                             echo '<li><input type="radio" name="', $field['id'], '" value="' . $term->slug . '  ' , $meta == $term->slug ? $meta : ' ' ,'  ">' . $term->name .'</li>';
  413.                         }
  414.                     }
  415.                     echo '</ul>';
  416.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  417.                     break;
  418.                 case 'taxonomy_multicheck':
  419.                     echo '<ul>';
  420.                     $names = wp_get_object_terms( $post->ID, $field['taxonomy'] );
  421.                     $terms = get_terms( $field['taxonomy'], 'hide_empty=0' );
  422.                     foreach ($terms as $term) {
  423.                         echo '<li><input type="checkbox" name="', $field['id'], '[]" id="', $field['id'], '" value="', $term->name , '"';
  424.                         foreach ($names as $name) {
  425.                             if ( $term->slug == $name->slug ){ echo ' checked="checked" ';};
  426.                         }
  427.                         echo' /><label>', $term->name , '</label></li>';
  428.                     }
  429.                     echo '</ul>';
  430.                     echo '<span class="cmb_metabox_description">', $field['desc'], '</span>';
  431.                 break;
  432.                 case 'file_list':
  433.                     echo '<input class="cmb_upload_file" type="text" size="36" name="', $field['id'], '" value="" />';
  434.                     echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
  435.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  436.                         $args = array(
  437.                                 'post_type' => 'attachment',
  438.                                 'numberposts' => null,
  439.                                 'post_status' => null,
  440.                                 'post_parent' => $post->ID
  441.                             );
  442.                             $attachments = get_posts($args);
  443.                             if ($attachments) {
  444.                                 echo '<ul class="attach_list">';
  445.                                 foreach ($attachments as $attachment) {
  446.                                     echo '<li>'.wp_get_attachment_link($attachment->ID, 'thumbnail', 0, 0, 'Download');
  447.                                     echo '<span>';
  448.                                     echo apply_filters('the_title', '&nbsp;'.$attachment->post_title);
  449.                                     echo '</span></li>';
  450.                                 }
  451.                                 echo '</ul>';
  452.                             }
  453.                         break;
  454.                 case 'file':
  455.                     $input_type_url = "hidden";
  456.                     if ( 'url' == $field['allow'] || ( is_array( $field['allow'] ) && in_array( 'url', $field['allow'] ) ) )
  457.                         $input_type_url="text";
  458.                     echo '<input class="cmb_upload_file" type="' . $input_type_url . '" size="45" id="', $field['id'], '" name="', $field['id'], '" value="', $meta, '" />';
  459.                     echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
  460.                     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), '" />';
  461.                     echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
  462.                     echo '<div id="', $field['id'], '_status" class="cmb_media_status">';
  463.                         if ( ! empty( $meta ) ) {
  464.  
  465.                             $parsed = @parse_url( $meta, PHP_URL_PATH );
  466.                             $file_ext = $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
  467.                             $valid = (array) apply_filters( 'cmb_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif', 'ico', 'icon' ) );
  468.  
  469.                             if ( $file_ext && in_array( $file_ext, $valid ) ) {
  470.                                 echo '<div class="img_status">';
  471.                                 echo '<img src="', $meta, '" alt="" />';
  472.                                 echo '<a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove Image</a>';
  473.                                 echo '</div>';
  474.                             } else {
  475.                                 $parts = explode( '/', $meta );
  476.                                 for( $i = 0; $i < count( $parts ); ++$i ) {
  477.                                     $title = $parts[$i];
  478.                                 }
  479.                                 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>)';
  480.                             }
  481.                         }
  482.                     echo '</div>';
  483.                 break;
  484.                 case 'oembed':
  485.                     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>';
  486.                     echo '<p class="cmb-spinner spinner"></p>';
  487.                     echo '<div id="', $field['id'], '_status" class="cmb_media_status ui-helper-clearfix embed_wrap">';
  488.                         if ( $meta != '' ) {
  489.                             $check_embed = $GLOBALS['wp_embed']->run_shortcode( '[embed]'. esc_url( $meta ) .'[/embed]' );
  490.                             if ( $check_embed ) {
  491.                                 echo '<div class="embed_status">';
  492.                                 echo $check_embed;
  493.                                 echo '<a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">Remove Embed</a>';
  494.                                 echo '</div>';
  495.                             } else {
  496.                                 echo 'URL is not a valid oEmbed URL.';
  497.                             }
  498.                         }
  499.                     echo '</div>';
  500.  
  501.                     break;
  502.  
  503.                 default:
  504.                     do_action('cmb_render_' . $field['type'] , $field, $meta);
  505.  
  506.                    
  507.             }
  508.  
  509.         }
  510.             // Clost clone items wrapper
  511.             echo '</div>';
  512.             echo'<div class="page-methodology">
  513.                 <div class="page-selected-elements-wrapper">
  514.                 <div id="page_selected_elements" name="page_selected_elements" class="page_selected_elements page-no-sidebar ui-sortable">';
  515.  
  516.             $values = get_post_custom( $post->ID );
  517.             $selected = isset( $values['addddData'] ) ? $values['addddData'][0] : '';
  518.             echo $selected;
  519.             echo '
  520.                 </div>
  521.                 </div>
  522.                 </div>';
  523.     }
  524.  
  525.     // Save data from metabox
  526.     function save( $post_id, $post )  {
  527.  
  528.         // verify nonce
  529.         if ( ! isset( $_POST['wp_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['wp_meta_box_nonce'], basename(__FILE__) ) ) {
  530.             return $post_id;
  531.         }
  532.  
  533.         // check autosave
  534.         if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  535.             return $post_id;
  536.         }
  537.  
  538.         // check permissions
  539.         if ( 'page' == $_POST['post_type'] ) {
  540.             if ( !current_user_can( 'edit_page', $post_id ) ) {
  541.                 return $post_id;
  542.             }
  543.         } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
  544.             return $post_id;
  545.         }
  546.  
  547.         $cloned = $_POST['page_selected_elements'];
  548.         update_post_meta( $post_id, 'addddData', $cloned );
  549.  
  550.         //print_r($_POST);
  551.  
  552.         }
  553.  
  554.  
  555.    
  556. }
  557.  
  558. /**
  559.  * Adding scripts and styles
  560.  */
  561. function cmb_scripts( $hook ) {
  562.     global $wp_version;
  563.     // only enqueue our scripts/styles on the proper pages
  564.     if ( $hook == 'post.php' || $hook == 'post-new.php' || $hook == 'page-new.php' || $hook == 'page.php' ) {
  565.         // scripts required for cmb
  566.         $cmb_script_array = array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'media-upload', 'thickbox' );
  567.         // styles required for cmb
  568.         $cmb_style_array = array( 'thickbox' );
  569.         // if we're 3.5 or later, user wp-color-picker
  570.         if ( 3.5 <= $wp_version ) {
  571.             $cmb_script_array[] = 'wp-color-picker';
  572.             $cmb_style_array[] = 'wp-color-picker';
  573.         } else {
  574.             // otherwise use the older 'farbtastic'
  575.             $cmb_script_array[] = 'farbtastic';
  576.             $cmb_style_array[] = 'farbtastic';
  577.         }
  578.         wp_register_script( 'cmb-timepicker', CMB_META_BOX_URL . 'js/jquery.timePicker.min.js' );
  579.         wp_register_script( 'cmb-scripts', CMB_META_BOX_URL . 'js/cmb.js', $cmb_script_array, '0.9.4' );
  580.         wp_localize_script( 'cmb-scripts', 'cmb_ajax_data', array( 'ajax_nonce' => wp_create_nonce( 'ajax_nonce' ), 'post_id' => get_the_ID() ) );
  581.         wp_enqueue_script( 'cmb-timepicker' );
  582.         wp_enqueue_script( 'cmb-scripts' );
  583.         wp_register_style( 'cmb-styles', CMB_META_BOX_URL . 'style.css', $cmb_style_array );
  584.         wp_enqueue_style( 'cmb-styles' );
  585.     }
  586. }
  587. add_action( 'admin_enqueue_scripts', 'cmb_scripts', 10 );
  588.  
  589. function cmb_editor_footer_scripts() { ?>
  590.     <?php
  591.     if ( isset( $_GET['cmb_force_send'] ) && 'true' == $_GET['cmb_force_send'] ) {
  592.         $label = $_GET['cmb_send_label'];
  593.         if ( empty( $label ) ) $label="Select File";
  594.         ?>
  595.         <script type="text/javascript">
  596.         jQuery(function($) {
  597.             $('td.savesend input').val('<?php echo $label; ?>');
  598.         });
  599.         </script>
  600.         <?php
  601.     }
  602. }
  603. add_action( 'admin_print_footer_scripts', 'cmb_editor_footer_scripts', 99 );
  604.  
  605. // Force 'Insert into Post' button from Media Library
  606. add_filter( 'get_media_item_args', 'cmb_force_send' );
  607. function cmb_force_send( $args ) {
  608.  
  609.     // if the Gallery tab is opened from a custom meta box field, add Insert Into Post button
  610.     if ( isset( $_GET['cmb_force_send'] ) && 'true' == $_GET['cmb_force_send'] )
  611.         $args['send'] = true;
  612.  
  613.     // if the From Computer tab is opened AT ALL, add Insert Into Post button after an image is uploaded
  614.     if ( isset( $_POST['attachment_id'] ) && '' != $_POST["attachment_id"] ) {
  615.  
  616.         $args['send'] = true;
  617.  
  618.         // TO DO: Are there any conditions in which we don't want the Insert Into Post
  619.         // button added? For example, if a post type supports thumbnails, does not support
  620.         // the editor, and does not have any cmb file inputs? If so, here's the first
  621.         // bits of code needed to check all that.
  622.         // $attachment_ancestors = get_post_ancestors( $_POST["attachment_id"] );
  623.         // $attachment_parent_post_type = get_post_type( $attachment_ancestors[0] );
  624.         // $post_type_object = get_post_type_object( $attachment_parent_post_type );
  625.     }
  626.  
  627.     // change the label of the button on the From Computer tab
  628.     if ( isset( $_POST['attachment_id'] ) && '' != $_POST["attachment_id"] ) {
  629.  
  630.         echo '
  631.             <script type="text/javascript">
  632.                 function cmbGetParameterByNameInline(name) {
  633.                     name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  634.                     var regexS = "[\\?&]" + name + "=([^&#]*)";
  635.                     var regex = new RegExp(regexS);
  636.                     var results = regex.exec(window.location.href);
  637.                     if(results == null)
  638.                         return "";
  639.                     else
  640.                         return decodeURIComponent(results[1].replace(/\+/g, " "));
  641.                 }
  642.  
  643.                 jQuery(function($) {
  644.                     if (cmbGetParameterByNameInline("cmb_force_send")=="true") {
  645.                         var cmb_send_label = cmbGetParameterByNameInline("cmb_send_label");
  646.                         $("td.savesend input").val(cmb_send_label);
  647.                     }
  648.                 });
  649.             </script>
  650.         ';
  651.     }
  652.  
  653.     return $args;
  654.  
  655. }
  656.  
  657. add_action( 'wp_ajax_cmb_oembed_handler', 'cmb_oembed_ajax_results' );
  658. /**
  659.  * Handles our oEmbed ajax request
  660.  */
  661. function cmb_oembed_ajax_results() {
  662.  
  663.     // verify our nonce
  664.     if ( ! ( isset( $_REQUEST['cmb_ajax_nonce'], $_REQUEST['oembed_url'] ) && wp_verify_nonce( $_REQUEST['cmb_ajax_nonce'], 'ajax_nonce' ) ) )
  665.         die();
  666.  
  667.     // sanitize our search string
  668.     $oembed_string = sanitize_text_field( $_REQUEST['oembed_url'] );
  669.  
  670.     if ( empty( $oembed_string ) ) {
  671.         $return = '<p class="ui-state-error-text">'. __( 'Please Try Again', 'cmb' ) .'</p>';
  672.         $found = 'not found';
  673.     } else {
  674.  
  675.         global $wp_embed;
  676.  
  677.         $oembed_url = esc_url( $oembed_string );
  678.         // Post ID is needed to check for embeds
  679.         if ( isset( $_REQUEST['post_id'] ) )
  680.             $GLOBALS['post'] = get_post( $_REQUEST['post_id'] );
  681.         // Set width of embed
  682.         $embed_width = isset( $_REQUEST['oembed_width'] ) && intval( $_REQUEST['oembed_width'] ) < 640 ? intval( $_REQUEST['oembed_width'] ) : '640';
  683.         // ping WordPress for an embed
  684.         $check_embed = $wp_embed->run_shortcode( '[embed width="'. $embed_width .'"]'. $oembed_url .'[/embed]' );
  685.         // fallback that WordPress creates when no oEmbed was found
  686.         $fallback = $wp_embed->maybe_make_link( $oembed_url );
  687.  
  688.         if ( $check_embed && $check_embed != $fallback ) {
  689.             // Embed data
  690.             $return = '<div class="embed_status">'. $check_embed .'<a href="#" class="cmb_remove_file_button" rel="'. $_REQUEST['field_id'] .'">'. __( 'Remove Embed', 'cmb' ) .'</a></div>';
  691.             // set our response id
  692.             $found = 'found';
  693.  
  694.         } else {
  695.             // error info when no oEmbeds were found
  696.             $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>';
  697.             // set our response id
  698.             $found = 'not found';
  699.         }
  700.     }
  701.  
  702.     // send back our encoded data
  703.     echo json_encode( array( 'result' => $return, 'id' => $found ) );
  704.     die();
  705. }
  706.  
  707. // End. That's it, folks! //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement