/**
* Read-only Form Shortcode panel (`wppb_fb_form_shortcode`). Register first in index.js.
*/
import { __ } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { Button, Notice } from '@wordpress/components';
import { useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useCopyToClipboard } from '@wordpress/compose';
import { useState, useEffect } from '@wordpress/element';
// Inline icons (project's inline-SVG convention — no @wordpress/icons dependency).
// NOTE: fill is set via inline `style`, NOT the `fill="none"` attribute.
// Gutenberg's `.components-button svg { fill: currentColor }` beats a presentation
// attribute and would fill the closed rectangles solid (hiding the copy glyph's
// interior lines); an inline style wins over that rule.
const CopyIcon = () => (
);
const CheckIcon = () => (
);
const CopyShortcodeButton = ( { text } ) => {
const [ copied, setCopied ] = useState( false );
const ref = useCopyToClipboard( text, () => setCopied( true ) );
useEffect( () => {
if ( ! copied ) return undefined;
const timer = setTimeout( () => setCopied( false ), 2000 );
return () => clearTimeout( timer );
}, [ copied ] );
return (
);
};
const FormShortcodePanel = () => {
const postType = useSelect(
( select ) => select( 'core/editor' ).getCurrentPostType(),
[]
);
const [ meta ] = useEntityProp( 'postType', postType, 'meta' );
if ( ! [ 'wppb-rf-cpt', 'wppb-epf-cpt' ].includes( postType ) ) return null;
const shortcode = ( meta && meta.wppb_fb_form_shortcode ) || '';
return (
{ shortcode === '' ? (
{ __( 'The shortcode will be available after you publish this form.', 'profile-builder' ) }
) : (
<>
{ __( 'Use this shortcode on the page where you want the form to be displayed.', 'profile-builder' ) }
{ shortcode }
{ /* The title-change caveat only applies when the shortcode
carries a title-derived form_name. Default forms embed as
a bare [wppb-register] / [wppb-edit-profile], so
their shortcode is title-independent — no warning. */ }
{ shortcode.includes( 'form_name=' ) && (
{ __( 'Changing the form title also changes the shortcode!', 'profile-builder' ) }