handle = $handle;
$this->label = $label;
$this->desc = $desc;
$this->prefix = $prefix;
$this->postTypes = $postTypes;
$this->customFields = $customFields;
$this->jquery_ui_datetime_datepicker_i18n = $jquery_ui_datetime_datepicker_i18n;
$this->jquery_ui_datetime_timepicker_i18n = $jquery_ui_datetime_timepicker_i18n;
add_action('admin_menu', array(&$this, 'createCustomFields'));
add_action('save_post', array(&$this, 'saveCustomFields'), 1, 2);
// Comment this line out if you want to keep default custom fields meta box
add_action('do_meta_boxes', array(&$this, 'removeDefaultCustomFields'), 10, 3);
}
// Inspired by http://ca1.php.net/manual/en/function.timezone-identifiers-list.php#79284
static function generateTimezoneSelectOptions($default_tz)
{
$timezone_identifiers = timezone_identifiers_list();
sort( $timezone_identifiers );
$current_continent = "";
$options_list = "";
foreach ($timezone_identifiers as $timezone_identifier) {
list($continent,) = explode('/', $timezone_identifier, 2);
if (in_array($continent,array(
'Africa',
'America',
'Antarctica',
'Arctic',
'Asia',
'Atlantic',
'Australia',
'Europe',
'Indian',
'Pacific'
))) {
list(, $city) = explode('/', $timezone_identifier, 2);
if (strlen($current_continent) === 0) {
// Start first continent optgroup
$options_list .= ''; // End last continent optgroup
return $options_list;
}
/**
* Remove the default Custom Fields meta box
*/
function removeDefaultCustomFields($type, $context, $post)
{
foreach (array( 'normal', 'advanced', 'side' ) as $context) {
foreach ($this->postTypes as $postType) {
remove_meta_box( 'postcustom', $postType, $context );
}
}
}
/**
* Create the new Custom Fields meta box
*/
function createCustomFields() {
// Only enqueue scripts if we're dealing with 'timed-content-rule' pages
foreach ( $this->postTypes as $a_postType ) {
if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == $a_postType )
|| ( isset( $post_type ) && $post_type == $a_postType )
|| ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) == $a_postType ) ) {
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_script( 'jquery-ui-spinner' );
wp_enqueue_style( TIMED_CONTENT_SLUG . '-jquery-ui-css', TIMED_CONTENT_JQUERY_UI_CSS );
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_register_style( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-css',
TIMED_CONTENT_JQUERY_UI_TIMEPICKER_CSS );
wp_enqueue_style( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-css' );
wp_register_script( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js',
TIMED_CONTENT_JQUERY_UI_TIMEPICKER_JS, array( 'jquery', 'jquery-ui-datepicker' ),
TIMED_CONTENT_VERSION );
wp_enqueue_script( TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js' );
if ( ! ( wp_script_is( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js', 'registered' ) ) ) {
wp_register_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js',
TIMED_CONTENT_PLUGIN_URL . "/js/timed-content-datetime-i18n.js",
array( 'jquery', 'jquery-ui-datepicker', TIMED_CONTENT_SLUG . '-jquery-ui-timepicker-js' ),
TIMED_CONTENT_VERSION );
wp_enqueue_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js' );
wp_localize_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js',
'TimedContentJQDatepickerI18n', $this->jquery_ui_datetime_datepicker_i18n );
wp_localize_script( TIMED_CONTENT_SLUG . '-jquery-ui-datetime-i18n-js',
'TimedContentJQTimepickerI18n', $this->jquery_ui_datetime_timepicker_i18n );
}
if ( function_exists( 'add_meta_box' ) ) {
add_meta_box( $this->handle, $this->label, array( &$this, 'displayCustomFields' ), $a_postType,
'normal', 'high' );
}
}
}
}
/**
* Display the new Custom Fields meta box
*/
function displayCustomFields() {
global $post;
?>
desc; ?>
handle, $this->handle . '_wpnonce', false, true );
foreach ( $this->customFields as $customField ) {
// Check scope
$scope = $customField['scope'];
$output = false;
foreach ( $scope as $scopeItem ) {
switch ( $scopeItem ) {
default:
{
if ( $post->post_type == $scopeItem ) {
$output = true;
}
break;
}
}
if ( $output ) {
break;
}
}
// Check capability
if ( ! current_user_can( $customField['capability'], $post->ID ) ) {
$output = false;
}
// Output if allowed
if ( $output ) { ?>
ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "" . $customField['title'] . "
\n";
foreach ( $customField['values'] as $value => $label ) {
echo "prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" value=\"" . $value . "\"";
if ( $checked_value == $value ) {
echo " checked=\"checked\"";
}
echo " />
\n";
}
}
break;
case "menu":
{
// menu
echo "
\n";
if ( sizeof( $customField['values'] ) == 0 ) {
echo "" . __( "This menu is empty.", "timed-content" ) . "\n";
} else {
$selected_value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "\n";
}
}
break;
case "list":
{
// list
echo "
\n";
if ( sizeof( $customField['values'] ) == 0 ) {
echo "" . __( "This menu is empty.", "timed-content" ) . "\n";
} else {
$selected_value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "\n";
}
}
break;
case "timezone-list":
{
// timezone list
$selected_value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "
\n";
echo "\n";
}
break;
case "checkbox":
{
// Checkbox
$checked_value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "\n";
}
break;
case "checkbox-list":
{
// Checkbox list
$checked_value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "" . $customField['title'] . "
\n";
if ( sizeof( $customField['values'] ) == 0 ) {
echo "" . __( "This menu is empty.", "timed-content" ) . "\n";
} else {
foreach ( $customField['values'] as $value => $label ) {
echo "prefix . $customField['name'] . "[]\" id=\"" . $this->prefix . $customField['name'] . "_" . $value . "\" value=\"" . $value . "\"";
if ( ( is_array( $checked_value ) ) && ( in_array( $value,
$checked_value ) ) ) {
echo " checked=\"checked\"";
}
echo " />
\n";
}
}
}
break;
case "textarea":
case "wysiwyg":
{
// Text area
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
echo "
\n";
echo "\n";
// WYSIWYG
if ( $customField['type'] == "wysiwyg" ) { ?>
ID, $this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
// Color picker using WP's built-in Iris jQuery plugin ?>
prefix . $customField['name'] . "\">" . $customField['title'] . "
\n";
echo "prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" size=\"7\" maxlength=\"7\" style=\"width: 100px;\" />\n";
break;
}
case "date": {
echo "
\n";
// echo "" . $customField[ 'title' ] . " \n";
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
// Date picker using WP's built-in Datepicker jQuery plugin ?>
prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . htmlspecialchars( $value ) . "\" style=\"width: 175px;\" />\n";
break;
}
case "datetime": {
echo "" . $customField['title'] . "
\n";
$value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
foreach ( $value as $k => $v ) {
$value[ $k ] = htmlspecialchars( $v );
}
// Date picker using WP's built-in Datepicker jQuery plugin
// Time picker using jQuery UI Timepicker: http://fgelinas.com/code/timepicker
?>
prefix . $customField['name'] . "_date\" style=\"display:inline;\">" . _x( 'Date',
'Date field label', 'timed-content' ) . ":\n";
echo "prefix . $customField['name'] . "[date]\" id=\"" . $this->prefix . $customField['name'] . "_date\" value=\"" . $value['date'] . "\" style=\"width: 175px;\" />\n";
echo "\n";
echo "prefix . $customField['name'] . "[time]\" id=\"" . $this->prefix . $customField['name'] . "_time\" value=\"" . $value['time'] . "\" style=\"width: 125px;\" />\n";
break;
}
case "number": {
(int) $value = ( "" === get_post_meta( $post->ID, $this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
// Number picker using WP's built-in Spinner jQuery plugin ?>
prefix . $customField['name'] . "\" style=\"display:inline;\">" . $customField['title'] . "
\n";
echo "prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" size=\"2\" />\n";
break;
}
case "hidden":
{
$value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
// Hidden field
echo "prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" />\n";
break;
}
default:
{
$value = ( "" === get_post_meta( $post->ID,
$this->prefix . $customField['name'],
true ) ? $customField['default'] : get_post_meta( $post->ID,
$this->prefix . $customField['name'], true ) );
// Plain text field
echo "
\n";
echo "prefix . $customField['name'] . "\" id=\"" . $this->prefix . $customField['name'] . "\" value=\"" . $value . "\" />\n";
break;
}
}
?>
" . $customField['description'] . "\n";
} ?>
handle . '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ $this->handle . '_wpnonce' ],
$this->handle ) ) {
return;
}
// Make sure the user can edit this specific post
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
// Make sure this meta box is associated with the right post types
if ( ! in_array( $post->post_type, $this->postTypes ) ) {
return;
}
foreach ( $this->customFields as $customField ) {
if ( current_user_can( $customField['capability'], $post_id ) ) {
if ( isset( $_POST[ $this->prefix . $customField['name'] ] ) ) {
if ( is_array( $_POST[ $this->prefix . $customField['name'] ] ) ) {
foreach ( $_POST[ $this->prefix . $customField['name'] ] as $k => $v ) {
$_POST[ $this->prefix . $customField['name'] ][ $k ] = trim( $v );
}
} else {
$_POST[ $this->prefix . $customField['name'] ] = trim( $_POST[ $this->prefix . $customField['name'] ] );
}
$value = $_POST[ $this->prefix . $customField['name'] ];
// Auto-paragraphs for any WYSIWYG
if ( $customField['type'] == "wysiwyg" ) {
$value = wpautop( $value );
}
if ( $customField['type'] == "checkbox" ) {
$value = "yes";
}
if ( $customField['type'] == "number" ) {
$value = intval( $value );
}
update_post_meta( $post_id, $this->prefix . $customField['name'], $value );
} else {
delete_post_meta( $post_id, $this->prefix . $customField['name'] );
}
}
}
}
}