render( $form_id_or_key, $atts ); $output = ob_get_clean(); return $output; } } /** * Handles submissions and enqueue of neccessary scripts * Relies on default ACF validations * * @since 1.0.0 * */ function pre_form() { if ( isset( $_POST['af_form'] ) && ! isset( $_POST['action'] ) ) { $form_key_or_id = $_POST['af_form']; $form = af_get_form( $form_key_or_id ); // Validate the posted data, this validation has already been performed once over AJAX if ( $form && acf_validate_save_post( true ) ) { // Increase the form submissions counter if ( $form['post_id'] ) { $submissions = get_post_meta( $form['post_id'], 'form_num_of_submissions', true ); $submissions = $submissions ? $submissions + 1 : 1; update_post_meta( $form['post_id'], 'form_num_of_submissions', $submissions ); } // Retrieve the args used to display the form $args = json_decode( base64_decode( $_POST['af_form_args'] ), true ); /** * Upload all files in $_FILES using ACFs helper function. Required for basic uploads to work painlessly. * TODO: Move to af_save_field() to avoid saving all files? * * @since 1.3.1 * */ if ( isset( $_FILES['acf'] ) ) { acf_upload_files(); } // Retrieve all form fields and their values $fields = array(); if ( isset( $_POST['acf'] ) ) { foreach ( $_POST['acf'] as $k => $value ) { $field = acf_get_field( $k ); $field['_input'] = $value; $field['value'] = acf_format_value( $value, 0, $field ); $fields[] = $field; } } // Save submission data to the global AF object AF()->submission = array( 'form' => $form, 'args' => $args, 'fields' => $fields, ); do_action( 'af/form/submission', $form, $fields, $args ); do_action( 'af/form/submission/id=' . $form['post_id'], $form, $fields, $args ); do_action( 'af/form/submission/key=' . $form['key'], $form, $fields, $args ); // Redirect to different URL if redirect argument has been passed if ( $args['redirect'] && '' != $args['redirect'] ) { wp_redirect( $args['redirect'] ); exit; } } } } /** * Renders the form specified by ID * * @since 1.0.0 * */ function render( $form_id_or_key, $args ) { $form = af_get_form( $form_id_or_key ); if ( ! $form ) { return; } /** * Enqueue ACF scripts and styles * * Normally ACF initializes the global JS object in wp_head but we only want to include the scripts when displaying a form. * To work around this we enqueue using the regular ACF function and then immediately include the acf-input.js script and all it's dependencies. * If acf-input.js is not initialized before the fields then conditional logic doesn't work. The remaining scripts/styles will be included in wp_footer. * * From ACF 5.7 and onwards this is no longer necessary. Conditional logic is no longer reliant on inline scripts and a regular enqueue is sufficient. * * @since 1.1.1 * */ acf_enqueue_scripts(); // Check if ACF version is < 5.7 if ( acf_version_compare( acf()->version, '<', '5.7' ) ) { global $wp_scripts; $wp_scripts->print_scripts( array( 'acf-input', 'acf-pro-input' ) ); } // Allow the form to be modified before rendering form $form = apply_filters( 'af/form/before_render', $form, $args ); $form = apply_filters( 'af/form/before_render/id=' . $form['post_id'], $form, $args ); $form = apply_filters( 'af/form/before_render/key=' . $form['key'], $form, $args ); $args = wp_parse_args($args, array( 'display_title' => false, 'display_description' => false, 'id' => $form['key'], 'values' => array(), 'submit_text' => __( 'Submit', 'advanced-forms' ), 'redirect' => false, 'target' => acf_get_current_url(), 'echo' => true, 'exclude_fields' => array(), 'uploader' => 'wp', 'filter_mode' => false, )); // Allow the arguments to be modified before rendering form $args = apply_filters( 'af/form/args', $args, $form ); $args = apply_filters( 'af/form/args/id=' . $form['post_id'], $args, $form ); $args = apply_filters( 'af/form/args/key=' . $form['key'], $args, $form ); // Increase the form view counter if ( $form['post_id'] ) { $views = get_post_meta( $form['post_id'], 'form_num_of_views', true ); $views = $views ? $views + 1 : 1; update_post_meta( $form['post_id'], 'form_num_of_views', $views ); } // Form element $form_attributes = array( 'class' => 'af-form acf-form', 'method' => 'POST', 'action' => $args['target'], 'id' => $args['id'], ); $form_attributes = apply_filters( 'af/form/attributes', $form_attributes, $form, $args ); $form_attributes = apply_filters( 'af/form/attributes/id=' . $form['post_id'], $form_attributes, $form, $args ); $form_attributes = apply_filters( 'af/form/attributes/key=' . $form['key'], $form_attributes, $form, $args ); echo sprintf( '