| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { |
| 6 |
Button, |
| 7 |
PanelBody, |
| 8 |
TextareaControl, |
| 9 |
TextControl, |
| 10 |
} from '@wordpress/components'; |
| 11 |
import { compose, useCopyToClipboard } from '@wordpress/compose'; |
| 12 |
import { useEntityProp } from '@wordpress/core-data'; |
| 13 |
import { useDispatch, withDispatch, withSelect } from '@wordpress/data'; |
| 14 |
import { useEffect, useMemo, useState } from '@wordpress/element'; |
| 15 |
import { store as noticesStore } from '@wordpress/notices'; |
| 16 |
|
| 17 |
export function PostInspectPanel( { |
| 18 |
// Current custom fields |
| 19 |
beyondwordsDeleteContent, |
| 20 |
beyondwordsDisabled, |
| 21 |
beyondwordsGenerateAudio, |
| 22 |
beyondwordsContentId, |
| 23 |
beyondwordsPreviewToken, |
| 24 |
beyondwordsPlayerStyle, |
| 25 |
beyondwordsLanguageId, |
| 26 |
beyondwordsBodyVoiceId, |
| 27 |
beyondwordsTitleVoiceId, |
| 28 |
beyondwordsSummaryVoiceId, |
| 29 |
beyondwordsProjectId, |
| 30 |
beyondwordsErrorMessage, |
| 31 |
// Deprecated custom fields |
| 32 |
beyondwordsPodcastId, |
| 33 |
publishPostToSpeechkit, |
| 34 |
speechkitAccessKey, |
| 35 |
speechkitGenerateAudio, |
| 36 |
speechkitPodcastId, |
| 37 |
speechkitProjectId, |
| 38 |
speechkitDisabled, |
| 39 |
speechkitError, |
| 40 |
speechkitErrorMessage, |
| 41 |
speechkitInfo, |
| 42 |
speechkitResponse, |
| 43 |
speechkitLink, |
| 44 |
speechkitText, |
| 45 |
speechkitRetries, |
| 46 |
speechkitStatus, |
| 47 |
// System |
| 48 |
pluginVersion, |
| 49 |
wpVersion, |
| 50 |
wpPostId, |
| 51 |
// Other |
| 52 |
currentPostType, |
| 53 |
createWarningNotice, |
| 54 |
removeWarningNotice, |
| 55 |
setDeleteContent, |
| 56 |
didPostSaveRequestSucceed, |
| 57 |
isSavingPost, |
| 58 |
isAutosavingPost, |
| 59 |
} ) { |
| 60 |
const [ removed, setRemoved ] = useState( false ); |
| 61 |
|
| 62 |
useEffect( () => { |
| 63 |
if ( isSavingPost && ! isAutosavingPost && didPostSaveRequestSucceed ) { |
| 64 |
removeWarningNotice(); |
| 65 |
} |
| 66 |
}, [ didPostSaveRequestSucceed, isAutosavingPost, isSavingPost ] ); |
| 67 |
|
| 68 |
useEffect( () => { |
| 69 |
if ( isSavingPost && ! isAutosavingPost && didPostSaveRequestSucceed && removed ) { |
| 70 |
setRemoved( false ); |
| 71 |
} |
| 72 |
}, [ didPostSaveRequestSucceed, isAutosavingPost, isSavingPost, removed ] ); |
| 73 |
|
| 74 |
const memoizedMeta = useMemo( |
| 75 |
() => ( { |
| 76 |
plugin_version: pluginVersion, |
| 77 |
wp_version: wpVersion, |
| 78 |
beyondwords_generate_audio: beyondwordsGenerateAudio, |
| 79 |
beyondwords_project_id: beyondwordsProjectId, |
| 80 |
beyondwords_content_id: beyondwordsContentId, |
| 81 |
beyondwords_preview_token: beyondwordsPreviewToken, |
| 82 |
beyondwords_player_style: beyondwordsPlayerStyle, |
| 83 |
beyondwords_language_id: beyondwordsLanguageId, |
| 84 |
beyondwords_body_voice_id: beyondwordsBodyVoiceId, |
| 85 |
beyondwords_title_voice_id: beyondwordsTitleVoiceId, |
| 86 |
beyondwords_summary_voice_id: beyondwordsSummaryVoiceId, |
| 87 |
beyondwords_error_message: beyondwordsErrorMessage, |
| 88 |
beyondwords_disabled: beyondwordsDisabled, |
| 89 |
beyondwords_delete_content: beyondwordsDeleteContent, |
| 90 |
// Deprecated |
| 91 |
beyondwords_podcast_id: beyondwordsPodcastId, |
| 92 |
publish_post_to_speechkit: publishPostToSpeechkit, |
| 93 |
speechkit_generate_audio: speechkitGenerateAudio, |
| 94 |
speechkit_project_id: speechkitProjectId, |
| 95 |
speechkit_podcast_id: speechkitPodcastId, |
| 96 |
speechkit_error_message: speechkitErrorMessage, |
| 97 |
speechkit_disabled: speechkitDisabled, |
| 98 |
speechkit_access_key: speechkitAccessKey, |
| 99 |
speechkit_error: speechkitError, |
| 100 |
speechkit_info: speechkitInfo, |
| 101 |
speechkit_response: speechkitResponse, |
| 102 |
speechkit_retries: speechkitRetries, |
| 103 |
speechkit_status: speechkitStatus, |
| 104 |
_speechkit_link: speechkitLink, |
| 105 |
_speechkit_text: speechkitText, |
| 106 |
} ), |
| 107 |
[] |
| 108 |
); |
| 109 |
|
| 110 |
const hasBeyondwordsData = Object.values( memoizedMeta ).some( |
| 111 |
( x ) => !! x?.length |
| 112 |
); |
| 113 |
|
| 114 |
function ClipboardToolbarButton( { text, disabled } ) { |
| 115 |
const { createNotice } = useDispatch( noticesStore ); |
| 116 |
const ref = useCopyToClipboard( text, () => { |
| 117 |
createNotice( 'info', __( 'Copied data to clipboard.' ), { |
| 118 |
isDismissible: true, |
| 119 |
type: 'snackbar', |
| 120 |
} ); |
| 121 |
} ); |
| 122 |
|
| 123 |
return ( |
| 124 |
<Button |
| 125 |
isSecondary |
| 126 |
id="beyondwords-inspect-copy" |
| 127 |
ref={ ref } |
| 128 |
disabled={ disabled } |
| 129 |
> |
| 130 |
{ __( 'Copy', 'speechkit' ) } |
| 131 |
</Button> |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
const handleRemoveButtonClick = ( e ) => { |
| 136 |
e.stopPropagation(); |
| 137 |
|
| 138 |
if ( removed ) { |
| 139 |
setRemoved( false ); |
| 140 |
setDeleteContent( false ); |
| 141 |
removeWarningNotice(); |
| 142 |
} else { |
| 143 |
setRemoved( true ); |
| 144 |
setDeleteContent( true ); |
| 145 |
createWarningNotice(); |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
const textToCopy = |
| 150 |
[ |
| 151 |
'```', |
| 152 |
`beyondwords_generate_audio\r\n${ beyondwordsGenerateAudio }`, |
| 153 |
`beyondwords_project_id\r\n${ beyondwordsProjectId }`, |
| 154 |
`beyondwords_content_id\r\n${ beyondwordsContentId }`, |
| 155 |
`beyondwords_preview_token\r\n${ beyondwordsPreviewToken }`, |
| 156 |
`beyondwords_player_style\r\n${ beyondwordsPlayerStyle }`, |
| 157 |
`beyondwords_language_id\r\n${ beyondwordsLanguageId }`, |
| 158 |
`beyondwords_body_voice_id\r\n${ beyondwordsBodyVoiceId }`, |
| 159 |
`beyondwords_title_voice_id\r\n${ beyondwordsTitleVoiceId }`, |
| 160 |
`beyondwords_summary_voice_id\r\n${ beyondwordsSummaryVoiceId }`, |
| 161 |
`beyondwords_error_message\r\n${ beyondwordsErrorMessage }`, |
| 162 |
`beyondwords_disabled\r\n${ beyondwordsDisabled }`, |
| 163 |
`beyondwords_delete_content\r\n${ beyondwordsDeleteContent }`, |
| 164 |
`=== ${ __( 'Deprecated', 'speechkit' ) } ===`, |
| 165 |
`beyondwords_podcast_id\r\n${ beyondwordsPodcastId }`, |
| 166 |
`publish_post_to_speechkit\r\n${ publishPostToSpeechkit }`, |
| 167 |
`speechkit_generate_audio\r\n${ speechkitGenerateAudio }`, |
| 168 |
`speechkit_project_id\r\n${ speechkitProjectId }`, |
| 169 |
`speechkit_podcast_id\r\n${ speechkitPodcastId }`, |
| 170 |
`speechkit_error_message\r\n${ speechkitErrorMessage }`, |
| 171 |
`speechkit_disabled\r\n${ speechkitDisabled }`, |
| 172 |
`speechkit_access_key\r\n${ speechkitAccessKey }`, |
| 173 |
`speechkit_error\r\n${ speechkitError }`, |
| 174 |
`speechkit_info\r\n${ speechkitInfo }`, |
| 175 |
`speechkit_response\r\n${ speechkitResponse }`, |
| 176 |
`speechkit_retries\r\n${ speechkitRetries }`, |
| 177 |
`speechkit_status\r\n${ speechkitStatus }`, |
| 178 |
`_speechkit_link\r\n${ speechkitLink }`, |
| 179 |
`_speechkit_text\r\n${ speechkitText }`, |
| 180 |
`=== ${ __( 'System', 'speechkit' ) } ===`, |
| 181 |
`plugin_version\r\n${ pluginVersion }`, |
| 182 |
`wp_version\r\n${ wpVersion }`, |
| 183 |
`wp_post_id\r\n${ wpPostId }`, |
| 184 |
`=== ${ __( 'Copied using the Block Editor', 'speechkit' ) } ===`, |
| 185 |
'```', |
| 186 |
].join( '\r\n\r\n' ) + '\r\n\r\n'; |
| 187 |
|
| 188 |
return ( |
| 189 |
<PanelBody |
| 190 |
title={ __( 'Inspect', 'speechkit' ) } |
| 191 |
initialOpen={ false } |
| 192 |
className={ 'beyondwords beyondwords-sidebar__inspect' } |
| 193 |
> |
| 194 |
<TextControl |
| 195 |
label="beyondwords_generate_audio" |
| 196 |
readOnly |
| 197 |
value={ beyondwordsGenerateAudio } |
| 198 |
/> |
| 199 |
|
| 200 |
<TextControl |
| 201 |
label="beyondwords_project_id" |
| 202 |
readOnly |
| 203 |
value={ beyondwordsProjectId } |
| 204 |
/> |
| 205 |
|
| 206 |
<TextControl |
| 207 |
label="beyondwords_preview_token" |
| 208 |
readOnly |
| 209 |
value={ beyondwordsPreviewToken } |
| 210 |
/> |
| 211 |
|
| 212 |
<TextControl |
| 213 |
label="beyondwords_content_id" |
| 214 |
readOnly |
| 215 |
value={ beyondwordsContentId } |
| 216 |
/> |
| 217 |
|
| 218 |
<TextControl |
| 219 |
label="beyondwords_player_style" |
| 220 |
readOnly |
| 221 |
value={ beyondwordsPlayerStyle } |
| 222 |
/> |
| 223 |
|
| 224 |
<TextControl |
| 225 |
label="beyondwords_language_id" |
| 226 |
readOnly |
| 227 |
value={ beyondwordsLanguageId } |
| 228 |
/> |
| 229 |
|
| 230 |
<TextControl |
| 231 |
label="beyondwords_body_voice_id" |
| 232 |
readOnly |
| 233 |
value={ beyondwordsBodyVoiceId } |
| 234 |
/> |
| 235 |
|
| 236 |
<TextControl |
| 237 |
label="beyondwords_title_voice_id" |
| 238 |
readOnly |
| 239 |
value={ beyondwordsTitleVoiceId } |
| 240 |
/> |
| 241 |
|
| 242 |
<TextControl |
| 243 |
label="beyondwords_summary_voice_id" |
| 244 |
readOnly |
| 245 |
value={ beyondwordsSummaryVoiceId } |
| 246 |
/> |
| 247 |
|
| 248 |
{ /* eslint-disable-next-line prettier/prettier */ } |
| 249 |
<TextareaControl |
| 250 |
label="beyondwords_error_message" |
| 251 |
readOnly |
| 252 |
rows="3" |
| 253 |
value={ beyondwordsErrorMessage } |
| 254 |
/> |
| 255 |
|
| 256 |
<TextControl |
| 257 |
label="beyondwords_disabled" |
| 258 |
readOnly |
| 259 |
value={ beyondwordsDisabled } |
| 260 |
/> |
| 261 |
|
| 262 |
<TextControl |
| 263 |
label="beyondwords_delete_content" |
| 264 |
readOnly |
| 265 |
value={ beyondwordsDeleteContent } |
| 266 |
/> |
| 267 |
|
| 268 |
<hr /> |
| 269 |
|
| 270 |
<ClipboardToolbarButton |
| 271 |
text={ textToCopy } |
| 272 |
disabled={ removed } |
| 273 |
/> |
| 274 |
|
| 275 |
<Button |
| 276 |
isDestructive |
| 277 |
style={ { float: 'right' } } |
| 278 |
id="beyondwords-inspect-remove" |
| 279 |
onClick={ handleRemoveButtonClick } |
| 280 |
disabled={ ! hasBeyondwordsData } |
| 281 |
> |
| 282 |
{ removed |
| 283 |
? __( 'Restore', 'speechkit' ) |
| 284 |
: __( 'Remove', 'speechkit' ) } |
| 285 |
</Button> |
| 286 |
</PanelBody> |
| 287 |
); |
| 288 |
} |
| 289 |
|
| 290 |
export default compose( [ |
| 291 |
withSelect( ( select ) => { |
| 292 |
const { |
| 293 |
didPostSaveRequestSucceed, |
| 294 |
getCurrentPostId, |
| 295 |
getCurrentPostType, |
| 296 |
getEditedPostAttribute, |
| 297 |
isSavingPost, |
| 298 |
isAutosavingPost, |
| 299 |
} = select( 'core/editor' ); |
| 300 |
|
| 301 |
const { getSettings } = select( 'beyondwords/settings' ); |
| 302 |
|
| 303 |
const { pluginVersion, wpVersion } = getSettings(); |
| 304 |
|
| 305 |
return { |
| 306 |
// Current custom fields |
| 307 |
beyondwordsDeleteContent: |
| 308 |
getEditedPostAttribute( 'meta' ).beyondwords_delete_content, |
| 309 |
beyondwordsDisabled: |
| 310 |
getEditedPostAttribute( 'meta' ).beyondwords_disabled, |
| 311 |
beyondwordsGenerateAudio: |
| 312 |
getEditedPostAttribute( 'meta' ).beyondwords_generate_audio, |
| 313 |
beyondwordsContentId: |
| 314 |
getEditedPostAttribute( 'meta' ).beyondwords_content_id, |
| 315 |
beyondwordsPreviewToken: |
| 316 |
getEditedPostAttribute( 'meta' ).beyondwords_preview_token, |
| 317 |
beyondwordsPlayerStyle: |
| 318 |
getEditedPostAttribute( 'meta' ).beyondwords_player_style, |
| 319 |
beyondwordsLanguageId: |
| 320 |
getEditedPostAttribute( 'meta' ).beyondwords_language_id, |
| 321 |
beyondwordsBodyVoiceId: |
| 322 |
getEditedPostAttribute( 'meta' ).beyondwords_body_voice_id, |
| 323 |
beyondwordsTitleVoiceId: |
| 324 |
getEditedPostAttribute( 'meta' ).beyondwords_title_voice_id, |
| 325 |
beyondwordsSummaryVoiceId: |
| 326 |
getEditedPostAttribute( 'meta' ).beyondwords_summary_voice_id, |
| 327 |
beyondwordsProjectId: |
| 328 |
getEditedPostAttribute( 'meta' ).beyondwords_project_id, |
| 329 |
beyondwordsErrorMessage: |
| 330 |
getEditedPostAttribute( 'meta' ).beyondwords_error_message, |
| 331 |
// Deprecated custom fields |
| 332 |
beyondwordsPodcastId: |
| 333 |
getEditedPostAttribute( 'meta' ).beyondwords_podcast_id, |
| 334 |
publishPostToSpeechkit: |
| 335 |
getEditedPostAttribute( 'meta' ).publish_post_to_speechkit, |
| 336 |
speechkitAccessKey: |
| 337 |
getEditedPostAttribute( 'meta' ).speechkit_access_key, |
| 338 |
speechkitGenerateAudio: |
| 339 |
getEditedPostAttribute( 'meta' ).speechkit_generate_audio, |
| 340 |
speechkitPodcastId: |
| 341 |
getEditedPostAttribute( 'meta' ).speechkit_podcast_id, |
| 342 |
speechkitProjectId: |
| 343 |
getEditedPostAttribute( 'meta' ).speechkit_project_id, |
| 344 |
speechkitDisabled: |
| 345 |
getEditedPostAttribute( 'meta' ).speechkit_disabled, |
| 346 |
speechkitError: getEditedPostAttribute( 'meta' ).speechkit_error, |
| 347 |
speechkitErrorMessage: |
| 348 |
getEditedPostAttribute( 'meta' ).speechkit_error_message, |
| 349 |
speechkitInfo: getEditedPostAttribute( 'meta' ).speechkit_info, |
| 350 |
speechkitResponse: |
| 351 |
getEditedPostAttribute( 'meta' ).speechkit_response, |
| 352 |
speechkitLink: getEditedPostAttribute( 'meta' )._speechkit_link, |
| 353 |
speechkitText: getEditedPostAttribute( 'meta' )._speechkit_text, |
| 354 |
speechkitRetries: |
| 355 |
getEditedPostAttribute( 'meta' ).speechkit_retries, |
| 356 |
speechkitStatus: getEditedPostAttribute( 'meta' ).speechkit_status, |
| 357 |
// System |
| 358 |
pluginVersion, |
| 359 |
wpVersion, |
| 360 |
wpPostId: getCurrentPostId(), |
| 361 |
// Other |
| 362 |
currentPostType: getCurrentPostType(), |
| 363 |
didPostSaveRequestSucceed: didPostSaveRequestSucceed(), |
| 364 |
isSavingPost: isSavingPost(), |
| 365 |
isAutosavingPost: isAutosavingPost(), |
| 366 |
}; |
| 367 |
} ), |
| 368 |
withDispatch( ( dispatch ) => { |
| 369 |
const { editPost } = dispatch( 'core/editor' ); |
| 370 |
const { createNotice, removeNotice } = dispatch( 'core/notices' ); |
| 371 |
|
| 372 |
return { |
| 373 |
createWarningNotice: () => |
| 374 |
createNotice( |
| 375 |
'warning', |
| 376 |
__( |
| 377 |
// eslint-disable-next-line max-len |
| 378 |
'The BeyondWords data for this post will be removed when the post is saved.', |
| 379 |
'speechkit' |
| 380 |
), |
| 381 |
{ |
| 382 |
id: 'beyondwords-remove-post-data--warning', |
| 383 |
isDismissible: false, |
| 384 |
speak: true, |
| 385 |
} |
| 386 |
), |
| 387 |
removeWarningNotice: () => |
| 388 |
removeNotice( 'beyondwords-remove-post-data--warning' ), |
| 389 |
setDeleteContent: ( deleteContent ) => { |
| 390 |
// Update the Post Meta (AKA the Custom Field) |
| 391 |
editPost( { |
| 392 |
meta: { |
| 393 |
/* eslint-disable-next-line camelcase */ |
| 394 |
beyondwords_delete_content: deleteContent ? '1' : '', |
| 395 |
}, |
| 396 |
} ); |
| 397 |
}, |
| 398 |
}; |
| 399 |
} ), |
| 400 |
] )( PostInspectPanel ); |
| 401 |
|