forked from pbc-web/wp-category-permalink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-category-permalink-gutenberg.js
115 lines (102 loc) · 3.66 KB
/
wp-category-permalink-gutenberg.js
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
/**
* This peace of JS-Code will add a DropDown selection menu in the gutenberg editor. The new element
* will allow to modify the canonical permalink option of this plugin.
*/
var __ = wp.i18n.__;
var PluginSidebar = wp.editPost.PluginSidebar,
PluginSidebarMoreMenuItem = wp.editPost.PluginSidebarMoreMenuItem,
PluginPostStatusInfo = wp.editPost.PluginPostStatusInfo;
var PanelBody = wp.components.PanelBody,
TextControl = wp.components.TextControl,
CheckboxControl = wp.components.CheckboxControl,
SelectControl = wp.components.SelectControl;
var Component = wp.element.Component,
Fragment = wp.element.Fragment;
var withSelect = wp.data.withSelect,
withDispatch = wp.data.withDispatch;
var withState = wp.compose.withState,
compose = wp.compose.compose;
var registerPlugin = wp.plugins.registerPlugin;
/**
* Definition of the new control element.
*
* @param {*} props
*/
var CanonicalCategory = function CanonicalCategory(props) {
return wp.element.createElement(
Fragment,
null,
wp.element.createElement("p", null, "Canonical category"),
wp.element.createElement(
SelectControl,
{
value: props.canonicalCatId,
options: props.categoryarray,
onChange: function onChange(value) {
props.onCanonicalCatIdChanged(value);
}
}
)
);
};
/**
* Data (property) assembly for the CanonicalCategory element. This chain will take care of
* all the data synchonization and automatic update of the element.
*/
CanonicalCategory = compose(
[
withSelect(function (select) {
return {
canonicalCatId: select('core/editor').getEditedPostAttribute('meta')['_category_permalink']
};
}),
withSelect(function (select) {
return {
postCategories: select('core/editor').getEditedPostAttribute('categories')
};
}),
withSelect(function (select, props) {
var categoryarray = [];
if (props.postCategories) {
for (var i = 0; i < props.postCategories.length; i++) {
var categorieId = props.postCategories[i];
var category = select('core').getEntityRecord('taxonomy', 'category', categorieId);
if (category) {
categoryarray.push({
label: category.name,
value: category.id
});
} else {
categoryarray.push({
label: categorieId + ' - Name is loading',
value: categorieId
});
}
}
return {
categoryarray: categoryarray
};
}
}),
withDispatch(function (dispatch) {
return {
onCanonicalCatIdChanged: function onCanonicalCatIdChanged(canonicalCatId) {
dispatch('core/editor').editPost({
meta: {
_category_permalink: canonicalCatId
}
});
}
};
})
])(CanonicalCategory);
// Registrate the new element to the PluginPostStatusInfo sidebar element
registerPlugin('babymarkt-canonical-category', {
render: function render() {
return wp.element.createElement(
PluginPostStatusInfo,
null,
wp.element.createElement(CanonicalCategory, null)
);
}
});