From d8b11cfd073b2a1119b7d61bacecc7a894d914d6 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 21 Feb 2017 13:32:08 -0500 Subject: [PATCH 1/4] #5 Don't send emails to disabled users --- init.php | 83 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 16 deletions(-) diff --git a/init.php b/init.php index cafae69..ae4537a 100644 --- a/init.php +++ b/init.php @@ -35,18 +35,19 @@ final class ja_disable_users { function __construct() { // Actions - add_action( 'init', array( $this, 'load_textdomain' ) ); - add_action( 'show_user_profile', array( $this, 'use_profile_field' ) ); - add_action( 'edit_user_profile', array( $this, 'use_profile_field' ) ); - add_action( 'personal_options_update', array( $this, 'user_profile_field_save' ) ); - add_action( 'edit_user_profile_update', array( $this, 'user_profile_field_save' ) ); - add_action( 'wp_login', array( $this, 'user_login' ), 10, 2 ); - add_action( 'manage_users_custom_column', array( $this, 'manage_users_column_content' ), 10, 3 ); - add_action( 'admin_footer-users.php', array( $this, 'manage_users_css' ) ); - + add_action( 'init', array( $this, 'load_textdomain' ) ); + add_action( 'show_user_profile', array( $this, 'use_profile_field' ) ); + add_action( 'edit_user_profile', array( $this, 'use_profile_field' ) ); + add_action( 'personal_options_update', array( $this, 'user_profile_field_save' ) ); + add_action( 'edit_user_profile_update', array( $this, 'user_profile_field_save' ) ); + add_action( 'wp_login', array( $this, 'user_login' ), 10, 2 ); + add_action( 'manage_users_custom_column', array( $this, 'manage_users_column_content' ), 10, 3 ); + add_action( 'admin_footer-users.php', array( $this, 'manage_users_css' ) ); + // Filters - add_filter( 'login_message', array( $this, 'user_login_message' ) ); - add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) ); + add_filter( 'login_message', array( $this, 'user_login_message' ) ); + add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) ); + add_filter( 'comment_notification_recipients', array( $this, 'remove_disabled_users_from_new_comment_notifications' ), 10, 2 ); } /** @@ -110,6 +111,27 @@ public function user_profile_field_save( $user_id ) { update_user_meta( $user_id, 'ja_disable_user', $disabled ); } + /** + * Checks if a user is disabled + * + * @todo ADD @since VERSION ON DEPLOYMENT + * @param int $user_id The user ID to check + * @param object $user + * @return boolean true if disabled, false if enabled + */ + public function is_user_disabled( $user_id ) { + + // Get user meta + $disabled = get_user_meta( $user_id, 'ja_disable_user', true ); + + // Is the use logging in disabled? + if ( $disabled == '1' ) { + return true; + } + + return false; + } + /** * After login check to see if user account is disabled * @@ -126,11 +148,9 @@ public function user_login( $user_login, $user = null ) { // not logged in - definitely not disabled return; } - // Get user meta - $disabled = get_user_meta( $user->ID, 'ja_disable_user', true ); - + // Is the use logging in disabled? - if ( $disabled == '1' ) { + if ( $this->is_user_disabled( $user->ID ) ) { // Clear cookies, a.k.a log user out wp_clear_auth_cookie(); @@ -183,7 +203,7 @@ public function manage_users_columns( $defaults ) { public function manage_users_column_content( $empty, $column_name, $user_ID ) { if ( $column_name == 'ja_user_disabled' ) { - if ( get_the_author_meta( 'ja_disable_user', $user_ID ) == 1 ) { + if ( $this->is_user_disabled( $user_ID ) ) { return __( 'Disabled', 'ja_disable_users' ); } } @@ -197,5 +217,36 @@ public function manage_users_column_content( $empty, $column_name, $user_ID ) { public function manage_users_css() { echo ''; } + + /** + * Remove the post author's email address from new commemnt/postback/trackback + * notifications if the author's account is disabled. + * + * @todo ADD @since VERSION ON DEPLOYMENT + * @param array $emails The array of email addresses to be notified when there is a new post comment + * @param int $comment_id The ID of the new comment + * + * @return array The updated array of email addresses to be notified + */ + public function remove_disabled_users_from_new_comment_notifications( $emails, $comment_id ) { + + // Retrieve the Comment + $comment = get_comment( $comment_id ); + + // Retrieve the related post + $post = get_post ( $comment->comment_post_ID ); + + // Check if the post author is disabled + if ( $this->is_user_disabled( $post->post_author ) ) { + // Retrieve the Author's User Record + $author = get_user_by( 'ID', $post->post_author ); + + // Remove the author from the comment notifications array + $emails = array_diff( $emails, array( $author->user_email ) ); + } + + return $emails; + } + } new ja_disable_users(); \ No newline at end of file From 5c912c49896d7647d7b725d42a1099c744a98b72 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 21 Feb 2017 13:33:56 -0500 Subject: [PATCH 2/4] is_user_disabled should be a private function --- init.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.php b/init.php index ae4537a..e0be9cf 100644 --- a/init.php +++ b/init.php @@ -119,7 +119,7 @@ public function user_profile_field_save( $user_id ) { * @param object $user * @return boolean true if disabled, false if enabled */ - public function is_user_disabled( $user_id ) { + private function is_user_disabled( $user_id ) { // Get user meta $disabled = get_user_meta( $user_id, 'ja_disable_user', true ); From 5bf97b3616fd39e087859a7cc7ff7de6e6125c3c Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Thu, 23 Feb 2017 17:47:13 -0500 Subject: [PATCH 3/4] Update to make all wp_mail calls exclude disabled users, not just new comments --- init.php | 88 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 56 insertions(+), 32 deletions(-) diff --git a/init.php b/init.php index e0be9cf..f483428 100644 --- a/init.php +++ b/init.php @@ -35,19 +35,19 @@ final class ja_disable_users { function __construct() { // Actions - add_action( 'init', array( $this, 'load_textdomain' ) ); - add_action( 'show_user_profile', array( $this, 'use_profile_field' ) ); - add_action( 'edit_user_profile', array( $this, 'use_profile_field' ) ); - add_action( 'personal_options_update', array( $this, 'user_profile_field_save' ) ); - add_action( 'edit_user_profile_update', array( $this, 'user_profile_field_save' ) ); - add_action( 'wp_login', array( $this, 'user_login' ), 10, 2 ); - add_action( 'manage_users_custom_column', array( $this, 'manage_users_column_content' ), 10, 3 ); - add_action( 'admin_footer-users.php', array( $this, 'manage_users_css' ) ); + add_action( 'init', array( $this, 'load_textdomain' ) ); + add_action( 'show_user_profile', array( $this, 'use_profile_field' ) ); + add_action( 'edit_user_profile', array( $this, 'use_profile_field' ) ); + add_action( 'personal_options_update', array( $this, 'user_profile_field_save' ) ); + add_action( 'edit_user_profile_update', array( $this, 'user_profile_field_save' ) ); + add_action( 'wp_login', array( $this, 'user_login' ), 10, 2 ); + add_action( 'manage_users_custom_column', array( $this, 'manage_users_column_content' ), 10, 3 ); + add_action( 'admin_footer-users.php', array( $this, 'manage_users_css' ) ); // Filters - add_filter( 'login_message', array( $this, 'user_login_message' ) ); - add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) ); - add_filter( 'comment_notification_recipients', array( $this, 'remove_disabled_users_from_new_comment_notifications' ), 10, 2 ); + add_filter( 'login_message', array( $this, 'user_login_message' ) ); + add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) ); + add_filter( 'wp_mail', array( $this, 'do_not_email_disabled_users' ) ); } /** @@ -114,7 +114,7 @@ public function user_profile_field_save( $user_id ) { /** * Checks if a user is disabled * - * @todo ADD @since VERSION ON DEPLOYMENT + * @since 1.1.0 * @param int $user_id The user ID to check * @param object $user * @return boolean true if disabled, false if enabled @@ -218,35 +218,59 @@ public function manage_users_css() { echo ''; } + /*** + * Returns an array containing the email addresses of all disabled users. + * If there are no disabled users, returns null. + * + * Using the 'ja_disable_users_disabled_email_addresses' filter you can modify the + * the returned array. + * + * @since 1.1.0 + * @return null|array + */ + private function get_disabled_users_email_addresses() { + global $wpdb; + $sql = "SELECT user_email FROM {$wpdb->prefix}users u INNER JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id AND um.meta_key = 'ja_disable_user' WHERE um.meta_value = '1'"; + + return apply_filters('ja_disable_users_disabled_email_addresses', $wpdb->get_col( $sql ) ); + } + /** - * Remove the post author's email address from new commemnt/postback/trackback - * notifications if the author's account is disabled. + * Removes any disabled user's email addresses from the To: email header. + * Works with comma separated values, or an array for to $args['to'] value. * - * @todo ADD @since VERSION ON DEPLOYMENT - * @param array $emails The array of email addresses to be notified when there is a new post comment - * @param int $comment_id The ID of the new comment + * @since 1.1.0 + * @param array $args A compacted array of wp_mail() arguments, including the "to" email, + * subject, message, headers, and attachments values. * - * @return array The updated array of email addresses to be notified + * @return array The updated array of arguments */ - public function remove_disabled_users_from_new_comment_notifications( $emails, $comment_id ) { + public function do_not_email_disabled_users( $args ) { - // Retrieve the Comment - $comment = get_comment( $comment_id ); + if ( ! isset( $args['to'] ) ) { + return $args; + } - // Retrieve the related post - $post = get_post ( $comment->comment_post_ID ); + // Get an array of the users being sent to + if ( is_array( $args['to'] ) ) { + $to = $args['to']; + } else { + $to = array_map( 'trim', explode( ',', $args['to'] ) ); + } - // Check if the post author is disabled - if ( $this->is_user_disabled( $post->post_author ) ) { - // Retrieve the Author's User Record - $author = get_user_by( 'ID', $post->post_author ); + // Get an array of disabled users + $disabled_users = $this->get_disabled_users_email_addresses(); + + // Update wp_mail() with the remaining email addresses + $args['to'] = implode(',', array_diff( $to, $disabled_users ) ); + + var_dump($args); + + die(); + + } - // Remove the author from the comment notifications array - $emails = array_diff( $emails, array( $author->user_email ) ); - } - return $emails; - } } new ja_disable_users(); \ No newline at end of file From 14e5b05119e9eb3e3b52ee336ee3e425f950e0f6 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Thu, 23 Feb 2017 17:49:55 -0500 Subject: [PATCH 4/4] Removed test code --- init.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/init.php b/init.php index f483428..a6ef53a 100644 --- a/init.php +++ b/init.php @@ -264,13 +264,8 @@ public function do_not_email_disabled_users( $args ) { // Update wp_mail() with the remaining email addresses $args['to'] = implode(',', array_diff( $to, $disabled_users ) ); - var_dump($args); - - die(); - - } - - + return $args; + } } new ja_disable_users(); \ No newline at end of file