AnsPress_Hooks::save_answer_hooks( integer $post_id, object $post, boolean $updated )

Description #

Trigger posts hooks right after saving answer.

Parameters #

  • $post_id
    integer (Required) Post ID.
  • $post
    object (Required) Post Object
  • $updated
    boolean (Required) Is updating post

Changelog #

VersionDescription
4.1.8Add ap_delete_images_not_in_content.
4.1.2Do not process if form not submitted. Insert updated to activity table.
4.1.0Introduced.

Source #

File: includes/hooks.php

	public static function save_answer_hooks( $post_id, $post, $updated ) {
		if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
			return;
		}

		if ( $updated ) {
			// Deleted unused images from meta.
			ap_delete_images_not_in_content( $post_id );
		}

		$form = anspress()->get_form( 'answer' );

		$values = $form->get_values();
		$activity_type = ! empty( $values['post_id']['value'] ) ? 'edit_answer' : 'new_answer';

		// Update parent question's answer count.
		ap_update_answers_count( $post->post_parent );

		$qameta = array(
			'last_updated' => current_time( 'mysql' ),
			'activities'   => array(
				'type'    => $activity_type,
				'user_id' => $post->post_author,
				'date'    => current_time( 'mysql' ),
			),
		);

		// Check if anonymous post and have name.
		if ( $form->is_submitted() && ! is_user_logged_in() && ap_allow_anonymous() && ! empty( $values['anonymous_name']['value'] ) ) {
			$qameta['fields'] = array(
				'anonymous_name' => $values['anonymous_name']['value'],
			);
		}

		/**
		 * Modify qameta args which will be inserted after inserting
		 * or updating answer.
		 *
		 * @param array   $qameta  Qameta arguments.
		 * @param object  $post    Post object.
		 * @param boolean $updated Is updated.
		 * @since 4.1.0
		 */
		$qameta = apply_filters( 'ap_insert_answer_qameta', $qameta, $post, $updated );
		ap_insert_qameta( $post_id, $qameta );

		if ( $updated ) {
			/**
			 * Action triggered right after updating answer.
			 *
			 * @param integer $post_id Inserted post ID.
			 * @param object	$post		Inserted post object.
			 * @since 0.9
			 * @since 4.1.0 Removed `$post->post_type` variable.
			 */
			do_action( 'ap_processed_update_answer' , $post_id, $post );

		} else {
			/**
			 * Action triggered right after inserting new answer.
			 *
			 * @param integer $post_id Inserted post ID.
			 * @param object	$post		Inserted post object.
			 * @since 0.9
			 * @since 4.1.0 Removed `$post->post_type` variable.
			 */
			do_action( 'ap_processed_new_answer', $post_id, $post );
		}

		// Update qameta terms.
		ap_update_qameta_terms( $post_id );
	}

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