render( $form_id_or_key, $atts ); $output = ob_get_clean(); return $output; } } /** * 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 scripts and styles af_enqueue(); do_action( 'af/form/enqueue', $form, $args ); do_action( 'af/form/enqueue/id=' . $form['post_id'], $form, $args ); do_action( 'af/form/enqueue/key=' . $form['key'], $form, $args ); // 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( 'ajax' => false, 'display_title' => false, 'display_description' => false, 'id' => $form['key'], 'values' => array(), 'submit_text' => __( 'Submit', 'advanced-forms' ), 'redirect' => null, 'target' => acf_get_current_url(), 'echo' => true, 'exclude_fields' => array(), 'uploader' => 'wp', 'filter_mode' => false, 'label_placement' => 'top', 'instruction_placement' => 'label', 'honeypot' => true, ) ); // 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 ); // Allow a comma separated string for excluded fields if ( is_string( $args['exclude_fields'] ) ) { $args['exclude_fields'] = explode( ',', $args['exclude_fields'] ); } // Set ACF uploader type setting acf_update_setting( 'uploader', $args['uploader'] ); // Form element $form_attributes = array( 'class' => 'af-form acf-form', 'method' => 'POST', 'action' => $args['target'], 'id' => $args['id'], 'data-key' => $form['key'], ); // Add data attribute to indicate that the form should use AJAX submissions if ( $args['ajax'] ) { $form_attributes['data-ajax'] = true; } $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( '
', acf_esc_atts( $form_attributes ) ); do_action( 'af/form/before_title', $form, $args ); do_action( 'af/form/before_title/id=' . $form['post_id'], $form, $args ); do_action( 'af/form/before_title/key=' . $form['key'], $form, $args ); // Render submission error message if one exists $this->render_submission_error( $form, $args ); // Render title and description if they should be visible $this->render_title_and_description( $form, $args ); /** * Check if form should be restricted and not displayed. * Filter will return false if no restriction is applied otherwise it will return a string to display. */ $restriction = false; $restriction = apply_filters( 'af/form/restriction', $restriction, $form, $args ); $restriction = apply_filters( 'af/form/restriction/id=' . $form['post_id'], $restriction, $form, $args ); $restriction = apply_filters( 'af/form/restriction/key=' . $form['key'], $restriction, $form, $args ); // Display success message, restriction message, or fields $instance_hash = af_form_instance_hash( $form['key'], $args ); if ( af_has_submission( $instance_hash ) && ! af_submission_failed( $form['key'] ) && ! $args['filter_mode'] ) { echo af_form_success_message( $form, $args ); } elseif ( $restriction ) { $this->render_restriction_message( $restriction ); } else { $this->render_fields( $form, $args ); } // End form echo '
'; do_action( 'af/form/after', $form, $args ); do_action( 'af/form/after/id=' . $form['post_id'], $form, $args ); do_action( 'af/form/after/key=' . $form['key'], $form, $args ); } /** * Renders all submission errors if any exist. * * @since 1.6.0 */ function render_submission_error( $form, $args ) { if ( af_submission_failed( $form['key'] ) ) { $errors = AF()->submission['errors']; foreach ( $errors as $error ) { echo '
'; echo sprintf( '

%s

', $error ); echo '
'; } } } /** * Renders title and description of form if they should be shown. * * @since 1.6.0 */ function render_title_and_description( $form, $args ) { // Display title if ( $args['display_title'] ) { echo sprintf( '

%s

', esc_html( $form['title'] ) ); } // Display description if ( $args['display_description'] ) { // Allow basic markup in the description but strip