Regularly store stats snapshots to track historical trends.

Fixes #37
This commit is contained in:
Ian Dunn 2019-12-06 08:03:54 -08:00 committed by GitHub
parent 152964f5cf
commit fda5842e86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 284 additions and 1 deletions

View file

@ -281,7 +281,9 @@ function get_pledge_contributors_data( $pledge_id ) {
}
/**
* Get the user objects that correspond with pledge contributor posts.
* Get the user objects that correspond with contributor posts.
*
* @see `get_contributor_user_ids()` for a similar function.
*
* @param WP_Post[] $contributor_posts
*
@ -293,6 +295,43 @@ function get_contributor_user_objects( array $contributor_posts ) {
}, $contributor_posts );
}
/**
* Get user IDs for the given `CPT_ID` posts.
*
* This is similar to `get_contributor_user_objects()`, but returns more specific data, and is more performant
* with large data sets (e.g., with `get_snapshot_data()`) because there is 1 query instead of
* `count( $contributor_posts )`.
*
* @param WP_Post[] $contributor_posts
*
* @return array
*/
function get_contributor_user_ids( $contributor_posts ) {
global $wpdb;
$usernames = wp_list_pluck( $contributor_posts, 'post_title' );
/*
* Generate placeholders dynamically, so that each username will be quoted individually rather than as a
* single string.
*
* @see https://developer.wordpress.org/reference/classes/wpdb/prepare/#comment-1557
*/
$usernames_placeholders = implode( ', ', array_fill( 0, count( $usernames ), '%s' ) );
$query = "
SELECT id
FROM $wpdb->users
WHERE user_login IN( $usernames_placeholders )
";
$user_ids = $wpdb->get_col(
$wpdb->prepare( $query, $usernames )
);
return $user_ids;
}
/**
* Only show the My Pledges menu to users who are logged in.
*