submission ); } /** * Searches input for tags {field:FIELD_NAME} and replaces with field values. * Also replaces general tags such as {all_fields}. * * @since 1.0.1 * */ function af_resolve_field_includes( $input, $fields = false ) { // Get fields from the global submission object if fields weren't passed if ( ! $fields && af_has_submission() ) { $fields = AF()->submission['fields']; } // Render all fields as a table if ( preg_match_all( "/{all_fields}/", $input, $matches ) ) { $output = ''; foreach ( $fields as $field ) { if ( 'clone' == $field['type'] ) { foreach ( $field['sub_fields'] as $sub_field ) { $output .= sprintf( '', $sub_field['label'] ); $output .= sprintf( '', _af_render_field_include( $sub_field, $field['value'][ $sub_field['name'] ] ) ); } } else { $output .= sprintf( '', $field['label'] ); $output .= sprintf( '', _af_render_field_include( $field ) ); } } $output .= '
%s
%s
%s
%s
'; $input = str_replace( '{all_fields}', $output, $input ); } // Render single fields individually if ( preg_match_all( "/{field:(.*?)}/", $input, $matches ) ) { foreach ($matches[1] as $i => $field_name ) { $field = af_get_field_object( $field_name ); $rendered_value = _af_render_field_include( $field ); $input = str_replace( $matches[0][$i], $rendered_value, $input ); } } return $input; } /** * Renders a single field include (for emails, success messages etc.) * * @since 1.2.0 * */ function _af_render_field_include( $field, $value = false ) { if ( ! $value ) { $value = $field['value']; } $output = ''; if ( 'repeater' == $field['type'] ) { $output .= ''; // Column headings $output .= ''; foreach ( $field['sub_fields'] as $sub_field ) { $output .= sprintf( '', $sub_field['label'] ); } $output .= ''; // Rows $output .= ''; foreach ( $value as $row_values ) { $output .= ''; foreach ( $field['sub_fields'] as $sub_field ) { $output .= sprintf( '', _af_render_field_include( $sub_field, $row_values[ $sub_field['name'] ] ) ); } $output .= ''; } $output .= ''; $output .= '
%s
%s
'; } else if ( 'clone' == $field['type'] ) { $output .= ''; foreach ( $field['sub_fields'] as $sub_field ) { $output .= sprintf( '', $sub_field['label'] ); $output .= sprintf( '', _af_render_field_include( $sub_field, $field['value'][ $sub_field['name'] ] ) ); } $output .= '
%s
%s
'; } else { $output = (string)$value; } // Allow third-parties to alter rendered field $output = apply_filters( 'af/field/render_include', $output, $field, $value ); $output = apply_filters( 'af/field/render_include/name=' . $field['name'], $output, $field, $value ); $output = apply_filters( 'af/field/render_include/key=' . $field['key'], $output, $field, $value ); return $output; } /** * Checks if the passed key is a valid form key (begins with form_) * * @since 1.0.1 * */ function af_is_valid_form_key( $key ) { if ( ! is_string( $key ) ) { return false; } if ( 'form_' == substr( $key, 0, 5 ) ) { return true; } return false; } /** * Output an "Insert field" button populated with $fields * $floating adds class "floating" to the wrapper making the button float right in an input field * * @since 1.1.1 * */ function _af_field_inserter_button( $fields, $floating = false ) { $classses = ( $floating ) ? 'floating' : ''; echo 'Insert field'; echo '
'; echo sprintf( '
%s
', __( 'All fields', 'advanced-forms' ) ); echo '
'; foreach ( $fields as $field ) { echo sprintf( '
%s
', $field['name'], $field['label'] ); } echo '
'; echo '
'; }