/** * Render the fields registered to the metabox. * * The $fields property is an indexed array of fields and their properties. * Accepted option for are: * id (string) The field ID. This value MUST be unique. * desc (string) [optional] The field description. * type (string) The type of field which should be registered. This can be any of the supported field types or a custom field type. * Core supported field types are: * checkbox * checkboxgroup * radio * radio_inline * select * text (input) * textarea * datepicker * slider * quicktag * rte * value (mixed) string | array [optional] The function name or class method to be used retrieve a value for the field. * size (string) [optional] The size if the text input and textarea field types. * NOTE: Only used for the `text` field type. Valid options: small', 'regular' or 'large' * NOTE: Only used for the `textarea` field type. Valid options: small' or 'large' * options (mixed) string | array [optional] Valid value depends on the field type being rendered. * Field type / valid value for options * checkboxgroup (array) An associative array where the key is the checkbox value and the value is the checkbox label. * radio / radio_inline (array) An associative array where the key is the radio value and the value is the radio label. * select (array) An associative array where the key is the option value and the value is the option name. * rte (array) @link http://codex.wordpress.org/Function_Reference/wp_editor#Arguments * slider (array) The slider options. * min (int) The minimum slider step. * max (int) The maximum slider step. * step (int) The step the slider steps at. * default (mixed) The default value to be used. * * @access private * @since 0.8 * @global $wp_version * @param $fields array An indexed array of fields to render.. * * @return string */ private function fields($fields) { global $wp_version; // do_action( 'cn_metabox_table_before', $entry, $meta, $this->metabox ); foreach ($fields as $field) { $defaults = array('before' => '', 'after' => '', 'desc' => ''); $field = wp_parse_args($field, $defaults); // If the meta field has a specific method defined call the method and set the field value. // Otherwise, assume pulling from the meta table of the supplied object. if (isset($field['value']) && !empty($field['value'])) { $value = call_user_func(array($this->object, $field['value'])); } else { $value = $this->object->getMeta(array('key' => $field['id'], 'single' => TRUE)); } if (empty($value)) { $value = isset($field['default']) ? $field['default'] : ''; } /** * Apply custom classes to a metabox table. * * @since 8.3.4 * * @param array $class An indexed array of classes that should applied to the table element. * @param string $type The field type. * @param string $id The field id. */ $class = apply_filters('cn_metabox_table_class', array('cn-metabox-type-' . $field['type']), $field['type'], $field['id']); /** * Apply a custom id to a metabox table. * * @since 8.3.4 * * @param string $id The field id. */ $id = apply_filters('cn_metabox_table_id', 'cn-metabox-id-' . $field['id']); /** * Apply custom classes to a metabox table. * * @since 8.3.4 * * @param array $style An associative array of inline style attributes where the array key is the property and the array value is the property value. * @param string $type The field type. * @param string $id The field id. */ $style = apply_filters('cn_metabox_table_style', array(), $field['type'], $field['id']); $class = cnHTML::attribute('class', $class); $id = cnHTML::attribute('id', $id); $style = cnHTML::attribute('style', $style); echo '<tr' . $class . $id . $style . '>'; // For a label to be rendered the $field['name'] has to be supplied. // Show the label if $field['show_label'] is TRUE, OR, if it is not supplied assume TRUE and show it anyway. // The result will be the label will be shown unless specifically $field['show_label'] is set to FALSE. if (isset($field['name']) && !empty($field['name']) && (!isset($field['show_label']) || $field['show_label'] == TRUE)) { echo '<th class="cn-metabox-label">' . esc_html($field['name']) . '</th>'; } elseif (isset($field['name']) && !empty($field['name']) && (isset($field['show_label']) && $field['show_label'] == TRUE)) { echo '<th class="cn-metabox-label">' . esc_html($field['name']) . '</th>'; } elseif (!isset($field['show_label']) || $field['show_label'] == FALSE) { echo '<th class="cn-metabox-label-empty"> </th>'; } echo '<td>'; echo empty($field['before']) ? '' : $field['before']; /** * Apply custom classes to the field type container element. * * NOTE: The dynamic portion of the hook is the field type. * * @since 8.3.4 * * @param array $class An indexed array of classes that should applied to the element. * @param string $id The field id. */ $class = apply_filters("cn_metabox_{$field['type']}_class", array("cn-meta-field-type-{$field['type']}"), $field['id']); /** * Apply a custom id to the field type container element. * * NOTE: The dynamic portion of the hook is the field type. * * @since 8.3.4 * * @param string $id The field id. */ $id = apply_filters("cn_metabox_{$field['type']}_id", ''); /** * Apply custom classes to a field type container element. * * NOTE: The dynamic portion of the hook is the field type. * * @since 8.3.4 * * @param array $style An associative array of inline style attributes * where the array key is the property and the array value is the property value. * @param string $id The field id. */ $style = apply_filters("cn_metabox_{$field['type']}_style", array(), $field['id']); $class = cnHTML::attribute('class', $class); $id = cnHTML::attribute('id', $id); $style = cnHTML::attribute('style', $style); /** * Chance to manipulate the field value before rendering the field. * * NOTE: The dynamic portion of the hook is the field type. * * @since 0.8 * * @param mixed $value The field value. * @param array $field The field attributes array. */ $value = apply_filters("cn_meta_field_value-{$field['type']}", $value, $field); switch ($field['type']) { case 'checkbox': cnHTML::field(array('type' => 'checkbox', 'prefix' => '', 'class' => 'cn-checkbox', 'id' => $field['id'], 'name' => $field['id'], 'label' => $field['desc'], 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); break; case 'checkboxgroup': case 'checkbox-group': self::fieldDescription($field['desc']); cnHTML::field(array('type' => 'checkbox-group', 'prefix' => '', 'class' => 'cn-checkbox', 'id' => $field['id'], 'name' => $field['id'] . '[]', 'display' => 'block', 'options' => $field['options'], 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); break; case 'radio': self::fieldDescription($field['desc']); cnHTML::field(array('type' => 'radio', 'prefix' => '', 'class' => 'cn-radio-option', 'id' => $field['id'], 'name' => $field['id'] . '[]', 'display' => 'block', 'options' => $field['options'], 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); break; case 'radio_inline': case 'radio-inline': self::fieldDescription($field['desc']); cnHTML::field(array('type' => 'radio', 'prefix' => '', 'class' => 'cn-radio-option', 'id' => $field['id'], 'name' => $field['id'] . '[]', 'display' => 'inline', 'options' => $field['options'], 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); break; case 'select': cnHTML::field(array('type' => 'select', 'prefix' => '', 'class' => 'cn-select', 'id' => $field['id'], 'name' => $field['id'], 'display' => 'inline', 'options' => $field['options'], 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); self::fieldDescription($field['desc']); break; case 'text': $sizes = array('small', 'regular', 'large'); cnHTML::field(array('type' => 'text', 'prefix' => '', 'class' => isset($field['size']) && !empty($field['size']) && in_array($field['size'], $sizes) ? esc_attr($field['size']) . '-text' : 'large-text', 'id' => $field['id'], 'name' => $field['id'], 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); self::fieldDescription($field['desc']); break; case 'textarea': $sizes = array('small', 'large'); self::fieldDescription($field['desc']); cnHTML::field(array('type' => 'textarea', 'prefix' => '', 'class' => isset($field['size']) && !empty($field['size']) && in_array($field['size'], $sizes) ? esc_attr($field['size']) . '-text' : 'small-text', 'id' => $field['id'], 'name' => $field['id'], 'rows' => 10, 'cols' => 50, 'before' => '<div' . $class . $id . $style . '>', 'after' => '</div>'), $value); break; case 'datepicker': printf('<input type="text" class="cn-datepicker" id="%1$s" name="%1$s" value="%2$s"/>', esc_attr($field['id']), !empty($value) ? date('m/d/Y', strtotime($value)) : ''); wp_enqueue_script('jquery-ui-datepicker'); wp_enqueue_style('cn-admin-jquery-datepicker'); add_action('admin_print_footer_scripts', array(__CLASS__, 'datepickerJS')); add_action('wp_footer', array(__CLASS__, 'datepickerJS')); self::fieldDescription($field['desc']); break; case 'colorpicker': self::fieldDescription($field['desc']); printf('<input type="text" class="cn-colorpicker" id="%1$s" name="%1$s" value="%2$s"/>', esc_attr($field['id']), esc_attr($value)); wp_enqueue_style('wp-color-picker'); if (is_admin()) { wp_enqueue_script('wp-color-picker'); add_action('admin_print_footer_scripts', array(__CLASS__, 'colorpickerJS')); } else { /* * WordPress seems to only register the color picker scripts for use in the admin. * So, for the frontend, we must manually register and then enqueue. * @url http://wordpress.stackexchange.com/a/82722/59053 */ wp_enqueue_script('iris', admin_url('js/iris.min.js'), array('jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch'), FALSE, 1); wp_enqueue_script('wp-color-picker', admin_url('js/color-picker.min.js'), array('iris'), FALSE, 1); $colorpicker_l10n = array('clear' => __('Clear', 'connections'), 'defaultString' => __('Default', 'connections'), 'pick' => __('Select Color', 'connections'), 'current' => __('Current Color', 'connections')); wp_localize_script('wp-color-picker', 'wpColorPickerL10n', $colorpicker_l10n); add_action('wp_footer', array(__CLASS__, 'colorpickerJS')); } break; case 'slider': // Set the slider defaults. $defaults = array('min' => 0, 'max' => 100, 'step' => 1, 'value' => 0); $field['options'] = wp_parse_args(isset($field['options']) ? $field['options'] : array(), $defaults); printf('<div class="cn-slider-container" id="cn-slider-%1$s"></div><input type="text" class="small-text" id="%1$s" name="%1$s" value="%2$s"/>', esc_attr($field['id']), absint($value)); self::fieldDescription($field['desc']); $field['options']['value'] = absint($value); self::$slider[$field['id']] = $field['options']; wp_enqueue_script('jquery-ui-slider'); add_action('admin_print_footer_scripts', array(__CLASS__, 'sliderJS')); add_action('wp_footer', array(__CLASS__, 'sliderJS')); break; case 'quicktag': self::fieldDescription($field['desc']); echo '<div class="wp-editor-container">'; printf('<textarea class="wp-editor-area" rows="20" cols="40" id="%1$s" name="%1$s">%2$s</textarea>', esc_attr($field['id']), wp_kses_data($value)); echo '</div>'; self::$quickTagIDs[] = esc_attr($field['id']); wp_enqueue_script('jquery'); add_action('admin_print_footer_scripts', array(__CLASS__, 'quickTagJS')); add_action('wp_print_footer_scripts', array(__CLASS__, 'quickTagJS')); break; case 'rte': self::fieldDescription($field['desc']); if ($wp_version >= 3.3 && function_exists('wp_editor')) { // Set the rte defaults. $defaults = array('textarea_name' => sprintf('%1$s', $field['id'])); $atts = wp_parse_args(isset($field['options']) ? $field['options'] : array(), $defaults); wp_editor(cnSanitize::html($value), sprintf('%1$s', $field['id']), $atts); } else { /* * If this is pre WP 3.3, lets drop in the quick tag editor instead. */ echo '<div class="wp-editor-container">'; printf('<textarea class="wp-editor-area" rows="20" cols="40" id="%1$s" name="%1$s">%2$s</textarea>', esc_attr($field['id']), cnSanitize::quicktag($value)); echo '</div>'; self::$quickTagIDs[] = esc_attr($field['id']); wp_enqueue_script('jquery'); add_action('admin_print_footer_scripts', array(__CLASS__, 'quickTagJS')); add_action('wp_print_footer_scripts', array(__CLASS__, 'quickTagJS')); } break; case 'repeatable': echo '<table id="' . esc_attr($field['id']) . '-repeatable" class="meta_box_repeatable" cellspacing="0">'; echo '<tbody>'; $i = 0; // create an empty array if ($meta == '' || $meta == array()) { $keys = wp_list_pluck($field['repeatable'], 'id'); $meta = array(array_fill_keys($keys, NULL)); } $meta = array_values($meta); foreach ($meta as $row) { echo '<tr> <td><span class="sort hndle"></span></td><td>'; // foreach ( $field['repeatable'] as $repeatable ) { // if ( ! array_key_exists( $repeatable['id'], $meta[ $field['id'] ] ) ) $meta[ $field['id'] ][ $repeatable['id'] ] = NULL; // echo '<label>' . $repeatable['label'] . '</label><p>'; // self::fields( $repeatable, $meta[ $i ][ $repeatable['id'] ], array( $field['id'], $i ) ); // echo '</p>'; // } self::fields($field['repeatable']); echo '</td><td><a class="meta_box_repeatable_remove" href="#"></a></td></tr>'; $i++; } // end each row echo '</tbody>'; echo ' <tfoot> <tr> <th colspan="4"><a class="meta_box_repeatable_add" href="#"></a></th> </tr> </tfoot>'; echo '</table>'; break; default: do_action('cn_meta_field-' . $field['type'], $field, $value, $this->object); break; } echo empty($field['after']) ? '' : $field['after']; echo '</td>', '</tr>'; } // do_action( 'cn_metabox_table_after', $entry, $meta, $this->metabox ); }
/** * Renders the social media network field. * * @access private * @since 8.5.11 * * @param stdClass $date * @param string $token */ private static function dateField($date, $token = '::FIELD::') { // Grab an instance of the Connections object. $instance = Connections_Directory(); // Grab the date types. $dateTypes = $instance->options->getDateOptions(); ?> <div class="widget-top"> <div class="widget-title-action"><a class="widget-action"></a></div> <div class="widget-title"> <h4> <?php cnHTML::field(array('type' => 'select', 'class' => '', 'id' => 'date[' . $token . '][type]', 'options' => $dateTypes, 'required' => FALSE, 'label' => __('Type', 'connections'), 'return' => FALSE), isset($date->type) ? $date->type : ''); cnHTML::field(array('type' => 'radio', 'format' => 'inline', 'class' => '', 'id' => 'date[preferred]', 'options' => array($token => __('Preferred', 'connections')), 'required' => FALSE, 'before' => '<span class="preferred">', 'after' => '</span>', 'return' => FALSE), isset($date->preferred) && $date->preferred ? $token : ''); // Only show this if there are visibility options that the user is permitted to see. if (!empty(self::$visibility)) { cnHTML::field(array('type' => 'radio', 'format' => 'inline', 'class' => '', 'id' => 'date[' . $token . '][visibility]', 'options' => self::$visibility, 'required' => FALSE, 'before' => '<span class="visibility">' . __('Visibility', 'connections') . ' ', 'after' => '</span>', 'return' => FALSE), isset($date->visibility) ? $date->visibility : 'public'); } ?> </h4> </div> </div> <div class="widget-inside"> <?php cnHTML::field(array('type' => 'text', 'class' => 'datepicker', 'id' => 'date[' . $token . '][date]', 'required' => FALSE, 'label' => __('Date', 'connections'), 'before' => '', 'after' => '', 'return' => FALSE), isset($date->date) ? date('m/d/Y', strtotime($date->date)) : ''); ?> <?php if (isset($date->id)) { ?> <input type="hidden" name="date[<?php echo $token; ?> ][id]" value="<?php echo $date->id; ?> "> <?php } ?> <p class="cn-remove-button"> <a href="#" class="cn-remove cn-button button cn-button-warning" data-type="date" data-token="<?php echo $token; ?> "><?php esc_html_e('Remove', 'connections'); ?> </a> </p> </div> <?php }
/** * Renders a radio group. * * This is deprecated method, left in place for backward compatibility only. * * @access private * @deprecated * @since 0.8 * @param string $name The input option id/name value. * @param string $id UNUSED * @param array $options An associative array. Key is the option name and the value is the option value. * @param string $value [optional] The selected option. * * @return string */ public function buildRadio($name, $id, $options, $value = '') { $radio = cnHTML::field(array('type' => 'radio', 'display' => 'block', 'class' => '', 'id' => $name, 'options' => array_flip($options), 'required' => FALSE, 'return' => TRUE), $value); return $radio; }
/** * Renders the dates metabox. * * @access public * @since 0.8 * @param object $entry An instance of the cnEntry object. * @param array $metabox The metabox options array from self::register(). * @return string The dates metabox. */ public static function date($entry, $metabox) { // Grab an instance of the Connections object. $instance = Connections_Directory(); // Grab the email types. $dateTypes = $instance->options->getDateOptions(); echo '<div class="widgets-sortables ui-sortable" id="dates">', PHP_EOL; // --> Start template <-- \\ echo '<textarea id="date-template" style="display: none;">', PHP_EOL; echo '<div class="widget-top">', PHP_EOL; echo '<div class="widget-title-action"><a class="widget-action"></a></div>', PHP_EOL; echo '<div class="widget-title"><h4>', PHP_EOL; cnHTML::field(array('type' => 'select', 'class' => '', 'id' => 'date[::FIELD::][type]', 'options' => $dateTypes, 'required' => FALSE, 'label' => __('Type', 'connections'), 'return' => FALSE)); cnHTML::field(array('type' => 'radio', 'format' => 'inline', 'class' => '', 'id' => 'date[preferred]', 'options' => array('::FIELD::' => __('Preferred', 'connections')), 'required' => FALSE, 'before' => '<span class="preferred">', 'after' => '</span>', 'return' => FALSE)); // Only show this if there are visibility options that the user is permitted to see. if (!empty(self::$visibility)) { cnHTML::field(array('type' => 'radio', 'format' => 'inline', 'class' => '', 'id' => 'date[::FIELD::][visibility]', 'options' => self::$visibility, 'required' => FALSE, 'before' => '<span class="visibility">' . __('Visibility', 'connections') . ' ', 'after' => '</span>', 'return' => FALSE), 'public'); } echo '</h4></div>', PHP_EOL; echo '</div>', PHP_EOL; echo '<div class="widget-inside">'; cnHTML::field(array('type' => 'text', 'class' => 'datepicker', 'id' => 'date[::FIELD::][date]', 'required' => FALSE, 'label' => __('Date', 'connections'), 'before' => '', 'after' => '', 'return' => FALSE)); echo '<p class="cn-remove-button"><a href="#" class="cn-remove cn-button button cn-button-warning" data-type="date" data-token="::FIELD::">', __('Remove', 'connections'), '</a></p>'; echo '</div>', PHP_EOL; echo '</textarea>', PHP_EOL; // --> End template <-- \\ $dates = $entry->getDates(array(), FALSE); //print_r($dates); if (!empty($dates)) { foreach ($dates as $date) { $token = str_replace('-', '', cnUtility::getUUID()); $selectName = 'date[' . $token . '][type]'; $preferred = $date->preferred ? $token : ''; echo '<div class="widget date" id="date-row-' . $token . '">', PHP_EOL; echo '<div class="widget-top">', PHP_EOL; echo '<div class="widget-title-action"><a class="widget-action"></a></div>', PHP_EOL; echo '<div class="widget-title"><h4>', PHP_EOL; cnHTML::field(array('type' => 'select', 'class' => '', 'id' => 'date[' . $token . '][type]', 'options' => $dateTypes, 'required' => FALSE, 'label' => __('Type', 'connections'), 'return' => FALSE), $date->type); cnHTML::field(array('type' => 'radio', 'format' => 'inline', 'class' => '', 'id' => 'date[preferred]', 'options' => array($token => __('Preferred', 'connections')), 'required' => FALSE, 'before' => '<span class="preferred">', 'after' => '</span>', 'return' => FALSE), $preferred); // Only show this if there are visibility options that the user is permitted to see. if (!empty(self::$visibility)) { cnHTML::field(array('type' => 'radio', 'format' => 'inline', 'class' => '', 'id' => 'date[' . $token . '][visibility]', 'options' => self::$visibility, 'required' => FALSE, 'before' => '<span class="visibility">' . __('Visibility', 'connections') . ' ', 'after' => '</span>', 'return' => FALSE), $date->visibility); } echo '</h4></div>', PHP_EOL; echo '</div>', PHP_EOL; echo '<div class="widget-inside">', PHP_EOL; cnHTML::field(array('type' => 'text', 'class' => 'datepicker', 'id' => 'date[' . $token . '][date]', 'required' => FALSE, 'label' => __('Date', 'connections'), 'before' => '', 'after' => '', 'return' => FALSE), date('m/d/Y', strtotime($date->date))); echo '<input type="hidden" name="date[', $token, '][id]" value="', $date->id, '">', PHP_EOL; echo '<p class="cn-remove-button"><a href="#" class="cn-remove cn-button button cn-button-warning" data-type="date" data-token="' . $token . '">', __('Remove', 'connections'), '</a></p>', PHP_EOL; echo '</div>', PHP_EOL; echo '</div>', PHP_EOL; } } echo '</div>', PHP_EOL; echo '<p class="add"><a href="#" class="cn-add cn-button button" data-type="date" data-container="dates">', __('Add Date', 'connections'), '</a></p>', PHP_EOL; }