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.
This commit is contained in:
Ian Dunn 2022-08-17 17:24:38 -07:00
parent 9c0d1371f3
commit c9783a17f5
2 changed files with 22 additions and 6 deletions

View file

@ -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;
}
}

View file

@ -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' );