diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts index 96ecb190aa4..6a5759c3597 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/constants.ts @@ -40,7 +40,6 @@ export const DEFAULT_QUERY: ProductCollectionQuery = { postType: 'product', order: 'asc', orderBy: 'title', - author: '', search: '', exclude: [], inherit: null, diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/author-control.tsx b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/author-control.tsx deleted file mode 100644 index 8fedd27376c..00000000000 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/author-control.tsx +++ /dev/null @@ -1,151 +0,0 @@ -/** - * External dependencies - */ -import { __ } from '@wordpress/i18n'; -import { useEntityRecords } from '@wordpress/core-data'; -import { - FormTokenField, - // @ts-expect-error Using experimental features - // eslint-disable-next-line @wordpress/no-unsafe-wp-apis - __experimentalToolsPanelItem as ToolsPanelItem, -} from '@wordpress/components'; - -/** - * Internal dependencies - */ -import { QueryControlProps } from '../types'; - -interface Author { - id: string; - name: string; -} - -interface AuthorsInfo { - authors: Author[]; - mapById: Map< number, Author >; - mapByName: Map< string, Author >; - names: string[]; -} - -const AUTHORS_QUERY = { - who: 'authors', - per_page: -1, - _fields: 'id,name', - context: 'view', -}; - -export const getAuthorsInfo = ( authors: Author[] ): AuthorsInfo => { - const mapById = new Map< number, Author >(); - const mapByName = new Map< string, Author >(); - const names: string[] = []; - - authors.forEach( ( author ) => { - mapById.set( Number( author.id ), author ); - mapByName.set( author.name, author ); - names.push( author.name ); - } ); - - return { - authors, - mapById, - mapByName, - names, - }; -}; - -const getIdByValue = ( - entitiesMappedByName: Map< string, Author >, - authorValue: string | Author -) => { - const id = - ( authorValue as Author )?.id || - entitiesMappedByName.get( authorValue as string )?.id; - if ( id ) return id; -}; - -function AuthorControl( { query, setQueryAttribute }: QueryControlProps ) { - const value = query.author; - const { records: authorsList, error } = useEntityRecords< Author[] >( - 'root', - 'user', - AUTHORS_QUERY - ); - - if ( error ) { - return ( - true } - > - - - ); - } - - if ( ! authorsList ) return null; - - const authorsInfo = getAuthorsInfo( authorsList as Author[] ); - - /** - * We need to normalize the value because the block operates on a - * comma(`,`) separated string value and `FormTokenFields` needs an - * array. - * - * Returns only the existing authors ids. This prevents the component - * from crashing in the editor, when non existing ids are provided. - */ - const sanitizedValue = value - ? ( value - .split( ',' ) - .map( ( authorId ) => { - const author = authorsInfo.mapById.get( - Number( authorId ) - ); - return author - ? { - id: author.id, - value: author.name, - } - : null; - } ) - .filter( Boolean ) as { id: string; value: string }[] ) - : []; - - const onAuthorChange = ( newValue: string[] ) => { - const ids = Array.from( - newValue.reduce( ( accumulator: Set< string >, author: string ) => { - // Verify that new values point to existing entities. - const id = getIdByValue( authorsInfo.mapByName, author ); - if ( id ) accumulator.add( id ); - return accumulator; - }, new Set() ) - ); - setQueryAttribute( { author: ids.join( ',' ) } ); - }; - - return ( - !! value } - label={ __( 'Authors', 'woo-gutenberg-products-block' ) } - onDeselect={ () => setQueryAttribute( { author: '' } ) } - > - - - ); -} - -export default AuthorControl; diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/index.tsx b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/index.tsx index a68c6a178ee..8d8ffed8f85 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/index.tsx +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/inspector-controls/index.tsx @@ -38,7 +38,6 @@ import KeywordControl from './keyword-control'; import AttributesControl from './attributes-control'; import TaxonomyControls from './taxonomy-controls'; import HandPickedProductsControl from './hand-picked-products-control'; -import AuthorControl from './author-control'; import LayoutOptionsControl from './layout-options-control'; const ProductCollectionInspectorControls = ( @@ -99,7 +98,6 @@ const ProductCollectionInspectorControls = ( - ) : null } diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/types.ts b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/types.ts index 7d20d723fee..ad2f0d4a26a 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-collection/types.ts +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-collection/types.ts @@ -29,7 +29,6 @@ export interface ProductCollectionDisplayLayout { } export interface ProductCollectionQuery { - author: string; exclude: string[]; inherit: boolean | null; offset: number; diff --git a/plugins/woocommerce-blocks/assets/js/blocks/product-template/edit.tsx b/plugins/woocommerce-blocks/assets/js/blocks/product-template/edit.tsx index 7bcff3bdbb1..15572127262 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/product-template/edit.tsx +++ b/plugins/woocommerce-blocks/assets/js/blocks/product-template/edit.tsx @@ -77,7 +77,6 @@ const ProductTemplateEdit = ( { offset = 0, order, orderBy, - author, search, exclude, inherit, @@ -155,9 +154,6 @@ const ProductTemplateEdit = ( { if ( perPage ) { query.per_page = perPage; } - if ( author ) { - query.author = author; - } if ( search ) { query.search = search; } @@ -186,7 +182,6 @@ const ProductTemplateEdit = ( { order, orderBy, clientId, - author, search, postType, exclude, diff --git a/plugins/woocommerce-blocks/src/BlockTypes/ProductCollection.php b/plugins/woocommerce-blocks/src/BlockTypes/ProductCollection.php index a486345f2be..eef32cfa7b5 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/ProductCollection.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/ProductCollection.php @@ -207,7 +207,9 @@ class ProductCollection extends AbstractBlock { $stock_status = $request->get_param( 'woocommerceStockStatus' ); $product_attributes = $request->get_param( 'woocommerceAttributes' ); $handpicked_products = $request->get_param( 'woocommerceHandPickedProducts' ); - $args['author'] = $request->get_param( 'author' ) ?? ''; + // This argument is required for the tests to PHP Unit Tests to run correctly. + // Most likely this argument is being accessed in the test environment image. + $args['author'] = ''; return $this->get_final_query_args( $args, @@ -297,7 +299,6 @@ class ProductCollection extends AbstractBlock { 'tax_query' => array(), 'paged' => $page, 's' => $query['search'], - 'author' => $query['author'] ?? '', ); $is_on_sale = $query['woocommerceOnSale'] ?? false; diff --git a/plugins/woocommerce-blocks/tests/php/BlockTypes/ProductCollection.php b/plugins/woocommerce-blocks/tests/php/BlockTypes/ProductCollection.php index 412c32d4213..5007f28960b 100644 --- a/plugins/woocommerce-blocks/tests/php/BlockTypes/ProductCollection.php +++ b/plugins/woocommerce-blocks/tests/php/BlockTypes/ProductCollection.php @@ -29,7 +29,6 @@ class ProductCollection extends \WP_UnitTestCase { 'postType' => 'product', 'order' => 'desc', 'orderBy' => 'date', - 'author' => '', 'search' => '', 'exclude' => array(), 'sticky' => '',