Skip to content

Commit

Permalink
Merge pull request #89 from ryanwelcher/feature/children-of-current-item
Browse files Browse the repository at this point in the history
Show children of current item only
  • Loading branch information
ryanwelcher authored Dec 5, 2024
2 parents a8897bf + 4b0ec80 commit 98031d4
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 1 deletion.
16 changes: 16 additions & 0 deletions includes/Query_Params_Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Query_Params_Generator {
use Traits\Date_Query;
use Traits\Exclude_Taxonomies;
use Traits\Disable_Pagination;
use Traits\Post_Parent;


/**
Expand All @@ -32,6 +33,7 @@ class Query_Params_Generator {
'date_query',
'exclude_taxonomies',
'disable_pagination',
'post_parent',
);

/**
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion includes/Traits/Exclude_Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions includes/Traits/Post_Parent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Post Parent Processing
*/

namespace AdvancedQueryLoop\Traits;

/**
* Trait
*/
trait Post_Parent {

/**
* Main processing function.
*/
public function process_post_parent(): void {
$parent = $this->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;
}
}
}
45 changes: 45 additions & 0 deletions src/components/child-items-toggle.js
Original file line number Diff line number Diff line change
@@ -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 (
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show child items only', 'advanced-query-loop' ) }
help={ __(
'Only show child items of this item. This option is only available for hierarchical post types such as pages.',
'advanced-query-loop'
) }
disabled={ ! isHierarchial && postTypeName !== 'wp_template' }
checked={ !! postParent }
onChange={ ( value ) =>
setAttributes( {
query: {
...attributes.query,
post_parent: value ? postID : 0,
},
} )
}
/>
);
};
1 change: 1 addition & 0 deletions src/components/pagination-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const PaginationToggle = ( { attributes, setAttributes } ) => {

return (
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Disable pagination', 'advanced-query-loop' ) }
help={ __(
'Disabling pagination will not show any pagination controls on the front end. It can also provide a performance improvement for complicated queries.',
Expand Down
1 change: 1 addition & 0 deletions src/components/post-exclude-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const PostExcludeControls = ( { attributes, setAttributes } ) => {
<>
<h2> { __( 'Exclude Posts', 'advanced-query-loop' ) }</h2>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Exclude Current Post', 'advanced-query-loop' ) }
checked={ !! excludeCurrent }
disabled={ isDisabled() }
Expand Down
1 change: 1 addition & 0 deletions src/components/post-order-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const PostOrderControls = ( { attributes, setAttributes } ) => {
} }
/>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Ascending Order', 'advanced-query-loop' ) }
checked={ order === 'asc' }
onChange={ () => {
Expand Down
2 changes: 2 additions & 0 deletions src/variations/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -66,6 +67,7 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => {
<PostExcludeControls { ...props } />
<ExcludeTaxonomies { ...props } />
<PostIncludeControls { ...props } />
<ChildItemsToggle { ...props } />
<PostMetaQueryControls { ...props } />
<PostDateQueryControls { ...props } />
<AQLControls.Slot fillProps={ { ...props } } />
Expand Down

0 comments on commit 98031d4

Please sign in to comment.