| 1 |
<?php |
| 2 |
class customFieldsInterface |
| 3 |
{ |
| 4 |
/** |
| 5 |
* @var string $handle The handle for the box containing the custom fields |
| 6 |
*/ |
| 7 |
var $handle = ""; |
| 8 |
/** |
| 9 |
* @var string $label The label for the box containing the custom fields |
| 10 |
*/ |
| 11 |
var $label = ""; |
| 12 |
/** |
| 13 |
* @var string $desc The description/introduction for the box containing the custom fields |
| 14 |
*/ |
| 15 |
var $desc = ""; |
| 16 |
/** |
| 17 |
* @var string $prefix The prefix for storing custom fields in the postmeta table |
| 18 |
*/ |
| 19 |
var $prefix = ""; |
| 20 |
/** |
| 21 |
* @var array $postTypes An array of public custom post types, plus the standard "post" and "page" - add the custom types you want to include here |
| 22 |
*/ |
| 23 |
var $postTypes = array(); |
| 24 |
/** |
| 25 |
* @var array $customFields Defines the custom fields available |
| 26 |
*/ |
| 27 |
var $customFields = array(); |
| 28 |
/** |
| 29 |
* @var array $jquery_ui_datetime_datepicker_i18n Array for jQuery datepicker UI localization |
| 30 |
*/ |
| 31 |
var $jquery_ui_datetime_datepicker_i18n; |
| 32 |
/** |
| 33 |
* @var array $jquery_ui_datetime_timepicker_i18n Array for jQuery timepicker UI localization |
| 34 |
*/ |
| 35 |
var $jquery_ui_datetime_timepicker_i18n; |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
function __construct($handle, $label, $desc, $prefix, $postTypes, $customFields, $jquery_ui_datetime_datepicker_i18n, $jquery_ui_datetime_timepicker_i18n) |
| 41 |
{ |
| 42 |
$this->handle = $handle; |
| 43 |
$this->label = $label; |
| 44 |
$this->desc = $desc; |
| 45 |
$this->prefix = $prefix; |
| 46 |
$this->postTypes = $postTypes; |
| 47 |
$this->customFields = $customFields; |
| 48 |
$this->jquery_ui_datetime_datepicker_i18n = $jquery_ui_datetime_datepicker_i18n; |
| 49 |
$this->jquery_ui_datetime_timepicker_i18n = $jquery_ui_datetime_timepicker_i18n; |
| 50 |
|
| 51 |
|
| 52 |
add_action('admin_menu', array(&$this, 'createCustomFields')); |
| 53 |
add_action('save_post', array(&$this, 'saveCustomFields'), 1, 2); |
| 54 |
// Comment this line out if you want to keep default custom fields meta box |
| 55 |
add_action('do_meta_boxes', array(&$this, 'removeDefaultCustomFields'), 10, 3); |
| 56 |
} |
| 57 |
|
| 58 |
// Inspired by http://ca1.php.net/manual/en/function.timezone-identifiers-list.php#79284 |
| 59 |
static function __generateTimezoneSelectOptions($default_tz) |
| 60 |
{ |
| 61 |
$timezone_identifiers = timezone_identifiers_list(); |
| 62 |
sort( $timezone_identifiers ); |
| 63 |
$current_continent = ""; |
| 64 |
$options_list = ""; |
| 65 |
|
| 66 |
foreach ($timezone_identifiers as $timezone_identifier) { |
| 67 |
list($continent,) = explode('/', $timezone_identifier, 2); |
| 68 |
if (in_array($continent,array( |
| 69 |
'Africa', |
| 70 |
'America', |
| 71 |
'Antarctica', |
| 72 |
'Arctic', |
| 73 |
'Asia', |
| 74 |
'Atlantic', |
| 75 |
'Australia', |
| 76 |
'Europe', |
| 77 |
'Indian', |
| 78 |
'Pacific' |
| 79 |
))) { |
| 80 |
list(, $city) = explode('/', $timezone_identifier, 2); |
| 81 |
if (strlen($current_continent) === 0) { |
| 82 |
// Start first continent optgroup |
| 83 |
$options_list .= '<optgroup label=\'' . $continent . '\'>'; |
| 84 |
} elseif ($current_continent != $continent) { |
| 85 |
// End old optgroup and start new continent optgroup |
| 86 |
$options_list .= '</optgroup><optgroup label=\'' . $continent . '\'>'; |
| 87 |
} |
| 88 |
// Timezone |
| 89 |
$options_list .= '<option' . |
| 90 |
(($timezone_identifier == $default_tz ) ? ' selected=\'selected\'' : '') . |
| 91 |
' value=\'' . $timezone_identifier . '\'>' . |
| 92 |
str_replace('_', ' ', $city) . '</option>'; |
| 93 |
} |
| 94 |
$current_continent = $continent; |
| 95 |
} |
| 96 |
$options_list .= '</optgroup>'; // End last continent optgroup |
| 97 |
|
| 98 |
return $options_list; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Remove the default Custom Fields meta box |
| 103 |
*/ |
| 104 |
function removeDefaultCustomFields($type, $context, $post) |
| 105 |
{ |
| 106 |
foreach (array( 'normal', 'advanced', 'side' ) as $context) { |
| 107 |
foreach ($this->postTypes as $postType) { |
| 108 |
remove_meta_box( 'postcustom', $postType, $context ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Create the new Custom Fields meta box |
| 115 |
*/ |
| 116 |
function createCustomFields() { |
| 117 |
// Only enqueue scripts if we're dealing with 'timed-content-rule' pages |
| 118 |
foreach ( $this->postTypes as $a_postType ) { |
| 119 |
if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == $a_postType ) |
| 120 |
|| ( isset( $post_type ) && $post_type == $a_postType ) |
| 121 |
|| ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == $a_postType ) ) { |
| 122 |
|
| 123 |
wp_enqueue_style( 'wp-color-picker' ); |
| 124 |
wp_enqueue_script( 'wp-color-picker' ); |
| 125 |
wp_enqueue_script( 'jquery-ui-spinner' ); |
| 126 |
|
| 127 |
wp_enqueue_style( TIMED_CONTENT_SLUG . '-jquery-ui-css', TIMED_CONTENT_JQUERY_UI_CSS ); |
| 128 |
wp_enqueue_script( 'jquery-ui-datepicker' ); |
| 129 |
wp_register_style( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-css', |
| 130 |
TIMED_CONTENT_JQUERY_UI_TIMEPICKER_CSS ); |
| 131 |
wp_enqueue_style( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-css' ); |
| 132 |
wp_register_script( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js', |
| 133 |
TIMED_CONTENT_JQUERY_UI_TIMEPICKER_JS, array( 'jquery', 'jquery-ui-datepicker' ), |
| 134 |
TIMED_CONTENT_VERSION ); |
| 135 |
wp_enqueue_script( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js' ); |
| 136 |
if ( ! ( wp_script_is( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', 'registered' ) ) ) { |
| 137 |
wp_register_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', |
| 138 |
TIMED_CONTENT_PLUGIN_URL . "/js/timed-content-datetime-i18n.js", |
| 139 |
array( 'jquery', 'jquery-ui-datepicker', TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js' ), |
| 140 |
TIMED_CONTENT_VERSION ); |
| 141 |
wp_enqueue_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js' ); |
| 142 |
wp_localize_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', |
| 143 |
'TimedContentJQDatepickerI18n', $this->jquery_ui_datetime_datepicker_i18n ); |
| 144 |
wp_localize_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', |
| 145 |
'TimedContentJQTimepickerI18n', $this->jquery_ui_datetime_timepicker_i18n ); |
| 146 |
} |
| 147 |
|
| 148 |
if ( function_exists( 'add_meta_box' ) ) { |
| 149 |
add_meta_box( $this->handle, $this->label, array( &$this, 'displayCustomFields' ), $a_postType, |
| 150 |
'normal', 'high' ); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Display the new Custom Fields meta box |
| 158 |
*/ |
| 159 |
function displayCustomFields() { |
| 160 |
global $post; |
| 161 |
?> |
| 162 |
<p><?php echo $this->desc; ?></p> |
| 163 |
<div class="form-wrap"> |
| 164 |
<?php |
| 165 |
wp_nonce_field( $this->handle, $this->handle . '_wpnonce', false, true ); |
| 166 |
foreach ( $this->customFields as $customField ) { |
| 167 |
// Check scope |
| 168 |
$scope = $customField['scope']; |
| 169 |
$output = false; |
| 170 |
foreach ( $scope as $scopeItem ) { |
| 171 |
switch ( $scopeItem ) { |
| 172 |
default: |
| 173 |
{ |
| 174 |
if ( $post->post_type == $scopeItem ) { |
| 175 |
$output = true; |
| 176 |
} |
| 177 |
break; |
| 178 |
} |
| 179 |
} |
| 180 |
if ( $output ) { |
| 181 |
break; |
| 182 |
} |
| 183 |
} |
| 184 |
// Check capability |
| 185 |
if ( ! current_user_can( $customField['capability'], $post->ID ) ) { |
| 186 |
$output = false; |
| 187 |
} |
| 188 |
// Output if allowed |
| 189 |
if ( $output ) { ?> |
| 190 |
<div class="form-field form-required" |
| 191 |
id="<?php echo $this->prefix . $customField['name']; ?>_div" |
| 192 |
style="display: <?php echo $customField['display']; ?>"> |
| 193 |
<?php |
| 194 |
switch ( $customField['type'] ) { |
| 195 |
case "radio": |
| 196 |
{ |
| 197 |
// radio |
| 198 |
$checked_value = ( "" === get_post_meta( $post->ID, |
| 199 |
$this->prefix . $customField['name'], |
| 200 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 201 |
$this->prefix . $customField['name'], true ) ); |
| 202 |
echo "<strong>" . $customField['title'] . "</strong><br />\n"; |
| 203 |
foreach ( $customField['values'] as $value => $label ) { |
| 204 |
echo "<input type=\"radio\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" value=\"" . $value . "\""; |
| 205 |
if ( $checked_value == $value ) { |
| 206 |
echo " checked=\"checked\""; |
| 207 |
} |
| 208 |
echo " /><label for=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" style=\"display: inline;\">" . $label . "</label><br />\n"; |
| 209 |
} |
| 210 |
} |
| 211 |
break; |
| 212 |
case "menu": |
| 213 |
{ |
| 214 |
// menu |
| 215 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\" style=\"display:inline;\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 216 |
if ( sizeof( $customField['values'] ) == 0 ) { |
| 217 |
echo "<em>" . __( "This menu is empty.", "timed-content" ) . "</em>\n"; |
| 218 |
} else { |
| 219 |
$selected_value = ( "" === get_post_meta( $post->ID, |
| 220 |
$this->prefix . $customField['name'], |
| 221 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 222 |
$this->prefix . $customField['name'], true ) ); |
| 223 |
echo "<select name=\"" . $this->prefix . $customField['name'] . "[]\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto; height: auto; padding: 3px;\" size=\"" . $customField['size'] . "\" multiple=\"multiple\">\n"; |
| 224 |
foreach ( $customField['values'] as $value => $label ) { |
| 225 |
echo "\t<option value=\"" . $value . "\""; |
| 226 |
if ( $selected_value == $value ) { |
| 227 |
echo " selected=\"selected\""; |
| 228 |
} |
| 229 |
echo ">" . $label . "</option>\n"; |
| 230 |
} |
| 231 |
echo "</select>\n"; |
| 232 |
} |
| 233 |
} |
| 234 |
break; |
| 235 |
case "list": |
| 236 |
{ |
| 237 |
// list |
| 238 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\" style=\"display:inline;\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 239 |
if ( sizeof( $customField['values'] ) == 0 ) { |
| 240 |
echo "<em>" . __( "This menu is empty.", "timed-content" ) . "</em>\n"; |
| 241 |
} else { |
| 242 |
$selected_value = ( "" === get_post_meta( $post->ID, |
| 243 |
$this->prefix . $customField['name'], |
| 244 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 245 |
$this->prefix . $customField['name'], true ) ); |
| 246 |
echo "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto;\">\n"; |
| 247 |
foreach ( $customField['values'] as $value => $label ) { |
| 248 |
echo "\t<option value=\"" . $value . "\""; |
| 249 |
if ( $selected_value == $value ) { |
| 250 |
echo " selected=\"selected\""; |
| 251 |
} |
| 252 |
echo ">" . $label . "</option>\n"; |
| 253 |
} |
| 254 |
echo "</select>\n"; |
| 255 |
} |
| 256 |
} |
| 257 |
break; |
| 258 |
case "timezone-list": |
| 259 |
{ |
| 260 |
// timezone list |
| 261 |
$selected_value = ( "" === get_post_meta( $post->ID, |
| 262 |
$this->prefix . $customField['name'], |
| 263 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 264 |
$this->prefix . $customField['name'], true ) ); |
| 265 |
|
| 266 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\" style=\"display:inline;\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 267 |
echo "<select name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" style=\"width: auto;\">\n"; |
| 268 |
echo customFieldsInterface::__generateTimezoneSelectOptions( $selected_value ); |
| 269 |
echo "</select>\n"; |
| 270 |
} |
| 271 |
break; |
| 272 |
case "checkbox": |
| 273 |
{ |
| 274 |
// Checkbox |
| 275 |
$checked_value = ( "" === get_post_meta( $post->ID, |
| 276 |
$this->prefix . $customField['name'], |
| 277 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 278 |
$this->prefix . $customField['name'], true ) ); |
| 279 |
|
| 280 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\" style=\"display:inline;\"><strong>" . $customField['title'] . "</strong><br />\n"; |
| 281 |
echo "<input type=\"checkbox\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"yes\""; |
| 282 |
if ( $checked_value == "yes" ) { |
| 283 |
echo " checked=\"checked\""; |
| 284 |
} |
| 285 |
echo " />".__('Yes', 'timed-content')."</label>\n"; |
| 286 |
} |
| 287 |
break; |
| 288 |
case "checkbox-list": |
| 289 |
{ |
| 290 |
// Checkbox list |
| 291 |
$checked_value = ( "" === get_post_meta( $post->ID, |
| 292 |
$this->prefix . $customField['name'], |
| 293 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 294 |
$this->prefix . $customField['name'], true ) ); |
| 295 |
|
| 296 |
echo "<strong>" . $customField['title'] . "</strong><br />\n"; |
| 297 |
if ( sizeof( $customField['values'] ) == 0 ) { |
| 298 |
echo "<em>" . __( "This menu is empty.", "timed-content" ) . "</em>\n"; |
| 299 |
} else { |
| 300 |
foreach ( $customField['values'] as $value => $label ) { |
| 301 |
echo "<input type=\"checkbox\" name=\"" . $this->prefix . $customField['name'] . "[]\" id=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" value=\"" . $value . "\""; |
| 302 |
if ( ( is_array( $checked_value ) ) && ( in_array( $value, |
| 303 |
$checked_value ) ) ) { |
| 304 |
echo " checked=\"checked\""; |
| 305 |
} |
| 306 |
echo " /><label for=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" style=\"display: inline;\" >" . $label . "</label><br />\n"; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
break; |
| 311 |
case "textarea": |
| 312 |
case "wysiwyg": |
| 313 |
{ |
| 314 |
// Text area |
| 315 |
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], |
| 316 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 317 |
$this->prefix . $customField['name'], true ) ); |
| 318 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 319 |
echo "<textarea name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" columns=\"30\" rows=\"3\">" . htmlspecialchars( $value ) . "</textarea>\n"; |
| 320 |
// WYSIWYG |
| 321 |
if ( $customField['type'] == "wysiwyg" ) { ?> |
| 322 |
<script type="text/javascript"> |
| 323 |
//<![CDATA[ |
| 324 |
jQuery(document).ready(function () { |
| 325 |
jQuery("<?php echo $this->prefix . $customField['name']; ?>").addClass("mceEditor"); |
| 326 |
if (typeof (tinyMCE) == "object" && typeof (tinyMCE.execCommand) == "function") { |
| 327 |
tinyMCE.execCommand("mceAddControl", false, "<?php echo $this->prefix . $customField['name']; ?>"); |
| 328 |
} |
| 329 |
}); |
| 330 |
//]]> |
| 331 |
</script> |
| 332 |
<?php } |
| 333 |
} |
| 334 |
break; |
| 335 |
case "color-picker": { |
| 336 |
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], |
| 337 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 338 |
$this->prefix . $customField['name'], true ) ); |
| 339 |
// Color picker using WP's built-in Iris jQuery plugin ?> |
| 340 |
<script type="text/javascript"> |
| 341 |
//<![CDATA[ |
| 342 |
jQuery(document).ready(function () { |
| 343 |
var <?php echo $this->prefix . $customField['name']; ?>Options = { |
| 344 |
defaultColor: false, |
| 345 |
hide: true, |
| 346 |
palettes: true |
| 347 |
}; |
| 348 |
|
| 349 |
jQuery("#<?php echo $this->prefix . $customField['name']; ?>").wpColorPicker(<?php echo $this->prefix . $customField['name']; ?>Options); |
| 350 |
}); |
| 351 |
//]]> |
| 352 |
</script> |
| 353 |
<?php |
| 354 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 355 |
echo "<input type=\"text\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" size=\"7\" maxlength=\"7\" style=\"width: 100px;\" />\n"; |
| 356 |
break; |
| 357 |
} |
| 358 |
case "date": { |
| 359 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\" style=\"display:inline;\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 360 |
// echo "<span style=\"display:inline;\"><strong>" . $customField[ 'title' ] . "</strong></span> \n"; |
| 361 |
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], |
| 362 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 363 |
$this->prefix . $customField['name'], true ) ); |
| 364 |
// Date picker using WP's built-in Datepicker jQuery plugin ?> |
| 365 |
<script type="text/javascript"> |
| 366 |
//<![CDATA[ |
| 367 |
jQuery(document).ready(function () { |
| 368 |
jQuery("#<?php echo $this->prefix . $customField['name']; ?>").datepicker( |
| 369 |
{ |
| 370 |
<?php if ( isset( $customField['custom_functions'] ) ) { |
| 371 |
echo $customField['custom_functions']; |
| 372 |
} ?> |
| 373 |
changeMonth: true, |
| 374 |
changeYear: true |
| 375 |
} |
| 376 |
); |
| 377 |
}); |
| 378 |
//]]> |
| 379 |
</script> |
| 380 |
<?php |
| 381 |
echo "<input type=\"text\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . htmlspecialchars( $value ) . "\" style=\"width: 175px;\" />\n"; |
| 382 |
break; |
| 383 |
} |
| 384 |
case "datetime": { |
| 385 |
echo "<span style=\"display:inline;\"><strong>" . $customField['title'] . "</strong></span><br />\n"; |
| 386 |
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], |
| 387 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 388 |
$this->prefix . $customField['name'], true ) ); |
| 389 |
foreach ( $value as $k => $v ) { |
| 390 |
$value[ $k ] = htmlspecialchars( $v ); |
| 391 |
} |
| 392 |
// Date picker using WP's built-in Datepicker jQuery plugin |
| 393 |
// Time picker using jQuery UI Timepicker: http://fgelinas.com/code/timepicker |
| 394 |
?> |
| 395 |
<script type="text/javascript"> |
| 396 |
//<![CDATA[ |
| 397 |
jQuery(document).ready(function () { |
| 398 |
jQuery("#<?php echo $this->prefix . $customField['name']; ?>_date").datepicker( |
| 399 |
{ |
| 400 |
changeMonth: true, |
| 401 |
changeYear: true |
| 402 |
} |
| 403 |
); |
| 404 |
jQuery("#<?php echo $this->prefix . $customField['name']; ?>_time").timepicker( |
| 405 |
{ |
| 406 |
defaultTime: 'now' |
| 407 |
} |
| 408 |
); |
| 409 |
}); |
| 410 |
//]]> |
| 411 |
</script> |
| 412 |
<?php |
| 413 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "_date\" style=\"display:inline;\"><em>" . _x( 'Date', |
| 414 |
'Date field label', 'timed-content' ) . ":</em></label>\n"; |
| 415 |
echo "<input type=\"text\" name=\"" . $this->prefix . $customField['name'] . "[date]\" id=\"" . $this->prefix . $customField['name'] . "_date\" value=\"" . $value['date'] . "\" style=\"width: 175px;\" />\n"; |
| 416 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "_time\" style=\"display:inline;\"><em>" . _x( 'Time', |
| 417 |
'Time field label', 'timed-content' ) . ":</em></label>\n"; |
| 418 |
echo "<input type=\"text\" name=\"" . $this->prefix . $customField['name'] . "[time]\" id=\"" . $this->prefix . $customField['name'] . "_time\" value=\"" . $value['time'] . "\" style=\"width: 125px;\" />\n"; |
| 419 |
break; |
| 420 |
} |
| 421 |
case "number": { |
| 422 |
(int) $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'], |
| 423 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 424 |
$this->prefix . $customField['name'], true ) ); |
| 425 |
// Number picker using WP's built-in Spinner jQuery plugin ?> |
| 426 |
<script type="text/javascript"> |
| 427 |
//<![CDATA[ |
| 428 |
jQuery(document).ready(function () { |
| 429 |
jQuery("#<?php echo $this->prefix . $customField['name']; ?>").spinner( |
| 430 |
{ |
| 431 |
stop: function (event, ui) { |
| 432 |
jQuery(this).trigger("change"); |
| 433 |
}, |
| 434 |
<?php if ( isset( $customField['min'] ) ) { ?>min: <?php echo $customField['min']; ?>, <?php } ?> |
| 435 |
<?php if ( isset( $customField['max'] ) ) { ?>max: <?php echo $customField['max']; ?> <?php } ?> |
| 436 |
} |
| 437 |
); |
| 438 |
}); |
| 439 |
//]]> |
| 440 |
</script> |
| 441 |
<?php |
| 442 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\" style=\"display:inline;\"><strong>" . $customField['title'] . "</strong></label><br />\n"; |
| 443 |
echo "<input type=\"text\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" size=\"2\" />\n"; |
| 444 |
break; |
| 445 |
} |
| 446 |
case "hidden": |
| 447 |
{ |
| 448 |
$value = ( "" === get_post_meta( $post->ID, |
| 449 |
$this->prefix . $customField['name'], |
| 450 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 451 |
$this->prefix . $customField['name'], true ) ); |
| 452 |
// Hidden field |
| 453 |
echo "<input type=\"hidden\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" />\n"; |
| 454 |
break; |
| 455 |
} |
| 456 |
default: |
| 457 |
{ |
| 458 |
$value = ( "" === get_post_meta( $post->ID, |
| 459 |
$this->prefix . $customField['name'], |
| 460 |
true ) ? $customField['default'] : get_post_meta( $post->ID, |
| 461 |
$this->prefix . $customField['name'], true ) ); |
| 462 |
// Plain text field |
| 463 |
echo "<label for=\"" . $this->prefix . $customField['name'] . "\"><strong>" . $customField['title'] . "</strong></label><br/>\n"; |
| 464 |
echo "<input type=\"text\" name=\"" . $this->prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" />\n"; |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
?> |
| 469 |
<?php if ( isset( $customField['description'] ) ) { |
| 470 |
echo "<p>" . $customField['description'] . "</p>\n"; |
| 471 |
} ?> |
| 472 |
</div> |
| 473 |
<?php |
| 474 |
} |
| 475 |
} ?> |
| 476 |
</div> |
| 477 |
<?php |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Save the new Custom Fields values |
| 482 |
*/ |
| 483 |
function saveCustomFields( $post_id, $post ) { |
| 484 |
// Only save if the user intends to save |
| 485 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 486 |
return; |
| 487 |
} |
| 488 |
// Make sure the Save request is coming from the right place |
| 489 |
if ( ! isset( $_POST[ $this->handle . '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ $this->handle . '_wpnonce' ], |
| 490 |
$this->handle ) ) { |
| 491 |
return; |
| 492 |
} |
| 493 |
// Make sure the user can edit this specific post |
| 494 |
if ( ! current_user_can( 'edit_post', $post_id ) ) { |
| 495 |
return; |
| 496 |
} |
| 497 |
// Make sure this meta box is associated with the right post types |
| 498 |
if ( ! in_array( $post->post_type, $this->postTypes ) ) { |
| 499 |
return; |
| 500 |
} |
| 501 |
foreach ( $this->customFields as $customField ) { |
| 502 |
if ( current_user_can( $customField['capability'], $post_id ) ) { |
| 503 |
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) ) { |
| 504 |
if ( is_array( $_POST[ $this->prefix . $customField['name'] ] ) ) { |
| 505 |
foreach ( $_POST[ $this->prefix . $customField['name'] ] as $k => $v ) { |
| 506 |
$_POST[ $this->prefix . $customField['name'] ][ $k ] = trim( $v ); |
| 507 |
} |
| 508 |
} else { |
| 509 |
$_POST[ $this->prefix . $customField['name'] ] = trim( $_POST[ $this->prefix . $customField['name'] ] ); |
| 510 |
} |
| 511 |
$value = $_POST[ $this->prefix . $customField['name'] ]; |
| 512 |
// Auto-paragraphs for any WYSIWYG |
| 513 |
if ( $customField['type'] == "wysiwyg" ) { |
| 514 |
$value = wpautop( $value ); |
| 515 |
} |
| 516 |
if ( $customField['type'] == "checkbox" ) { |
| 517 |
$value = "yes"; |
| 518 |
} |
| 519 |
if ( $customField['type'] == "number" ) { |
| 520 |
$value = intval( $value ); |
| 521 |
} |
| 522 |
update_post_meta( $post_id, $this->prefix . $customField['name'], $value ); |
| 523 |
} else { |
| 524 |
delete_post_meta( $post_id, $this->prefix . $customField['name'] ); |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|