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 a391123c9c
commit 977613e8d9
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB
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 ); $user_ids = array_keys( $xprofiles );
$id_placeholders = implode( ', ', array_fill( 0, count( $user_ids ), '%d' ) ); $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. // phpcs:disable -- `$id_placeholders` is safely created above.
$established_users = $wpdb->get_results( $wpdb->prepare( " $established_users = $wpdb->get_results( $wpdb->prepare( "
SELECT 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_key ) AS meta_keys,
GROUP_CONCAT( um.meta_value ) AS meta_values GROUP_CONCAT( um.meta_value ) AS meta_values
FROM `$wpdb->users` u FROM `$wpdb->users` u
JOIN `$wpdb->usermeta` um ON u.ID = um.user_id JOIN `$wpdb->usermeta` um ON u.ID = um.user_id
WHERE WHERE
um.user_id IN ( $id_placeholders ) AND um.user_id IN ( $id_placeholders ) AND
um.meta_key IN ( 'last_logged_in', '5ftf_last_inactivity_email', 'first_name' ) AND um.meta_key IN ( 'last_logged_in', '5ftf_last_inactivity_email', 'first_name' )
u.user_registered < CURDATE() - INTERVAL %d MONTH
GROUP BY um.user_id GROUP BY um.user_id
ORDER BY u.ID", ORDER BY u.ID",
array_merge( $user_ids, array( INACTIVITY_THRESHOLD_MONTHS ) ) $user_ids
) ); ) );
// phpcs:enable // phpcs:enable
@ -732,6 +729,7 @@ function add_user_data_to_xprofile( array $xprofiles ) : array {
$full_user = array( $full_user = array(
'user_id' => absint( $user->ID ), 'user_id' => absint( $user->ID ),
'user_email' => $user->user_email, 'user_email' => $user->user_email,
'user_registered' => intval( strtotime( $user->user_registered ) ),
'hours_per_week' => $xprofiles[ $user->ID ]->hours_per_week, 'hours_per_week' => $xprofiles[ $user->ID ]->hours_per_week,
'user_nicename' => $user->user_nicename, 'user_nicename' => $user->user_nicename,
); );
@ -760,12 +758,20 @@ function prune_unnotifiable_users( array $contributors ) : array {
$inactivity_threshold = strtotime( INACTIVITY_THRESHOLD_MONTHS . ' months ago' ); $inactivity_threshold = strtotime( INACTIVITY_THRESHOLD_MONTHS . ' months ago' );
foreach ( $contributors as $index => $contributor ) { 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'] ) ) { if ( is_active( $contributor['last_logged_in'] ) ) {
unset( $contributors[ $index ] ); unset( $contributors[ $index ] );
continue;
} }
if ( $contributor['5ftf_last_inactivity_email'] > $inactivity_threshold ) { if ( $contributor['5ftf_last_inactivity_email'] > $inactivity_threshold ) {
unset( $contributors[ $index ] ); unset( $contributors[ $index ] );
continue;
} }
} }

View file

@ -275,23 +275,33 @@ class Test_Contributor extends WP_UnitTestCase {
$contributors = array( $contributors = array(
'active + due for email' => array( 'active + due for email' => array(
'last_logged_in' => strtotime( '1 week ago' ), 'last_logged_in' => strtotime( '1 week ago' ),
'user_registered' => strtotime( '1 year ago' ),
'5ftf_last_inactivity_email' => 0, '5ftf_last_inactivity_email' => 0,
), ),
'active + not due for email' => array( 'active + not due for email' => array(
'last_logged_in' => strtotime( '1 week ago' ), 'last_logged_in' => strtotime( '1 week ago' ),
'user_registered' => strtotime( '1 year ago' ),
'5ftf_last_inactivity_email' => strtotime( '1 month ago' ), '5ftf_last_inactivity_email' => strtotime( '1 month ago' ),
), ),
'inactive + due for email' => array( 'inactive + due for email' => array(
'last_logged_in' => strtotime( '4 months ago' ), 'last_logged_in' => strtotime( '4 months ago' ),
'user_registered' => strtotime( '1 year ago' ),
'5ftf_last_inactivity_email' => strtotime( '4 months ago' ), '5ftf_last_inactivity_email' => strtotime( '4 months ago' ),
), ),
'inactive + not due for email' => array( 'inactive + not due for email' => array(
'last_logged_in' => strtotime( '4 months ago' ), 'last_logged_in' => strtotime( '4 months ago' ),
'user_registered' => strtotime( '1 year ago' ),
'5ftf_last_inactivity_email' => strtotime( '2 months 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' ); $expected = array( 'inactive + due for email' );