From f67004a57f43b7791968d198af12a7644a36b277 Mon Sep 17 00:00:00 2001 From: Barry Ceelen Date: Fri, 30 Sep 2016 13:54:15 +0200 Subject: [PATCH 01/20] Fix check repeatable value exists --- class.cmb-meta-box.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/class.cmb-meta-box.php b/class.cmb-meta-box.php index 52635753..96d1c111 100644 --- a/class.cmb-meta-box.php +++ b/class.cmb-meta-box.php @@ -43,7 +43,8 @@ public function init_fields( $post_id = 0 ) { // If we are on a post edit screen - get metadata value of the field for this post if ( $post_id ) { - $values = (array) get_post_meta( $post_id, $field['id'], ! empty( $field['repeatable'] ) ); + $single = ! empty( $field['repeatable'] ) ? ! $field['repeatable'] : true; + $values = (array) get_post_meta( $post_id, $field['id'], $single ); } if ( class_exists( $class ) ) { From 8633d959920226cbcc987778c33ce1561c165823 Mon Sep 17 00:00:00 2001 From: Barry Ceelen Date: Sat, 1 Oct 2016 15:50:53 +0200 Subject: [PATCH 02/20] Spidify repeatable value check --- class.cmb-meta-box.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class.cmb-meta-box.php b/class.cmb-meta-box.php index 96d1c111..9554db73 100644 --- a/class.cmb-meta-box.php +++ b/class.cmb-meta-box.php @@ -43,7 +43,7 @@ public function init_fields( $post_id = 0 ) { // If we are on a post edit screen - get metadata value of the field for this post if ( $post_id ) { - $single = ! empty( $field['repeatable'] ) ? ! $field['repeatable'] : true; + $single = ( ! isset( $field['repeatable'] ) || false === $field['repeatable'] ); $values = (array) get_post_meta( $post_id, $field['id'], $single ); } From 95f1833e0bd73f2dfa8ced64c47018af0dfd2030 Mon Sep 17 00:00:00 2001 From: Ajit Bohra Date: Thu, 6 Oct 2016 02:34:42 +0530 Subject: [PATCH 03/20] Readme screenshot update - Main screenshot updated in readme.md - Code for the same added to example-function.php so we can quickly change screenshots using the same --- custom-meta-boxes.php | 3 +- example-functions.php | 65 +++++++++++++++++++++++++++++++++++++++---- readme.md | 2 +- 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/custom-meta-boxes.php b/custom-meta-boxes.php index 3ab68063..f01cd6a9 100755 --- a/custom-meta-boxes.php +++ b/custom-meta-boxes.php @@ -29,7 +29,8 @@ define( 'CMB_PATH', plugin_dir_path( __FILE__ ) ); if ( ! defined( 'CMB_URL' ) ) - define( 'CMB_URL', plugins_url( '', __FILE__ ) ); + //define( 'CMB_URL', plugins_url( '', __FILE__ ) ); + define( 'CMB_URL', 'http://localhost/wp-dev/wp-content/themes/twentysixteen/Custom-Meta-Boxes' ); include_once( CMB_PATH . '/classes.fields.php' ); include_once( CMB_PATH . '/class.cmb-meta-box.php' ); diff --git a/example-functions.php b/example-functions.php index 8339f11c..7d5b85c6 100644 --- a/example-functions.php +++ b/example-functions.php @@ -6,8 +6,7 @@ * @return array */ function cmb_sample_metaboxes( array $meta_boxes ) { - - // Example of all available fields + // Example of all available fields $fields = array( @@ -44,7 +43,7 @@ function cmb_sample_metaboxes( array $meta_boxes ) { array( 'id' => 'field-22', 'name' => 'Color', 'type' => 'colorpicker' ), - array( 'id' => 'field-23', 'name' => 'Location', 'type' => 'gmap', 'google_api_key' => '{CUSTOM_KEY}' ), + array( 'id' => 'field-23', 'name' => 'Location', 'type' => 'gmap' ), array( 'id' => 'field-24', 'name' => 'Title Field', 'type' => 'title' ), @@ -52,7 +51,7 @@ function cmb_sample_metaboxes( array $meta_boxes ) { $meta_boxes[] = array( 'title' => 'CMB Test - all fields', - 'pages' => 'post', + 'pages' => array('post','page'), 'fields' => $fields ); @@ -73,7 +72,7 @@ function cmb_sample_metaboxes( array $meta_boxes ) { $meta_boxes[] = array( 'title' => 'Groups and Columns', - 'pages' => 'post', + 'pages' => array('post','page'), 'fields' => $groups_and_cols ); @@ -86,7 +85,7 @@ function cmb_sample_metaboxes( array $meta_boxes ) { $meta_boxes[] = array( 'title' => 'CMB Test - group (all fields)', - 'pages' => 'post', + 'pages' => array('post','page'), 'fields' => array( array( 'id' => 'gp', @@ -100,6 +99,60 @@ function cmb_sample_metaboxes( array $meta_boxes ) { ) ); + // Main Screenshot Demo + $prefix = "example-"; + $cmb_samples = array( + + array( 'id' => $prefix.'field-0', 'name' => 'Title field (Display plain text)', 'type' => 'title' ), + + array( 'id' => $prefix.'field-2', 'name' => 'WYSIWYG field', 'type' => 'wysiwyg', 'options' => array( 'editor_height' => '100'),'cols' => 4 ), + array( 'id' => $prefix.'field-6', 'name' => 'Textare field', 'type' => 'textarea','rows' => 11, 'cols' => 4 ), + array( 'id' => $prefix.'field-7', 'name' => 'Code field', 'type' => 'textarea_code','rows' => 11,'cols' => 4 ), + + array( 'id' => $prefix.'field-13', 'name' => 'Location', 'type' => 'gmap','cols' => 6), + array( 'id' => $prefix.'field-1', 'name' => 'Text input field', 'type' => 'text','cols' => 5 ), + array( 'id' => $prefix.'field-8', 'name' => 'URL field', 'type' => 'text_url','cols' => 5 ), + array( 'id' => $prefix.'field-9', 'name' => 'Text small field', 'type' => 'text_small','cols' => 2 ), + array( 'id' => $prefix.'field-10', 'name' => 'A unix date field', 'type' => 'date_unix','cols' => 2 ), + array( 'id' => $prefix.'field-11', 'name' => 'A time field', 'type' => 'time','cols' => 2 ), + array( + 'id' => $prefix.'field-13', + 'name' => 'Select Field', + 'type' => 'select', + 'cols' => 5, + 'options' => array( + 'option-1' => 'Option 1', + 'option-2' => 'Option 2', + 'option-3' => 'Option 3', + ) + ), + + array( 'id' => $prefix.'field-4', 'name' => 'File field', 'type' => 'file', 'file_type' => 'image','cols' => 2), + array( 'id' => $prefix.'field-5', 'name' => 'Image upload field', 'type' => 'image', 'show_size' => true,'cols' => 2 ), + array( 'id' => $prefix.'field-3', 'name' => 'Date (unix) input field', 'type' => 'datetime_unix','repeatable' => true, 'repeatable_max' => 5, 'sortable' => false,'cols' => 3 ), + array( 'id' => $prefix.'field-12', 'name' => 'Color', 'type' => 'colorpicker','cols' => 4 ), + + + array( + 'id' => $prefix.'field-12', + 'type' => 'radio', + 'name' => 'Radio field', + 'cols' => 5, + 'options' => array( + 'option-1' => 'Option 1', + 'option-2' => 'Option 2', + 'option-3' => 'Option 3', + ) + ), + array( 'id' => $prefix.'field-12', 'name' => 'Checkbox field', 'type' => 'checkbox', 'cols' => 2 ), + ); + + $meta_boxes[] = array( + 'title' => 'Custom Meta Boxes - Example', + 'pages' => array('post','page'), + 'fields' => $cmb_samples + ); + return $meta_boxes; } diff --git a/readme.md b/readme.md index 9cbac657..7e0dac9a 100644 --- a/readme.md +++ b/readme.md @@ -23,7 +23,7 @@ This project is aimed at developers and is easily extended and customized. It ta The framework also features a basic layout engine for fields, allowing you to align fields to a simple 12 column grid. -![Overview](https://f.cloud.github.com/assets/494927/386456/1ea0d6f6-a6a7-11e2-88ab-ce6497c2b757.png) +![Overview](https://cloud.githubusercontent.com/assets/1039236/19131223/426658c4-8b6c-11e6-808d-b689ee6820ac.jpg) ## Usage From b32079b586c0741430c42563ad17ae5d95503ddb Mon Sep 17 00:00:00 2001 From: Shady Sharaf Date: Mon, 10 Oct 2016 16:49:38 +0200 Subject: [PATCH 04/20] Save location text as well --- classes.fields.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes.fields.php b/classes.fields.php index 6299a7e3..6bb74100 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -1680,7 +1680,7 @@ public function html() { // Ensure all args used are set $value = wp_parse_args( $this->get_value(), - array( 'lat' => null, 'long' => null, 'elevation' => null ) + array( 'lat' => null, 'long' => null, 'elevation' => null, 'text' => null ) ); $style = array( @@ -1692,7 +1692,7 @@ public function html() { ?> - class_attr( 'map-search' ); ?> id_attr(); ?> /> + class_attr( 'map-search' ); ?> id_attr(); ?> name_attr( '[text]' ); ?> value="" />
From 183acd72d5064786b6a2b7c79fcff62eca11e4ba Mon Sep 17 00:00:00 2001 From: Shady Sharaf Date: Mon, 10 Oct 2016 17:08:58 +0200 Subject: [PATCH 05/20] Load gmaps scripts from JS --- classes.fields.php | 13 +++---------- js/field-gmap.js | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/classes.fields.php b/classes.fields.php index ecffea34..3a65dd4e 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -1662,25 +1662,18 @@ public function enqueue_scripts() { parent::enqueue_scripts(); - $maps_src = '//maps.google.com/maps/api/js?libraries=places'; + wp_enqueue_script( 'cmb-google-maps-script', trailingslashit( CMB_URL ) . 'js/field-gmap.js', array( 'jquery' ) ); // Check for our key with either a field argument or constant. + $key = ''; if ( ! empty( $this->args['google_api_key'] ) ){ $key = $this->args['google_api_key']; } elseif ( defined( 'CMB_GAPI_KEY' ) ) { $key = CMB_GAPI_KEY; } - // Only add the key argument if it's been set. - if ( ! empty( $key ) ) { - $maps_src = add_query_arg( 'key', $key, $maps_src ); - } - - // Enqueue our scripts. - wp_enqueue_script( 'cmb-google-maps', $maps_src ); - wp_enqueue_script( 'cmb-google-maps-script', trailingslashit( CMB_URL ) . 'js/field-gmap.js', array( 'jquery', 'cmb-google-maps' ) ); - wp_localize_script( 'cmb-google-maps-script', 'CMBGmaps', array( + 'key' => $key, 'defaults' => array( 'latitude' => $this->args['default_lat'], 'longitude' => $this->args['default_long'], diff --git a/js/field-gmap.js b/js/field-gmap.js index 354c8985..3acff1e1 100644 --- a/js/field-gmap.js +++ b/js/field-gmap.js @@ -88,14 +88,19 @@ } }); - } + }; - CMB.addCallbackForInit( function() { - $('.CMB_Gmap_Field .field-item').each(function() { - CMBGmapsInit( $(this) ); - }); - } ); + window.CMB_CMAPS_INIT = function() { + 'use strict'; + + $( '.CMB_Gmap_Field .field-item' ).each( function() { + CMBGmapsInit( $( this ) ); + } ); + + CMB.addCallbackForClonedField( ['CMB_Gmap_Field'], CMBGmapsInit ); + }; + + $.getScript( 'https://maps.google.com/maps/api/js?sensor=true&libraries=places&callback=CMB_CMAPS_INIT&key=' + CMBGmaps.key ); - CMB.addCallbackForClonedField( ['CMB_Gmap_Field'], CMBGmapsInit ); }(jQuery)); From 192b27a563d01c842ffb6b01ff68eaf351105097 Mon Sep 17 00:00:00 2001 From: Shady Sharaf Date: Mon, 10 Oct 2016 17:11:49 +0200 Subject: [PATCH 06/20] Schemeless URL --- js/field-gmap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/field-gmap.js b/js/field-gmap.js index 3acff1e1..e4927336 100644 --- a/js/field-gmap.js +++ b/js/field-gmap.js @@ -100,7 +100,7 @@ CMB.addCallbackForClonedField( ['CMB_Gmap_Field'], CMBGmapsInit ); }; - $.getScript( 'https://maps.google.com/maps/api/js?sensor=true&libraries=places&callback=CMB_CMAPS_INIT&key=' + CMBGmaps.key ); + $.getScript( '//maps.google.com/maps/api/js?sensor=true&libraries=places&callback=CMB_CMAPS_INIT&key=' + CMBGmaps.key ); }(jQuery)); From c2cfe7f9bc75d4eef49270273e1f2fc1414429e1 Mon Sep 17 00:00:00 2001 From: mikeselander Date: Fri, 11 Nov 2016 22:42:37 +0100 Subject: [PATCH 07/20] Spaced out the array items --- classes.fields.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/classes.fields.php b/classes.fields.php index 474fc499..f1465a3c 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -1692,7 +1692,12 @@ public function html() { // Ensure all args used are set $value = wp_parse_args( $this->get_value(), - array( 'lat' => null, 'long' => null, 'elevation' => null, 'text' => null ) + array( + 'lat' => null, + 'long' => null, + 'elevation' => null, + 'text' => null + ) ); $style = array( From 73a01b08f65e67e3b4a415f63130070edca8a1aa Mon Sep 17 00:00:00 2001 From: Mike Selander Date: Sat, 12 Nov 2016 13:01:31 +0100 Subject: [PATCH 08/20] Added Pull Request Template --- .github/PULL_REQUEST_TEMPLATE | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE new file mode 100644 index 00000000..f22ff583 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE @@ -0,0 +1,10 @@ +{{Description of feature here}} + +Resolves # . + +*I have:* + - [ ] Run WP VIP PHPCS and code parses without errors + - [ ] Run PHPUnit and all tests are passing after adding any necessary new tests + - [ ] Tested feature/bugfix on single and multisite install + +@mikeselander From f9a029608a0cd5e1236c1b170d1c6e17f7e3d005 Mon Sep 17 00:00:00 2001 From: mikeselander Date: Sat, 12 Nov 2016 23:23:19 +0100 Subject: [PATCH 09/20] Fixed all general errors --- class.cmb-meta-box.php | 88 +++++----- classes.fields.php | 288 ++++++++++++++++++------------- custom-meta-boxes.php | 66 +++---- example-functions.php | 40 +++-- fields-anywhere.php | 46 ++--- tests/bootstrap.php | 7 +- tests/testDateField.php | 53 +++--- tests/testField.php | 11 +- tests/testGroupField.php | 15 +- tests/testIsMetaBoxDisplayed.php | 21 ++- 10 files changed, 354 insertions(+), 281 deletions(-) diff --git a/class.cmb-meta-box.php b/class.cmb-meta-box.php index 52635753..ae441bc3 100644 --- a/class.cmb-meta-box.php +++ b/class.cmb-meta-box.php @@ -12,8 +12,9 @@ function __construct( $meta_box ) { $this->_meta_box = $meta_box; - if ( empty( $this->_meta_box['id'] ) ) + if ( empty( $this->_meta_box['id'] ) ) { $this->_meta_box['id'] = sanitize_title( $this->_meta_box['title'] ); + } add_action( 'dbx_post_advanced', array( &$this, 'init_fields_for_post' ) ); add_action( 'cmb_init_fields', array( &$this, 'init_fields' ) ); @@ -31,7 +32,6 @@ function __construct( $meta_box ) { public function init_fields( $post_id = 0 ) { foreach ( $this->_meta_box['fields'] as $key => $field ) { - $values = array(); $args = $field; @@ -49,27 +49,26 @@ public function init_fields( $post_id = 0 ) { if ( class_exists( $class ) ) { $this->fields[] = new $class( $field['id'], $field['name'], (array) $values, $args ); } - } } public function init_fields_for_post() { - global $post, $temp_ID; + global $post; // Get the current ID - if( isset( $_GET['post'] ) ) + if ( isset( $_GET['post'] ) ) { $post_id = $_GET['post']; - - elseif( isset( $_POST['post_ID'] ) ) + } elseif ( isset( $_POST['post_ID'] ) ) { $post_id = $_POST['post_ID']; - - elseif ( ! empty( $post->ID ) ) + } elseif ( ! empty( $post->ID ) ) { $post_id = $post->ID; + } - if ( is_page() || ! isset( $post_id ) ) + if ( is_page() || ! isset( $post_id ) ) { return false; + } $this->init_fields( (int) $post_id ); @@ -82,11 +81,12 @@ function enqueue_scripts() { wp_localize_script( 'cmb-scripts', 'CMBData', array( 'strings' => array( 'confirmDeleteField' => esc_html__( 'Are you sure you want to delete this field?', 'cmb' ), - ) + ), ) ); - foreach ( $this->fields as $field ) + foreach ( $this->fields as $field ) { $field->enqueue_scripts(); + } } @@ -94,21 +94,23 @@ function enqueue_styles() { $suffix = CMB_DEV ? '' : '.min'; - if ( version_compare( get_bloginfo( 'version' ), '3.8', '>=' ) ) + if ( version_compare( get_bloginfo( 'version' ), '3.8', '>=' ) ) { wp_enqueue_style( 'cmb-styles', trailingslashit( CMB_URL ) . "css/dist/cmb$suffix.css" ); - else + } else { wp_enqueue_style( 'cmb-styles', trailingslashit( CMB_URL ) . 'css/legacy.css' ); + } - foreach ( $this->fields as $field ) + foreach ( $this->fields as $field ) { $field->enqueue_styles(); + } } // Add metabox function add() { - $this->_meta_box['context'] = empty($this->_meta_box['context']) ? 'normal' : $this->_meta_box['context']; - $this->_meta_box['priority'] = empty($this->_meta_box['priority']) ? 'low' : $this->_meta_box['priority']; + $this->_meta_box['context'] = empty( $this->_meta_box['context'] ) ? 'normal' : $this->_meta_box['context']; + $this->_meta_box['priority'] = empty( $this->_meta_box['priority'] ) ? 'low' : $this->_meta_box['priority']; // Backwards compatablilty. if ( isset( $this->_meta_box['show_on']['key'] ) ) { @@ -119,7 +121,7 @@ function add() { foreach ( (array) $this->_meta_box['pages'] as $page ) { if ( $this->is_metabox_displayed() ) { - add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority'] ) ; + add_meta_box( $this->_meta_box['id'], $this->_meta_box['title'], array( &$this, 'show' ), $page, $this->_meta_box['context'], $this->_meta_box['priority'] ); } } @@ -242,7 +244,7 @@ function show() { $this->description(); ?> - + fields ); @@ -255,15 +257,15 @@ function show() { * * @param CMB_Field[] $fields */ - static function layout_fields( array $fields ) { ?> - + static function layout_fields( array $fields ) { + ?>
+ if ( 0 == $current_colspan ) : ?>
@@ -271,13 +273,15 @@ static function layout_fields( array $fields ) { ?> $current_colspan += $field->args['cols']; - $classes = array( 'field', get_class($field) ); + $classes = array( 'field', get_class( $field ) ); - if ( ! empty( $field->args['repeatable'] ) ) + if ( ! empty( $field->args['repeatable'] ) ) { $classes[] = 'repeatable'; + } - if ( ! empty( $field->args['sortable'] ) ) + if ( ! empty( $field->args['sortable'] ) ) { $classes[] = 'cmb-sortable'; + } // Assign extra class for has label or has no label if ( ! empty( $field->title ) ) { @@ -288,13 +292,13 @@ static function layout_fields( array $fields ) { ?> $attrs = array( sprintf( 'id="%s"', sanitize_html_class( $field->id ) ), - sprintf( 'class="%s"', esc_attr( implode(' ', array_map( 'sanitize_html_class', $classes ) ) ) ) + sprintf( 'class="%s"', esc_attr( implode( ' ', array_map( 'sanitize_html_class', $classes ) ) ) ), ); // Field Repeatable Max. - if ( isset( $field->args['repeatable_max'] ) ) + if ( isset( $field->args['repeatable_max'] ) ) { $attrs[] = sprintf( 'data-rep-max="%s"', intval( $field->args['repeatable_max'] ) ); - + } ?>
@@ -307,7 +311,7 @@ static function layout_fields( array $fields ) { ?>
- @@ -324,13 +328,11 @@ static function layout_fields( array $fields ) { ?> function strip_repeatable( $values ) { foreach ( $values as $key => $value ) { - - if ( false !== strpos( $key, 'cmb-group-x' ) || false !== strpos( $key, 'cmb-field-x' ) ) - unset( $values[$key] ); - - elseif ( is_array( $value ) ) - $values[$key] = $this->strip_repeatable( $value ); - + if ( false !== strpos( $key, 'cmb-group-x' ) || false !== strpos( $key, 'cmb-field-x' ) ) { + unset( $values[ $key ] ); + } elseif ( is_array( $value ) ) { + $values[ $key ] = $this->strip_repeatable( $value ); + } } return $values; @@ -340,19 +342,22 @@ function strip_repeatable( $values ) { function save( $post_id = 0 ) { // Verify nonce - if ( ! isset( $_POST['wp_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['wp_meta_box_nonce'], basename( __FILE__ ) ) ) + if ( ! isset( $_POST['wp_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['wp_meta_box_nonce'], basename( __FILE__ ) ) ) { return $post_id; + } foreach ( $this->_meta_box['fields'] as $field ) { // Verify this meta box was shown on the page - if ( ! isset( $_POST['_cmb_present_' . $field['id'] ] ) ) + if ( ! isset( $_POST[ '_cmb_present_' . $field['id'] ] ) ) { continue; + } - if ( isset( $_POST[ $field['id'] ] ) ) + if ( isset( $_POST[ $field['id'] ] ) ) { $value = (array) $_POST[ $field['id'] ]; - else + } else { $value = array(); + } $value = $this->strip_repeatable( $value ); @@ -377,8 +382,9 @@ function save( $post_id = 0 ) { function save_for_post( $post_id ) { // check autosave - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; + } $this->save( $post_id ); diff --git a/classes.fields.php b/classes.fields.php index f1465a3c..6601d6f9 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -27,16 +27,17 @@ public function __construct( $name, $title, array $values, $args = array() ) { if ( ! empty( $this->args['options'] ) && is_array( reset( $this->args['options'] ) ) ) { $re_format = array(); foreach ( $this->args['options'] as $option ) { - $re_format[$option['value']] = $option['name']; + $re_format[ $option['value'] ] = $option['name']; } $this->args['options'] = $re_format; } // If the field has a custom value populator callback - if ( ! empty( $args['values_callback'] ) ) + if ( ! empty( $args['values_callback'] ) ) { $this->values = call_user_func( $args['values_callback'], get_the_id() ); - else + } else { $this->values = $values; + } $this->value = reset( $this->values ); @@ -79,8 +80,9 @@ public function get_default_args() { */ public function enqueue_scripts() { - if ( isset( $this->args['sortable'] ) && $this->args['sortable'] ) + if ( isset( $this->args['sortable'] ) && $this->args['sortable'] ) { wp_enqueue_script( 'jquery-ui-sortable' ); + } } @@ -128,8 +130,9 @@ public function get_the_id_attr( $append = null ) { $id .= '-cmb-field-' . $this->field_index; - if ( ! is_null( $append ) ) + if ( ! is_null( $append ) ) { $id .= '-' . $append; + } $id = str_replace( array( '[', ']', '--' ), '-', $id ); @@ -169,8 +172,9 @@ public function get_the_name_attr( $append = null ) { $name .= "[cmb-field-$this->field_index]"; - if ( ! is_null( $append ) ) + if ( ! is_null( $append ) ) { $name .= $append; + } return $name; @@ -199,16 +203,19 @@ public function get_js_id() { public function boolean_attr( $attrs = array() ) { - if ( $this->args['readonly'] ) + if ( $this->args['readonly'] ) { $attrs[] = 'readonly'; + } - if ( $this->args['disabled'] ) + if ( $this->args['disabled'] ) { $attrs[] = 'disabled'; + } $attrs = array_filter( array_unique( $attrs ) ); - foreach ( $attrs as $attr ) + foreach ( $attrs as $attr ) { echo esc_html( $attr ) . '="' . esc_attr( $attr ) . '"'; + } } @@ -228,15 +235,16 @@ public function has_data_delegate() { */ protected function get_delegate_data() { - if ( $this->args['data_delegate'] ) + if ( $this->args['data_delegate'] ) { return call_user_func_array( $this->args['data_delegate'], array( $this ) ); + } return array(); } public function get_value() { - return ( $this->value || $this->value === '0' ) ? $this->value : $this->args['default']; + return ( $this->value || '0' === $this->value ) ? $this->value : $this->args['default']; } public function &get_values() { @@ -262,8 +270,9 @@ public function parse_save_value() {} public function save( $post_id, $values ) { // Don't save readonly values. - if ( $this->args['readonly'] ) + if ( $this->args['readonly'] ) { return; + } $this->values = $values; $this->parse_save_values(); @@ -278,25 +287,26 @@ public function save( $post_id, $values ) { } // If we are not on a post edit screen - if ( ! $post_id ) + if ( ! $post_id ) { return; + } delete_post_meta( $post_id, $this->id ); - foreach( $this->values as $v ) { + foreach ( $this->values as $v ) { $this->value = $v; $this->parse_save_value(); - if ( $this->value || $this->value === '0' ) + if ( $this->value || '0' === $this->value ) { add_post_meta( $post_id, $this->id, $this->value ); - + } } } public function title() { - if ( $this->title ) { ?> + if ( $this->title ) : ?>
- args['desc'] ) ) { ?> + if ( ! empty( $this->args['desc'] ) ) : ?>
args['desc'] ); ?>
- get_values() && ! $this->args['repeatable'] ) + if ( ! $this->get_values() && ! $this->args['repeatable'] ) { $values = array( '' ); - else + } else { $values = $this->get_values(); + } $this->title(); $this->description(); $i = 0; - if( isset( $this->args['type'] ) && $this->args['type'] == 'gmap' ) { + if ( isset( $this->args['type'] ) && 'gmap' == $this->args['type'] ) { $values = array( $values ); } + foreach ( $values as $key => $value ) { $this->field_index = $i; @@ -380,10 +392,10 @@ public function display() { - + public function html() { + ?> id_attr(); ?> boolean_attr(); ?> class_attr(); ?> name_attr(); ?> value="get_value() ); ?>" /> - array( 'video', 'audio', 'text', 'application' ) + 'library-type' => array( + 'video', + 'audio', + 'text', + 'application', + ), ) ); } @@ -435,11 +454,11 @@ public function get_default_args() { function enqueue_scripts() { global $post_ID; - $post_ID = isset($post_ID) ? (int) $post_ID : 0; + $post_ID = isset( $post_ID ) ? (int) $post_ID : 0; parent::enqueue_scripts(); - wp_enqueue_media( array( 'post' => $post_ID )); + wp_enqueue_media( array( 'post' => $post_ID ) ); wp_enqueue_script( 'cmb-file-upload', trailingslashit( CMB_URL ) . 'js/file-upload.js', array( 'jquery', 'cmb-scripts' ) ); } @@ -468,7 +487,11 @@ public function html() { get_value() ) : ?> - +
get_value() ) ) ); ?> @@ -490,8 +513,8 @@ public function html() {
- 'thumbnail', - 'library-type' => array( 'image' ), - 'show_size' => false + 'size' => 'thumbnail', + 'library-type' => array( + 'image', + ), + 'show_size' => false, ) ); } public function html() { - if ( $this->get_value() ) + if ( $this->get_value() ) { $image = wp_get_attachment_image_src( $this->get_value(), $this->args['size'], true ); + } // Convert size arg to array of width, height, crop $size = $this->parse_image_size( $this->args['size'] ); @@ -579,28 +605,29 @@ private function parse_image_size( $size ) { return array( 'width' => get_option( $size . '_size_w' ), 'height' => get_option( $size . '_size_h' ), - 'crop' => get_option( $size . '_crop' ) + 'crop' => get_option( $size . '_crop' ), ); } // Handle string for additional image sizes global $_wp_additional_image_sizes; - if ( is_string( $size ) && isset( $_wp_additional_image_sizes[$size] ) ) { + if ( is_string( $size ) && isset( $_wp_additional_image_sizes[ $size ] ) ) { return array( - 'width' => $_wp_additional_image_sizes[$size]['width'], - 'height' => $_wp_additional_image_sizes[$size]['height'], - 'crop' => $_wp_additional_image_sizes[$size]['crop'] + 'width' => $_wp_additional_image_sizes[ $size ]['width'], + 'height' => $_wp_additional_image_sizes[ $size ]['height'], + 'crop' => $_wp_additional_image_sizes[ $size ]['crop'], ); } // Handle default WP size format. - if ( is_array( $size ) && isset( $size[0] ) && isset( $size[1] ) ) + if ( is_array( $size ) && isset( $size[0] ) && isset( $size[1] ) ) { $size = array( 'width' => $size[0], 'height' => $size[1] ); + } return wp_parse_args( $size, array( 'width' => get_option( 'thumbnail_size_w' ), 'height' => get_option( 'thumbnail_size_h' ), - 'crop' => get_option( 'thumbnail_crop' ) + 'crop' => get_option( 'thumbnail_crop' ), ) ); } @@ -612,15 +639,16 @@ private function parse_image_size( $size ) { */ static function request_image_ajax_callback() { - if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'cmb-file-upload-nonce' ) ) ) + if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'cmb-file-upload-nonce' ) ) ) { return; + } $id = intval( $_POST['id'] ); $size = array( intval( $_POST['width'] ), intval( $_POST['height'] ), - 'crop' => (bool) $_POST['crop'] + 'crop' => (bool) $_POST['crop'], ); $image = wp_get_attachment_image_src( $id, $size ); @@ -628,7 +656,6 @@ static function request_image_ajax_callback() { die(); // this is required to return a proper result } - } add_action( 'wp_ajax_cmb_request_image', array( 'CMB_Image_Field', 'request_image_ajax_callback' ) ); @@ -647,11 +674,13 @@ public function get_default_args() { ); } - public function html() { ?> + public function html() { + ?> id_attr(); ?> boolean_attr(); ?> class_attr( 'cmb_text_number code' ); ?> name_attr(); ?> value="get_value() ); ?>" /> - */ class CMB_URL_Field extends CMB_Field { - public function html() { ?> + public function html() { + ?> id_attr(); ?> boolean_attr(); ?> class_attr( 'cmb_text_url code' ); ?> name_attr(); ?> value="value ) ); ?>" /> - id_attr(); ?> boolean_attr(); ?> class_attr( $classes ); ?> type="text" name_attr(); ?> value="value ); ?>" /> - + public function html() { + ?> id_attr(); ?> boolean_attr(); ?> class_attr( 'cmb_text_small cmb_timepicker' ); ?> type="text" name_attr(); ?> value="value ); ?>"/> - + public function html() { + ?> id_attr(); ?> boolean_attr(); ?> class_attr( 'cmb_text_small cmb_datepicker' ); ?> type="text" name_attr(); ?> value="value ? esc_attr( date( 'm\/d\/Y', $this->value ) ) : '' ?>" /> - values as &$value ) + foreach ( $this->values as &$value ) { $value = strtotime( $value ); + } sort( $this->values ); } - } /** @@ -760,22 +795,25 @@ public function enqueue_scripts() { wp_enqueue_script( 'cmb-datetime', trailingslashit( CMB_URL ) . 'js/field.datetime.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-scripts' ) ); } - public function html() { ?> + public function html() { + ?> - id_attr('date'); ?> boolean_attr(); ?> class_attr( 'cmb_text_small cmb_datepicker' ); ?> type="text" name_attr( '[date]' ); ?> value="value ? esc_attr( date( 'm\/d\/Y', $this->value ) ) : '' ?>" /> - id_attr('time'); ?> boolean_attr(); ?> class_attr( 'cmb_text_small cmb_timepicker' ); ?> type="text" name_attr( '[time]' ); ?> value="value ? esc_attr( date( 'h:i A', $this->value ) ) : '' ?>" /> + id_attr( 'date' ); ?> boolean_attr(); ?> class_attr( 'cmb_text_small cmb_datepicker' ); ?> type="text" name_attr( '[date]' ); ?> value="value ? esc_attr( date( 'm\/d\/Y', $this->value ) ) : '' ?>" /> + id_attr( 'time' ); ?> boolean_attr(); ?> class_attr( 'cmb_text_small cmb_timepicker' ); ?> type="text" name_attr( '[time]' ); ?> value="value ? esc_attr( date( 'h:i A', $this->value ) ) : '' ?>" /> - values as $key => &$value ) { - if ( empty( $value['date'] ) ) - unset( $this->values[$key] ); - else + foreach ( $this->values as $key => &$value ) { + if ( empty( $value['date'] ) ) { + unset( $this->values[ $key ] ); + } else { $value = strtotime( $value['date'] . ' ' . $value['time'] ); + } } $this->values = array_filter( $this->values ); @@ -784,7 +822,6 @@ public function parse_save_values() { parent::parse_save_values(); } - } /** @@ -795,12 +832,13 @@ public function parse_save_values() { */ class CMB_Textarea_Field extends CMB_Field { - public function html() { ?> + public function html() { + ?> - + public function html() { + ?> id_attr(); ?> boolean_attr(); ?> class_attr( 'cmb_colorpicker cmb_text_small' ); ?> type="text" name_attr(); ?> value="get_value() ); ?>" /> - has_data_delegate() ) - $this->args['options'] = $this->get_delegate_data(); ?> + if ( $this->has_data_delegate() ) { + $this->args['options'] = $this->get_delegate_data(); + } ?> - args['options'] as $key => $value ): ?> + args['options'] as $key => $value ) : ?> id_attr( 'item-' . $key ); ?> boolean_attr(); ?> class_attr(); ?> type="radio" name_attr(); ?> value="" get_value() ); ?> />