AP_Form_Hooks::answer_form()

Description #

Register answer form.

Changelog #

VersionDescription
4.1.6Fixed: editing answer creates new answer.
4.1.0Introduced.

Source #

File: includes/class-form-hooks.php

	public static function answer_form() {
		$editing     = false;
		$editing_id  = ap_sanitize_unslash( 'id', 'r' );
		$question_id = ap_sanitize_unslash( 'question_id', 'r', get_question_id() );

		$form = array(
			'submit_label' => __( 'Post Answer', 'anspress-question-answer' ),
			'fields'       => array(
				'post_content' => array(
					'type'        => 'editor',
					'label'       => __( 'Description', 'anspress-question-answer' ),
					'min_length'  => ap_opt( 'minimum_ans_length' ),
					'validate'    => 'required,min_string_length,badwords',
					'editor_args' => array(
						'quicktags' => ap_opt( 'question_text_editor' ) ? true : false,
					),
				),
			),
		);

		// Add private field checkbox if enabled.
		if ( ap_opt( 'allow_private_posts' ) ) {
			$form['fields']['is_private'] = array(
				'type'  => 'checkbox',
				'label' => __( 'Is private?', 'anspress-question-answer' ),
				'desc'  => __( 'Only visible to admin and moderator.', 'anspress-question-answer' ),
			);
		}

		// Add name fields if anonymous is allowed.
		if ( ! is_user_logged_in() && ap_allow_anonymous() ) {
			$form['fields']['anonymous_name'] = array(
				'label'      => __( 'Your Name', 'anspress-question-answer' ),
				'attr'       => array(
					'placeholder' => __( 'Enter your name to display', 'anspress-question-answer' ),
				),
				'order'      => 20,
				'validate'   => 'max_string_length,badwords',
				'max_length' => 20,
			);

			if ( empty( $editing_id ) && ap_opt( 'create_account' ) ) {
				$form['fields']['email'] = array(
					'label'      => __( 'Your Email', 'anspress-question-answer' ),
					'attr'       => array(
						'placeholder' => __( 'Enter your email', 'anspress-question-answer' ),
					),
					'desc'       => 'An account for you will be created and a confirmation link will be sent to you with the password.',
					'order'      => 20,
					'validate'   => 'is_email,required',
					'sanitize'   => 'email,required',
					'max_length' => 64,
				);
			}
		}

		$form['fields']['post_id'] = array(
			'type'     => 'input',
			'subtype'  => 'hidden',
			'value'    => $editing_id,
			'sanitize' => 'absint',
		);

		// Add value when editing post.
		if ( ! empty( $editing_id ) ) {
			$form['editing']      = true;
			$form['editing_id']   = $editing_id;
			$form['submit_label'] = __( 'Update Answer', 'anspress-question-answer' );
		}

		/**
		 * Filter for modifying answer form `$args`.
		 *
		 * @param   array $fields   Answer form fields.
		 * @param   bool    $editing    Currently editing form.
		 * @since   4.1.0
		 */
		return apply_filters( 'ap_answer_form_fields', $form, $editing );
	}

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