From 20521ade91c3941022636222c02fae8080094bb5 Mon Sep 17 00:00:00 2001 From: Enrico Sorcinelli Date: Sun, 4 Feb 2018 08:34:03 +0100 Subject: [PATCH 1/2] Attributes checks --- php/class-fieldmanager-autocomplete.php | 5 ++-- php/class-fieldmanager-checkbox.php | 5 ++-- php/class-fieldmanager-colorpicker.php | 5 ++-- php/class-fieldmanager-field.php | 18 ++++++++++-- php/class-fieldmanager-media.php | 2 +- php/class-fieldmanager-textarea.php | 3 +- templates/datepicker.php | 8 +++--- templates/options-checkboxes.php | 2 +- templates/options-radios.php | 2 +- templates/textfield.php | 2 +- .../php/test-fieldmanager-checkbox-field.php | 28 +++++++++++++++++++ 11 files changed, 63 insertions(+), 17 deletions(-) diff --git a/php/class-fieldmanager-autocomplete.php b/php/class-fieldmanager-autocomplete.php index 4d6286d99b..d974606020 100644 --- a/php/class-fieldmanager-autocomplete.php +++ b/php/class-fieldmanager-autocomplete.php @@ -136,11 +136,12 @@ public function form_element( $value = null ) { } $element = sprintf( - '', + '', + $this->get_element_attributes( 'class' ), esc_attr( $this->get_element_id() ), esc_attr( $display_value ), ( ! empty( $this->custom_args_js_event ) ) ? ' data-custom-args-js-event="' . esc_attr( $this->custom_args_js_event ) . '"' : '', - $this->get_element_attributes() + $this->get_element_attributes( '', array( 'data-custom-args-js-event' ) ) ); $element .= sprintf( diff --git a/php/class-fieldmanager-checkbox.php b/php/class-fieldmanager-checkbox.php index 682ec6e168..2e23d4cdd5 100644 --- a/php/class-fieldmanager-checkbox.php +++ b/php/class-fieldmanager-checkbox.php @@ -55,14 +55,15 @@ public function form_element( $value = null ) { return sprintf( ' - + ', esc_attr( $this->get_form_name() ), esc_attr( (string) $this->checked_value ), $this->get_element_attributes(), ( $value == $this->checked_value ) ? 'checked="checked"' : '', esc_attr( $this->get_element_id() ), - $this->unchecked_value + $this->unchecked_value, + $this->get_element_attributes( 'class' ) ); } diff --git a/php/class-fieldmanager-colorpicker.php b/php/class-fieldmanager-colorpicker.php index a98758094d..acd7f52331 100644 --- a/php/class-fieldmanager-colorpicker.php +++ b/php/class-fieldmanager-colorpicker.php @@ -58,12 +58,13 @@ public function __construct( $label = '', $options = array() ) { */ public function form_element( $value = '' ) { return sprintf( - '', + '', esc_attr( $this->get_form_name() ), esc_attr( $this->get_element_id() ), esc_attr( $this->default_color ), esc_attr( $value ), - $this->get_element_attributes() + $this->get_element_attributes( '', array( 'data-default-color' ) ), + $this->get_element_attributes( 'class' ) ); } } diff --git a/php/class-fieldmanager-field.php b/php/class-fieldmanager-field.php index 6ae658c165..ea2d3dd1a3 100644 --- a/php/class-fieldmanager-field.php +++ b/php/class-fieldmanager-field.php @@ -1056,12 +1056,26 @@ public function presave( $value, $current_value = array() ) { /** * Generates an HTML attribute string based on the value of $this->attributes. * + * @param string $attr Attribute name. + * @param array $attr_exclude Add attributes to black list. Default to ('class', + * 'type', 'name', 'id', 'value', 'checked'). + * * @see Fieldmanager_Field::$attributes - * @return string attributes ready to insert into an HTML tag. + * @return string Attributes or single attribute value ready to insert into an HTML tag. */ - public function get_element_attributes() { + public function get_element_attributes( $attr = '', $attr_exclude = array() ) { + + if ( ! empty( $attr ) ) { + return array_key_exists( $attr, $this->attributes ) ? ' ' . esc_attr( $this->attributes[$attr] ) : ''; + } + + $attr_exclude = array_merge( is_array( $attr_exclude ) ? $attr_exclude : array(), array( 'class', 'type', 'name', 'id', 'value', 'checked' ) ); + $attr_str = array(); foreach ( $this->attributes as $attr => $val ) { + if ( in_array( $attr, $attr_exclude ) ) { + continue; + } if ( true === $val ) { $attr_str[] = sanitize_key( $attr ); } else { diff --git a/php/class-fieldmanager-media.php b/php/class-fieldmanager-media.php index 93015872d3..2fb3fe7220 100644 --- a/php/class-fieldmanager-media.php +++ b/php/class-fieldmanager-media.php @@ -181,7 +181,7 @@ public function form_element( $value = array() ) { esc_attr( $this->modal_title ), esc_attr( $this->modal_button_label ), esc_attr( $this->mime_type ), - $this->get_element_attributes() + $this->get_element_attributes( '', array( 'data-choose', 'data-update', 'data-preview-size', 'data-mime-type' ) ) ); } diff --git a/php/class-fieldmanager-textarea.php b/php/class-fieldmanager-textarea.php index 9bfd727249..8b6c81d21b 100644 --- a/php/class-fieldmanager-textarea.php +++ b/php/class-fieldmanager-textarea.php @@ -43,7 +43,8 @@ public function __construct( $label = '', $options = array() ) { */ public function form_element( $value = '' ) { return sprintf( - '', + '', + $this->get_element_attributes( 'class' ), esc_attr( $this->get_form_name() ), esc_attr( $this->get_element_id() ), $this->get_element_attributes(), diff --git a/templates/datepicker.php b/templates/datepicker.php index f9f30072aa..60cbde3b46 100644 --- a/templates/datepicker.php +++ b/templates/datepicker.php @@ -8,7 +8,7 @@ ?> use_time ) : ?> @ - + : - + use_am_pm ) : ?> - diff --git a/templates/options-checkboxes.php b/templates/options-checkboxes.php index c62fc61690..290765305e 100644 --- a/templates/options-checkboxes.php +++ b/templates/options-checkboxes.php @@ -10,7 +10,7 @@
render( $group, $this->post ); $this->assertNotContains( 'checked', $html ); } + + /** + * Test element attributes + */ + public function test_attributes () { + $checkbox = new Fieldmanager_Checkbox( array( + 'name' => 'test_checkbox', + 'attributes' => array( + 'id' => 'foo', + 'class' => 'baz', + 'name' => 'foo', + 'value' => 10, + 'type' => 'text', + 'checked' => true, + 'data-bar' => 'bar' + ), + ) ); + $html = $this->render( $checkbox, $this->post ); + $this->assertNotContains( 'id="foo"', $html ); + $this->assertNotContains( 'class="baz"', $html ); + $this->assertNotContains( 'name="foo"', $html ); + $this->assertNotContains( 'value="10"', $html ); + $this->assertNotContains( 'type="text"', $html ); + $this->assertNotContains( 'checked', $html ); + $this->assertRegExp( '/class="[^"]+baz"/', $html ); + $this->assertContains( 'data-bar="bar"', $html ); + } + } From c92dfbb00447df30c41c10af04e10c040bf6a592 Mon Sep 17 00:00:00 2001 From: Enrico Sorcinelli Date: Sun, 4 Feb 2018 11:05:51 +0100 Subject: [PATCH 2/2] Fixes for PHPCS --- php/class-fieldmanager-field.php | 2 +- templates/datepicker.php | 8 ++++---- templates/options-checkboxes.php | 2 +- templates/options-radios.php | 2 +- templates/textfield.php | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/php/class-fieldmanager-field.php b/php/class-fieldmanager-field.php index ea2d3dd1a3..4e67b5a584 100644 --- a/php/class-fieldmanager-field.php +++ b/php/class-fieldmanager-field.php @@ -1066,7 +1066,7 @@ public function presave( $value, $current_value = array() ) { public function get_element_attributes( $attr = '', $attr_exclude = array() ) { if ( ! empty( $attr ) ) { - return array_key_exists( $attr, $this->attributes ) ? ' ' . esc_attr( $this->attributes[$attr] ) : ''; + return array_key_exists( $attr, $this->attributes ) ? ' ' . esc_attr( $this->attributes[ $attr ] ) : ''; } $attr_exclude = array_merge( is_array( $attr_exclude ) ? $attr_exclude : array(), array( 'class', 'type', 'name', 'id', 'value', 'checked' ) ); diff --git a/templates/datepicker.php b/templates/datepicker.php index 60cbde3b46..103a318706 100644 --- a/templates/datepicker.php +++ b/templates/datepicker.php @@ -8,7 +8,7 @@ ?> @ - + : - + use_am_pm ) : ?> - diff --git a/templates/options-checkboxes.php b/templates/options-checkboxes.php index 290765305e..b77b429ba5 100644 --- a/templates/options-checkboxes.php +++ b/templates/options-checkboxes.php @@ -10,7 +10,7 @@