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