-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpost-exclude-controls.js
More file actions
237 lines (221 loc) · 6.63 KB
/
Copy pathpost-exclude-controls.js
File metadata and controls
237 lines (221 loc) · 6.63 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* WordPress dependencies
*/
import {
ToggleControl,
FormTokenField,
BaseControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityRecord, store as coreDataStore } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
/**
* 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
* @param {Array} props.allowedControls Allowed controls
*
* @return {Element} PostExcludeControls
*/
export const PostExcludeControls = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
// If the control is not allowed, return null.
if (
! allowedControls.includes( 'exclude_current_post' ) &&
! allowedControls.includes( 'exclude_posts' )
) {
return null;
}
return (
<>
<h2> { __( 'Exclude Posts', 'advanced-query-loop' ) }</h2>
<ExcludeCurrentPostControl
attributes={ attributes }
setAttributes={ setAttributes }
allowedControls={ allowedControls }
/>
<ExcludePostsControl
attributes={ attributes }
setAttributes={ setAttributes }
allowedControls={ allowedControls }
/>
</>
);
};
/**
* ExcludeCurrentPostControl is a React functional component used within the context
* of advanced query loop settings. It toggles the exclusion of the current post
* or content associated with the current template from query results.
*
* @param {Object} props The properties passed to the component.
* @param {Object} props.attributes The block attributes.
* @param {Function} props.setAttributes Function to update block attributes.
* @param {Array} props.allowedControls List of control identifiers that are allowed for this block.
*
* @return {Element|null} A `ToggleControl` component if the control is allowed, or `null` if not.
*/
const ExcludeCurrentPostControl = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
const { query: { exclude_current: excludeCurrent } = {} } = attributes;
const { record: siteOptions } = useEntityRecord( 'root', 'site' );
const { currentPost, isAdmin } = useSelect( ( select ) => {
return {
currentPost: select( 'core/editor' ).getCurrentPost(),
isAdmin: select( coreDataStore ).canUser( 'update', {
kind: 'root',
name: 'site',
} ),
};
}, [] );
if ( ! allowedControls.includes( 'exclude_current_post' ) ) {
return null;
}
if ( ! currentPost ) {
return <div>{ __( 'Loading…', 'advanced-query-loop' ) }</div>;
}
const isDisabled = () => {
// If the user is not an admin, they cannot edit template anyway
if ( ! isAdmin ) {
return false;
}
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 (
<ToggleControl
__nextHasNoMarginBottom
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'
)
}
/>
);
};
/**
* The ExcludePostsControl component allows users to exclude specific posts
* from queries based on post titles, providing search and selection
* functionality in the form of a token field.
*
* @param {Object} props The component props.
* @param {Object} props.attributes The block attributes.
* @param {Function} props.setAttributes Function to update the block attributes.
* @param {Array} props.allowedControls List of controls allowed for the current context.
*
* @return {Element|null} Returns the control for selecting excluded posts,
* or null if the 'exclude_posts' control is not allowed.
*/
const ExcludePostsControl = ( {
attributes,
setAttributes,
allowedControls,
} ) => {
const {
query: {
exclude_posts: excludePosts = [],
multiple_posts: multiplePosts = [],
postType,
} = {},
} = attributes;
// 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, type ) => {
// Depending on the number of posts this could take a while, since we can't paginate here
const records = getEntityRecords( 'postType', type, {
per_page: -1,
} );
return [ ...accumulator, ...( records || [] ) ];
},
[]
);
},
[ postType, multiplePosts ]
);
if ( ! allowedControls.includes( 'exclude_posts' ) ) {
return null;
}
// 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 ? [ decodeEntities( post.title.rendered.trim() ) ] : [];
};
const titleToId = ( title ) => {
const post = posts.find(
( p ) => decodeEntities( p.title.rendered.trim() ) === title
);
return post ? [ post.id ] : [];
};
if ( ! posts ) {
return <div>{ __( 'Loading…', 'advanced-query-loop' ) }</div>;
}
return (
<BaseControl
help={ __(
'Start typing to search for a post title to exclude, or manually enter one.',
'advanced-query-loop'
) }
>
<FormTokenField
label={ __( 'Posts to Exclude', 'advanced-query-loop' ) }
value={ excludePosts.flatMap( ( id ) => idToTitle( id ) ) }
suggestions={ posts.map( ( post ) =>
decodeEntities( post.title.rendered.trim() )
) }
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>
);
};