From d8b11cfd073b2a1119b7d61bacecc7a894d914d6 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Tue, 21 Feb 2017 13:32:08 -0500 Subject: [PATCH 1/3] #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/3] 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 137a63f67c5be0a0568b93f73bc8b12d4d706b63 Mon Sep 17 00:00:00 2001 From: Shawn Hooper Date: Wed, 22 Feb 2017 12:02:48 -0500 Subject: [PATCH 3/3] Reverted to jaredtech's master version --- init.php | 115 ++++++++++++++++--------------------------------------- 1 file changed, 32 insertions(+), 83 deletions(-) diff --git a/init.php b/init.php index e0be9cf..9f8142f 100644 --- a/init.php +++ b/init.php @@ -5,13 +5,13 @@ * Description: This plugin provides the ability to disable specific user accounts. * Version: 1.0.5 * Author: Jared Atchison - * Author URI: http://jaredatchison.com + * Author URI: http://jaredatchison.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -35,19 +35,18 @@ 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' ) ); } /** @@ -74,19 +73,19 @@ public function use_profile_field( $user ) { if ( !current_user_can( 'edit_users' ) ) return; ?> - - - - - - - -
- - - ID ) ); ?> /> - -
+ + + + + + + +
+ + + ID ) ); ?> /> + +
ID, 'ja_disable_user', true ); // Is the use logging in disabled? - if ( $this->is_user_disabled( $user->ID ) ) { + if ( $disabled == '1' ) { // Clear cookies, a.k.a log user out wp_clear_auth_cookie(); @@ -172,7 +152,7 @@ public function user_login( $user_login, $user = null ) { public function user_login_message( $message ) { // Show the error message if it seems to be a disabled user - if ( isset( $_GET['disabled'] ) && $_GET['disabled'] == 1 ) + if ( isset( $_GET['disabled'] ) && $_GET['disabled'] == 1 ) $message = '
' . apply_filters( 'ja_disable_users_notice', __( 'Account disabled', 'ja_disable_users' ) ) . '
'; return $message; @@ -203,7 +183,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 ( $this->is_user_disabled( $user_ID ) ) { + if ( get_the_author_meta( 'ja_disable_user', $user_ID ) == 1 ) { return __( 'Disabled', 'ja_disable_users' ); } } @@ -213,40 +193,9 @@ public function manage_users_column_content( $empty, $column_name, $user_ID ) { * Specifiy the width of our custom column * * @since 1.0.3 - */ + */ 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