| 1 |
/** |
| 2 |
* Read-only Form Shortcode panel (`wppb_fb_form_shortcode`). Register first in index.js. |
| 3 |
*/ |
| 4 |
|
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import { registerPlugin } from '@wordpress/plugins'; |
| 7 |
import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; |
| 8 |
import { Button, Notice } from '@wordpress/components'; |
| 9 |
import { useEntityProp } from '@wordpress/core-data'; |
| 10 |
import { useSelect } from '@wordpress/data'; |
| 11 |
import { useCopyToClipboard } from '@wordpress/compose'; |
| 12 |
import { useState, useEffect } from '@wordpress/element'; |
| 13 |
|
| 14 |
// Inline icons (project's inline-SVG convention — no @wordpress/icons dependency). |
| 15 |
// NOTE: fill is set via inline `style`, NOT the `fill="none"` attribute. |
| 16 |
// Gutenberg's `.components-button svg { fill: currentColor }` beats a presentation |
| 17 |
// attribute and would fill the closed rectangles solid (hiding the copy glyph's |
| 18 |
// interior lines); an inline style wins over that rule. |
| 19 |
const CopyIcon = () => ( |
| 20 |
<svg width="16" height="16" viewBox="0 0 24 24" style={ { fill: 'none' } } aria-hidden="true" focusable="false"> |
| 21 |
<rect x="8" y="8" width="12" height="12" rx="2" stroke="currentColor" strokeWidth="1.6" /> |
| 22 |
<path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" stroke="currentColor" strokeWidth="1.6" /> |
| 23 |
</svg> |
| 24 |
); |
| 25 |
|
| 26 |
const CheckIcon = () => ( |
| 27 |
<svg width="16" height="16" viewBox="0 0 24 24" style={ { fill: 'none' } } aria-hidden="true" focusable="false"> |
| 28 |
<path d="M5 12l5 5 9-10" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" /> |
| 29 |
</svg> |
| 30 |
); |
| 31 |
|
| 32 |
const CopyShortcodeButton = ( { text } ) => { |
| 33 |
const [ copied, setCopied ] = useState( false ); |
| 34 |
const ref = useCopyToClipboard( text, () => setCopied( true ) ); |
| 35 |
|
| 36 |
useEffect( () => { |
| 37 |
if ( ! copied ) return undefined; |
| 38 |
const timer = setTimeout( () => setCopied( false ), 2000 ); |
| 39 |
return () => clearTimeout( timer ); |
| 40 |
}, [ copied ] ); |
| 41 |
|
| 42 |
return ( |
| 43 |
<Button |
| 44 |
ref={ ref } |
| 45 |
className="wppb-fb-shortcode-copy" |
| 46 |
showTooltip |
| 47 |
label={ copied ? __( 'Copied!', 'profile-builder' ) : __( 'Copy shortcode', 'profile-builder' ) } |
| 48 |
> |
| 49 |
{ copied ? <CheckIcon /> : <CopyIcon /> } |
| 50 |
</Button> |
| 51 |
); |
| 52 |
}; |
| 53 |
|
| 54 |
const FormShortcodePanel = () => { |
| 55 |
const postType = useSelect( |
| 56 |
( select ) => select( 'core/editor' ).getCurrentPostType(), |
| 57 |
[] |
| 58 |
); |
| 59 |
const [ meta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 60 |
|
| 61 |
if ( ! [ 'wppb-rf-cpt', 'wppb-epf-cpt' ].includes( postType ) ) return null; |
| 62 |
|
| 63 |
const shortcode = ( meta && meta.wppb_fb_form_shortcode ) || ''; |
| 64 |
|
| 65 |
return ( |
| 66 |
<PluginDocumentSettingPanel |
| 67 |
name="wppb-form-shortcode" |
| 68 |
title={ __( 'Form Shortcode', 'profile-builder' ) } |
| 69 |
> |
| 70 |
{ shortcode === '' ? ( |
| 71 |
<Notice status="warning" isDismissible={ false }> |
| 72 |
{ __( 'The shortcode will be available after you publish this form.', 'profile-builder' ) } |
| 73 |
</Notice> |
| 74 |
) : ( |
| 75 |
<> |
| 76 |
<p className="wppb-fb-shortcode-intro"> |
| 77 |
{ __( 'Use this shortcode on the page where you want the form to be displayed.', 'profile-builder' ) } |
| 78 |
</p> |
| 79 |
<div className="wppb-fb-shortcode-box"> |
| 80 |
<code className="wppb-fb-shortcode-code">{ shortcode }</code> |
| 81 |
<CopyShortcodeButton text={ shortcode } /> |
| 82 |
</div> |
| 83 |
{ /* The title-change caveat only applies when the shortcode |
| 84 |
carries a title-derived form_name. Default forms embed as |
| 85 |
a bare [wppb-register] / [wppb-edit-profile], so |
| 86 |
their shortcode is title-independent — no warning. */ } |
| 87 |
{ shortcode.includes( 'form_name=' ) && ( |
| 88 |
<p className="wppb-fb-shortcode-help"> |
| 89 |
{ __( 'Changing the form title also changes the shortcode!', 'profile-builder' ) } |
| 90 |
</p> |
| 91 |
) } |
| 92 |
</> |
| 93 |
) } |
| 94 |
</PluginDocumentSettingPanel> |
| 95 |
); |
| 96 |
}; |
| 97 |
|
| 98 |
registerPlugin( 'wppb-form-shortcode-plugin', { |
| 99 |
render: FormShortcodePanel, |
| 100 |
} ); |
| 101 |
|