mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-04-19 01:43:44 +03:00
Track number of sponsored contributors and hours.
This commit is contained in:
parent
7464a9e585
commit
c1ce72d447
|
@ -10,6 +10,10 @@ use WordPressDotOrg\FiveForTheFuture;
|
|||
use WordPressDotOrg\FiveForTheFuture\{ Contributor, Pledge, XProfile };
|
||||
use WP_Query;
|
||||
|
||||
use function WordPressDotOrg\FiveForTheFuture\XProfile\{
|
||||
get_xprofile_contribution_data, prepare_xprofile_contribution_data
|
||||
};
|
||||
|
||||
use const WordPressDotOrg\FiveForTheFuture\PREFIX;
|
||||
|
||||
defined( 'WPINC' ) || die();
|
||||
|
@ -72,6 +76,8 @@ function record_snapshot() {
|
|||
|
||||
add_post_meta( $post_id, PREFIX . '_total_pledged_hours', $stats['confirmed_hours'] );
|
||||
add_post_meta( $post_id, PREFIX . '_total_pledged_contributors', $stats['confirmed_contributors'] );
|
||||
add_post_meta( $post_id, PREFIX . '_total_sponsored_hours', $stats['confirmed_sponsored_hours'] );
|
||||
add_post_meta( $post_id, PREFIX . '_total_sponsored_contributors', $stats['confirmed_sponsored_contributors'] );
|
||||
add_post_meta( $post_id, PREFIX . '_total_pledged_companies', $stats['confirmed_pledges'] );
|
||||
add_post_meta( $post_id, PREFIX . '_total_pledged_team_contributors', $stats['confirmed_team_contributors'] );
|
||||
}
|
||||
|
@ -116,13 +122,24 @@ function get_snapshot_data() {
|
|||
*/
|
||||
$confirmed_user_ids = array_unique( Contributor\get_contributor_user_ids( $confirmed_contributors ) );
|
||||
$snapshot_data['confirmed_contributors'] = count( $confirmed_user_ids );
|
||||
$snapshot_data['confirmed_sponsored_contributors'] = 0;
|
||||
|
||||
$contributors_profile_data = XProfile\get_xprofile_contribution_data( $confirmed_user_ids );
|
||||
$contributors_profile_data = get_xprofile_contribution_data( $confirmed_user_ids );
|
||||
$prepared_profile_data = prepare_xprofile_contribution_data( $contributors_profile_data );
|
||||
|
||||
// Note: This was set before `$prepared_profile_data` was available. Refactoring to use that would simplify this.
|
||||
foreach ( $contributors_profile_data as $profile_data ) {
|
||||
switch ( (int) $profile_data['field_id'] ) {
|
||||
case XProfile\FIELD_IDS['hours_per_week']:
|
||||
$user_id = (int) $profile_data['user_id'];
|
||||
|
||||
$snapshot_data['confirmed_hours'] += absint( $profile_data['value'] );
|
||||
|
||||
if ( $prepared_profile_data[ $user_id ]['sponsored'] ) {
|
||||
$snapshot_data['confirmed_sponsored_hours'] += absint( $profile_data['value'] );
|
||||
$snapshot_data['confirmed_sponsored_contributors']++;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case XProfile\FIELD_IDS['team_names']:
|
||||
|
@ -165,6 +182,8 @@ function render_shortcode() {
|
|||
$stat_keys = array(
|
||||
PREFIX . '_total_pledged_hours',
|
||||
PREFIX . '_total_pledged_contributors',
|
||||
PREFIX . '_total_sponsored_hours',
|
||||
PREFIX . '_total_sponsored_contributors',
|
||||
PREFIX . '_total_pledged_companies',
|
||||
PREFIX . '_total_pledged_team_contributors',
|
||||
);
|
||||
|
|
|
@ -9,6 +9,7 @@ use wpdb;
|
|||
* because those are more likely to change.
|
||||
*/
|
||||
const FIELD_IDS = array(
|
||||
'sponsored' => 24,
|
||||
'hours_per_week' => 29,
|
||||
'team_names' => 30,
|
||||
);
|
||||
|
@ -18,7 +19,8 @@ defined( 'WPINC' ) || die();
|
|||
/**
|
||||
* Pull relevant data from profiles.wordpress.org.
|
||||
*
|
||||
* Note that this does not unserialize anything, it just pulls the raw values from the database table.
|
||||
* Note that this does not unserialize anything, it just pulls the raw values from the database table. If you
|
||||
* want unserialized data, use `prepare_xprofile_contribution_data()`.
|
||||
*
|
||||
* @global wpdb $wpdb
|
||||
*
|
||||
|
@ -43,6 +45,38 @@ function get_xprofile_contribution_data( array $user_ids ) {
|
|||
return $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL -- prepare called above.
|
||||
}
|
||||
|
||||
/**
|
||||
* Reindex the values by user ID, normalize it, and format it.
|
||||
*
|
||||
* This makes the data much easier to work with in many cases.
|
||||
*
|
||||
* @param array $raw_data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function prepare_xprofile_contribution_data( array $raw_data ) {
|
||||
$prepared_data = array();
|
||||
$field_keys_by_id = array_flip( FIELD_IDS );
|
||||
|
||||
foreach ( $raw_data as $datum ) {
|
||||
$user_id = $datum['user_id'];
|
||||
$field_key = $field_keys_by_id[ (int) $datum['field_id'] ];
|
||||
$field_value = maybe_unserialize( $datum['value'] );
|
||||
|
||||
if ( ! isset( $prepared_data[ $user_id ]['sponsored'] ) ) {
|
||||
$prepared_data[ $user_id ]['sponsored'] = false;
|
||||
}
|
||||
|
||||
if ( 'sponsored' === $field_key ) {
|
||||
$prepared_data[ $user_id ]['sponsored'] = 'Yes' === $field_value;
|
||||
} else {
|
||||
$prepared_data[ $user_id ][ $field_key ] = $field_value;
|
||||
}
|
||||
}
|
||||
|
||||
return $prepared_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate the raw xprofile data for all contributors linked to a given pledge.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue