@ideasonpurpose/wp-theme-addons 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CssLocalProps/CssLocalProps.php +102 -0
- package/CssLocalProps/css-local-props.js +79 -0
- package/README.md +168 -0
- package/editor/Block/Variation/Group/LinkedGroup/LinkedGroup.php +84 -0
- package/editor/Block/Variation/Group/LinkedGroup/linked-group.css +22 -0
- package/editor/Block/Variation/Group/LinkedGroup/linked-group.js +366 -0
- package/editor/Block/Variation/Query/RelatedPostsQuery/RelatedPostsQuery.php +168 -0
- package/editor/Block/Variation/Query/RelatedPostsQuery/related-posts-query.js +250 -0
- package/index.js +7 -0
- package/lib/RelatedPosts/RelatedPosts.php +527 -0
- package/lib/use-public-post-types.js +36 -0
- package/lib/use-public-taxonomies.js +21 -0
- package/package.json +63 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { registerBlockVariation } from "@wordpress/blocks";
|
|
2
|
+
import { addFilter } from "@wordpress/hooks";
|
|
3
|
+
import { SVG, Path } from "@wordpress/primitives";
|
|
4
|
+
|
|
5
|
+
import { usePublicPostTypes } from "../../../../../lib/use-public-post-types.js";
|
|
6
|
+
import { usePublicTaxonomies } from "../../../../../lib/use-public-taxonomies.js";
|
|
7
|
+
|
|
8
|
+
import { createHigherOrderComponent } from "@wordpress/compose";
|
|
9
|
+
|
|
10
|
+
import { store as coreStore } from "@wordpress/core-data";
|
|
11
|
+
import { store as editorStore } from "@wordpress/editor";
|
|
12
|
+
|
|
13
|
+
import { InspectorControls } from "@wordpress/block-editor";
|
|
14
|
+
import { select } from "@wordpress/data";
|
|
15
|
+
import {
|
|
16
|
+
RangeControl,
|
|
17
|
+
SelectControl,
|
|
18
|
+
Spinner,
|
|
19
|
+
Tip,
|
|
20
|
+
PanelBody,
|
|
21
|
+
} from "@wordpress/components";
|
|
22
|
+
|
|
23
|
+
const icon = (
|
|
24
|
+
<SVG
|
|
25
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
26
|
+
height="24px"
|
|
27
|
+
viewBox="0 -960 960 960"
|
|
28
|
+
width="24px"
|
|
29
|
+
>
|
|
30
|
+
<Path d="M160-120q-33 0-56.5-23.5T80-200v-280h80v280h360v80H160Zm160-160q-33 0-56.5-23.5T240-360v-280h80v280h360v80H320Zm160-160q-33 0-56.5-23.5T400-520v-240q0-33 23.5-56.5T480-840h320q33 0 56.5 23.5T880-760v240q0 33-23.5 56.5T800-440H480Zm0-80h320v-160H480v160Z" />
|
|
31
|
+
</SVG>
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
export function initRelatedPostsQueryBlock() {
|
|
35
|
+
registerBlockVariation("core/query", {
|
|
36
|
+
name: "query-related-posts",
|
|
37
|
+
title: "Related Posts Query",
|
|
38
|
+
description: "Display Related Posts",
|
|
39
|
+
attributes: {
|
|
40
|
+
namespace: "ideasonpurpose/query-related-posts",
|
|
41
|
+
query: {
|
|
42
|
+
inherit: false, // required for initial results
|
|
43
|
+
postType: "post", // required for initial results, no way of knowing the exact post_type at this point
|
|
44
|
+
perPage: 3, // required for initial results
|
|
45
|
+
postTypes: [],
|
|
46
|
+
weights: { post_type: 3, tag: 1, category: 4 },
|
|
47
|
+
relatedPost: 0,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
// To expose the native perPage controls allow ["pagination", "offset", "perPage"]
|
|
51
|
+
// allowedControls: ["pagination", "offset", "perPage"],
|
|
52
|
+
allowedControls: [],
|
|
53
|
+
icon,
|
|
54
|
+
isActive: ["namespace"],
|
|
55
|
+
scope: ["inserter"],
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
addFilter(
|
|
59
|
+
"editor.BlockEdit",
|
|
60
|
+
"ideasonpurpose/group-linked/add-controls",
|
|
61
|
+
wrappedEdit,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Reference for core-data methods:
|
|
67
|
+
* @link https://developer.wordpress.org/block-editor/reference-guides/data/data-core/
|
|
68
|
+
*/
|
|
69
|
+
const wrappedEdit = createHigherOrderComponent((BlockEdit) => {
|
|
70
|
+
return (props) => {
|
|
71
|
+
const { name, attributes, setAttributes } = props;
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
name !== "core/query" ||
|
|
75
|
+
attributes?.namespace !== "ideasonpurpose/query-related-posts"
|
|
76
|
+
) {
|
|
77
|
+
return <BlockEdit {...props} />;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const postId = select(editorStore).getCurrentPostId();
|
|
81
|
+
const currentPostType = select(editorStore).getCurrentPostType();
|
|
82
|
+
|
|
83
|
+
const { query } = attributes;
|
|
84
|
+
|
|
85
|
+
if (!query.postTypes.length) {
|
|
86
|
+
query.postType = currentPostType;
|
|
87
|
+
query.postTypes.push(currentPostType);
|
|
88
|
+
query.relatedPost = postId;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let availableTaxonomies = [];
|
|
92
|
+
const selectedPostTypes = [];
|
|
93
|
+
const allPostTypes = usePublicPostTypes() || [];
|
|
94
|
+
const allTaxonomies = usePublicTaxonomies() || [];
|
|
95
|
+
|
|
96
|
+
for (const type of allPostTypes) {
|
|
97
|
+
if (query.postTypes.includes(type.slug)) {
|
|
98
|
+
selectedPostTypes.push(type);
|
|
99
|
+
availableTaxonomies = [...availableTaxonomies, ...type.taxonomies];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const selectedTaxonomies = [...new Set(availableTaxonomies)];
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Set weights based on weighting slider values. Ensure that only visible options are stored.
|
|
107
|
+
*/
|
|
108
|
+
const setWeights = (singleWeightVal) => {
|
|
109
|
+
const newWeights = {};
|
|
110
|
+
for (const taxSlug of selectedTaxonomies) {
|
|
111
|
+
newWeights[taxSlug] = query.weights[taxSlug] ?? 2;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (query.postTypes?.length > 1) {
|
|
115
|
+
newWeights.postType = query.weights.postType ?? 2;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
console.log(newWeights);
|
|
119
|
+
setAttributes({
|
|
120
|
+
query: { ...query, weights: { ...newWeights, ...singleWeightVal } },
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const setPostTypes = (val) => {
|
|
125
|
+
console.log({ setPostTypes1: val });
|
|
126
|
+
val = val.length ? val : [currentPostType];
|
|
127
|
+
|
|
128
|
+
setAttributes({
|
|
129
|
+
query: { ...query, postTypes: [...val], postType: val[0] },
|
|
130
|
+
// relatedContent: { ...relatedContent, postTypes: [...val] },
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const weightsSliders = selectedTaxonomies
|
|
135
|
+
.map((taxSlug) => select(coreStore).getTaxonomy(taxSlug))
|
|
136
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
137
|
+
.filter((tax) => tax !== undefined)
|
|
138
|
+
.map((tax) => (
|
|
139
|
+
<RangeControl
|
|
140
|
+
key={tax.slug}
|
|
141
|
+
label={tax.labels.singular_name}
|
|
142
|
+
value={query.weights[tax.slug] ?? 2}
|
|
143
|
+
onChange={(n) => setWeights({ [tax.slug]: n })}
|
|
144
|
+
min={0}
|
|
145
|
+
max={7}
|
|
146
|
+
marks
|
|
147
|
+
__nextHasNoMarginBottom
|
|
148
|
+
__next40pxDefaultSize
|
|
149
|
+
/>
|
|
150
|
+
));
|
|
151
|
+
|
|
152
|
+
if (query.postTypes?.length > 1) {
|
|
153
|
+
weightsSliders.push(
|
|
154
|
+
<RangeControl
|
|
155
|
+
key="post_type"
|
|
156
|
+
label="Post Type"
|
|
157
|
+
value={query.weights.postType ?? 2}
|
|
158
|
+
onChange={(n) => setWeights({ postType: n })}
|
|
159
|
+
min={0}
|
|
160
|
+
max={7}
|
|
161
|
+
marks
|
|
162
|
+
__nextHasNoMarginBottom
|
|
163
|
+
__next40pxDefaultSize
|
|
164
|
+
/>,
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (weightsSliders.length < 1) {
|
|
169
|
+
weightsSliders.push(
|
|
170
|
+
<Tip key="no-taxonomy-warning">
|
|
171
|
+
No Taxonomies are associated with this post type.
|
|
172
|
+
</Tip>,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* perPage is stored in the native query attribute
|
|
178
|
+
* query.inherit must be set to false or perPage will inherit the global perPage value
|
|
179
|
+
*/
|
|
180
|
+
const perPageSlider = (
|
|
181
|
+
<RangeControl
|
|
182
|
+
key="perPage"
|
|
183
|
+
label="Number of Items"
|
|
184
|
+
value={query.perPage ?? 3}
|
|
185
|
+
onChange={(n) => setAttributes({ query: { ...query, perPage: n } })}
|
|
186
|
+
min={1}
|
|
187
|
+
max={12}
|
|
188
|
+
__nextHasNoMarginBottom
|
|
189
|
+
__next40pxDefaultSize
|
|
190
|
+
/>
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* perPage is stored in the native query attribute
|
|
195
|
+
* query.inherit must be set to false or perPage will inherit the global perPage value
|
|
196
|
+
*/
|
|
197
|
+
const offsetSlider = (
|
|
198
|
+
<RangeControl
|
|
199
|
+
key="offset"
|
|
200
|
+
label="Offset"
|
|
201
|
+
value={query.offset ?? 0}
|
|
202
|
+
onChange={(n) => setAttributes({ query: { ...query, offset: n } })}
|
|
203
|
+
min={0}
|
|
204
|
+
max={12}
|
|
205
|
+
__nextHasNoMarginBottom
|
|
206
|
+
__next40pxDefaultSize
|
|
207
|
+
help="Offset the returned items by this amount."
|
|
208
|
+
/>
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Set up postTypes for the selectControl input
|
|
213
|
+
*/
|
|
214
|
+
const postTypeOptions = allPostTypes.map((pt) => ({
|
|
215
|
+
value: pt.slug,
|
|
216
|
+
label: pt.name,
|
|
217
|
+
}));
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
<>
|
|
221
|
+
<BlockEdit key="edit" {...props} />
|
|
222
|
+
{props.attributes.namespace === "ideasonpurpose/query-related-posts" &&
|
|
223
|
+
props.isSelected && (
|
|
224
|
+
<>
|
|
225
|
+
<InspectorControls>
|
|
226
|
+
<PanelBody title="Display">{perPageSlider}</PanelBody>
|
|
227
|
+
<PanelBody title="Related Item Weighting">
|
|
228
|
+
<SelectControl
|
|
229
|
+
label="Post Types"
|
|
230
|
+
multiple={true}
|
|
231
|
+
value={query.postTypes}
|
|
232
|
+
options={postTypeOptions}
|
|
233
|
+
onChange={setPostTypes}
|
|
234
|
+
__nextHasNoMarginBottom
|
|
235
|
+
__next40pxDefaultSize
|
|
236
|
+
help="Show Related Content from these post types."
|
|
237
|
+
/>
|
|
238
|
+
{weightsSliders || Spinner}
|
|
239
|
+
</PanelBody>
|
|
240
|
+
</InspectorControls>
|
|
241
|
+
|
|
242
|
+
<InspectorControls group="advanced">
|
|
243
|
+
{offsetSlider}
|
|
244
|
+
</InspectorControls>
|
|
245
|
+
</>
|
|
246
|
+
)}
|
|
247
|
+
</>
|
|
248
|
+
);
|
|
249
|
+
};
|
|
250
|
+
}, "blockEditInterface");
|
package/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { usePublicPostTypes } from "./lib/use-public-post-types.js";
|
|
2
|
+
export { usePublicTaxonomies } from "./lib/use-public-taxonomies.js";
|
|
3
|
+
|
|
4
|
+
export { initLinkedGroupBlock } from "./editor/Block/Variation/Group/LinkedGroup/linked-group.js";
|
|
5
|
+
export { initRelatedPostsQueryBlock } from "./editor/Block/Variation/Query/RelatedPostsQuery/related-posts-query.js";
|
|
6
|
+
|
|
7
|
+
export { initCssLocalProps } from "./CssLocalProps/css-local-props.js";
|