| 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 { store as editorStore } from '@wordpress/editor'; |
| 15 |
import { useEffect, useMemo, useState } from '@wordpress/element'; |
| 16 |
import { store as noticesStore } from '@wordpress/notices'; |
| 17 |
|
| 18 |
export function PostInspectPanel( { |
| 19 |
areCustomFieldsEnabled, |
| 20 |
beyondwordsDisabled, |
| 21 |
beyondwordsGenerateAudio, |
| 22 |
beyondwordsContentId, |
| 23 |
beyondwordsLanguageId, |
| 24 |
beyondwordsBodyVoiceId, |
| 25 |
beyondwordsTitleVoiceId, |
| 26 |
beyondwordsSummaryVoiceId, |
| 27 |
beyondwordsProjectId, |
| 28 |
beyondwordsErrorMessage, |
| 29 |
// Deprecated |
| 30 |
beyondwordsPodcastId, |
| 31 |
publishPostToSpeechkit, |
| 32 |
speechkitAccessKey, |
| 33 |
speechkitGenerateAudio, |
| 34 |
speechkitPodcastId, |
| 35 |
speechkitProjectId, |
| 36 |
speechkitDisabled, |
| 37 |
speechkitError, |
| 38 |
speechkitErrorMessage, |
| 39 |
speechkitInfo, |
| 40 |
speechkitResponse, |
| 41 |
speechkitLink, |
| 42 |
speechkitText, |
| 43 |
speechkitRetries, |
| 44 |
speechkitStatus, |
| 45 |
wordpressPostId, |
| 46 |
currentPostType, |
| 47 |
createWarningNotice, |
| 48 |
removeWarningNotice, |
| 49 |
createErrorNotice, |
| 50 |
removeErrorNotice, |
| 51 |
didPostSaveRequestSucceed, |
| 52 |
isSavingPost, |
| 53 |
isAutosavingPost, |
| 54 |
} ) { |
| 55 |
useEffect( () => { |
| 56 |
if ( isSavingPost && ! isAutosavingPost && didPostSaveRequestSucceed ) { |
| 57 |
setRemoved( false ); |
| 58 |
removeWarningNotice(); |
| 59 |
removeErrorNotice(); |
| 60 |
} |
| 61 |
}, [ isSavingPost, isAutosavingPost, didPostSaveRequestSucceed ] ); |
| 62 |
|
| 63 |
const memoizedMeta = useMemo( |
| 64 |
() => ( { |
| 65 |
beyondwords_generate_audio: beyondwordsGenerateAudio, |
| 66 |
beyondwords_project_id: beyondwordsProjectId, |
| 67 |
beyondwords_content_id: beyondwordsContentId, |
| 68 |
beyondwords_language_id: beyondwordsLanguageId, |
| 69 |
beyondwords_body_voice_id: beyondwordsBodyVoiceId, |
| 70 |
beyondwords_title_voice_id: beyondwordsTitleVoiceId, |
| 71 |
beyondwords_summary_voice_id: beyondwordsSummaryVoiceId, |
| 72 |
beyondwords_error_message: beyondwordsErrorMessage, |
| 73 |
beyondwords_disabled: beyondwordsDisabled, |
| 74 |
// Deprecated |
| 75 |
beyondwords_podcast_id: beyondwordsPodcastId, |
| 76 |
publish_post_to_speechkit: publishPostToSpeechkit, |
| 77 |
speechkit_generate_audio: speechkitGenerateAudio, |
| 78 |
speechkit_project_id: speechkitProjectId, |
| 79 |
speechkit_podcast_id: speechkitPodcastId, |
| 80 |
speechkit_error_message: speechkitErrorMessage, |
| 81 |
speechkit_disabled: speechkitDisabled, |
| 82 |
speechkit_access_key: speechkitAccessKey, |
| 83 |
speechkit_error: speechkitError, |
| 84 |
speechkit_info: speechkitInfo, |
| 85 |
speechkit_response: speechkitResponse, |
| 86 |
speechkit_retries: speechkitRetries, |
| 87 |
speechkit_status: speechkitStatus, |
| 88 |
_speechkit_link: speechkitLink, |
| 89 |
_speechkit_text: speechkitText, |
| 90 |
} ), |
| 91 |
[] |
| 92 |
); |
| 93 |
|
| 94 |
const hasBeyondwordsData = Object.values( memoizedMeta ).some( |
| 95 |
( x ) => x !== null && x.length !== '' |
| 96 |
); |
| 97 |
|
| 98 |
const [ meta, setMeta ] = useEntityProp( |
| 99 |
'postType', |
| 100 |
currentPostType, |
| 101 |
'meta' |
| 102 |
); |
| 103 |
|
| 104 |
const [ removed, setRemoved ] = useState( false ); |
| 105 |
|
| 106 |
function ClipboardToolbarButton( { text, disabled } ) { |
| 107 |
const { createNotice } = useDispatch( noticesStore ); |
| 108 |
const ref = useCopyToClipboard( text, () => { |
| 109 |
createNotice( 'info', __( 'Copied data to clipboard.' ), { |
| 110 |
isDismissible: true, |
| 111 |
type: 'snackbar', |
| 112 |
} ); |
| 113 |
} ); |
| 114 |
|
| 115 |
return ( |
| 116 |
<Button |
| 117 |
isSecondary |
| 118 |
id="beyondwords-inspect-copy" |
| 119 |
ref={ ref } |
| 120 |
disabled={ disabled } |
| 121 |
> |
| 122 |
{ __( 'Copy', 'speechkit' ) } |
| 123 |
</Button> |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
const handleRemoveButtonClick = ( e ) => { |
| 128 |
e.stopPropagation(); |
| 129 |
|
| 130 |
if ( removed ) { |
| 131 |
restorePluginMeta() |
| 132 |
.then( () => { |
| 133 |
setRemoved( false ); |
| 134 |
removeWarningNotice(); |
| 135 |
removeErrorNotice(); |
| 136 |
} ) |
| 137 |
.catch( () => { |
| 138 |
createErrorNotice(); |
| 139 |
} ); |
| 140 |
} else { |
| 141 |
removePluginMeta() |
| 142 |
.then( () => { |
| 143 |
setRemoved( true ); |
| 144 |
createWarningNotice(); |
| 145 |
removeErrorNotice(); |
| 146 |
} ) |
| 147 |
.catch( () => createErrorNotice() ); |
| 148 |
} |
| 149 |
}; |
| 150 |
|
| 151 |
const restorePluginMeta = async () => { |
| 152 |
return setMeta( memoizedMeta ); |
| 153 |
}; |
| 154 |
|
| 155 |
const removePluginMeta = async () => { |
| 156 |
// tinyurl.com/2p9xev6n |
| 157 |
const emptiedMeta = { |
| 158 |
...meta, |
| 159 |
...Object.fromEntries( |
| 160 |
Object.entries( memoizedMeta ).map( ( [ k ] ) => [ k, '' ] ) |
| 161 |
), |
| 162 |
}; |
| 163 |
|
| 164 |
return setMeta( emptiedMeta ); |
| 165 |
}; |
| 166 |
|
| 167 |
const textToCopy = |
| 168 |
[ |
| 169 |
'```', |
| 170 |
`post_id\r\n${ wordpressPostId }`, |
| 171 |
`beyondwords_generate_audio\r\n${ beyondwordsGenerateAudio }`, |
| 172 |
`beyondwords_project_id\r\n${ beyondwordsProjectId }`, |
| 173 |
`beyondwords_content_id\r\n${ beyondwordsContentId }`, |
| 174 |
`beyondwords_language_id\r\n${ beyondwordsLanguageId }`, |
| 175 |
`beyondwords_body_voice_id\r\n${ beyondwordsBodyVoiceId }`, |
| 176 |
`beyondwords_title_voice_id\r\n${ beyondwordsTitleVoiceId }`, |
| 177 |
`beyondwords_summary_voice_id\r\n${ beyondwordsSummaryVoiceId }`, |
| 178 |
`beyondwords_error_message\r\n${ beyondwordsErrorMessage }`, |
| 179 |
`beyondwords_disabled\r\n${ beyondwordsDisabled }`, |
| 180 |
`=== ${ __( 'Deprecated', 'speechkit' ) } ===`, |
| 181 |
`beyondwords_podcast_id\r\n${ beyondwordsPodcastId }`, |
| 182 |
`publish_post_to_speechkit\r\n${ publishPostToSpeechkit }`, |
| 183 |
`speechkit_generate_audio\r\n${ speechkitGenerateAudio }`, |
| 184 |
`speechkit_project_id\r\n${ speechkitProjectId }`, |
| 185 |
`speechkit_podcast_id\r\n${ speechkitPodcastId }`, |
| 186 |
`speechkit_error_message\r\n${ speechkitErrorMessage }`, |
| 187 |
`speechkit_disabled\r\n${ speechkitDisabled }`, |
| 188 |
`speechkit_access_key\r\n${ speechkitAccessKey }`, |
| 189 |
`speechkit_error\r\n${ speechkitError }`, |
| 190 |
`speechkit_info\r\n${ speechkitInfo }`, |
| 191 |
`speechkit_response\r\n${ speechkitResponse }`, |
| 192 |
`speechkit_retries\r\n${ speechkitRetries }`, |
| 193 |
`speechkit_status\r\n${ speechkitStatus }`, |
| 194 |
`_speechkit_link\r\n${ speechkitLink }`, |
| 195 |
`_speechkit_text\r\n${ speechkitText }`, |
| 196 |
`=== ${ __( 'Copied using the Block Editor', 'speechkit' ) } ===`, |
| 197 |
'```', |
| 198 |
].join( '\r\n\r\n' ) + '\r\n\r\n'; |
| 199 |
|
| 200 |
return ( |
| 201 |
<PanelBody |
| 202 |
title={ __( 'Inspect', 'speechkit' ) } |
| 203 |
initialOpen={ false } |
| 204 |
className={ 'beyondwords beyondwords-sidebar__inspect' } |
| 205 |
> |
| 206 |
<TextControl label="post_id" readOnly value={ wordpressPostId } /> |
| 207 |
|
| 208 |
<TextControl |
| 209 |
label="beyondwords_generate_audio" |
| 210 |
readOnly |
| 211 |
value={ beyondwordsGenerateAudio } |
| 212 |
/> |
| 213 |
|
| 214 |
<TextControl |
| 215 |
label="beyondwords_project_id" |
| 216 |
readOnly |
| 217 |
value={ beyondwordsProjectId } |
| 218 |
/> |
| 219 |
|
| 220 |
<TextControl |
| 221 |
label="beyondwords_content_id" |
| 222 |
readOnly |
| 223 |
value={ beyondwordsContentId } |
| 224 |
/> |
| 225 |
|
| 226 |
<TextControl |
| 227 |
label="beyondwords_language_id" |
| 228 |
readOnly |
| 229 |
value={ beyondwordsLanguageId } |
| 230 |
/> |
| 231 |
|
| 232 |
<TextControl |
| 233 |
label="beyondwords_body_voice_id" |
| 234 |
readOnly |
| 235 |
value={ beyondwordsBodyVoiceId } |
| 236 |
/> |
| 237 |
|
| 238 |
<TextControl |
| 239 |
label="beyondwords_title_voice_id" |
| 240 |
readOnly |
| 241 |
value={ beyondwordsTitleVoiceId } |
| 242 |
/> |
| 243 |
|
| 244 |
<TextControl |
| 245 |
label="beyondwords_summary_voice_id" |
| 246 |
readOnly |
| 247 |
value={ beyondwordsSummaryVoiceId } |
| 248 |
/> |
| 249 |
|
| 250 |
{ /* eslint-disable-next-line prettier/prettier */ } |
| 251 |
<TextareaControl |
| 252 |
label="beyondwords_error_message" |
| 253 |
readOnly |
| 254 |
rows="3" |
| 255 |
value={ beyondwordsErrorMessage } |
| 256 |
/> |
| 257 |
|
| 258 |
<TextControl |
| 259 |
label="beyondwords_disabled" |
| 260 |
readOnly |
| 261 |
value={ beyondwordsDisabled } |
| 262 |
/> |
| 263 |
|
| 264 |
<hr /> |
| 265 |
|
| 266 |
<h2>{ __( 'Deprecated', 'speechkit' ) }</h2> |
| 267 |
|
| 268 |
<TextControl |
| 269 |
label="beyondwords_podcast_id" |
| 270 |
readOnly |
| 271 |
value={ beyondwordsPodcastId } |
| 272 |
/> |
| 273 |
|
| 274 |
<TextControl |
| 275 |
label="publish_post_to_speechkit" |
| 276 |
readOnly |
| 277 |
value={ publishPostToSpeechkit } |
| 278 |
/> |
| 279 |
|
| 280 |
<TextControl |
| 281 |
label="speechkit_generate_audio" |
| 282 |
readOnly |
| 283 |
value={ speechkitGenerateAudio } |
| 284 |
/> |
| 285 |
|
| 286 |
<TextControl |
| 287 |
label="speechkit_project_id" |
| 288 |
readOnly |
| 289 |
value={ speechkitProjectId } |
| 290 |
/> |
| 291 |
|
| 292 |
<TextControl |
| 293 |
label="speechkit_podcast_id" |
| 294 |
readOnly |
| 295 |
value={ speechkitPodcastId } |
| 296 |
/> |
| 297 |
|
| 298 |
{ /* eslint-disable-next-line prettier/prettier */ } |
| 299 |
<TextareaControl |
| 300 |
label="speechkit_access_key" |
| 301 |
readOnly |
| 302 |
rows="2" |
| 303 |
value={ speechkitAccessKey } |
| 304 |
/> |
| 305 |
|
| 306 |
{ /* eslint-disable-next-line prettier/prettier */ } |
| 307 |
<TextareaControl |
| 308 |
label="speechkit_error_message" |
| 309 |
readOnly |
| 310 |
rows="3" |
| 311 |
value={ speechkitErrorMessage } |
| 312 |
/> |
| 313 |
|
| 314 |
<TextControl |
| 315 |
label="speechkit_disabled" |
| 316 |
readOnly |
| 317 |
value={ speechkitDisabled } |
| 318 |
/> |
| 319 |
|
| 320 |
{ /* eslint-disable-next-line prettier/prettier */ } |
| 321 |
<TextareaControl |
| 322 |
label="speechkit_error" |
| 323 |
readOnly |
| 324 |
rows="3" |
| 325 |
value={ speechkitError } |
| 326 |
/> |
| 327 |
|
| 328 |
<TextareaControl |
| 329 |
label="speechkit_info" |
| 330 |
readOnly |
| 331 |
rows="3" |
| 332 |
value={ speechkitInfo } |
| 333 |
/> |
| 334 |
|
| 335 |
{ /* eslint-disable-next-line prettier/prettier */ } |
| 336 |
<TextareaControl |
| 337 |
label="speechkit_response" |
| 338 |
readOnly |
| 339 |
rows="3" |
| 340 |
value={ speechkitResponse } |
| 341 |
/> |
| 342 |
|
| 343 |
<TextControl |
| 344 |
label="speechkit_retries" |
| 345 |
readOnly |
| 346 |
value={ speechkitRetries } |
| 347 |
/> |
| 348 |
|
| 349 |
<TextControl |
| 350 |
label="speechkit_status" |
| 351 |
readOnly |
| 352 |
value={ speechkitStatus } |
| 353 |
/> |
| 354 |
|
| 355 |
<TextControl |
| 356 |
label="_speechkit_link" |
| 357 |
readOnly |
| 358 |
value={ speechkitLink } |
| 359 |
/> |
| 360 |
|
| 361 |
<TextareaControl |
| 362 |
label="_speechkit_text" |
| 363 |
readOnly |
| 364 |
rows="3" |
| 365 |
value={ speechkitText } |
| 366 |
/> |
| 367 |
|
| 368 |
<hr /> |
| 369 |
|
| 370 |
<ClipboardToolbarButton |
| 371 |
text={ textToCopy } |
| 372 |
disabled={ removed } |
| 373 |
/> |
| 374 |
|
| 375 |
<Button |
| 376 |
isDestructive |
| 377 |
style={ { float: 'right' } } |
| 378 |
id="beyondwords-inspect-remove" |
| 379 |
onClick={ handleRemoveButtonClick } |
| 380 |
disabled={ areCustomFieldsEnabled || ! hasBeyondwordsData } |
| 381 |
> |
| 382 |
{ removed |
| 383 |
? __( 'Restore', 'speechkit' ) |
| 384 |
: __( 'Remove', 'speechkit' ) } |
| 385 |
</Button> |
| 386 |
|
| 387 |
{ areCustomFieldsEnabled && ( |
| 388 |
<div |
| 389 |
className="notice notice-info" |
| 390 |
style={ { margin: '15px 0 0 0', padding: '8px 12px 8px' } } |
| 391 |
> |
| 392 |
<p> |
| 393 |
{ __( |
| 394 |
'“Remove” is not compatible with the WordPress Custom Fields panel.', |
| 395 |
'' |
| 396 |
) } |
| 397 |
</p> |
| 398 |
<p> |
| 399 |
{ __( |
| 400 |
// eslint-disable-next-line max-len |
| 401 |
'To remove BeyondWords data for individual posts please disable the Custom Fields panel in the editor preferences.', |
| 402 |
'speechkit' |
| 403 |
) } |
| 404 |
</p> |
| 405 |
</div> |
| 406 |
) } |
| 407 |
</PanelBody> |
| 408 |
); |
| 409 |
} |
| 410 |
|
| 411 |
export default compose( [ |
| 412 |
withSelect( ( select ) => { |
| 413 |
const { getEditorSettings } = select( editorStore ); |
| 414 |
const { |
| 415 |
didPostSaveRequestSucceed, |
| 416 |
getCurrentPostId, |
| 417 |
getCurrentPostType, |
| 418 |
getEditedPostAttribute, |
| 419 |
isSavingPost, |
| 420 |
isAutosavingPost, |
| 421 |
} = select( 'core/editor' ); |
| 422 |
|
| 423 |
return { |
| 424 |
areCustomFieldsEnabled: |
| 425 |
getEditorSettings().enableCustomFields === true, |
| 426 |
beyondwordsDisabled: |
| 427 |
getEditedPostAttribute( 'meta' ).beyondwords_disabled, |
| 428 |
beyondwordsGenerateAudio: |
| 429 |
getEditedPostAttribute( 'meta' ).beyondwords_generate_audio, |
| 430 |
beyondwordsContentId: |
| 431 |
getEditedPostAttribute( 'meta' ).beyondwords_content_id, |
| 432 |
beyondwordsLanguageId: |
| 433 |
getEditedPostAttribute( 'meta' ).beyondwords_language_id, |
| 434 |
beyondwordsBodyVoiceId: |
| 435 |
getEditedPostAttribute( 'meta' ).beyondwords_body_voice_id, |
| 436 |
beyondwordsTitleVoiceId: |
| 437 |
getEditedPostAttribute( 'meta' ).beyondwords_title_voice_id, |
| 438 |
beyondwordsSummaryVoiceId: |
| 439 |
getEditedPostAttribute( 'meta' ).beyondwords_summary_voice_id, |
| 440 |
beyondwordsProjectId: |
| 441 |
getEditedPostAttribute( 'meta' ).beyondwords_project_id, |
| 442 |
beyondwordsErrorMessage: |
| 443 |
getEditedPostAttribute( 'meta' ).beyondwords_error_message, |
| 444 |
// Deprecated |
| 445 |
beyondwordsPodcastId: |
| 446 |
getEditedPostAttribute( 'meta' ).beyondwords_podcast_id, |
| 447 |
publishPostToSpeechkit: |
| 448 |
getEditedPostAttribute( 'meta' ).publish_post_to_speechkit, |
| 449 |
speechkitAccessKey: |
| 450 |
getEditedPostAttribute( 'meta' ).speechkit_access_key, |
| 451 |
speechkitGenerateAudio: |
| 452 |
getEditedPostAttribute( 'meta' ).speechkit_generate_audio, |
| 453 |
speechkitPodcastId: |
| 454 |
getEditedPostAttribute( 'meta' ).speechkit_podcast_id, |
| 455 |
speechkitProjectId: |
| 456 |
getEditedPostAttribute( 'meta' ).speechkit_project_id, |
| 457 |
speechkitDisabled: |
| 458 |
getEditedPostAttribute( 'meta' ).speechkit_disabled, |
| 459 |
speechkitError: getEditedPostAttribute( 'meta' ).speechkit_error, |
| 460 |
speechkitErrorMessage: |
| 461 |
getEditedPostAttribute( 'meta' ).speechkit_error_message, |
| 462 |
speechkitInfo: getEditedPostAttribute( 'meta' ).speechkit_info, |
| 463 |
speechkitResponse: |
| 464 |
getEditedPostAttribute( 'meta' ).speechkit_response, |
| 465 |
speechkitLink: getEditedPostAttribute( 'meta' )._speechkit_link, |
| 466 |
speechkitText: getEditedPostAttribute( 'meta' )._speechkit_text, |
| 467 |
speechkitRetries: |
| 468 |
getEditedPostAttribute( 'meta' ).speechkit_retries, |
| 469 |
speechkitStatus: getEditedPostAttribute( 'meta' ).speechkit_status, |
| 470 |
wordpressPostId: getCurrentPostId(), |
| 471 |
currentPostType: getCurrentPostType(), |
| 472 |
didPostSaveRequestSucceed: didPostSaveRequestSucceed(), |
| 473 |
isSavingPost: isSavingPost(), |
| 474 |
isAutosavingPost: isAutosavingPost(), |
| 475 |
}; |
| 476 |
} ), |
| 477 |
withDispatch( ( dispatch ) => { |
| 478 |
const { createNotice, removeNotice } = dispatch( 'core/notices' ); |
| 479 |
|
| 480 |
return { |
| 481 |
createWarningNotice: () => |
| 482 |
createNotice( |
| 483 |
'warning', |
| 484 |
__( |
| 485 |
// eslint-disable-next-line max-len |
| 486 |
'The BeyondWords data for this post will be removed when the post is saved.', |
| 487 |
'speechkit' |
| 488 |
), |
| 489 |
{ |
| 490 |
id: 'beyondwords-remove-post-data--warning', |
| 491 |
isDismissible: false, |
| 492 |
speak: true, |
| 493 |
} |
| 494 |
), |
| 495 |
removeWarningNotice: () => |
| 496 |
removeNotice( 'beyondwords-remove-post-data--warning' ), |
| 497 |
createErrorNotice: () => |
| 498 |
createNotice( |
| 499 |
'error', |
| 500 |
__( |
| 501 |
'Unable to remove the BeyondWords data. Please email our support team.', |
| 502 |
'speechkit' |
| 503 |
), |
| 504 |
{ |
| 505 |
id: 'beyondwords-remove-post-data--error', |
| 506 |
isDismissible: true, |
| 507 |
speak: true, |
| 508 |
} |
| 509 |
), |
| 510 |
removeErrorNotice: () => |
| 511 |
removeNotice( 'beyondwords-remove-post-data--error' ), |
| 512 |
}; |
| 513 |
} ), |
| 514 |
] )( PostInspectPanel ); |
| 515 |
|