AP_Form_Hooks::question_form()

Description #

Register question form.

Changelog #

VersionDescription
4.1.5Fixed: anonymous_name field value.
4.1.0Introduced.

Source #

File: includes/class-form-hooks.php

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

		$form = array(
			'submit_label' => __( 'Submit Question', 'anspress-question-answer' ),
			'fields'       => array(
				'post_title'   => array(
					'type'       => 'input',
					'label'      => __( 'Title', 'anspress-question-answer' ),
					'desc'       => __( 'Question in one sentence', 'anspress-question-answer' ),
					'attr'       => array(
						'autocomplete'   => 'off',
						'placeholder'    => __( 'Question title', 'anspress-question-answer' ),
						'data-action'    => 'suggest_similar_questions',
						'data-loadclass' => 'q-title',
					),
					'min_length' => ap_opt( 'minimum_qtitle_length' ),
					'max_length' => 100,
					'validate'   => 'required,min_string_length,max_string_length,badwords',
					'order'      => 2,
				),
				'post_content' => array(
					'type'        => 'editor',
					'label'       => __( 'Description', 'anspress-question-answer' ),
					'min_length'  => ap_opt( 'minimum_question_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' => 64,
			);

			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',
		);

		// Set post parent field and nonce.
		$post_parent = ap_isset_post_value( 'post_parent', false );
		if ( $post_parent && wp_verify_nonce( ap_isset_post_value( '__nonce_pp' ), 'post_parent_' . $post_parent ) ) {
			$form['hidden_fields'] = array(
				array(
					'name'  => 'post_parent',
					'value' => $post_parent,
				),
				array(
					'name'  => '__nonce_pp',
					'value' => wp_create_nonce( 'post_parent_' . $post_parent ),
				),
			);
		}

		// Add value when editing post.
		if ( ! empty( $editing_id ) ) {
			$question = ap_get_post( $editing_id );

			$form['editing']                         = true;
			$form['editing_id']                      = $editing_id;
			$form['submit_label']                    = __( 'Update Question', 'anspress-question-answer' );
			$form['fields']['post_title']['value']   = $question->post_title;
			$form['fields']['post_content']['value'] = $question->post_content;
			$form['fields']['is_private']['value']   = 'private_post' === $question->post_status ? true : false;

			if ( isset( $form['fields']['anonymous_name'] ) ) {
				$fields                                    = ap_get_post_field( 'fields', $question );
				$form['fields']['anonymous_name']['value'] = ! empty( $fields['anonymous_name'] ) ? $fields['anonymous_name'] : '';
			}
		}

		/**
		 * Filter for modifying question form `$args`.
		 *
		 * @param   array $fields   Ask form fields.
		 * @param   bool    $editing    Currently editing form.
		 * @since   4.1.0
		 */
		$form = apply_filters( 'ap_question_form_fields', $form, $editing );

		return $form;
	}

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