diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 7b7dbe9291567..6fd1a43252c9a 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -811,7 +811,7 @@ Help visitors find your content. ([Source](https://github.com/WordPress/gutenber - **Name:** core/search - **Category:** widgets - **Supports:** align (center, left, right), color (background, gradients, text), interactivity, typography (fontSize, lineHeight), ~~html~~ -- **Attributes:** buttonPosition, buttonText, buttonUseIcon, isSearchFieldHidden, label, placeholder, query, showLabel, width, widthUnit +- **Attributes:** buttonPosition, buttonText, buttonUseIcon, categories, isSearchFieldHidden, label, limitResults, placeholder, postType, query, showLabel, width, widthUnit ## Separator diff --git a/packages/block-library/src/search/block.json b/packages/block-library/src/search/block.json index 8d5e208045068..f7ae740bca913 100644 --- a/packages/block-library/src/search/block.json +++ b/packages/block-library/src/search/block.json @@ -46,6 +46,18 @@ "isSearchFieldHidden": { "type": "boolean", "default": false + }, + "limitResults": { + "type": "boolean", + "default": false + }, + "postType": { + "type": "string", + "default": "post" + }, + "categories": { + "type": "object", + "default": {} } }, "supports": { diff --git a/packages/block-library/src/search/edit.js b/packages/block-library/src/search/edit.js index cfe7b29caf5de..c9ac667544313 100644 --- a/packages/block-library/src/search/edit.js +++ b/packages/block-library/src/search/edit.js @@ -31,6 +31,8 @@ import { BaseControl, __experimentalUseCustomUnits as useCustomUnits, __experimentalUnitControl as UnitControl, + SelectControl, + ToggleControl, } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; import { Icon, search } from '@wordpress/icons'; @@ -53,7 +55,10 @@ import { PX_WIDTH_DEFAULT, MIN_WIDTH, isPercentageUnit, + usePostTypes, + useTaxonomies, } from './utils.js'; +import { TaxonomyControls } from './taxonomy-controls.js'; // Used to calculate border radius adjustment to avoid "fat" corners when // button is placed inside wrapper. @@ -79,8 +84,27 @@ export default function SearchEdit( { buttonUseIcon, isSearchFieldHidden, style, + postType, + limitResults, + categories, } = attributes; + const { postTypesTaxonomiesMap, postTypesSelectOptions } = usePostTypes(); + const taxonomies = useTaxonomies( postType ); + const onPostTypeChange = ( newValue ) => { + // We need to dynamically update the `taxQuery` property, + // by removing any not supported taxonomy from the query. + const supportedTaxonomies = postTypesTaxonomiesMap[ newValue ]; + + // Update the categories attribute + const updatedCategories = {}; + supportedTaxonomies.forEach( ( taxonomy ) => { + updatedCategories[ taxonomy ] = []; + } ); + + setAttributes( { categories: updatedCategories, postType: newValue } ); + }; + const wasJustInsertedIntoNavigationBlock = useSelect( ( select ) => { const { getBlockParentsByBlockName, wasBlockJustInserted } = @@ -307,6 +331,10 @@ export default function SearchEdit( { ); }; + const handleTaxonomiesChange = ( newCategories ) => { + setAttributes( { categories: newCategories } ); + }; + const renderButton = () => { // If the button is inside the wrapper, the wrapper gets the border color styles/classes, not the button. const buttonClasses = clsx( @@ -474,6 +502,35 @@ export default function SearchEdit( { } ) } + + setAttributes( { + limitResults: ! limitResults, + } ) + } + /> + { limitResults && ( + + ) } + { limitResults && taxonomies?.length && ( + + ) } diff --git a/packages/block-library/src/search/index.php b/packages/block-library/src/search/index.php index 39b8591c86600..6f064f047faf6 100644 --- a/packages/block-library/src/search/index.php +++ b/packages/block-library/src/search/index.php @@ -196,12 +196,41 @@ function render_block_core_search( $attributes ) { '; } + $post_type_input = ''; + $taxonomy_inputs = ''; + + if ( $attributes['limitResults'] ) { + $post_type = ! empty( $attributes['postType'] ) ? $attributes['postType'] : ''; + $taxonomies = ! empty( $attributes['categories'] ) ? $attributes['categories'] : array(); + + foreach ( $taxonomies as $taxonomy => $terms ) { + if ( ! empty( $terms ) ) { + $taxonomy_terms = array_map( + function ( $term_id ) use ( $taxonomy ) { + $term = get_term_by( 'id', $term_id, $taxonomy ); + return $term ? $term->slug : ''; + }, + $terms + ); + + $input_name = ( 'category' === $taxonomy ) ? 'category_name' : ( ( 'post_tag' === $taxonomy ) ? 'tag' : $taxonomy ); + $taxonomy_input = sprintf( '', esc_attr( $input_name ), esc_attr( implode( ',', $taxonomy_terms ) ) ); + $taxonomy_inputs .= $taxonomy_input; + } + } + + if ( ! empty( $post_type ) ) { + $post_type_input = sprintf( '', esc_attr( $post_type ) ); + } + } + return sprintf( - '
%4s
', + '
%4s %5$s
', esc_url( home_url( '/' ) ), $wrapper_attributes, $form_directives, - $label . $field_markup + $label . $field_markup, + $post_type_input . $taxonomy_inputs ); }