array(), 'display' => 'group', 'prefix_name' => 0, ); } public static function render_settings( $field, $prefix ) { $selected = isset( $field['clone'] ) && is_array( $field['clone'] ) ? $field['clone'] : array(); $current = (int) get_the_ID(); $groups = get_posts( array( 'post_type' => CFStudio_Field_Group::POST_TYPE, 'post_status' => array( 'publish', 'draft', 'pending', 'private' ), 'numberposts' => -1, 'orderby' => 'title', 'order' => 'ASC', ) ); ?>
ID === $current ) { continue; } ++$shown; $key = CFStudio_Field_Group::get_group_key( $group->ID ); ?>

__( 'Group (cloned fields are wrapped in one group)', 'fieldloom' ), 'seamless' => __( 'Seamless (cloned fields replace this field)', 'fieldloom' ), ), 'group' ); self::setting_checkbox( 'clone', $prefix, 'prefix_name', __( 'Prefix field names with this field\'s name (seamless display)', 'fieldloom' ), $field ); } public static function sanitize( $raw ) { $keys = array(); foreach ( (array) ( $raw['clone'] ?? array() ) as $key ) { if ( is_scalar( $key ) && '' !== sanitize_key( (string) $key ) ) { $keys[] = sanitize_key( (string) $key ); } } return array( 'clone' => array_values( array_unique( $keys ) ), 'display' => self::pick( $raw, 'display', array( 'group', 'seamless' ), 'group' ), 'prefix_name' => ! empty( $raw['prefix_name'] ) ? 1 : 0, ); } public static function render_input( $field, $value, $name ) { echo '

' . esc_html__( 'Clone field: its fields are shown in place of this one.', 'fieldloom' ) . '

'; } /** * Replace every clone field in a field list by the fields it points at. * Recurses into group / repeater / flexible content containers. * * @param array $fields Field list. * @param array $sources group key => array( 'id' => int, 'fields' => array ) of EVERY group. * @param array $visiting Group keys being resolved right now (cycle guard). * @return array */ public static function resolve( $fields, $sources, $visiting = array() ) { $out = array(); foreach ( (array) $fields as $field ) { if ( ! is_array( $field ) ) { continue; } if ( 'clone' !== ( $field['type'] ?? '' ) ) { $out[] = self::resolve_children( $field, $sources, $visiting ); continue; } $cloned = array(); foreach ( (array) ( $field['clone'] ?? array() ) as $group_key ) { if ( ! isset( $sources[ $group_key ] ) || isset( $visiting[ $group_key ] ) ) { continue; // Missing group, or a group that (indirectly) clones itself. } $cloned = array_merge( $cloned, self::resolve( $sources[ $group_key ]['fields'], $sources, $visiting + array( $group_key => true ) ) ); } $seamless = 'seamless' === ( $field['display'] ?? 'group' ); $prefix = ( $seamless && ! empty( $field['prefix_name'] ) ) ? $field['name'] . '_' : ''; $cloned = self::rekey( $cloned, $field['key'] ?? $field['name'], $prefix ); if ( $seamless ) { $out = array_merge( $out, $cloned ); } elseif ( $cloned ) { $out[] = array( 'key' => $field['key'] ?? $field['name'], 'label' => $field['label'] ?? $field['name'], 'name' => $field['name'], 'type' => 'group', 'instructions' => $field['instructions'] ?? '', 'required' => 0, 'layout' => 'block', 'sub_fields' => $cloned, ) + ( isset( $field['conditional_logic'] ) ? array( 'conditional_logic' => $field['conditional_logic'] ) : array() ); } } return $out; } /** Resolve clones nested inside group / repeater / flexible fields. */ protected static function resolve_children( $field, $sources, $visiting ) { if ( isset( $field['sub_fields'] ) && is_array( $field['sub_fields'] ) ) { $field['sub_fields'] = self::resolve( $field['sub_fields'], $sources, $visiting ); } if ( isset( $field['layouts'] ) && is_array( $field['layouts'] ) ) { foreach ( $field['layouts'] as $i => $layout ) { if ( isset( $layout['sub_fields'] ) ) { $field['layouts'][ $i ]['sub_fields'] = self::resolve( $layout['sub_fields'], $sources, $visiting ); } } } return $field; } /** * Give cloned fields keys that cannot collide with the originals' keys * (the same group can be cloned twice), and optionally prefix names — * keeping conditional-logic references among the cloned siblings valid. */ protected static function rekey( $fields, $clone_key, $prefix ) { $names = array(); foreach ( $fields as $field ) { $names[ $field['name'] ] = $prefix . $field['name']; } foreach ( $fields as $i => $field ) { $fields[ $i ]['key'] = $clone_key . '_' . ( $field['key'] ?? $field['name'] ); $fields[ $i ]['name'] = $names[ $field['name'] ]; if ( ! empty( $field['conditional_logic']['rules'] ) ) { foreach ( $field['conditional_logic']['rules'] as $r => $rule ) { if ( isset( $names[ $rule['field'] ] ) ) { $fields[ $i ]['conditional_logic']['rules'][ $r ]['field'] = $names[ $rule['field'] ]; } } } } return $fields; } }