| 1 |
/** |
| 2 |
* Form Fields in Columns document sidebar: per-form on/off (`wppb_fb_ffc_enable`). |
| 3 |
*/ |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { registerPlugin } from '@wordpress/plugins'; |
| 6 |
import { PluginDocumentSettingPanel } from '@wordpress/edit-post'; |
| 7 |
import { ToggleControl, Notice } from '@wordpress/components'; |
| 8 |
import { useEntityProp } from '@wordpress/core-data'; |
| 9 |
import { useSelect } from '@wordpress/data'; |
| 10 |
|
| 11 |
const FFC_COLUMNS_BLOCK = 'profile-builder/ffc-columns'; |
| 12 |
|
| 13 |
const FfcPanel = () => { |
| 14 |
const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] ); |
| 15 |
if ( ! [ 'wppb-rf-cpt', 'wppb-epf-cpt' ].includes( postType ) ) return null; |
| 16 |
// Gate behind the Form Fields in Columns add-on's bridge (see file header): |
| 17 |
// the `wppb_fb_ffc_enable` virtual REST key, the Columns block's inserter |
| 18 |
// entry and the front-end wrapping all ship with the add-on, so with it |
| 19 |
// deactivated the toggle would write an unregistered meta key that REST |
| 20 |
// drops. Same contract as the Progress Bar / Multi-Step Forms panels. |
| 21 |
if ( ! ( window.wppbFb && window.wppbFb.ffc ) ) return null; |
| 22 |
|
| 23 |
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' ); |
| 24 |
if ( ! meta ) return null; |
| 25 |
|
| 26 |
// Treat empty / missing as 'yes' — matches the legacy default (see |
| 27 |
// wppb_in_ffc_meta_boxes_content's first-load default of ['ffc-enable' => 'yes']). |
| 28 |
const stored = meta.wppb_fb_ffc_enable; |
| 29 |
const enabled = stored === '' || stored === undefined ? true : stored === 'yes'; |
| 30 |
|
| 31 |
const wrapperCount = useSelect( ( select ) => { |
| 32 |
const editor = select( 'core/block-editor' ); |
| 33 |
if ( ! editor ) return 0; |
| 34 |
return editor.getBlocks().filter( ( b ) => b && b.name === FFC_COLUMNS_BLOCK ).length; |
| 35 |
}, [] ); |
| 36 |
|
| 37 |
return ( |
| 38 |
<PluginDocumentSettingPanel |
| 39 |
name="wppb-ffc-settings" |
| 40 |
title={ __( 'Form Fields in Columns', 'profile-builder' ) } |
| 41 |
> |
| 42 |
{ wrapperCount === 0 && ( |
| 43 |
<Notice status="info" isDismissible={ false }> |
| 44 |
{ __( 'Insert a "Columns" block and drop two or more field blocks inside it to wrap them in a horizontal row on the front end.', 'profile-builder' ) } |
| 45 |
</Notice> |
| 46 |
) } |
| 47 |
|
| 48 |
<ToggleControl |
| 49 |
label={ __( 'Render columns on the front end', 'profile-builder' ) } |
| 50 |
checked={ enabled } |
| 51 |
onChange={ ( on ) => setMeta( { wppb_fb_ffc_enable: on ? 'yes' : 'no' } ) } |
| 52 |
help={ __( 'When off, Columns blocks are ignored at render time and their fields render in a normal vertical stack. Useful for temporarily disabling the layout without removing the blocks.', 'profile-builder' ) } |
| 53 |
/> |
| 54 |
</PluginDocumentSettingPanel> |
| 55 |
); |
| 56 |
}; |
| 57 |
|
| 58 |
registerPlugin( 'wppb-ffc-settings-plugin', { render: FfcPanel } ); |
| 59 |
|