Skip to content
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

Fix/invalid field ids #41564

Open
wants to merge 3 commits into
base: trunk
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
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/fix-invalid-field-ids
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Forms: Fix invalid html IDs.
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function render_textarea_field( $id, $label, $value, $class, $required, $
/**
* Return the HTML for the radio field.
*
* @param int $id - the ID.
* @param string $id - the ID (starts with 'g' - see constructor).
* @param string $label - the label.
* @param string $value - the value of the field.
* @param string $class - the field class.
Expand All @@ -674,11 +674,11 @@ public function render_radio_field( $id, $label, $value, $class, $required, $req
$option = Contact_Form_Plugin::strip_tags( $option );
if ( is_string( $option ) && $option !== '' ) {
$radio_value = $this->get_option_value( $this->get_attribute( 'values' ), $option_index, $option );
$radio_id = "$id-$radio_value";
$radio_id = sanitize_html_class( $id ) . '-' . sanitize_html_class( $radio_value );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to wrap $id with sanitize_html_class( $id ). Since if this is the case we would need to do it in a few different places as well see line 668.


$field .= "<p class='contact-form-field'>";
$field .= "<input
id='" . esc_attr( $radio_id ) . "'
id='" . $radio_id . "'
type='radio'
name='" . esc_attr( $id ) . "'
value='" . esc_attr( $radio_value ) . "' "
Expand Down Expand Up @@ -745,7 +745,7 @@ private function render_consent_field( $id, $class ) {
/**
* Return the HTML for the multiple checkbox field.
*
* @param int $id - the ID.
* @param string $id - the ID (starts with 'g' - see constructor).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$id is always a string. See contructor around line 150 where it is set. This is relevant because a valid html ID need to start with a letter, not a number.

* @param string $label - the label.
* @param string $value - the value of the field.
* @param string $class - the field class.
Expand All @@ -768,11 +768,11 @@ public function render_checkbox_multiple_field( $id, $label, $value, $class, $re
$option = Contact_Form_Plugin::strip_tags( $option );
if ( is_string( $option ) && $option !== '' ) {
$checkbox_value = $this->get_option_value( $this->get_attribute( 'values' ), $option_index, $option );
$checkbox_id = "$id-$checkbox_value";
$checkbox_id = sanitize_html_class( $id ) . '-' . sanitize_html_class( $checkbox_value );

$field .= "<p class='contact-form-field'>";
$field .= "<input
id='" . esc_attr( $checkbox_id ) . "'
id='" . $checkbox_id . "'
type='checkbox'
name='" . esc_attr( $id ) . "[]'
value='" . esc_attr( $checkbox_value ) . "' "
Expand Down
4 changes: 4 additions & 0 deletions projects/plugins/jetpack/changelog/fix-invalid-field-ids
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Forms: Fix invalid html IDs.
Loading