From ef78481ea68a01ee6391e2482fa83bcb22f82b2d Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 14 Aug 2024 13:14:34 -0400 Subject: [PATCH 1/8] Add some utilities to allow checking for GB and core versions. --- composer.json | 3 ++- includes/utilities.php | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 includes/utilities.php diff --git a/composer.json b/composer.json index d23833f..9be7448 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ }, "files": [ "includes/enqueues.php", - "includes/query-loop.php" + "includes/query-loop.php", + "includes/utilities.php" ] }, "require-dev": { diff --git a/includes/utilities.php b/includes/utilities.php new file mode 100644 index 0000000..a046d72 --- /dev/null +++ b/includes/utilities.php @@ -0,0 +1,36 @@ +=' ); + } + return false; +} + +/** + * Helper to determine is the current WP install is at or higher than a given version. + * + * @param string $version The version to check for. + + * @return boolean. + */ +function is_core_version_or_higher( string $version ) { + $core = get_bloginfo( 'version' ); + return version_compare( $core, $version, '>=' ); +} + From 331ecda646094a55951013ff5e5dd942b7919edd Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 14 Aug 2024 13:15:13 -0400 Subject: [PATCH 2/8] Create and expose a new internal slot so we can re-insert the legacy controls. --- src/slots/aql-legacy-controls.js | 25 +++++++++++++++++++++++++ src/variations/controls.js | 8 ++++---- 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/slots/aql-legacy-controls.js diff --git a/src/slots/aql-legacy-controls.js b/src/slots/aql-legacy-controls.js new file mode 100644 index 0000000..2ca7d13 --- /dev/null +++ b/src/slots/aql-legacy-controls.js @@ -0,0 +1,25 @@ +/** + * WordPress dependencies + */ +import { createSlotFill } from '@wordpress/components'; + +/** + * Create our Slot and Fill components + */ +const { Fill, Slot } = createSlotFill( 'AQLLegacyControls' ); + +/** + * This slot is not exposed and is used to try to maintain the same UI + */ + +const AQLLegacyControls = ( { children } ) => { children }; + +AQLLegacyControls.Slot = ( { fillProps } ) => ( + + { ( fills ) => { + return fills.length ? fills : null; + } } + +); + +export default AQLLegacyControls; diff --git a/src/variations/controls.js b/src/variations/controls.js index 9a604d5..e55a69c 100644 --- a/src/variations/controls.js +++ b/src/variations/controls.js @@ -12,8 +12,7 @@ import { createBlock } from '@wordpress/blocks'; import { AQL } from '.'; import AQLControls from '../slots/aql-controls'; import AQLControlsInheritedQuery from '../slots/aql-controls-inherited-query'; -import { PostCountControls } from '../components/post-count-controls'; -import { PostOffsetControls } from '../components/post-offset-controls'; +import AQLLegacyControls from '../slots/aql-legacy-controls'; import { PostMetaQueryControls } from '../components/post-meta-query-controls'; import { PostDateQueryControls } from '../components/post-date-query-controls'; import { MultiplePostSelect } from '../components/multiple-post-select'; @@ -56,9 +55,10 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => { 'advanced-query-loop' ) } > + - - From 24f689ba440635f7c471b7a79d27c4cc7b3c81bb Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 14 Aug 2024 13:15:37 -0400 Subject: [PATCH 3/8] Create new file and entry point for the GB 19 changes. --- src/legacy-controls/pre-gb-19.js | 28 ++++++++++++++++++++++++++++ webpack.config.js | 1 + 2 files changed, 29 insertions(+) create mode 100644 src/legacy-controls/pre-gb-19.js diff --git a/src/legacy-controls/pre-gb-19.js b/src/legacy-controls/pre-gb-19.js new file mode 100644 index 0000000..51d2280 --- /dev/null +++ b/src/legacy-controls/pre-gb-19.js @@ -0,0 +1,28 @@ +/** + * WordPress dependencies + */ +import { registerPlugin } from '@wordpress/plugins'; + +/** + * Internal dependencies + */ +import AQLLegacyControls from '../slots/aql-legacy-controls'; +import { PostCountControls } from '../components/post-count-controls'; +import { PostOffsetControls } from '../components/post-offset-controls'; + +registerPlugin( 'aql-pre-gb-19-controls', { + render: () => { + return ( + <> + + { ( props ) => ( + <> + + + + ) } + + + ); + }, +} ); diff --git a/webpack.config.js b/webpack.config.js index f805079..c3dcf7b 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -10,6 +10,7 @@ module.exports = { entry: { ...getWebpackEntryPoints(), variations: './src/variations/index.js', + 'legacy-pre-gb-19': './src/legacy-controls/pre-gb-19.js', }, output: { ...defaultConfig.output, From 4d4ccdd97806153fe3d70ae386167ac4bd72c9bd Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 14 Aug 2024 13:15:52 -0400 Subject: [PATCH 4/8] Add a check to see if we need to add the legacy controls. --- includes/enqueues.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/includes/enqueues.php b/includes/enqueues.php index 443e14f..c5ebbcf 100644 --- a/includes/enqueues.php +++ b/includes/enqueues.php @@ -7,6 +7,9 @@ namespace AdvancedQueryLoop; +use function AdvancedQueryLoop\Utils\{ is_gutenberg_plugin_version_or_higher,is_core_version_or_higher }; + + // Bail on unit tests. if ( ! function_exists( 'add_action' ) ) { return; @@ -33,5 +36,23 @@ function () { // Allow for translation. wp_set_script_translations( 'advanced-query-loop', 'advanced-query-loop' ); } + + // Per Page, Offset, and Max count controls where merged into GB 19. + if ( ! is_gutenberg_plugin_version_or_higher( '19' ) && ! is_core_version_or_higher( '6.7' ) ) { + // Enqueue the legacy controls. + $pre_gb_19_assets_file = BUILD_DIR_PATH . 'legacy-pre-gb-19.asset.php'; + + if ( file_exists( $pre_gb_19_assets_file ) ) { + $pre_gb_19_assets = include $pre_gb_19_assets_file; + + \wp_enqueue_script( + 'advanced-query-loop-legacy-pre-gb-19', + BUILD_DIR_URL . 'legacy-pre-gb-19.js', + array_merge( array( 'advanced-query-loop' ), $pre_gb_19_assets['dependencies'] ), + $pre_gb_19_assets['version'], + true + ); + } + } } ); From 0f0ef1db5be3984325e5996d12c6f2b7ac4a826d Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Mon, 7 Oct 2024 15:19:57 -0400 Subject: [PATCH 5/8] Add taxonomy control and helper to use data in FormTokenField --- src/components/exclude-taxonomies.js | 93 ++++++++++++++++++++++++++++ src/variations/controls.js | 2 + 2 files changed, 95 insertions(+) create mode 100644 src/components/exclude-taxonomies.js diff --git a/src/components/exclude-taxonomies.js b/src/components/exclude-taxonomies.js new file mode 100644 index 0000000..1bc4dcb --- /dev/null +++ b/src/components/exclude-taxonomies.js @@ -0,0 +1,93 @@ +/** + * WordPress dependencies + */ +/** + * WordPress dependencies + */ +import { FormTokenField, BaseControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; +import { store as coreDataStore } from '@wordpress/core-data'; + +/** + * A helper to retrieve the correct items to display or save in the token field + * + * @param {Array} subSet + * @param {Array} fullSet + * @param {string} lookupProperty + * @param {string} returnProperty + * @return {Array} The correct items to display or save in the token field + */ +function prepDataFromTokenField( + subSet, + fullSet, + lookupProperty, + returnProperty +) { + const subsetFullObjects = fullSet.filter( ( item ) => + subSet.includes( item[ lookupProperty ] ) + ); + return subsetFullObjects.map( + ( { [ returnProperty ]: returnVal } ) => returnVal + ); +} + +export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => { + const { + query: { + multiple_posts: multiplePosts = [], + postType, + excluded_taxonomies: excludedTaxonomies = [], + } = {}, + } = attributes; + + const taxonomies = useSelect( + ( select ) => { + const knownTaxes = select( coreDataStore ).getTaxonomies(); + return knownTaxes.filter( + ( { types } ) => + types.includes( postType ) || + types.some( ( i ) => multiplePosts.includes( i ) ) + ); + }, + [ multiplePosts, postType ] + ); + + return ( + + name ) ] } + onChange={ ( selectedTaxonomies ) => { + setAttributes( { + query: { + ...attributes.query, + excluded_taxonomies: + prepDataFromTokenField( + selectedTaxonomies, + taxonomies, + 'name', + 'slug' + ) || [], + }, + } ); + } } + __experimentalExpandOnFocus + __experimentalShowHowTo={ false } + /> + + ); +}; diff --git a/src/variations/controls.js b/src/variations/controls.js index 9a604d5..1532fe6 100644 --- a/src/variations/controls.js +++ b/src/variations/controls.js @@ -20,6 +20,7 @@ import { MultiplePostSelect } from '../components/multiple-post-select'; import { PostOrderControls } from '../components/post-order-controls'; import { PostExcludeControls } from '../components/post-exclude-controls'; import { PostIncludeControls } from '../components/post-include-controls'; +import { ExcludeTaxonomies } from '../components/exclude-taxonomies'; /** * Determines if the active variation is this one @@ -61,6 +62,7 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => { + From fb95d64f9cd36083f08ba434e4373deb066a4bef Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Mon, 7 Oct 2024 15:32:58 -0400 Subject: [PATCH 6/8] Add the trait to process the taxonomies. --- includes/Query_Params_Generator.php | 2 ++ includes/Traits/Exclude_Taxonomies.php | 30 ++++++++++++++++++++++++++ src/components/exclude-taxonomies.js | 6 +++--- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 includes/Traits/Exclude_Taxonomies.php diff --git a/includes/Query_Params_Generator.php b/includes/Query_Params_Generator.php index 8ca588e..ce97347 100644 --- a/includes/Query_Params_Generator.php +++ b/includes/Query_Params_Generator.php @@ -17,6 +17,7 @@ class Query_Params_Generator { use Traits\Include_Posts; use Traits\Meta_Query; use Traits\Date_Query; + use Traits\Exclude_Taxonomies; /** @@ -28,6 +29,7 @@ class Query_Params_Generator { 'include_posts', 'meta_query', 'date_query', + 'exclude_taxonomies' ); /** diff --git a/includes/Traits/Exclude_Taxonomies.php b/includes/Traits/Exclude_Taxonomies.php new file mode 100644 index 0000000..63fee91 --- /dev/null +++ b/includes/Traits/Exclude_Taxonomies.php @@ -0,0 +1,30 @@ +custom_params['exclude_taxonomies']; + if( count( $taxonomies_to_exclude ) ) { + $tax_query = []; + foreach ( $taxonomies_to_exclude as $slug ) { + $tax_query[] = [ + 'taxonomy' => $slug, + 'operator' => 'NOT EXISTS' + ]; + } + $this->custom_args['tax_query'] = $tax_query; + } + } +} + diff --git a/src/components/exclude-taxonomies.js b/src/components/exclude-taxonomies.js index 1bc4dcb..1ee3bfb 100644 --- a/src/components/exclude-taxonomies.js +++ b/src/components/exclude-taxonomies.js @@ -37,7 +37,7 @@ export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => { query: { multiple_posts: multiplePosts = [], postType, - excluded_taxonomies: excludedTaxonomies = [], + exclude_taxonomies: excludeTaxonomies = [], } = {}, } = attributes; @@ -64,7 +64,7 @@ export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => { label={ __( 'Exclude Taxonomies', 'advanced-query-loop' ) } value={ prepDataFromTokenField( - excludedTaxonomies, + excludeTaxonomies, taxonomies, 'slug', 'name' @@ -75,7 +75,7 @@ export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => { setAttributes( { query: { ...attributes.query, - excluded_taxonomies: + exclude_taxonomies: prepDataFromTokenField( selectedTaxonomies, taxonomies, From 068bb0e1bb60b7ca9e80dd7b42f752b7b7406208 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 9 Oct 2024 12:36:00 -0400 Subject: [PATCH 7/8] Add control to disable pagination controls. --- includes/Query_Params_Generator.php | 6 ++++-- includes/Traits/Disable_Pagination.php | 13 ++++++++++++ src/components/pagination-toggle.js | 29 ++++++++++++++++++++++++++ src/variations/controls.js | 2 ++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 includes/Traits/Disable_Pagination.php create mode 100644 src/components/pagination-toggle.js diff --git a/includes/Query_Params_Generator.php b/includes/Query_Params_Generator.php index 8ca588e..d237b3c 100644 --- a/includes/Query_Params_Generator.php +++ b/includes/Query_Params_Generator.php @@ -17,6 +17,7 @@ class Query_Params_Generator { use Traits\Include_Posts; use Traits\Meta_Query; use Traits\Date_Query; + use Traits\Disable_Pagination; /** @@ -28,6 +29,7 @@ class Query_Params_Generator { 'include_posts', 'meta_query', 'date_query', + 'disable_pagination', ); /** @@ -58,8 +60,8 @@ class Query_Params_Generator { * @param array $custom_params Custom values from AQL. */ public function __construct( $default_params, $custom_params ) { - $this->default_params = is_array( $default_params ) ? $default_params : []; - $this->custom_params = is_array( $custom_params ) ? $custom_params : []; + $this->default_params = is_array( $default_params ) ? $default_params : array(); + $this->custom_params = is_array( $custom_params ) ? $custom_params : array(); } /** diff --git a/includes/Traits/Disable_Pagination.php b/includes/Traits/Disable_Pagination.php new file mode 100644 index 0000000..d46ac81 --- /dev/null +++ b/includes/Traits/Disable_Pagination.php @@ -0,0 +1,13 @@ +custom_args['no_found_rows'] = $this->get_custom_param( 'disable_pagination' ); + } +} diff --git a/src/components/pagination-toggle.js b/src/components/pagination-toggle.js new file mode 100644 index 0000000..d654527 --- /dev/null +++ b/src/components/pagination-toggle.js @@ -0,0 +1,29 @@ +/** + * WordPress dependencies + */ +import { ToggleControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; + +export const PaginationToggle = ( { attributes, setAttributes } ) => { + const { query: { disable_pagination: disablePagination } = {} } = + attributes; + + return ( + { + setAttributes( { + query: { + ...attributes.query, + disable_pagination: ! disablePagination, + }, + } ); + } } + /> + ); +}; diff --git a/src/variations/controls.js b/src/variations/controls.js index 9a604d5..80b46ee 100644 --- a/src/variations/controls.js +++ b/src/variations/controls.js @@ -20,6 +20,7 @@ import { MultiplePostSelect } from '../components/multiple-post-select'; import { PostOrderControls } from '../components/post-order-controls'; import { PostExcludeControls } from '../components/post-exclude-controls'; import { PostIncludeControls } from '../components/post-include-controls'; +import { PaginationToggle } from '../components/pagination-toggle'; /** * Determines if the active variation is this one @@ -56,6 +57,7 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => { 'advanced-query-loop' ) } > + From 5e17ad7233a67043471c3653445bbee8417439a9 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 9 Oct 2024 12:36:20 -0400 Subject: [PATCH 8/8] Fix for include posts firing a fatal. --- includes/Traits/Include_Posts.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/includes/Traits/Include_Posts.php b/includes/Traits/Include_Posts.php index 5982d8d..51e5d12 100644 --- a/includes/Traits/Include_Posts.php +++ b/includes/Traits/Include_Posts.php @@ -19,6 +19,9 @@ public function process_include_posts() { * @return array */ protected function get_include_ids( $include_posts ) { - return array_column( $include_posts, 'id' ); + if ( is_array( $include_posts ) ) { + return array_column( $include_posts, 'id' ); + } + return array(); } }