← Back
Editing: class-email.php
<?php /** * Comment email manager class. * * @package Directorist\Review * @since 7.1.0 */ namespace Directorist\Review; if ( ! defined( 'ABSPATH' ) ) { exit; } class Email { /** * Approved reviews awaiting their metadata before notification. * * @var array */ private static $pending_owner_notifications = []; /** * New reviews awaiting their metadata before administrator notification. * * @var array */ private static $pending_admin_notifications = []; public static function init() { add_action( 'wp_insert_comment', [ __CLASS__, 'queue_owner_notification' ] ); add_action( 'transition_comment_status', [ __CLASS__, 'on_status_transition' ], 10, 3 ); add_action( 'shutdown', [ __CLASS__, 'send_pending_owner_notifications' ] ); add_filter( 'notify_post_author', [ __CLASS__, 'disable_default_owner_notification' ], 10, 2 ); add_filter( 'comment_notification_recipients', [ __CLASS__, 'exclude_default_owner_recipients' ], 10, 2 ); add_action( 'comment_post', [ __CLASS__, 'queue_admin_notification' ] ); add_action( 'shutdown', [ __CLASS__, 'send_pending_admin_notifications' ] ); add_action( 'comment_post', [ __CLASS__, 'maybe_disable_default_email' ], 0 ); } public static function disable_default_owner_notification( $notify, $comment_id ) { return self::get_review( $comment_id ) ? false : $notify; } public static function exclude_default_owner_recipients( $recipients, $comment_id ) { return self::get_review( $comment_id ) ? [] : $recipients; } public static function queue_owner_notification( $comment_id ) { $review = self::get_review( $comment_id ); if ( $review && '1' === $review->comment_approved ) { self::$pending_owner_notifications[ $comment_id ] = $comment_id; } } public static function on_status_transition( $new_status, $old_status, $comment ) { if ( 'approved' === $new_status && $new_status !== $old_status ) { self::queue_owner_notification( $comment->comment_ID ); } } public static function send_pending_owner_notifications() { $comment_ids = self::$pending_owner_notifications; self::$pending_owner_notifications = []; foreach ( $comment_ids as $comment_id ) { self::notify_owner( $comment_id ); } } public static function queue_admin_notification( $comment_id ) { if ( self::get_review( $comment_id ) ) { self::$pending_admin_notifications[ $comment_id ] = $comment_id; } } public static function send_pending_admin_notifications() { $comment_ids = self::$pending_admin_notifications; self::$pending_admin_notifications = []; foreach ( $comment_ids as $comment_id ) { self::notify_admin( $comment_id ); } } public static function maybe_disable_default_email() { $post_id = isset( $_POST['comment_post_ID'] ) ? absint( $_POST['comment_post_ID'] ) : 0; // @codingStandardsIgnoreLine. if ( $post_id && ATBDP_POST_TYPE === get_post_type( $post_id ) ) { remove_action( 'comment_post', 'wp_new_comment_notify_moderator' ); remove_action( 'comment_post', 'wp_new_comment_notify_postauthor' ); } } public static function notify_owner( $comment_id ) { if ( get_directorist_option( 'disable_email_notification' ) || ! directorist_is_owner_notifiable_event( 'listing_review' ) ) { return false; } $review = self::get_review( $comment_id ); if ( ! $review || '1' !== $review->comment_approved || get_comment_meta( $comment_id, '_directorist_owner_notified', true ) ) { return false; } $post = get_post( $review->comment_post_ID ); $user = get_userdata( $post->post_author ); if ( ! $user ) { return false; } // The comment was left by the user. if ( $user && $review->user_id == $post->post_author ) { return false; } // The post author is no longer a member of the blog. if ( $user && ! user_can( $post->post_author, 'read_post', $post->ID ) ) { return false; } $content = self::get_email_content( $review ); $sent = ATBDP()->email->send_mail( $user->user_email, $content['subject'], $content['message'], ATBDP()->email->get_email_headers() ); if ( $sent ) { update_comment_meta( $comment_id, '_directorist_owner_notified', 1 ); } return $sent; } public static function notify_admin( $comment_id ) { if ( get_directorist_option( 'disable_email_notification' ) || ! directorist_is_admin_notifiable_event( 'listing_review' ) ) { return false; } $review = self::get_review( $comment_id ); if ( ! $review ) { return false; } $to = get_directorist_option( 'admin_email_lists' ); if ( empty( $to ) ) { $to = get_bloginfo( 'admin_email' ); } $content = self::get_email_content( $review, true ); $headers = "From: {$review->comment_author_email} <{$review->comment_author_email}>\r\n"; $headers .= "Reply-To: {$review->comment_author_email}\r\n"; return ATBDP()->email->send_mail( $to, $content['subject'], $content['message'], $headers ); } /** * Get the review email's default subject without requiring saved options. * * @param bool $admin Whether this is an administrator notification. * @return string */ public static function get_default_subject( $admin = false ) { if ( $admin ) { return __( '[==SITE_NAME==] New review at ==LISTING_TITLE==', 'directorist' ); } /* translators: 1: Site name, 2: Listing title. */ return sprintf( __( '[%1$s] New review at "%2$s"', 'directorist' ), '==SITE_NAME==', '==LISTING_TITLE==' ); } /** * Get the existing review email body expressed with editable placeholders. * * @param bool $admin Whether this is an administrator notification. * @return string */ public static function get_default_body( $admin = false ) { if ( $admin ) { /* translators: 1: Listing URL, 2: Reviewer name, 3: Reviewer email, 4: Review content. */ return sprintf( __( 'Dear Admin,<br /><br />A new review at %1$s.<br /><br />Name: %2$s<br />Email: %3$s<br />Review: %4$s', 'directorist' ), '==LISTING_URL==', '==REVIEWER_NAME==', '==REVIEWER_EMAIL==', '==REVIEW==' ); } /* translators: 1: Listing title, 2: Public reviewer name, 3: Star rating, 4: Review text, 5: Public review link. */ return sprintf( __( 'Listing: %1$s<br />Reviewer: %2$s<br />Rating: %3$s / 5<br />Review: %4$s<br /><br />%5$s', 'directorist' ), '==LISTING_TITLE==', '==REVIEWER_NAME==', '==RATING==', '==REVIEW==', '==REVIEW_LINK==' ); } /** * Render a recipient's template using only the review data it may expose. * * @param \WP_Comment $review Listing review. * @param bool $admin Whether this is an administrator notification. * @return array */ private static function get_email_content( $review, $admin = false ) { $post = get_post( $review->comment_post_ID ); $owner = get_userdata( $post->post_author ); $reviewer = get_userdata( $review->user_id ); $reviewer_name = $reviewer ? $reviewer->display_name : $review->comment_author; $reviewer_name = $reviewer_name ? $reviewer_name : __( 'Anonymous', 'directorist' ); $listing_url = get_permalink( $post->ID ); $review_url = get_comment_link( $review ); $site_url = home_url(); $tokens = [ '==NAME==' => $owner ? $owner->display_name : '', '==SITE_NAME==' => get_bloginfo( 'name' ), '==SITE_LINK==' => $site_url, '==LISTING_ID==' => (string) $post->ID, '==LISTING_TITLE==' => get_the_title( $post->ID ), '==LISTING_LINK==' => $listing_url, '==LISTING_URL==' => $listing_url, '==LISTING_PERMALINK==' => $listing_url, '==REVIEWER_NAME==' => $reviewer_name, '==RATING==' => number_format_i18n( (float) get_comment_meta( $review->comment_ID, 'rating', true ), 1 ), '==REVIEW==' => $review->comment_content, '==REVIEW_LINK==' => $review_url, '==REVIEW_URL==' => $review_url, ]; // Reviewer contact details are never resolved in listing-owner templates. if ( $admin ) { $tokens['==REVIEWER_EMAIL=='] = $review->comment_author_email; } $html_tokens = array_map( 'esc_html', $tokens ); $html_tokens['==REVIEW=='] = nl2br( esc_html( $review->comment_content ) ); $html_tokens['==SITE_LINK=='] = sprintf( '<a href="%s">%s</a>', esc_url( $site_url ), esc_html( $tokens['==SITE_NAME=='] ) ); $html_tokens['==LISTING_LINK=='] = sprintf( '<a href="%s">%s</a>', esc_url( $listing_url ), esc_html( $tokens['==LISTING_TITLE=='] ) ); $html_tokens['==LISTING_URL=='] = sprintf( '<a href="%1$s">%1$s</a>', esc_url( $listing_url ) ); $html_tokens['==LISTING_PERMALINK=='] = esc_url( $listing_url ); $html_tokens['==REVIEW_LINK=='] = sprintf( '<a href="%s">%s</a>', esc_url( $review_url ), esc_html__( 'View review', 'directorist' ) ); $html_tokens['==REVIEW_URL=='] = esc_url( $review_url ); $suffix = $admin ? '_admin' : ''; $subject_template = get_directorist_option( 'email_sub_listing_review' . $suffix, self::get_default_subject( $admin ) ); $body_template = get_directorist_option( 'email_tmpl_listing_review' . $suffix, self::get_default_body( $admin ) ); $subject = sanitize_text_field( wp_specialchars_decode( strtr( wp_strip_all_tags( $subject_template ), $tokens ), ENT_QUOTES ) ); $body = strtr( wp_kses_post( $body_template ), $html_tokens ); return [ 'subject' => $subject, 'message' => atbdp_email_html( esc_html( $subject ), $body ), ]; } public static function get_review( $comment_id ) { $comment = get_comment( $comment_id ); if ( empty( $comment ) || empty( $comment->comment_post_ID ) ) { return false; } if ( get_post_type( $comment->comment_post_ID ) !== ATBDP_POST_TYPE ) { return false; } if ( ! isset( $comment->comment_type ) || $comment->comment_type !== 'review' ) { return false; } return $comment; } } Email::init();
Save File
Cancel