From df3111321639ec9ac983e7d66471baeb47979055 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Sun, 23 Feb 2014 18:07:05 +0000 Subject: [PATCH 1/3] Don't encode html entities in json response --- classes.fields.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/classes.fields.php b/classes.fields.php index 8f767d61..e311a6a6 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -1250,8 +1250,9 @@ function cmb_ajax_post_select() { $json = array( 'total' => $query->found_posts, 'posts' => array() ); - foreach ( $query->posts as $post_id ) - array_push( $json['posts'], array( 'id' => $post_id, 'text' => get_the_title( $post_id ) ) ); + foreach ( $query->posts as $post_id ) { + array_push( $json['posts'], array( 'id' => $post_id, 'text' => html_entity_decode( get_the_title( $post_id ) ) ) ); + } echo json_encode( $json ); From 8c643304236f1edc10b9772c65df037e9c98eef2 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Sun, 23 Feb 2014 18:12:22 +0000 Subject: [PATCH 2/3] Don't manually construct JSON. Also, don't encode html entities before json_encode-ing --- classes.fields.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes.fields.php b/classes.fields.php index e311a6a6..cb01613b 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -1181,12 +1181,12 @@ public function output_script() { args['multiple'] ) : ?> value as $post_id ) : ?> - data.push( ); + data.push( $post_id, 'text' => html_entity_decode( get_the_title( $post_id ) ) ) ); ?> ); - - data = value, get_the_title( $this->value ) ); ?>; + + data = $post_id, 'text' => html_entity_decode( get_the_title( $post_id ) ) ) ); ?>; From 13b711f1a6d8775b41c15f1c6657ea3a2a5d0096 Mon Sep 17 00:00:00 2001 From: Matth_eu Date: Mon, 17 Mar 2014 15:50:20 +0000 Subject: [PATCH 3/3] cherry pick trim whitespace --- classes.fields.php | 174 ++++++++++++++++++++++----------------------- 1 file changed, 87 insertions(+), 87 deletions(-) diff --git a/classes.fields.php b/classes.fields.php index d8a7a828..aafca6c1 100644 --- a/classes.fields.php +++ b/classes.fields.php @@ -16,7 +16,7 @@ public function __construct( $name, $title, array $values, $args = array() ) { $this->id = $name; $this->name = $name . '[]'; $this->title = $title; - + $this->args = wp_parse_args( $args, array( 'repeatable' => false, 'std' => '', @@ -124,16 +124,16 @@ public function get_the_id_attr( $append = null ) { $id = str_replace( array( '[', ']', '--' ), '-', $id ); return $id; - + } /** * Return the field input ID attribute value. * - * If multiple inputs are required for a single field, + * If multiple inputs are required for a single field, * use the append parameter to add unique identifier. - * - * @param string $append + * + * @param string $append * @return string id attribute value. */ public function for_attr( $append = null ) { @@ -143,9 +143,9 @@ public function for_attr( $append = null ) { } public function name_attr( $append = null ) { - + printf( 'name="%s"', esc_attr( $this->get_the_name_attr( $append ) ) ); - + } public function get_the_name_attr( $append = null ) { @@ -184,7 +184,7 @@ class="" public function get_js_id() { return str_replace( array( '-', '[', ']', '--' ),'_', $this->get_the_id_attr() ); // JS friendly ID - + } public function boolean_attr( $attrs = array() ) { @@ -336,7 +336,7 @@ public function display() { - class CMB_Text_Small_Field extends CMB_Text_Field { - public function html() { + public function html() { - $this->args['class'] .= ' cmb_text_small'; + $this->args['class'] .= ' cmb_text_small'; parent::html(); @@ -403,10 +403,10 @@ function enqueue_scripts() { parent::enqueue_scripts(); wp_enqueue_media(); wp_enqueue_script( 'cmb-file-upload', trailingslashit( CMB_URL ) . 'js/file-upload.js', array( 'jquery', 'cmb-scripts' ) ); - + } - public function html() { + public function html() { $args = wp_parse_args( $this->args, array( 'library-type' => array( 'video', 'audio', 'text', 'application' ) @@ -469,13 +469,13 @@ public function html() { if ( $this->get_value() ) $image = wp_get_attachment_image_src( $this->get_value(), $args['size'], true ); - // Convert size arg to array of width, height, crop. + // Convert size arg to array of width, height, crop. $size = $this->parse_image_size( $args['size'] ); // Inline styles. $styles = sprintf( 'width: %1$dpx; height: %2$dpx; line-height: %2$dpx', intval( $size['width'] ), intval( $size['height'] ) ); $placeholder_styles = sprintf( 'width: %dpx; height: %dpx;', intval( $size['width'] ) - 8, intval( $size['height'] ) - 8 ); - + $data_type = ( ! empty( $args['library-type'] ) ? implode( ',', $args['library-type'] ) : null ); ?> @@ -516,12 +516,12 @@ public function html() { /** * Parse the size argument to get pixel width, pixel height and crop information. - * + * * @param string $size * @return array width, height, crop */ private function parse_image_size( $size ) { - + // Handle string for built-in image sizes if ( is_string( $size ) && in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) ) { return array( @@ -541,7 +541,7 @@ private function parse_image_size( $size ) { ); } - // Handle default WP size format. + // Handle default WP size format. if ( is_array( $size ) && isset( $size[0] ) && isset( $size[1] ) ) $size = array( 'width' => $size[0], 'height' => $size[1] ); @@ -559,7 +559,7 @@ private function parse_image_size( $size ) { * @return null */ static function request_image_ajax_callback() { - + if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'cmb-file-upload-nonce' ) ) ) return; @@ -647,7 +647,7 @@ public function enqueue_scripts() { wp_enqueue_style( 'cmb-jquery-ui', trailingslashit( CMB_URL ) . 'css/vendor/jquery-ui/jquery-ui.css', '1.10.3' ); - wp_enqueue_script( 'cmb-timepicker', trailingslashit( CMB_URL ) . 'js/jquery.timePicker.min.js', array( 'jquery', 'cmb-scripts' ) ); + wp_enqueue_script( 'cmb-timepicker', trailingslashit( CMB_URL ) . 'js/jquery.timePicker.min.js', array( 'jquery', 'cmb-scripts' ) ); wp_enqueue_script( 'cmb-datetime', trailingslashit( CMB_URL ) . 'js/field.datetime.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-scripts' ) ); } @@ -659,12 +659,12 @@ public function html() { ?> values as &$value ) $value = strtotime( $value ); sort( $this->values ); - + } } @@ -674,18 +674,18 @@ public function parse_save_values() { * */ class CMB_Datetime_Timestamp_Field extends CMB_Field { - + public function enqueue_scripts() { parent::enqueue_scripts(); wp_enqueue_style( 'cmb-jquery-ui', trailingslashit( CMB_URL ) . 'css/vendor/jquery-ui/jquery-ui.css', '1.10.3' ); - + wp_enqueue_script( 'cmb-timepicker', trailingslashit( CMB_URL ) . 'js/jquery.timePicker.min.js', array( 'jquery', 'cmb-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 ) ) : '' ?>" /> @@ -695,7 +695,7 @@ public function html() { ?> public function parse_save_values() { // Convert all [date] and [time] values to a unix timestamp. - // If date is empty, assume delete. If time is empty, assume 00:00. + // If date is empty, assume delete. If time is empty, assume 00:00. foreach( $this->values as $key => &$value ) { if ( empty( $value['date'] ) ) unset( $this->values[$key] ); @@ -738,7 +738,7 @@ class CMB_Textarea_Field_Code extends CMB_Textarea_Field { public function html() { - $this->args['class'] .= ' code'; + $this->args['class'] .= ' code'; parent::html(); @@ -848,15 +848,15 @@ function enqueue_scripts() { wp_enqueue_script( 'cmb-wysiwyg', trailingslashit( CMB_URL ) . 'js/field-wysiwyg.js', array( 'jquery', 'cmb-scripts' ) ); } - public function html() { + public function html() { $id = $this->get_the_id_attr(); - $name = $this->get_the_name_attr(); + $name = $this->get_the_name_attr(); $field_id = $this->get_js_id(); printf( '
', $id, $name, $field_id ); - + if ( $this->is_placeholder() ) { // For placeholder, output the markup for the editor in a JS var. @@ -868,20 +868,20 @@ public function html() { $editor = str_replace( array( "'" ), '"', $editor ); ?> - + args['options']['textarea_name'] = $name; echo wp_editor( $this->get_value(), $id, $this->args['options'] ); - + } echo '
'; @@ -891,7 +891,7 @@ public function html() { /** * Check if this is a placeholder field. * Either the field itself, or because it is part of a repeatable group. - * + * * @return bool */ public function is_placeholder() { @@ -917,7 +917,7 @@ public function is_placeholder() { class CMB_Select extends CMB_Field { public function __construct() { - + $args = func_get_args(); call_user_func_array( array( 'parent', '__construct' ), $args ); @@ -962,7 +962,7 @@ public function html() { $this->args['options'] = $this->get_delegate_data(); $this->output_field(); - + $this->output_script(); } @@ -973,17 +973,17 @@ public function output_field() { $name = $this->get_the_name_attr(); $name .= ! empty( $this->args['multiple'] ) ? '[]' : null; - + ?> - id_attr(); ?> + boolean_attr(); ?> + + get_js_id() ) ); ?> + args['multiple'] ) ? 'multiple' : '' ?> + class="cmb_select" + style="width: 100%" > args['allow_none'] ) ) : ?> @@ -996,7 +996,7 @@ class="cmb_select" - (function($) { - + var options = {}; - + options.placeholder = ; options.allowClear = true; if ( 'undefined' === typeof( window.cmb_select_fields ) ) window.cmb_select_fields = {}; - + var id = get_js_id() ); ?>; window.cmb_select_fields[id] = options; @@ -1021,8 +1021,8 @@ public function output_script() { - get_terms(); - if ( is_wp_error( $terms ) ) { - return array(); + if ( is_wp_error( $terms ) ) { + return array(); } $term_options = array(); @@ -1085,7 +1085,7 @@ public function __construct() { $this->args['query'] = isset( $this->args['query'] ) ? $this->args['query'] : array(); if ( ! $this->args['use_ajax'] ) { - + $this->args['data_delegate'] = array( $this, 'get_delegate_data' ); } else { @@ -1098,7 +1098,7 @@ public function __construct() { } public function get_delegate_data() { - + $data = array(); foreach ( $this->get_posts() as $post_id ) @@ -1128,23 +1128,23 @@ public function parse_save_value() { } public function output_field() { - - // If AJAX, must use input type not standard select. + + // If AJAX, must use input type not standard select. if ( $this->args['ajax_url'] ) : ?> - id_attr(); ?> + id_attr(); ?> value ) ) ); ?> - get_the_name_attr() ) ); ?> - get_js_id() ) ); ?> - boolean_attr(); ?> - class="cmb_select" - style="width: 100%" + get_the_name_attr() ) ); ?> + get_js_id() ) ); ?> + boolean_attr(); ?> + class="cmb_select" + style="width: 100%" /> - @@ -1165,8 +1165,8 @@ public function output_script() { (function($) { if ( 'undefined' === typeof( window.cmb_select_fields ) ) - return false; - + return false; + // Get options for this field so we can modify it. var id = get_js_id() ); ?>; var options = window.cmb_select_fields[id]; @@ -1177,38 +1177,38 @@ public function output_script() { args['ajax_url'] && ! empty( $this->value ) ) : ?> - + options.initSelection = function( element, callback ) { - + var data = []; args['multiple'] ) : ?> - + value as $post_id ) : ?> data.push( $post_id, 'text' => html_entity_decode( get_the_title( $post_id ) ) ) ); ?> ); - + - + data = $post_id, 'text' => html_entity_decode( get_the_title( $post_id ) ) ) ); ?>; - + callback( data ); - + }; args['ajax_url'] ) : ?> - + var ajaxData = { action : 'cmb_post_select', post_id : '', // Used for user capabilty check. nonce : , query : args['ajax_args'] ); ?> }; - + options.ajax = { url: args['ajax_url'] ) ); ?>, type: 'POST', @@ -1220,12 +1220,12 @@ public function output_script() { }, results : function( results, page ) { var postsPerPage = ajaxData.query.posts_per_page = ( 'posts_per_page' in ajaxData.query ) ? ajaxData.query.posts_per_page : ( 'showposts' in ajaxData.query ) ? ajaxData.query.showposts : 10; - var isMore = ( page * postsPerPage ) < results.total; + var isMore = ( page * postsPerPage ) < results.total; return { results: results.posts, more: isMore }; } } - + })( jQuery ); @@ -1238,7 +1238,7 @@ public function output_script() { // TODO this should be in inside the class function cmb_ajax_post_select() { - + $post_id = ! empty( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : false; $nonce = ! empty( $_POST['nonce'] ) ? $_POST['nonce'] : false; $args = ! empty( $_POST['query'] ) ? $_POST['query'] : array(); @@ -1251,7 +1251,7 @@ function cmb_ajax_post_select() { $args['fields'] = 'ids'; // Only need to retrieve post IDs. $query = new WP_Query( $args ); - + $json = array( 'total' => $query->found_posts, 'posts' => array() ); foreach ( $query->posts as $post_id ) { @@ -1342,7 +1342,7 @@ public function display() { foreach ( $values as $value ) { $this->field_index = $i; - $this->value = $value; + $this->value = $value; ?> @@ -1353,8 +1353,8 @@ public function display() { args['repeatable'] ) { @@ -1413,7 +1413,7 @@ public function parse_save_values() { $field_value = array(); continue; } - + $field = $fields[$field_id]; $field->values = $field_value; $field->parse_save_values(); @@ -1439,7 +1439,7 @@ public function &get_fields() { } public function set_values( array $values ) { - + $this->values = $values; $fields = &$this->get_fields();