From a3ea4f70a05ba3a955fcd564e16b8e26926243a6 Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 10 Jul 2024 22:44:37 +0200 Subject: [PATCH 1/3] Query Loop Block: add keywords 'content' and 'blog' --- packages/block-library/src/query/block.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index b602032df36005..ef7b8a799ec863 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -5,6 +5,7 @@ "title": "Query Loop", "category": "theme", "description": "An advanced block that allows displaying post types based on different query parameters and visual configurations.", + "keywords": [ "blog", "content" ], "textdomain": "default", "attributes": { "queryId": { From 851c8de44803000f120cb08f129a63b1e56a8e4c Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 10 Jul 2024 23:36:29 +0200 Subject: [PATCH 2/3] Add variations --- packages/block-library/src/query/block.json | 1 - packages/block-library/src/query/index.js | 55 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/query/block.json b/packages/block-library/src/query/block.json index ef7b8a799ec863..b602032df36005 100644 --- a/packages/block-library/src/query/block.json +++ b/packages/block-library/src/query/block.json @@ -5,7 +5,6 @@ "title": "Query Loop", "category": "theme", "description": "An advanced block that allows displaying post types based on different query parameters and visual configurations.", - "keywords": [ "blog", "content" ], "textdomain": "default", "attributes": { "queryId": { diff --git a/packages/block-library/src/query/index.js b/packages/block-library/src/query/index.js index 0a757b8ee11e92..599d33435b0fb3 100644 --- a/packages/block-library/src/query/index.js +++ b/packages/block-library/src/query/index.js @@ -2,6 +2,13 @@ * WordPress dependencies */ import { loop as icon } from '@wordpress/icons'; +import { subscribe, select } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; +import { + registerBlockVariation, + unregisterBlockVariation, +} from '@wordpress/blocks'; +import { __, sprintf } from '@wordpress/i18n'; /** * Internal dependencies @@ -64,4 +71,52 @@ export const settings = { deprecated, }; +const previousPostTypes = []; +let haltSubscribing = false; + +const keywords = { + post: [ 'blog' ], +}; + +subscribe( () => { + if ( haltSubscribing ) { + return; + } + const { getPostTypes } = select( coreStore ); + const excludedPostTypes = [ 'attachment' ]; + const filteredPostTypes = ( getPostTypes( { per_page: -1 } ) ?? [] ).filter( + ( { viewable, slug } ) => + viewable && ! excludedPostTypes.includes( slug ) + ); + + for ( const postType of previousPostTypes ) { + if ( ! filteredPostTypes.includes( postType ) ) { + haltSubscribing = true; + unregisterBlockVariation( name, postType.slug ); + haltSubscribing = false; + } + } + + for ( const postType of filteredPostTypes ) { + if ( ! previousPostTypes.includes( postType ) ) { + haltSubscribing = true; + registerBlockVariation( name, { + name: postType.slug, + title: sprintf( + /* translators: %s: post type */ + __( '%s Query Loop' ), + postType.labels.singular_name + ), + attributes: { + query: { + postType: postType.slug, + }, + }, + keywords: keywords[ postType.slug ] ?? [], + } ); + haltSubscribing = false; + } + } +} ); + export const init = () => initBlock( { name, metadata, settings } ); From 1256f264b776ac1fecf94bbd2b7921cf71b9f541 Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 10 Jul 2024 23:42:16 +0200 Subject: [PATCH 3/3] Fix keywords --- packages/block-library/src/query/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/query/index.js b/packages/block-library/src/query/index.js index 599d33435b0fb3..fc17fcd2e1e28e 100644 --- a/packages/block-library/src/query/index.js +++ b/packages/block-library/src/query/index.js @@ -71,7 +71,7 @@ export const settings = { deprecated, }; -const previousPostTypes = []; +let previousPostTypes = []; let haltSubscribing = false; const keywords = { @@ -112,11 +112,17 @@ subscribe( () => { postType: postType.slug, }, }, - keywords: keywords[ postType.slug ] ?? [], + keywords: [ + postType.labels.name, + postType.labels.menu_name, + ...( keywords[ postType.slug ] ?? [] ), + ], } ); haltSubscribing = false; } } + + previousPostTypes = filteredPostTypes; } ); export const init = () => initBlock( { name, metadata, settings } );