| 1 |
/** |
| 2 |
* Seed mandatory fields on new Registration forms when the canvas opens empty. |
| 3 |
* Server seeding covers REST; `setupEditor` paints from bootstrapped content and |
| 4 |
* skips that refresh for a clean new post. |
| 5 |
* |
| 6 |
* Gates: RF only, zero blocks, once per session, `post_status === 'auto-draft'`. |
| 7 |
* Not `isCleanNewPost()` — REST can populate `content.raw` and mark an untouched |
| 8 |
* post dirty. |
| 9 |
*/ |
| 10 |
|
| 11 |
import { registerPlugin } from '@wordpress/plugins'; |
| 12 |
import { useSelect, useDispatch } from '@wordpress/data'; |
| 13 |
import { useEffect, useRef } from '@wordpress/element'; |
| 14 |
import { createBlock } from '@wordpress/blocks'; |
| 15 |
|
| 16 |
import { getExistingFieldsSnapshot } from '../lib/existingFieldsSnapshot'; |
| 17 |
|
| 18 |
// Bridge is source of truth; fallback fails closed (seed nothing if missing). |
| 19 |
const MANDATORY_FIELD_TYPES_IN_ORDER = ( |
| 20 |
typeof window !== 'undefined' |
| 21 |
&& window.wppbFb |
| 22 |
&& Array.isArray( window.wppbFb.mandatoryTypes ) |
| 23 |
&& window.wppbFb.mandatoryTypes.length |
| 24 |
) |
| 25 |
? window.wppbFb.mandatoryTypes |
| 26 |
: [ 'Default - Username', 'Default - E-mail', 'Default - Password' ]; |
| 27 |
|
| 28 |
function useAutoSeedRegistrationForm() { |
| 29 |
const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] ); |
| 30 |
const postStatus = useSelect( ( select ) => select( 'core/editor' ).getEditedPostAttribute( 'status' ), [] ); |
| 31 |
const blockCount = useSelect( ( select ) => select( 'core/block-editor' ).getBlocks().length, [] ); |
| 32 |
const { insertBlocks, __unstableMarkNextChangeAsNotPersistent } = useDispatch( 'core/block-editor' ); |
| 33 |
const seededRef = useRef( false ); |
| 34 |
|
| 35 |
useEffect( () => { |
| 36 |
if ( seededRef.current ) return; |
| 37 |
if ( postType !== 'wppb-rf-cpt' ) return; |
| 38 |
if ( postStatus !== 'auto-draft' ) return; |
| 39 |
if ( blockCount !== 0 ) return; |
| 40 |
|
| 41 |
const fieldsBridge = getExistingFieldsSnapshot(); |
| 42 |
if ( ! fieldsBridge.length ) return; |
| 43 |
|
| 44 |
const byType = new Map(); |
| 45 |
for ( const f of fieldsBridge ) { |
| 46 |
if ( f && f.fieldType && ! byType.has( f.fieldType ) ) { |
| 47 |
byType.set( f.fieldType, f ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
const toInsert = []; |
| 52 |
for ( const type of MANDATORY_FIELD_TYPES_IN_ORDER ) { |
| 53 |
const entry = byType.get( type ); |
| 54 |
if ( ! entry ) continue; |
| 55 |
toInsert.push( |
| 56 |
createBlock( entry.blockName, { |
| 57 |
...( entry.attributes || {} ), |
| 58 |
id: entry.id, |
| 59 |
lock: { remove: true }, |
| 60 |
} ) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! toInsert.length ) return; |
| 65 |
|
| 66 |
// Non-persistent: opening a new form must not count as unsaved edits. |
| 67 |
seededRef.current = true; |
| 68 |
if ( typeof __unstableMarkNextChangeAsNotPersistent === 'function' ) { |
| 69 |
__unstableMarkNextChangeAsNotPersistent(); |
| 70 |
} |
| 71 |
insertBlocks( toInsert, 0, undefined, false ); |
| 72 |
}, [ postType, postStatus, blockCount ] ); |
| 73 |
} |
| 74 |
|
| 75 |
const AutoSeedRegistrationForm = () => { |
| 76 |
useAutoSeedRegistrationForm(); |
| 77 |
return null; |
| 78 |
}; |
| 79 |
|
| 80 |
registerPlugin( 'wppb-auto-seed-registration-form', { |
| 81 |
render: AutoSeedRegistrationForm, |
| 82 |
} ); |
| 83 |
|