From 95c0b0ec6072c6a32a647557fa6f57efc18bbd53 Mon Sep 17 00:00:00 2001 From: Justin Sternberg Date: Fri, 7 Feb 2014 10:01:28 -0500 Subject: [PATCH 01/71] revamp types to work with more repeatable field types --- helpers/cmb_Meta_Box_types.php | 256 ++-- init.php | 9 +- js/cmb.js | 175 ++- js/cmb.min.js | 30 +- js/jquery.datePicker.min.js | 2038 ++++++++++++++++++++++++++++++++ 5 files changed, 2286 insertions(+), 222 deletions(-) create mode 100644 js/jquery.datePicker.min.js diff --git a/helpers/cmb_Meta_Box_types.php b/helpers/cmb_Meta_Box_types.php index b462939..5c28a3a 100644 --- a/helpers/cmb_Meta_Box_types.php +++ b/helpers/cmb_Meta_Box_types.php @@ -73,22 +73,25 @@ public static function get() { * @return strgin Field's description markup */ private static function desc( $paragraph = false ) { + // Prevent description from printing multiple times for repeatable fields + if ( self::$iterator > 0 ) { + return ''; + } + $tag = $paragraph ? 'p' : 'span'; $desc = cmb_Meta_Box::$field['desc']; return "\n<$tag class=\"cmb_metabox_description\">$desc\n"; } /** - * Generates repeatable text fields + * Generates repeatable fields * @since 1.0.0 - * @param string $field Metabox field + * @param array $field Metabox field * @param mixed $meta Field's meta value - * @param string $class Field's class attribute - * @param string $type Field Type + * @param int $object_id Object ID + * @param string $object_type Object Type */ - private static function repeat_text_field( $meta, $class = '', $type = 'text' ) { - - $field = cmb_Meta_Box::$field; self::$meta = $meta; self::$type = $type; + public static function render_repeatable_field( $field, $meta, $object_id, $object_type ) { // check for default content $default = isset( $field['default'] ) ? array( $field['default'] ) : false; @@ -100,39 +103,33 @@ private static function repeat_text_field( $meta, $class = '', $type = 'text' ) $meta = $default; } - self::repeat_table_open( $class ); - - $class = $class ? $class .' widefat' : 'widefat'; + $class = 'widefat'; + self::repeat_table_open(); if ( !empty( $meta ) ) { foreach ( (array) $meta as $val ) { - self::repeat_row( self::text_input( $class, $val ) ); + self::open_repeat_row(); + self::$iterator = self::$iterator ? self::$iterator + 1 : 1; + call_user_func( array( __CLASS__, $field['type'] ), $field, $val, $object_id, $object_type ); + self::close_repeat_row(); } } else { - self::repeat_row( self::text_input( $class ) ); + self::open_repeat_row(); + self::$iterator = 1; + call_user_func( array( __CLASS__, $field['type'] ), $field, $meta, $object_id, $object_type ); + self::close_repeat_row(); } - self::empty_row( self::text_input( $class ) ); + self::open_empty_row(); + self::$iterator = self::$iterator ? self::$iterator + 1 : 1; + call_user_func( array( __CLASS__, $field['type'] ), $field, null, $object_id, $object_type ); + self::close_repeat_row(); + self::repeat_table_close(); // reset iterator self::$iterator = 0; } - /** - * Text input field used by repeatable fields - * @since 1.0.0 - * @param string $class Field's class attribute - * @param mixed $val Field's meta value - * @return string HTML text input - */ - private static function text_input( $class = '', $val = '' ) { - self::$iterator = self::$iterator ? self::$iterator + 1 : 1; - $before = ''; - if ( cmb_Meta_Box::$field['type'] == 'text_money' ) - $before = ! empty( cmb_Meta_Box::$field['before'] ) ? ' ' : '$ '; - return $before . ''; - } - /** * Generates repeatable field opening table markup for repeatable fields * @since 1.0.0 @@ -150,32 +147,19 @@ private static function repeat_table_close() { echo '

'. __( 'Add Row', 'cmb' ) .'

'; } - /** - * Generates table row markup for repeatable fields - * @since 1.0.0 - * @param string $input Table cell markup - */ - private static function repeat_row( $input ) { - echo '', self::repeat_cell( $input ) ,''; + private static function open_repeat_row() { + echo ''; + echo ''; } - /** - * Generates the empty table row markup (for duplication) for repeatable fields - * @since 1.0.0 - * @param string $input Table cell markup - */ - private static function empty_row( $input ) { - echo '', self::repeat_cell( $input ) ,''; + private static function open_empty_row() { + echo ''; + echo ''; } - /** - * Generates table cell markup for repeatable fields - * @since 1.0.0 - * @param string $input Text input field - * @return string HTML table cell markup - */ - private static function repeat_cell( $input ) { - return ''. $input .''. __( 'Remove', 'cmb' ) .''; + private static function close_repeat_row() { + echo ''. __( 'Remove', 'cmb' ) .''; + echo ''; } /** @@ -262,75 +246,77 @@ public static function esc( $meta_value, $func = '' ) { $func = is_string( $func ) && ! empty( $func ) ? $func : 'esc_attr'; $meta_value = ! empty( $meta_value ) ? $meta_value : $field['default']; - return call_user_func( $func, $meta_value ); + if ( is_array( $meta_value ) ) { + foreach ( $meta_value as $key => $value ) { + $meta_value[ $key ] = call_user_func( $func, $value ); + } + } else { + $meta_value = call_user_func( $func, $meta_value ); + } + + return $meta_value; + } + + public static function _name( $suffix ) { + return cmb_Meta_Box::$field['id'] . $suffix . ( cmb_Meta_Box::$field['repeatable'] ? '[]' : '' ); + } + + public static function _id( $suffix = '' ) { + return cmb_Meta_Box::$field['id'] . $suffix . ( cmb_Meta_Box::$field['repeatable'] ? '_'. self::$iterator .'" data-iterator="'. self::$iterator : '' ); } + public static function _input( $meta, $args = array() ) { + $args = wp_parse_args( $args, array( + 'classes' => 'regular-text', + 'type' => 'text', + ) ); + return '' . self::desc( true ); + } /** * Begin Field Types */ public static function text( $field, $meta ) { - if ( $field['repeatable'] ) { - return self::repeat_text_field( $meta ); - } - - echo '', self::desc( true ); + echo '', self::desc( true ); } public static function text_small( $field, $meta ) { - if ( $field['repeatable'] ) { - return self::repeat_text_field( $meta, 'cmb_text_small' ); - } - - echo '', self::desc(); + echo '', self::desc(); } public static function text_medium( $field, $meta ) { - if ( $field['repeatable'] ) { - return self::repeat_text_field( $meta, 'cmb_text_medium' ); - } - echo '', self::desc(); + echo '', self::desc(); } public static function text_email( $field, $meta ) { - if ( $field['repeatable'] ) { - return self::repeat_text_field( $meta, 'cmb_text_email cmb_text_medium', 'email' ); - } - - echo '', self::desc( true ); + echo '', self::desc( true ); } public static function text_url( $field, $meta ) { - if ( $field['repeatable'] ) { - return self::repeat_text_field( $meta, 'cmb_text_url cmb_text_medium' ); - } - - echo '', self::desc( true ); + echo '', self::desc( true ); } public static function text_date( $field, $meta ) { - echo '', self::desc(); + echo '', self::desc(); } public static function text_date_timestamp( $field, $meta ) { - echo '', self::desc(); + echo '', self::desc(); } public static function text_datetime_timestamp( $field, $meta, $object_id ) { - // This will be used if there is a select_timezone set for this field $tz_offset = cmb_Meta_Box::field_timezone_offset( $object_id ); if ( ! empty( $tz_offset ) ) { $meta -= $tz_offset; } - echo ''; - echo '', self::desc(); + echo ''; + echo '', self::desc(); } public static function text_datetime_timestamp_timezone( $field, $meta ) { - $datetime = unserialize( $meta ); $meta = $tzstring = false; @@ -341,16 +327,16 @@ public static function text_datetime_timestamp_timezone( $field, $meta ) { $meta = $datetime->getTimestamp() + $tz->getOffset( new DateTime('NOW') ); } - echo ''; - echo ''; + echo ''; + echo ''; - echo ''; echo wp_timezone_choice( $tzstring ); echo '', self::desc(); } public static function text_time( $field, $meta ) { - echo '', self::desc(); + echo '', self::desc(); } public static function select_timezone( $field, $meta ) { @@ -358,17 +344,13 @@ public static function select_timezone( $field, $meta ) { if ( '' === $meta ) $meta = cmb_Meta_Box::timezone_string(); - echo ''; echo wp_timezone_choice( $meta ); echo ''; } public static function text_money( $field, $meta ) { - if ( $field['repeatable'] ) { - return self::repeat_text_field( $meta, 'cmb_text_money' ); - } - - echo ! empty( $field['before'] ) ? '' : '$', ' ', self::desc(); + echo ! empty( $field['before'] ) ? '' : '$', ' ', self::desc(); } public static function colorpicker( $field, $meta ) { @@ -378,31 +360,31 @@ public static function colorpicker( $field, $meta ) { $meta = '#' . $meta; elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta ) ) // Value doesn't match #123abc, so sanitize to just #. $meta = "#"; - echo '', self::desc(); + echo '', self::desc(); } public static function textarea( $field, $meta ) { - echo '', self::desc( true ); + echo '', self::desc( true ); } public static function textarea_small( $field, $meta ) { - echo '', self::desc( true ); + echo '', self::desc( true ); } public static function textarea_code( $field, $meta ) { - echo '
', self::desc( true ); + echo '
', self::desc( true ); } public static function select( $field, $meta ) { $meta = self::esc( $meta ); - echo ''; foreach ( $field['options'] as $option_key => $option ) { // Check for the "old" way $label = isset( $option['name'] ) ? $option['name'] : $option; $value = isset( $option['value'] ) ? $option['value'] : $option_key; - echo ''; + echo ''; } echo '', self::desc( true ); @@ -418,7 +400,7 @@ public static function radio( $field, $meta ) { $label = isset( $option['name'] ) ? $option['name'] : $option; $value = isset( $option['value'] ) ? $option['value'] : $option_key; - echo '
  • '; + echo '
  • '; $i++; } echo '', self::desc( true ); @@ -429,14 +411,14 @@ public static function radio_inline( $field, $meta ) { } public static function checkbox( $field, $meta ) { - echo ' '; + echo ' '; } public static function multicheck( $field, $meta ) { echo '', self::desc(); @@ -458,7 +440,7 @@ public static function wysiwyg( $field, $meta ) { public static function taxonomy_select( $field, $meta, $object_id ) { - echo ''; $names = self::get_object_terms( $object_id, $field['taxonomy'] ); $terms = get_terms( $field['taxonomy'], 'hide_empty=0' ); foreach ( $terms as $term ) { @@ -472,15 +454,15 @@ public static function taxonomy_select( $field, $meta, $object_id ) { } public static function taxonomy_radio( $field, $meta, $object_id ) { - $names = self::get_object_terms( $object_id, $field['taxonomy'] ); $terms = get_terms( $field['taxonomy'], 'hide_empty=0' ); + echo '', self::desc( true ); @@ -491,18 +473,24 @@ public static function taxonomy_radio_inline( $field, $meta ) { } public static function taxonomy_multicheck( $field, $meta, $object_id ) { - echo '