From f9a029608a0cd5e1236c1b170d1c6e17f7e3d005 Mon Sep 17 00:00:00 2001 From: mikeselander Date: Sat, 12 Nov 2016 23:23:19 +0100 Subject: [PATCH 1/2] 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() ); ?> />