Form::generate( array $form_args = array() )

Description #

Generate form.

Parameters #

  • $form_args
    array (Optional) Form generate arguments.
    • 'form_action'
      (string) Custom form action url.
    • 'hidden_fields'
      (array) Custom hidden input fields.
    Default value: array()

Changelog #

VersionDescription
4.1.8Inherit hidden_fields from form args.
4.1.0Introduced.

Source #

File: lib/class-form.php

	public function generate( $form_args = array() ) {
		// Don't do anything if no fields.
		if ( empty( $this->args['fields'] ) ) {
			echo '<p class="ap-form-nofields">';
			echo esc_attr(
				sprintf(
					// Translators: Placeholder contain form name.
					esc_attr__( 'No fields found for form: %s', 'anspress-question-answer' ),
					$this->form_name
				)
			);
			echo '</p>';

			return;
		}

		$form_args = wp_parse_args(
			$form_args,
			array(
				'form_action'   => '',
				'hidden_fields' => false,
				'ajax_submit'   => true,
				'submit_button' => $this->args['submit_button'],
				'form_tag'      => $this->args['form_tag'],
			)
		);

		if ( ! empty( $this->args['hidden_fields'] ) ) {
			$form_args['hidden_fields'] = wp_parse_args( $form_args['hidden_fields'], $this->args['hidden_fields'] );
		}

		/**
		 * Allows filtering arguments passed to @see AnsPress\Form\generate() method. Passed
		 * by reference.
		 *
		 * @param array  $form_args Form arguments.
		 * @param object $form      Current form object.
		 * @since 4.1.0
		 */
		$form_args = apply_filters_ref_array( 'ap_generate_form_args', array( $form_args, $this ) );

		$action = ! empty( $form_args['form_action'] ) ? esc_url( $form_args['form_action'] ) : '';

		if ( true === $form_args['form_tag'] ) {
			echo '<form id="' . esc_attr( $this->form_name ) . '" name="' . esc_attr( $this->form_name ) . '" method="POST" enctype="multipart/form-data" action="' . esc_attr( $action ) . '" ' . ( true === $form_args['ajax_submit'] ? ' apform' : '' ) . '>';
		}

		// Output form errors.
		if ( $this->have_errors() ) {
			echo '<div class="ap-form-errors">';
			foreach ( (array) $this->errors as $code => $msg ) {
				echo '<span class="ap-form-error ecode-' . esc_attr( $code ) . '">' . esc_html( $msg ) . '</span>';
			}
			echo '</div>';
		}

		echo $this->generate_fields(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		echo '<input type="hidden" name="ap_form_name" value="' . esc_attr( $this->form_name ) . '" />';

		if ( true === $form_args['submit_button'] ) {
			echo '<button type="submit" class="ap-btn ap-btn-submit">' . esc_html( $this->args['submit_label'] ) . '</button>';
		}

		echo '<input type="hidden" name="' . esc_attr( $this->form_name ) . '_nonce" value="' . esc_attr( wp_create_nonce( $this->form_name ) ) . '" />';
		echo '<input type="hidden" name="' . esc_attr( $this->form_name ) . '_submit" value="true" />';

		// Add custom hidden fields.
		if ( ! empty( $form_args['hidden_fields'] ) ) {
			foreach ( $form_args['hidden_fields'] as $field ) {
				echo '<input type="hidden" name="' . esc_attr( $field['name'] ) . '" value="' . esc_attr( $field['value'] ) . '" />';
			}
		}

		/**
		 * Action triggered after all form fields are generated and before closing
		 * form tag. This action can be used to append more fields or HTML in form.
		 *
		 * @param object $form Current form class.
		 */
		do_action_ref_array( 'ap_after_form_field', array( $this ) );

		if ( true === $this->args['form_tag'] ) {
			echo '</form>';
		}

		if ( ! empty( $this->after_form ) ) {
			echo $this->after_form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment