[ key => field schema (Field_Block::schema()) + 'step' + 'parents' ], * 'order' => [ key, … ] input fields in form order, * 'layout' => [ key => schema ] non-input blocks (conditions of columns, steps…), * 'steps' => [ [ 'title', 'next', 'prev' ], … ], * 'flags' => [ has_upload, has_payment, has_steps, has_calc, has_captcha_slot, has_signature ], * ] * * @package FWREN */ namespace FWREN; defined( 'ABSPATH' ) || exit; class Schema { /** @var array */ private static $cache = array(); /** * Compile and store. * * @param int $form_id Form id. * @param string $content Post content. * @return array */ public static function compile( $form_id, $content ) { Fields::load(); $schema = array( 'version' => 1, 'fields' => array(), 'order' => array(), 'layout' => array(), 'steps' => array(), 'flags' => array(), ); $step = 0; $blocks = parse_blocks( (string) $content ); $first = ''; foreach ( $blocks as $b ) { if ( 'fwren/step' === ( $b['blockName'] ?? '' ) ) { $type = Fields::by_block( 'fwren/step' ); $s = $type->prepare_settings( (array) $b['attrs'] ); if ( 0 === $step ) { $first = (string) $s['first_title']; } $schema['steps'][] = array( 'title' => (string) $s['title'], 'next' => (string) $s['next_text'], 'prev' => (string) $s['prev_text'] ); $step++; continue; } self::walk( $b, $schema, $step, array(), $form_id ); } if ( $schema['steps'] ) { array_unshift( $schema['steps'], array( 'title' => $first ? $first : __( 'Step', 'fieldwren' ), 'next' => '', 'prev' => '' ) ); } $flags = array( 'has_steps' => count( $schema['steps'] ) > 1 ); foreach ( $schema['fields'] as $f ) { $flags['has_upload'] = ! empty( $flags['has_upload'] ) || ! empty( $f['is_upload'] ); $flags['has_payment'] = ! empty( $flags['has_payment'] ) || ! empty( $f['payment'] ); $flags['has_calc'] = ! empty( $flags['has_calc'] ) || ! empty( $f['calculated'] ); $flags['has_signature'] = ! empty( $flags['has_signature'] ) || ! empty( $f['signature'] ); } foreach ( $schema['layout'] as $l ) { $flags['has_captcha_slot'] = ! empty( $flags['has_captcha_slot'] ) || ! empty( $l['captcha_slot'] ); $flags['has_submit'] = ! empty( $flags['has_submit'] ) || ! empty( $l['submit'] ); } $schema['flags'] = $flags; $schema = apply_filters( 'fwren/schema', $schema, $form_id ); if ( $form_id ) { update_post_meta( $form_id, '_fwren_schema', $schema ); } self::$cache[ (int) $form_id ] = $schema; return $schema; } /** * Walk a block tree. * * @param array $b Parsed block. * @param array $schema Schema (by ref). * @param int $step Step index. * @param array $parents Keys of ancestor layout blocks with conditions. * @param int $form_id Form id. */ private static function walk( array $b, array &$schema, $step, array $parents, $form_id ) { $name = $b['blockName'] ?? ''; $type = $name ? Fields::by_block( $name ) : null; if ( ! $type ) { $cond = Field_Block::conditions_of( (array) ( $b['attrs'] ?? array() ) ); if ( $cond ) { $key = '_container_' . preg_replace( '/[^a-z0-9]/', '', strtolower( (string) ( $b['attrs']['_id'] ?? wp_generate_password( 6, false ) ) ) ); $schema['layout'][ $key ] = array( 'name' => $key, 'type' => 'container', 'block' => $name, 'conditions' => $cond, 'is_input' => false ); $parents[] = $key; } foreach ( (array) ( $b['innerBlocks'] ?? array() ) as $child ) { self::walk( $child, $schema, $step, $parents, $form_id ); } return; } $s = $type->prepare_settings( (array) $b['attrs'] ); $f = $type->schema( $s ); $f['step'] = $step; $f['parents'] = $parents; $f['form_id'] = (int) $form_id; if ( ! empty( $f['repeater'] ) ) { foreach ( (array) ( $b['innerBlocks'] ?? array() ) as $child ) { $ct = Fields::by_block( $child['blockName'] ?? '' ); if ( $ct && $ct->is_input() ) { $cf = $ct->schema( $ct->prepare_settings( (array) $child['attrs'] ) ); $cf['form_id'] = (int) $form_id; $f['children'][] = $cf; } } self::add_field( $schema, $f ); return; } if ( $type->is_input() ) { self::add_field( $schema, $f ); } else { $schema['layout'][ $f['name'] ] = $f; if ( $f['conditions'] ) { $parents[] = $f['name']; } } foreach ( (array) ( $b['innerBlocks'] ?? array() ) as $child ) { self::walk( $child, $schema, $step, $parents, $form_id ); } } /** Add a field, renaming duplicate keys (key_2, key_3…). */ private static function add_field( array &$schema, array $f ) { $key = $f['name']; $n = 2; while ( isset( $schema['fields'][ $key ] ) ) { $key = $f['name'] . '_' . $n++; } $f['name'] = $key; $schema['fields'][ $key ] = $f; $schema['order'][] = $key; } /** * Schema of a form (compiled on demand when missing or stale). * * @param int $form_id Form id. * @return array */ public static function get( $form_id ) { $form_id = (int) $form_id; if ( isset( self::$cache[ $form_id ] ) ) { return self::$cache[ $form_id ]; } $schema = get_post_meta( $form_id, '_fwren_schema', true ); if ( ! is_array( $schema ) || empty( $schema['version'] ) ) { $post = get_post( $form_id ); $schema = self::compile( $form_id, $post ? $post->post_content : '' ); } self::$cache[ $form_id ] = $schema; return $schema; } /** @return array Input fields keyed by name. */ public static function fields( $form_id ) { return self::get( $form_id )['fields']; } /** @return array|null One field. */ public static function field( $form_id, $key ) { $fields = self::fields( $form_id ); return $fields[ $key ] ?? null; } /** @return array Field choices for admin dropdowns: key => label. */ public static function field_choices( $form_id, $filter = null ) { $out = array(); foreach ( self::fields( $form_id ) as $k => $f ) { if ( $filter && ! call_user_func( $filter, $f ) ) { continue; } $out[ $k ] = ( $f['admin_label'] ? $f['admin_label'] : ( $f['label'] ? $f['label'] : $k ) ) . ' (' . $k . ')'; } return $out; } /** @return string Display label of a field. */ public static function label( array $f ) { return $f['admin_label'] ? $f['admin_label'] : ( $f['label'] ? $f['label'] : $f['name'] ); } }