Skip to content

Fields extra attributes checks #691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions php/class-fieldmanager-autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ public function form_element( $value = null ) {
}

$element = sprintf(
'<input class="fm-autocomplete fm-element fm-incrementable" type="text" id="%s" value="%s"%s %s />',
'<input class="fm-autocomplete fm-element fm-incrementable%s" type="text" id="%s" value="%s"%s %s />',
$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(
Expand Down
5 changes: 3 additions & 2 deletions php/class-fieldmanager-checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ public function form_element( $value = null ) {
return sprintf(
'
<input class="fm-checkbox-hidden fm-element" type="hidden" name="%1$s" value="%6$s" />
<input class="fm-element" type="checkbox" name="%1$s" value="%2$s" %3$s %4$s id="%5$s" />
<input class="fm-element%7$s" type="checkbox" name="%1$s" value="%2$s" %3$s %4$s id="%5$s" />
',
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' )
);
}

Expand Down
5 changes: 3 additions & 2 deletions php/class-fieldmanager-colorpicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public function __construct( $label = '', $options = array() ) {
*/
public function form_element( $value = '' ) {
return sprintf(
'<input class="fm-element fm-colorpicker-popup" name="%1$s" id="%2$s" data-default-color="%3$s" value="%4$s" %5$s />',
'<input class="fm-element fm-colorpicker-popup%6$s" name="%1$s" id="%2$s" data-default-color="%3$s" value="%4$s" %5$s />',
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' )
);
}
}
18 changes: 16 additions & 2 deletions php/class-fieldmanager-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion php/class-fieldmanager-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) )
);
}

Expand Down
3 changes: 2 additions & 1 deletion php/class-fieldmanager-textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function __construct( $label = '', $options = array() ) {
*/
public function form_element( $value = '' ) {
return sprintf(
'<textarea class="fm-element" name="%s" id="%s" %s >%s</textarea>',
'<textarea class="fm-element%s" name="%s" id="%s" %s >%s</textarea>',
$this->get_element_attributes( 'class' ),
esc_attr( $this->get_form_name() ),
esc_attr( $this->get_element_id() ),
$this->get_element_attributes(),
Expand Down
8 changes: 4 additions & 4 deletions templates/datepicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
?>

<input
class="fm-element fm-datepicker-popup"
class="fm-element fm-datepicker-popup<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>"
type="text"
data-datepicker-opts="<?php echo esc_attr( json_encode( $this->js_opts ) ); ?>"
name="<?php echo esc_attr( $this->get_form_name( '[date]' ) ); ?>"
Expand All @@ -24,11 +24,11 @@ class="fm-element fm-datepicker-popup"
<?php if ( $this->use_time ) : ?>
<span class="fm-datepicker-time-wrapper">
@
<input class="fm-element fm-datepicker-time" type="text" value="<?php echo esc_attr( $this->get_hour( $value ) ); ?>" name="<?php echo esc_attr( $this->get_form_name( '[hour]' ) ); ?>" />
<input class="fm-element fm-datepicker-time<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>" type="text" value="<?php echo esc_attr( $this->get_hour( $value ) ); ?>" name="<?php echo esc_attr( $this->get_form_name( '[hour]' ) ); ?>" />
:
<input class="fm-element fm-datepicker-time" type="text" value="<?php echo esc_attr( $this->get_minute( $value ) ); ?>" name="<?php echo esc_attr( $this->get_form_name( '[minute]' ) ); ?>" />
<input class="fm-element fm-datepicker-time<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>" type="text" value="<?php echo esc_attr( $this->get_minute( $value ) ); ?>" name="<?php echo esc_attr( $this->get_form_name( '[minute]' ) ); ?>" />
<?php if ( $this->use_am_pm ) : ?>
<select class="fm-element" name="<?php echo esc_attr( $this->get_form_name( '[ampm]' ) ); ?>">
<select class="fm-element<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>" name="<?php echo esc_attr( $this->get_form_name( '[ampm]' ) ); ?>">
<option value="am"<?php selected( $this->get_am_pm( $value ), 'am' ); ?>>A.M.</option>
<option value="pm"<?php selected( $this->get_am_pm( $value ), 'pm' ); ?>>P.M.</option>
</select>
Expand Down
2 changes: 1 addition & 1 deletion templates/options-checkboxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="fm-option">
<input
class="fm-element"
class="fm-element<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>"
type="checkbox"
value="<?php echo esc_attr( $data_row['value'] ); ?>"
name="<?php echo esc_attr( $this->get_form_name( '[]' ) ); ?>"
Expand Down
2 changes: 1 addition & 1 deletion templates/options-radios.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<div class="fm-option">
<input
class="fm-element"
class="fm-element<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>"
type="radio"
value="<?php echo esc_attr( $data_row['value'] ); ?>"
name="<?php echo esc_attr( $this->get_form_name() ); ?>"
Expand Down
2 changes: 1 addition & 1 deletion templates/textfield.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

?>
<input
class="fm-element"
class="fm-element<?php echo $this->get_element_attributes( 'class' ); /* Escaped internally. WPCS XSS okay. */ ?>"
type="<?php echo esc_attr( $this->input_type ); ?>"
name="<?php echo esc_attr( $this->get_form_name() ); ?>"
id="<?php echo esc_attr( $this->get_element_id() ); ?>"
Expand Down
28 changes: 28 additions & 0 deletions tests/php/test-fieldmanager-checkbox-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,32 @@ public function test_save_group_custom_values_default_checked() {
$html = $this->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 );
}

}