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

Allow a subset of HTML tags for Multiple Choices label rendering #2185

Merged
Merged
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
23 changes: 22 additions & 1 deletion inc/render/class-form-multiple-choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,28 @@ public function render_field( $type, $label, $value, $name, $id, $checked = fals
$output = '<div class="o-form-multiple-choice-field">';

$output .= '<input type="' . esc_attr( $type ) . '" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" value="' . esc_attr( $value ) . '" ' . ( $is_required ? 'required' : '' ) . ( $checked ? ' checked' : '' ) . ' />';
$output .= '<label for="' . esc_attr( $id ) . '" class="o-form-choice-label">' . esc_html( $label ) . '</label>';

$allowed_tags = array(
'a' => array(
'href' => true,
'target' => true,
),
'img' => array(
'src' => true,
'alt' => true,
'width' => true,
'height' => true,
),
'span' => array(),
'em' => array(),
'strong' => array(),
'i' => array(),
'b' => array(),
);

$label = wp_kses( $label, $allowed_tags );

$output .= '<label for="' . esc_attr( $id ) . '" class="o-form-choice-label">' . $label . '</label>';

$output .= '</div>';

Expand Down
37 changes: 37 additions & 0 deletions tests/test-choices-field-block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Test Multiple Choice Field Block.
*
* @package otter-blocks
*/

use ThemeIsle\GutenbergBlocks\Render\Form_Multiple_Choice_Block;

/**
* Class Test Multiple Choices Block
*/
class Test_Multiple_Choices_Block extends WP_UnitTestCase {

public function test_label_sanitization_render() {
$block_render = new Form_Multiple_Choice_Block();

$expected = '<div class="o-form-multiple-choice-field">';
$expected .= '<input type="checkbox" name="otter-blocks" id="otter-blocks" value="otter-blocks" />';
$expected .= '<label for="otter-blocks" class="o-form-choice-label">Option with <a href="www.example.com">link</a></label>';
$expected .= '</div>';

$output = $block_render->render_field( 'checkbox', 'Option with <a href="www.example.com">link</a>', 'otter-blocks', 'otter-blocks', 'otter-blocks' );

$this->assertEquals( $expected, $output );

$malicious_label = 'Option with <a href="www.example.com">link</a><script></script>';
$output = $block_render->render_field( 'checkbox', $malicious_label, 'otter-blocks', 'otter-blocks', 'otter-blocks' );

$this->assertEquals( $expected, $output );

$malicious_label = 'Option with <a href="www.example.com" onclick="alert(123)">link</a>';
$output = $block_render->render_field( 'checkbox', $malicious_label, 'otter-blocks', 'otter-blocks', 'otter-blocks' );

$this->assertEquals( $expected, $output );
}
}
Loading