From 0d2f288d4487973e0358c60dde34f3e825b9b315 Mon Sep 17 00:00:00 2001 From: Vipul Gupta Date: Thu, 9 May 2024 02:17:50 +0530 Subject: [PATCH 1/3] feat: Makes the search title editable. --- docs/reference-guides/core-blocks.md | 2 +- .../block-library/src/query-title/block.json | 6 +++ .../block-library/src/query-title/edit.js | 50 ++++++++++++++++--- .../block-library/src/query-title/index.php | 18 +++++-- .../block-library/src/query-title/style.scss | 4 ++ 5 files changed, 69 insertions(+), 11 deletions(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 3a0a88048e982..063c3e7ec78b4 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -775,7 +775,7 @@ Display the query title. ([Source](https://github.com/WordPress/gutenberg/tree/t - **Name:** core/query-title - **Category:** theme - **Supports:** align (full, wide), color (background, gradients, text), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** level, showPrefix, showSearchTerm, textAlign, type +- **Attributes:** level, searchResultsTerm, searchResultsTermSuffix, showPrefix, showSearchTerm, textAlign, type ## Quote diff --git a/packages/block-library/src/query-title/block.json b/packages/block-library/src/query-title/block.json index 674daadee3bb6..c64dc15a00276 100644 --- a/packages/block-library/src/query-title/block.json +++ b/packages/block-library/src/query-title/block.json @@ -24,6 +24,12 @@ "showSearchTerm": { "type": "boolean", "default": true + }, + "searchResultsTerm": { + "type": "string" + }, + "searchResultsTermSuffix": { + "type": "string" } }, "supports": { diff --git a/packages/block-library/src/query-title/edit.js b/packages/block-library/src/query-title/edit.js index 0194f3f99f4e1..02162349ee14b 100644 --- a/packages/block-library/src/query-title/edit.js +++ b/packages/block-library/src/query-title/edit.js @@ -14,6 +14,7 @@ import { Warning, HeadingLevelDropdown, store as blockEditorStore, + RichText, } from '@wordpress/block-editor'; import { ToggleControl, PanelBody } from '@wordpress/components'; import { __, sprintf } from '@wordpress/i18n'; @@ -22,7 +23,15 @@ import { useSelect } from '@wordpress/data'; const SUPPORTED_TYPES = [ 'archive', 'search' ]; export default function QueryTitleEdit( { - attributes: { type, level, textAlign, showPrefix, showSearchTerm }, + attributes: { + type, + level, + textAlign, + showPrefix, + showSearchTerm, + searchResultsTerm, + searchResultsTermSuffix, + }, setAttributes, } ) { const { archiveTypeTitle, archiveNameLabel } = useSelect( ( select ) => { @@ -123,11 +132,40 @@ export default function QueryTitleEdit( { - - { showSearchTerm - ? __( 'Search results for: “search term”' ) - : __( 'Search results' ) } - + { showSearchTerm ? ( +
+ + setAttributes( { searchResultsTerm: content } ) + } + placeholder={ __( 'Search Results for:' ) } + /> + { __( 'Search Term' ) } + + setAttributes( { + searchResultsTermSuffix: content, + } ) + } + placeholder={ __( 'Suffix' ) } + /> +
+ ) : ( + + setAttributes( { searchResultsTerm: content } ) + } + placeholder={ __( 'Search Results' ) } + /> + ) } ); } diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 88a945535a22d..0c4c438482f79 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -38,13 +38,23 @@ function render_block_core_query_title( $attributes ) { } } if ( $is_search ) { - $title = __( 'Search results' ); + $title = isset($attributes['searchResultsTerm']) && !empty($attributes['searchResultsTerm']) ? $attributes['searchResultsTerm'] : __( 'Search results' ); if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) { + + // Get the prefix and suffix. + $prefix = isset($attributes['searchResultsTerm']) ? $attributes['searchResultsTerm'] : __('Search results for: '); + $suffix = isset($attributes['searchResultsTermSuffix']) ? $attributes['searchResultsTermSuffix'] : ''; + + // Get the search query. + $search_query = get_search_query(); + $title = sprintf( - /* translators: %s is the search term. */ - __( 'Search results for: "%s"' ), - get_search_query() + /* translators: 1: Search term prefix, 2: Search term, 3: Search term suffix. */ + __('%1$s "%2$s" %3$s'), + $prefix, + $search_query, + $suffix ); } } diff --git a/packages/block-library/src/query-title/style.scss b/packages/block-library/src/query-title/style.scss index d4a3236cdfff9..11593175ba867 100644 --- a/packages/block-library/src/query-title/style.scss +++ b/packages/block-library/src/query-title/style.scss @@ -2,3 +2,7 @@ // This block has customizable padding, border-box makes that more predictable. box-sizing: border-box; } +div.wp-block-query-title__placeholder { + display: flex; + gap: 10px; +} From a7698f6cccd0ddf7c924166e223b7ef90483b367 Mon Sep 17 00:00:00 2001 From: Vipul Gupta Date: Thu, 9 May 2024 02:50:32 +0530 Subject: [PATCH 2/3] fix: phpcs linting. --- packages/block-library/src/query-title/index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index 0c4c438482f79..dd6b4c37fa282 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -38,20 +38,20 @@ function render_block_core_query_title( $attributes ) { } } if ( $is_search ) { - $title = isset($attributes['searchResultsTerm']) && !empty($attributes['searchResultsTerm']) ? $attributes['searchResultsTerm'] : __( 'Search results' ); + $title = isset( $attributes['searchResultsTerm'] ) && ! empty( $attributes['searchResultsTerm'] ) ? $attributes['searchResultsTerm'] : __( 'Search results' ); if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) { // Get the prefix and suffix. - $prefix = isset($attributes['searchResultsTerm']) ? $attributes['searchResultsTerm'] : __('Search results for: '); - $suffix = isset($attributes['searchResultsTermSuffix']) ? $attributes['searchResultsTermSuffix'] : ''; + $prefix = isset( $attributes['searchResultsTerm'] ) ? $attributes['searchResultsTerm'] : __( 'Search results for: ' ); + $suffix = isset( $attributes['searchResultsTermSuffix'] ) ? $attributes['searchResultsTermSuffix'] : ''; // Get the search query. $search_query = get_search_query(); $title = sprintf( /* translators: 1: Search term prefix, 2: Search term, 3: Search term suffix. */ - __('%1$s "%2$s" %3$s'), + __( '%1$s "%2$s" %3$s' ), $prefix, $search_query, $suffix From 188af27140f074bc0dd6afda098173677c4d738d Mon Sep 17 00:00:00 2001 From: Vipul Gupta Date: Fri, 10 May 2024 02:12:26 +0530 Subject: [PATCH 3/3] fix: Removes the default value for search results. --- packages/block-library/src/query-title/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/query-title/index.php b/packages/block-library/src/query-title/index.php index dd6b4c37fa282..c30517cbdbe1d 100644 --- a/packages/block-library/src/query-title/index.php +++ b/packages/block-library/src/query-title/index.php @@ -38,7 +38,7 @@ function render_block_core_query_title( $attributes ) { } } if ( $is_search ) { - $title = isset( $attributes['searchResultsTerm'] ) && ! empty( $attributes['searchResultsTerm'] ) ? $attributes['searchResultsTerm'] : __( 'Search results' ); + $title = ! empty( $attributes['searchResultsTerm'] ) ? $attributes['searchResultsTerm'] : ''; if ( isset( $attributes['showSearchTerm'] ) && $attributes['showSearchTerm'] ) {