+
+ onChange( { ...registration, active } ) }
+ />
+ { registration.active && (
+ onChange( { ...registration, require_verification } ) }
+ />
+ ) }
+
+
+);
+
+/**
+ * Inspector panel for block access control.
+ */
+const BlockVisibilityPanel = ( { attributes, setAttributes }: BlockEditProps ) => {
+ const rules: BlockVisibilityRules = attributes.newspackAccessControlRules ?? {};
+ const visibility: string = attributes.newspackAccessControlVisibility ?? 'visible';
+ const mode: string = attributes.newspackAccessControlMode ?? 'gate';
+ const gateIds: number[] = attributes.newspackAccessControlGateIds ?? [];
+
+ const registration: RegistrationRule = rules.registration ?? { active: false };
+ const customAccess: CustomAccessRule = rules.custom_access ?? { active: false, access_rules: [] };
+ // Flatten grouped OR rules for display: [[rule]] → [rule]
+ const activeRules: ActiveRule[] = customAccess.access_rules.map( group => group[ 0 ] ).filter( Boolean );
+
+ const rulesActive = hasActiveRules( rules, mode, gateIds );
+
+ const updateRules = ( updates: Partial< BlockVisibilityRules > ) => {
+ const newRules: BlockVisibilityRules = { ...rules, ...updates };
+ const stillActive = hasActiveRules( newRules, mode, gateIds );
+ setAttributes( {
+ newspackAccessControlRules: newRules,
+ // Reset visibility to 'visible' when all custom rules are cleared.
+ ...( ! stillActive ? { newspackAccessControlVisibility: 'visible' } : {} ),
+ } );
+ };
+
+ const setRegistration = ( newRegistration: RegistrationRule ) => {
+ updateRules( {
+ registration: {
+ ...newRegistration,
+ // Ensure require_verification is cleared when registration is turned off.
+ require_verification: newRegistration.active ? newRegistration.require_verification : false,
+ },
+ } );
+ };
+
+ const setAccessRules = ( flatRules: ActiveRule[] ) => {
+ const grouped: ActiveRule[][] = flatRules.map( rule => [ rule ] );
+ updateRules( {
+ custom_access: {
+ ...customAccess,
+ active: grouped.length > 0,
+ access_rules: grouped,
+ },
+ } );
+ };
+
+ return (
+