-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutenberg-translate-button.js
47 lines (44 loc) · 1.61 KB
/
gutenberg-translate-button.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
const { registerPlugin } = wp.plugins;
const { PluginMoreMenuItem } = wp.editPost;
const { withDispatch } = wp.data;
const { compose } = wp.compose;
const { __ } = wp.i18n;
const { createElement } = wp.element;
const TranslateButton = compose(
withDispatch( ( dispatch, ownProps ) => {
return {
onClick: function() {
var postId = wp.data.select('core/editor').getCurrentPostId();
var nonce = wpApiSettings.nonce;
wp.ajax.post('translate_content', {
nonce: wpApiSettings.nonce,
post_id: postId,
})
.done(function(response) {
if (response.success) {
alert('Перевод успешно выполнен.');
location.reload();
} else {
alert('Ошибка при переводе: ' + response.data.message);
}
})
.fail(function (jqXHR, textStatus, errorThrown) {
alert('Ошибка при переводе: ' + textStatus + ' ' + errorThrown);
});
},
};
} )
)( ( { onClick } ) => (
createElement(
PluginMoreMenuItem,
{
icon: "translation",
onClick: onClick
},
__( 'Перевести', 'auto-translate-content' )
)
) );
registerPlugin( 'my-translate-plugin', {
render: TranslateButton,
icon: 'translation'
} );