Email::ap_after_update_answer( object $answer_id, string $answer )

Description #

Notify users after answer gets updated.

Parameters #

  • $answer_id
    object (Required) Answer id.
  • $answer
    string (Required) Event post object.

Source #

File: addons/email/email.php

	public 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 = $this->get_admin_emails( 'email_admin_edit_answer' );
		if ( ! empty( $admin_emails ) ) {
			$args['users'] = $admin_emails;
		}

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

		$a_subscribers = (array) ap_get_subscribers( [ 'subs_event' => '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 );

		// Exclude current post author email.
		if ( ! empty( $answer->post_author ) ) {
			$post_author = get_user_by( 'id', $answer->post_author );

			if ( $post_author && ! 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. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment