Form::prepare()

Description #

Prepare input field.

Changelog #

VersionDescription
4.1.5Return current instance.
4.1.0Introduced.

Source #

File: lib/class-form.php

	public function prepare() {
		if ( empty( $this->args['fields'] ) ) {
			return $this;
		}

		$fields = ap_sort_array_by_order( $this->args['fields'] );

		if ( ! ap_opt( 'allow_private_posts' ) && isset( $fields['is_private'] ) ) {
			unset( $fields['is_private'] );
		}

		foreach ( $fields as $field_name => $field_args ) {
			if ( empty( $field_args['type'] ) ) {
				$field_args['type'] = 'input';
			}

			$type_class  = ucfirst( trim( $field_args['type'] ) );
			$field_class = 'AnsPress\\Form\\Field\\' . $type_class;

			if ( class_exists( $field_class ) ) {
				/**
				 * Allows filtering field argument before its being passed to Field class.
				 *
				 * @param array $field_args Field arguments.
				 * @param object $form Form class, passed by reference.
				 * @since 4.1.0
				 */
				$field_args                  = apply_filters_ref_array( 'ap_before_prepare_field', array( $field_args, $this ) );
				$this->fields[ $field_name ] = new $field_class( $this->form_name, $field_name, $field_args, $this );
			}
		}

		$this->prepared = true;
		$this->sanitize_validate();

		return $this;
	}

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