From 8dc10941edc81ebdaefdc265c8cd0abb23ce6246 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 20 Nov 2024 15:37:57 -0500 Subject: [PATCH 1/4] Add the Post_Parent trait and a new helper./ --- includes/Query_Params_Generator.php | 16 ++++++++++++++++ includes/Traits/Exclude_Current.php | 2 +- includes/Traits/Post_Parent.php | 27 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 includes/Traits/Post_Parent.php diff --git a/includes/Query_Params_Generator.php b/includes/Query_Params_Generator.php index 5f30139..9e17f4f 100644 --- a/includes/Query_Params_Generator.php +++ b/includes/Query_Params_Generator.php @@ -19,6 +19,7 @@ class Query_Params_Generator { use Traits\Date_Query; use Traits\Exclude_Taxonomies; use Traits\Disable_Pagination; + use Traits\Post_Parent; /** @@ -32,6 +33,7 @@ class Query_Params_Generator { 'date_query', 'exclude_taxonomies', 'disable_pagination', + 'post_parent', ); /** @@ -66,6 +68,20 @@ public function __construct( $default_params, $custom_params ) { $this->custom_params = is_array( $custom_params ) ? $custom_params : array(); } + + /** + * Checks to see if the item that is passed is a post ID. + * + * This is used to check if the user is editing a template + * + * @param mixed $possible_post_id The potential post id + * + * @return bool Whether the passed item is a post id or not. + */ + private function is_post_id( $possible_post_id ) { + return is_int( $possible_post_id ) || ! preg_match( '/[a-z\-]+\/\/[a-z\-]+/', $possible_post_id ); + } + /** * Check to see if a param is in the list. * diff --git a/includes/Traits/Exclude_Current.php b/includes/Traits/Exclude_Current.php index a93d111..14ec544 100644 --- a/includes/Traits/Exclude_Current.php +++ b/includes/Traits/Exclude_Current.php @@ -28,7 +28,7 @@ public function get_exclude_ids( $to_exclude ) { // If there are already posts to be excluded, we need to add to them. $exclude_ids = $this->custom_args['post__not_in'] ?? array(); - if ( is_int( $to_exclude ) || ! preg_match( '/[a-z\-]+\/\/[a-z\-]+/', $to_exclude ) ) { + if ( $this->is_post_id( $to_exclude ) ) { array_push( $exclude_ids, intval( $to_exclude ) ); } else { // This is usually when this was set on a template. diff --git a/includes/Traits/Post_Parent.php b/includes/Traits/Post_Parent.php new file mode 100644 index 0000000..e08d28e --- /dev/null +++ b/includes/Traits/Post_Parent.php @@ -0,0 +1,27 @@ +custom_params['post_parent']; + + if ( $this->is_post_id( $parent ) ) { + $this->custom_args['post_parent'] = $parent; + } else { + // This is usually when this was set on a template. + global $post; + $this->custom_args['post_parent'] = $post->ID; + } + } +} From d703ca104c820d7280557e8b7c49e22a976ad3b9 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 20 Nov 2024 15:38:13 -0500 Subject: [PATCH 2/4] Remove .vscode code from version control. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 82dc413..4226692 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ DNU composer.lock .phpunit.* artifacts +.vscode From f5de3cf59d7d53f00e60d81812a823bcd301ae1d Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 20 Nov 2024 15:38:40 -0500 Subject: [PATCH 3/4] Add __nextHasNoMarginBottom prop to existing components. --- src/components/pagination-toggle.js | 1 + src/components/post-exclude-controls.js | 1 + src/components/post-order-controls.js | 1 + 3 files changed, 3 insertions(+) diff --git a/src/components/pagination-toggle.js b/src/components/pagination-toggle.js index d654527..36ca3f0 100644 --- a/src/components/pagination-toggle.js +++ b/src/components/pagination-toggle.js @@ -10,6 +10,7 @@ export const PaginationToggle = ( { attributes, setAttributes } ) => { return ( { <>

{ __( 'Exclude Posts', 'advanced-query-loop' ) }

{ } } /> { From 4b0ec80cb22e11779814339f6597f4fa88b97165 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 20 Nov 2024 15:38:58 -0500 Subject: [PATCH 4/4] Introduce the new control to only show child items. --- src/components/child-items-toggle.js | 45 ++++++++++++++++++++++++++++ src/variations/controls.js | 2 ++ 2 files changed, 47 insertions(+) create mode 100644 src/components/child-items-toggle.js diff --git a/src/components/child-items-toggle.js b/src/components/child-items-toggle.js new file mode 100644 index 0000000..025ad41 --- /dev/null +++ b/src/components/child-items-toggle.js @@ -0,0 +1,45 @@ +/** + * WordPress dependencies + */ +import { ToggleControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; +import { store as editorStore } from '@wordpress/editor'; + +export const ChildItemsToggle = ( { attributes, setAttributes } ) => { + const { query: { post_parent: postParent } = {} } = attributes; + + const { isHierarchial, postTypeName, postID } = useSelect( ( select ) => { + const post = select( editorStore ).getCurrentPost(); + const postType = select( editorStore ).getCurrentPostType(); + const postTypeObject = select( coreStore ).getPostType( postType ); + + return { + isHierarchial: postTypeObject?.hierarchical, + postTypeName: postType, + postID: post?.id, + }; + }, [] ); + + return ( + + setAttributes( { + query: { + ...attributes.query, + post_parent: value ? postID : 0, + }, + } ) + } + /> + ); +}; diff --git a/src/variations/controls.js b/src/variations/controls.js index 32ac2de..3a8c7dc 100644 --- a/src/variations/controls.js +++ b/src/variations/controls.js @@ -21,6 +21,7 @@ import { PostExcludeControls } from '../components/post-exclude-controls'; import { PostIncludeControls } from '../components/post-include-controls'; import { ExcludeTaxonomies } from '../components/exclude-taxonomies'; import { PaginationToggle } from '../components/pagination-toggle'; +import { ChildItemsToggle } from '../components/child-items-toggle'; /** * Determines if the active variation is this one @@ -66,6 +67,7 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => { +