From 0ad353d2503b2689ff0002c7490a70a2af1ce046 Mon Sep 17 00:00:00 2001 From: Jb Audras Date: Mon, 12 Sep 2022 22:29:02 +0000 Subject: [PATCH] Comments: Make `wp_required_field_indicator()` and `wp_required_field_message()` output filterable. This changeset introduces two new hooks: - `wp_required_field_indicator` allows developers to filter the HTML output of the `wp_required_field_indicator()` function. - `wp_required_field_message` does the same for the `wp_required_field_message()` function. The changeset also adds new phpunit tests for these filters. Follow-up to [53888], [54136]. Props kebbet, audrasjb, sabernhardt, costdev, mukesh27. Fixes #56389. See #54394. git-svn-id: https://develop.svn.wordpress.org/trunk@54137 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 18 ++++++- .../general/wpRequiredFieldIndicator.php | 45 +++++++++++++++++ .../tests/general/wpRequiredFieldMessage.php | 48 +++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/general/wpRequiredFieldIndicator.php create mode 100644 tests/phpunit/tests/general/wpRequiredFieldMessage.php diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index acc3b5bda2..1a23cb4316 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -5026,7 +5026,14 @@ function wp_required_field_indicator() { $glyph = __( '*' ); $indicator = '' . esc_html( $glyph ) . ''; - return $indicator; + /** + * Filters the markup for a visual indicator of required form fields. + * + * @since 6.1.0 + * + * @param string $indicator Markup for the indicator element. + */ + return apply_filters( 'wp_required_field_indicator', $indicator ); } /** @@ -5043,7 +5050,14 @@ function wp_required_field_message() { sprintf( __( 'Required fields are marked %s' ), wp_required_field_indicator() ) ); - return $message; + /** + * Filters the message to explain required form fields. + * + * @since 6.1.0 + * + * @param string $message Message text and glyph wrapped in a `span` tag. + */ + return apply_filters( 'wp_required_field_message', $message ); } /** diff --git a/tests/phpunit/tests/general/wpRequiredFieldIndicator.php b/tests/phpunit/tests/general/wpRequiredFieldIndicator.php new file mode 100644 index 0000000000..bd66379a2a --- /dev/null +++ b/tests/phpunit/tests/general/wpRequiredFieldIndicator.php @@ -0,0 +1,45 @@ +assertSame( '*', wp_required_field_indicator() ); + } + + /** + * Tests that `wp_required_field_indicator()` applies 'wp_required_field_indicator' filters. + * + * @ticket 56389 + */ + public function test_wp_required_field_indicator_should_apply_wp_required_field_indicator_filters() { + $filter = new MockAction(); + add_filter( 'wp_required_field_indicator', array( &$filter, 'filter' ) ); + + wp_required_field_indicator(); + + $this->assertSame( 1, $filter->get_call_count() ); + } + + /** + * Tests that the final return value of `wp_required_field_indicator()` is the result of + * 'wp_required_field_indicator' filters. + * + * @ticket 56389 + */ + public function test_wp_required_field_indicator_should_return_wp_required_field_indicator_filters() { + add_filter( 'wp_required_field_indicator', '__return_empty_string' ); + $this->assertSame( '', wp_required_field_indicator() ); + } +} diff --git a/tests/phpunit/tests/general/wpRequiredFieldMessage.php b/tests/phpunit/tests/general/wpRequiredFieldMessage.php new file mode 100644 index 0000000000..2dc1a3b8b8 --- /dev/null +++ b/tests/phpunit/tests/general/wpRequiredFieldMessage.php @@ -0,0 +1,48 @@ +'; + $expected .= 'Required fields are marked *'; + $expected .= ''; + $this->assertSame( $expected, wp_required_field_message() ); + } + + /** + * Tests that `wp_required_field_message()` applies 'wp_required_field_message' filters. + * + * @ticket 56389 + */ + public function test_wp_required_field_message_should_apply_wp_required_field_message_filters() { + $filter = new MockAction(); + add_filter( 'wp_required_field_message', array( &$filter, 'filter' ) ); + + wp_required_field_message(); + + $this->assertSame( 1, $filter->get_call_count() ); + } + + /** + * Tests that the final return value of `wp_required_field_message()` is the result of + * 'wp_required_field_message' filters. + * + * @ticket 56389 + */ + public function test_wp_required_field_message_should_return_wp_required_field_message_filters() { + add_filter( 'wp_required_field_message', '__return_empty_string' ); + $this->assertSame( '', wp_required_field_message() ); + } +}