AnsPress_Email_Hooks::ap_after_update_answer( $answer_id, object $answer )

Description #

Notify users after answer gets updated.

Parameters #

  • $answer
    object (Required) Answer object.
  • $event
    string (Required) Event type.

Source #

File: addons/free/email.php

	public static function ap_after_update_answer( $answer_id, $answer ) {
		if ( in_array( $answer->post_status, [ 'trash', 'draft' ] ) ) {
			return;
		}

		$current_user = wp_get_current_user();

		$args = [];

		$admin_emails = self::get_admin_emails( 'email_admin_edit_answer' );
		if ( ! empty( $admin_emails ) ) {
			$args['users'] = $admin_emails;
		}

		$email         = new Email( 'edit_answer', $args );

		$a_subscribers = (array) ap_get_subscribers( [ 'subs_events' => 'answer_' . $answer->post_parent ] );
		$q_subscribers = (array) ap_get_subscribers( [ 'subs_event' => 'question', 'subs_ref_id' => $answer->post_parent ] );
		$subscribers   = array_merge( $a_subscribers, $q_subscribers );
		$post_author   = get_user_by( 'id', $answer->post_author );

		if ( ! ap_in_array_r( $post_author->data->user_email, $subscribers ) &&
			$current_user->user_email !== $post_author->data->user_email ) {
			$email->add_email( $post_author->data->user_email );
		}

		foreach ( (array) $subscribers as $s ) {
			if ( ap_user_can_view_post( $answer ) && ! empty( $s->user_email ) &&
				$s->user_email !== $current_user->user_email ) {
					$email->add_email( $s->user_email );
			}
		}

		$email->add_template_tags( array(
			'answerer'          => ap_user_display_name( $answer->post_author ),
			'editor'            => ap_user_display_name( get_current_user_id() ),
			'question_title'    => $answer->post_title,
			'answer_link'       => get_permalink( $answer->ID ),
			'answer_content'    => $answer->post_content,
		) );

		$email->send_emails();
	}

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