handle = $handle; $this->label = $label; $this->desc = $desc; $this->prefix = $prefix; $this->post_types = $post_types; $this->custom_fields = $custom_fields; $this->jquery_ui_datetime_datepicker_i18n = $jquery_ui_datetime_datepicker_i18n; $this->jquery_ui_datetime_timepicker_i18n = $jquery_ui_datetime_timepicker_i18n; $this->rule_description_as_html = $rule_description_as_html; add_action( 'admin_menu', array( &$this, 'create_custom_fields' ) ); add_action( 'save_post', array( &$this, 'save_custom_fields' ), 1, 2 ); // Comment this line out if you want to keep default custom fields meta box add_action( 'do_meta_boxes', array( &$this, 'remove_default_custom_fields' ), 10, 3 ); } // Inspired by http://ca1.php.net/manual/en/function.timezone-identifiers-list.php#79284 static function generate_timezone_select_options( $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', ), true ) ) { list(, $city) = explode( '/', $timezone_identifier, 2 ); if ( strlen( $current_continent ) === 0 ) { // Start first continent optgroup $options_list .= ''; } elseif ( $current_continent !== $continent ) { // End old optgroup and start new continent optgroup $options_list .= ''; } // Timezone $options_list .= '' . str_replace( '_', ' ', $city ) . ''; } $current_continent = $continent; } $options_list .= ''; // End last continent optgroup return $options_list; } /** * Remove the default Custom Fields meta box */ function remove_default_custom_fields( $type, $context, $post ) { foreach ( array( 'normal', 'advanced', 'side' ) as $context ) { foreach ( $this->post_types as $post_type ) { remove_meta_box( 'postcustom', $post_type, $context ); } } } /** * Create the new Custom Fields meta box */ function create_custom_fields() { // Only enqueue scripts if we're dealing with 'timed-content-rule' pages foreach ( $this->post_types as $a_post_type ) { if ( ( isset( $_GET['post_type'] ) && $_GET['post_type'] === $a_post_type ) || ( isset( $post_type ) && $post_type === $a_post_type ) || ( isset( $_GET['post'] ) && get_post_type( $_GET['post'] ) === $a_post_type ) ) { 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, 'display_custom_fields' ), $a_post_type, 'normal', 'high' ); } } } } /** * Display the new Custom Fields meta box */ function display_custom_fields() { global $post; if ( '' !== $this->rule_description_as_html ) { // If there is a rule description, then use this as we are not allowed to use generated // HTML code as variable without escaping or sanitizing but we need the button element echo '
'; echo wp_kses_post( $this->rule_description_as_html ); echo '
'; echo ''; echo '
'; echo ''; echo '
'; } else { // Otherwise output the description as text echo '

' . esc_html( $this->desc ) . '

'; } echo '
'; wp_nonce_field( $this->handle, $this->handle . '_wpnonce', false, true ); foreach ( $this->custom_fields as $custom_field ) { // Check scope $scope = $custom_field['scope']; $output = false; foreach ( $scope as $scope_item ) { switch ( $scope_item ) { default: if ( $post->post_type === $scope_item ) { $output = true; } break; } if ( $output ) { break; } } // Check capability if ( ! current_user_can( $custom_field['capability'], $post->ID ) ) { $output = false; } // Output if allowed if ( $output ) { $field_name = $this->prefix . $custom_field['name']; $field_title = $custom_field['title']; echo '
'; switch ( $custom_field['type'] ) { case 'radio': $checked_value = get_post_meta( $post->ID, $field_name, true ); if ( '' === $checked_value || false === $checked_value ) { $checked_value = $custom_field['default']; } echo '' . esc_html( $field_title ) . '
'; foreach ( $custom_field['values'] as $value => $label ) { echo '
'; } break; case 'menu': echo '
'; if ( sizeof( $custom_field['values'] ) === 0 ) { echo '' . __( 'This menu is empty.', 'timed-content' ) . ''; } else { $selected_value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); echo ''; } break; case 'list': // list echo '
'; if ( sizeof( $custom_field['values'] ) === 0 ) { echo '' . __( 'This menu is empty.', 'timed-content' ) . ''; } else { $selected_value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); echo ''; } break; case 'timezone-list': // timezone list $selected_value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); echo '
'; echo ''; break; case 'checkbox': // Checkbox $checked_value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); echo ''; break; case 'checkbox-list': // Checkbox list $checked_value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); echo '' . esc_html( $field_title ) . '
'; if ( sizeof( $custom_field['values'] ) === 0 ) { echo '' . __( 'This menu is empty.', 'timed-content' ) . ''; } else { foreach ( $custom_field['values'] as $value => $label ) { echo '
'; } } break; case 'textarea': case 'wysiwyg': // Text area $value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); echo '
'; echo ''; // WYSIWYG if ( 'wysiwyg' === $custom_field['type'] ) { echo '' . PHP_EOL; } break; case 'color-picker': $value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); // Color picker using WP's built-in Iris jQuery plugin echo '' . PHP_EOL; echo '
'; echo ''; break; case 'date': echo '
'; $value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); // Date picker using WP's built-in Datepicker jQuery plugin echo '' . PHP_EOL; echo ''; break; case 'datetime': echo '' . esc_html( $field_title ) . '
'; $value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); foreach ( $value as $k => $v ) { $value[ $k ] = esc_html( $v ); } // Date picker using WP's built-in Datepicker jQuery plugin // Time picker using jQuery UI Timepicker: http://fgelinas.com/code/timepicker echo '' . PHP_EOL; echo ''; echo ''; echo '\n"; echo ''; break; case 'number': $value = get_post_meta( $post->ID, $field_name, true ); if ( '' === $value ) { $value = $custom_field['default']; } else { $value = intval( $value ); } // Number picker using WP's built-in Spinner jQuery plugin echo '' . PHP_EOL; echo '
'; echo ''; break; case 'hidden': $value = ( '' === get_post_meta( $post->ID, $field_name, true ) ? $custom_field['default'] : get_post_meta( $post->ID, $field_name, true ) ); // Hidden field echo '' . esc_html( $field_title ) . '
'; echo '