| 1 |
(function() { |
| 2 |
if (typeof wp !== 'undefined' && wp.editPost && wp.components && wp.element) { |
| 3 |
var PluginDocumentSettingPanel = wp.editPost.PluginDocumentSettingPanel; |
| 4 |
var RadioControl = wp.components.RadioControl; |
| 5 |
var SelectControl = wp.components.SelectControl; |
| 6 |
var PanelRow = wp.components.PanelRow; |
| 7 |
var Notice = wp.components.Notice; |
| 8 |
var useSelect = wp.data.useSelect; |
| 9 |
var useDispatch = wp.data.useDispatch; |
| 10 |
|
| 11 |
function MxChatSettingsPanel() { |
| 12 |
var meta = useSelect(function(select) { |
| 13 |
return select('core/editor').getEditedPostAttribute('meta') || {}; |
| 14 |
}); |
| 15 |
|
| 16 |
var editPost = useDispatch('core/editor').editPost; |
| 17 |
|
| 18 |
// Determine effective visibility with backward compat |
| 19 |
var visibility = meta._mxchat_page_visibility || ''; |
| 20 |
if (!visibility && meta._mxchat_hide_chatbot === '1') { |
| 21 |
visibility = 'hide'; |
| 22 |
} |
| 23 |
|
| 24 |
var selectedBot = meta._mxchat_selected_bot || ''; |
| 25 |
|
| 26 |
var elements = []; |
| 27 |
|
| 28 |
// Visibility radio control |
| 29 |
elements.push( |
| 30 |
wp.element.createElement( |
| 31 |
PanelRow, |
| 32 |
null, |
| 33 |
wp.element.createElement(RadioControl, { |
| 34 |
label: mxchatMetaBox.strings.visibilityLabel, |
| 35 |
selected: visibility, |
| 36 |
options: [ |
| 37 |
{ label: mxchatMetaBox.strings.useGlobalSetting, value: '' }, |
| 38 |
{ label: mxchatMetaBox.strings.showChatbot, value: 'show' }, |
| 39 |
{ label: mxchatMetaBox.strings.hideChatbot, value: 'hide' } |
| 40 |
], |
| 41 |
onChange: function(value) { |
| 42 |
var newMeta = Object.assign({}, meta, { |
| 43 |
_mxchat_page_visibility: value, |
| 44 |
// Sync legacy field |
| 45 |
_mxchat_hide_chatbot: value === 'hide' ? '1' : '' |
| 46 |
}); |
| 47 |
editPost({ meta: newMeta }); |
| 48 |
} |
| 49 |
}) |
| 50 |
) |
| 51 |
); |
| 52 |
|
| 53 |
// Info notice about global setting |
| 54 |
elements.push( |
| 55 |
wp.element.createElement( |
| 56 |
Notice, |
| 57 |
{ |
| 58 |
status: 'info', |
| 59 |
isDismissible: false, |
| 60 |
className: 'mxchat-info-notice' |
| 61 |
}, |
| 62 |
mxchatMetaBox.globalAutoshow |
| 63 |
? mxchatMetaBox.strings.globalAutoshowOn |
| 64 |
: mxchatMetaBox.strings.globalAutoshowOff |
| 65 |
) |
| 66 |
); |
| 67 |
|
| 68 |
// Bot selection if multi-bot is available |
| 69 |
if (mxchatMetaBox.hasMultibot && mxchatMetaBox.availableBots) { |
| 70 |
var botOptions = [ |
| 71 |
{ label: mxchatMetaBox.strings.useDefaultBot, value: '' } |
| 72 |
]; |
| 73 |
|
| 74 |
Object.keys(mxchatMetaBox.availableBots).forEach(function(botId) { |
| 75 |
botOptions.push({ |
| 76 |
label: mxchatMetaBox.availableBots[botId], |
| 77 |
value: botId |
| 78 |
}); |
| 79 |
}); |
| 80 |
|
| 81 |
if (botOptions.length > 1) { |
| 82 |
elements.push( |
| 83 |
wp.element.createElement( |
| 84 |
PanelRow, |
| 85 |
null, |
| 86 |
wp.element.createElement(SelectControl, { |
| 87 |
label: mxchatMetaBox.strings.selectBot, |
| 88 |
value: selectedBot, |
| 89 |
options: botOptions, |
| 90 |
onChange: function(value) { |
| 91 |
editPost({ |
| 92 |
meta: Object.assign({}, meta, { |
| 93 |
_mxchat_selected_bot: value |
| 94 |
}) |
| 95 |
}); |
| 96 |
} |
| 97 |
}) |
| 98 |
) |
| 99 |
); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return wp.element.createElement( |
| 104 |
PluginDocumentSettingPanel, |
| 105 |
{ |
| 106 |
name: 'mxchat-settings', |
| 107 |
title: mxchatMetaBox.strings.panelTitle, |
| 108 |
className: 'mxchat-settings-panel' |
| 109 |
}, |
| 110 |
elements |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
wp.plugins.registerPlugin('mxchat-settings', { |
| 115 |
render: MxChatSettingsPanel |
| 116 |
}); |
| 117 |
} |
| 118 |
})(); |
| 119 |
|