From 977613e8d9c235e9e2a07ee3de27af4fed20a6ab Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Wed, 17 Aug 2022 17:24:38 -0700 Subject: [PATCH] Contributors: Move `user_registered` check to pruning function. That allows `add_user_data_to_xprofile()` to be reused in other contexts. `user_login` was removed from the `SELECT` because it wasn't being used. --- plugins/wporg-5ftf/includes/contributor.php | 18 ++++++++++++------ plugins/wporg-5ftf/tests/test-contributor.php | 10 ++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 26951cd..88eebab 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -708,23 +708,20 @@ function add_user_data_to_xprofile( array $xprofiles ) : array { $user_ids = array_keys( $xprofiles ); $id_placeholders = implode( ', ', array_fill( 0, count( $user_ids ), '%d' ) ); - // Get user data. - // Ignore new users because they haven't had a chance to contribute yet. // phpcs:disable -- `$id_placeholders` is safely created above. $established_users = $wpdb->get_results( $wpdb->prepare( " SELECT - u.ID, u.user_email, u.user_login, u.user_nicename, + u.ID, u.user_email, u.user_registered, u.user_nicename, GROUP_CONCAT( um.meta_key ) AS meta_keys, GROUP_CONCAT( um.meta_value ) AS meta_values FROM `$wpdb->users` u JOIN `$wpdb->usermeta` um ON u.ID = um.user_id WHERE um.user_id IN ( $id_placeholders ) AND - um.meta_key IN ( 'last_logged_in', '5ftf_last_inactivity_email', 'first_name' ) AND - u.user_registered < CURDATE() - INTERVAL %d MONTH + um.meta_key IN ( 'last_logged_in', '5ftf_last_inactivity_email', 'first_name' ) GROUP BY um.user_id ORDER BY u.ID", - array_merge( $user_ids, array( INACTIVITY_THRESHOLD_MONTHS ) ) + $user_ids ) ); // phpcs:enable @@ -732,6 +729,7 @@ function add_user_data_to_xprofile( array $xprofiles ) : array { $full_user = array( 'user_id' => absint( $user->ID ), 'user_email' => $user->user_email, + 'user_registered' => intval( strtotime( $user->user_registered ) ), 'hours_per_week' => $xprofiles[ $user->ID ]->hours_per_week, 'user_nicename' => $user->user_nicename, ); @@ -760,12 +758,20 @@ function prune_unnotifiable_users( array $contributors ) : array { $inactivity_threshold = strtotime( INACTIVITY_THRESHOLD_MONTHS . ' months ago' ); foreach ( $contributors as $index => $contributor ) { + // Skip new users because they haven't had a chance to contribute yet. + if ( $contributor['user_registered'] > $inactivity_threshold ) { + unset( $contributors[ $index ] ); + continue; + } + if ( is_active( $contributor['last_logged_in'] ) ) { unset( $contributors[ $index ] ); + continue; } if ( $contributor['5ftf_last_inactivity_email'] > $inactivity_threshold ) { unset( $contributors[ $index ] ); + continue; } } diff --git a/plugins/wporg-5ftf/tests/test-contributor.php b/plugins/wporg-5ftf/tests/test-contributor.php index 24b37d7..aec7ce6 100644 --- a/plugins/wporg-5ftf/tests/test-contributor.php +++ b/plugins/wporg-5ftf/tests/test-contributor.php @@ -275,23 +275,33 @@ class Test_Contributor extends WP_UnitTestCase { $contributors = array( 'active + due for email' => array( 'last_logged_in' => strtotime( '1 week ago' ), + 'user_registered' => strtotime( '1 year ago' ), '5ftf_last_inactivity_email' => 0, ), 'active + not due for email' => array( 'last_logged_in' => strtotime( '1 week ago' ), + 'user_registered' => strtotime( '1 year ago' ), '5ftf_last_inactivity_email' => strtotime( '1 month ago' ), ), 'inactive + due for email' => array( 'last_logged_in' => strtotime( '4 months ago' ), + 'user_registered' => strtotime( '1 year ago' ), '5ftf_last_inactivity_email' => strtotime( '4 months ago' ), ), 'inactive + not due for email' => array( 'last_logged_in' => strtotime( '4 months ago' ), + 'user_registered' => strtotime( '1 year ago' ), '5ftf_last_inactivity_email' => strtotime( '2 months ago' ), ), + + 'new user' => array( + 'last_logged_in' => 0, + 'user_registered' => strtotime( '1 week ago' ), + '5ftf_last_inactivity_email' => 0, + ), ); $expected = array( 'inactive + due for email' );