* Discussion ("Email me whenever"); this file only changes who receives them. * With no valid address stored, WordPress's own recipients are left unchanged so * enabling the setting without filling it in never silences notifications. * * Loaded only when the "comment_notification_recipients" option is enabled. * * @package Agency_Toolkit */ // Standard WP check if direct. defined( 'ABSPATH' ) || exit; /** * The recipient addresses stored by the settings form. * * The form stores a comma separated string (see inspry_toolkit_sanitize_email_list); * WP-CLI may store an array or, with --format=json and an object, a stdClass. All * three are read the same way the settings field renders them. * * @return string[] Valid, de-duplicated email addresses, possibly empty. */ function inspry_toolkit_comment_notification_recipients_list() { $stored = get_option( 'inspry_toolkit_comment_notification_recipients_list' ); if ( is_array( $stored ) || is_object( $stored ) ) { $candidates = (array) $stored; } elseif ( is_scalar( $stored ) ) { // Same Unicode-space normalisation as the sanitizer (options.php, always loaded), for lists stored by WP-CLI. $candidates = preg_split( '/[\s,;]+/', inspry_toolkit_normalise_address_separators( $stored ) ); } else { $candidates = array(); } $emails = array(); $seen = array(); foreach ( $candidates as $candidate ) { $candidate = is_scalar( $candidate ) ? trim( (string) $candidate ) : ''; if ( '' !== $candidate && is_email( $candidate ) && ! in_array( strtolower( $candidate ), $seen, true ) ) { $emails[] = $candidate; $seen[] = strtolower( $candidate ); } } return $emails; } /** * Comment types the setting redirects. * * Anything else ("note", "pingback", "trackback", or a type another plugin * invented for its own purposes) keeps WordPress's own recipients. * * @return string[] Comment types, as stored in comment_type. */ function inspry_toolkit_comment_notification_redirected_types() { // 'comment' covers ordinary comments; WooCommerce stores product reviews as 'review'. return array( 'comment', 'review' ); } /** * Replace the recipients of a comment notification or moderation email. * * Runs late so the stored list is what is sent, after any plugin that added its * own recipients earlier in the chain. That is the documented behaviour of the * setting: "instead of", not "as well as". * * @param string[] $emails Recipients WordPress (and earlier filters) chose. * @param int $comment_id Comment being reported. * @return string[] */ function inspry_toolkit_comment_notification_recipients( $emails, $comment_id ) { $comment = get_comment( $comment_id ); // Unknown comment: leave core's choice alone rather than guess. if ( ! $comment instanceof WP_Comment ) { return $emails; } // An empty comment_type is an ordinary comment on older rows and some importers. $type = '' === (string) $comment->comment_type ? 'comment' : (string) $comment->comment_type; if ( ! in_array( $type, inspry_toolkit_comment_notification_redirected_types(), true ) ) { return $emails; } $configured = inspry_toolkit_comment_notification_recipients_list(); return $configured ? $configured : $emails; } add_filter( 'comment_notification_recipients', 'inspry_toolkit_comment_notification_recipients', 99, 2 ); add_filter( 'comment_moderation_recipients', 'inspry_toolkit_comment_notification_recipients', 99, 2 );