My Pledges: Mark up page with live data.

See #56
This commit is contained in:
Ian Dunn 2019-10-29 12:45:53 -07:00
parent 5c5ae83287
commit 858d70b045
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB
3 changed files with 115 additions and 46 deletions

View file

@ -4,6 +4,15 @@ namespace WordPressDotOrg\FiveForTheFuture\XProfile;
use WordPressDotOrg\FiveForTheFuture\Contributor;
use wpdb;
/*
* The IDs of the xprofile fields we need. Better to use the numerical IDs than the field labels,
* because those are more likely to change.
*/
const FIELD_IDS = array(
'hours_per_week' => 29,
'team_names' => 30,
);
defined( 'WPINC' ) || die();
/**
@ -20,13 +29,6 @@ defined( 'WPINC' ) || die();
function get_xprofile_contribution_data( array $user_ids ) {
global $wpdb;
// The IDs of the xprofile fields we need. Better to use the numerical IDs than the field labels,
// because those are more likely to change.
$field_ids = array(
29, // Hours per week.
30, // Teams, the value of this field is serialized in the database.
);
$sql = $wpdb->prepare(
'
SELECT user_id, field_id, value
@ -35,7 +37,7 @@ function get_xprofile_contribution_data( array $user_ids ) {
AND field_id IN ( %2$s )
',
implode( ', ', array_map( 'absint', $user_ids ) ),
implode( ', ', array_map( 'absint', $field_ids ) )
implode( ', ', array_map( 'absint', array_values( FIELD_IDS ) ) )
);
return $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL -- prepare called above.
@ -87,3 +89,30 @@ function get_aggregate_contributor_data_for_pledge( $pledge_id ) {
return $aggregate_data;
}
/**
* Fetch the profile data for a specific user.
*
* @param int $user_id
*
* @return array
*/
function get_contributor_user_data( $user_id ) {
$formatted_data = array();
$raw_data = get_xprofile_contribution_data( array( $user_id ) );
foreach ( $raw_data as $datum ) {
$key = array_search( $datum['field_id'], FIELD_IDS );
switch ( $key ) {
case 'hours_per_week':
$formatted_data[ $key ] = absint( $datum['value'] );
break;
case 'team_names':
$formatted_data[ $key ] = maybe_unserialize( $datum['value'] );
}
}
return $formatted_data;
}