forked from ryanwelcher/advanced-query-loop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-exclude-controls.js
More file actions
144 lines (133 loc) · 3.89 KB
/
post-exclude-controls.js
File metadata and controls
144 lines (133 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* WordPress dependencies
*/
import {
ToggleControl,
FormTokenField,
BaseControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityRecord } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
/**
* A component that lets you pick posts to be excluded from the query
*
* @param {Object} props Component props
* @param {Object} props.attributes Block attributes
* @param {Function} props.setAttributes Block attributes setter
*
* @return {Element} PostExcludeControls
*/
export const PostExcludeControls = ( { attributes, setAttributes } ) => {
const {
query: {
exclude_posts: excludePosts = [],
exclude_current: excludeCurrent,
postType,
multiple_posts: multiplePosts = [],
} = {},
} = attributes;
const { record: siteOptions } = useEntityRecord( 'root', 'site' );
const currentPost = useSelect( ( select ) => {
return select( 'core/editor' ).getCurrentPost();
}, [] );
// Get the posts for all post types used in the query.
const posts = useSelect(
( select ) => {
const { getEntityRecords } = select( 'core' );
// Fetch posts for each post type and combine them into one array
return [ ...multiplePosts, postType ].reduce(
( accumulator, postType ) => {
// Depending on the number of posts this could take a while, since we can't paginate here
const records = getEntityRecords( 'postType', postType, {
per_page: -1,
} );
return [ ...accumulator, ...( records || [] ) ];
},
[]
);
},
[ postType, multiplePosts ]
);
// For use with flatMap(), as this lets us remove elements during a map()
const idToTitle = ( id ) => {
const post = posts.find( ( p ) => p.id === id );
return post ? [ post.title.rendered ] : [];
};
const titleToId = ( title ) => {
const post = posts.find( ( p ) => p.title.rendered === title );
return post ? [ post.id ] : [];
};
if ( ! currentPost || ! posts ) {
return <div>{ __( 'Loading…', 'advanced-query-loop' ) }</div>;
}
const isDisabled = () => {
const templatesToExclude = [ 'archive', 'search' ];
const {
show_on_front: showOnFront, // What is the front page set to show? Options: 'posts' or 'page'
} = siteOptions;
const disabledTemplates = [
...templatesToExclude,
...( showOnFront === 'posts' ? [ 'home', 'front-page' ] : [] ),
];
return (
currentPost.type === 'wp_template' &&
disabledTemplates.includes( currentPost.slug )
);
};
return (
<>
<h2> { __( 'Exclude Posts', 'advanced-query-loop' ) }</h2>
<BaseControl
help={ __(
'Start typing to search for a post title or manually enter one.',
'advanced-query-loop'
) }
>
<FormTokenField
label={ __( 'Posts', 'advanced-query-loop' ) }
value={ excludePosts.flatMap( ( id ) => idToTitle( id ) ) }
suggestions={ posts.map( ( post ) => post.title.rendered ) }
onChange={ ( titles ) => {
// Converts the Titles to Post IDs before saving them
setAttributes( {
query: {
...attributes.query,
exclude_posts:
titles.flatMap( ( title ) =>
titleToId( title )
) || [],
},
} );
} }
__experimentalExpandOnFocus
__experimentalShowHowTo={ false }
/>
</BaseControl>
<ToggleControl
label={ __( 'Exclude Current Post', 'advanced-query-loop' ) }
checked={ !! excludeCurrent }
disabled={ isDisabled() }
onChange={ ( value ) => {
setAttributes( {
query: {
...attributes.query,
exclude_current: value ? currentPost.id : 0,
},
} );
} }
help={
isDisabled()
? __(
'This option is disabled for this template as there is no dedicated post to exclude.',
'advanced-query-loop'
)
: __(
'Remove the associated post for this template/content from the query results.',
'advanced-query-loop'
)
}
/>
</>
);
};