From 858d70b045b57bd1c585e4708b9dedb03357b247 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Tue, 29 Oct 2019 12:45:53 -0700 Subject: [PATCH] My Pledges: Mark up page with live data. See #56 --- plugins/wporg-5ftf/includes/contributor.php | 15 ++- plugins/wporg-5ftf/includes/xprofile.php | 45 +++++++-- plugins/wporg-5ftf/views/list-my-pledges.php | 101 ++++++++++++------- 3 files changed, 115 insertions(+), 46 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 1e05955..25c684c 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -2,7 +2,7 @@ namespace WordPressDotOrg\FiveForTheFuture\Contributor; use WordPressDotOrg\FiveForTheFuture; -use WordPressDotOrg\FiveForTheFuture\Pledge; +use WordPressDotOrg\FiveForTheFuture\{ Pledge, XProfile }; use WP_Error, WP_Post, WP_User; defined( 'WPINC' ) || die(); @@ -249,16 +249,25 @@ function get_contributor_user_objects( array $contributor_posts ) { */ function render_my_pledges() { $user = wp_get_current_user(); - $success_message = process_my_pledges_form(); + $profile_data = XProfile\get_contributor_user_data( $user->ID ); $pledge_url = get_permalink( get_page_by_path( 'for-organizations' ) ); + $success_message = process_my_pledges_form(); $contributor_posts = get_posts( array( + 'title' => $user->user_login, 'post_type' => CPT_ID, - 'post_title' => $user->user_login, 'post_status' => array( 'pending', 'publish' ), 'numberposts' => 100, ) ); + $confirmed_pledge_ids = array_reduce( $contributor_posts, function( $carry, $post ) { + if ( 'publish' === $post->post_status ) { + $carry[] = $post->ID; + } + + return $carry; + }, array() ); + ob_start(); require FiveForTheFuture\get_views_path() . 'list-my-pledges.php'; return ob_get_clean(); diff --git a/plugins/wporg-5ftf/includes/xprofile.php b/plugins/wporg-5ftf/includes/xprofile.php index 56d981e..237726d 100644 --- a/plugins/wporg-5ftf/includes/xprofile.php +++ b/plugins/wporg-5ftf/includes/xprofile.php @@ -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; +} diff --git a/plugins/wporg-5ftf/views/list-my-pledges.php b/plugins/wporg-5ftf/views/list-my-pledges.php index 3e76bd0..b5cc1ab 100644 --- a/plugins/wporg-5ftf/views/list-my-pledges.php +++ b/plugins/wporg-5ftf/views/list-my-pledges.php @@ -9,6 +9,8 @@ use WP_User, WP_Post; * @var WP_Post $contributor_post * @var string $success_message * @var string $pledge_url + * @var array $profile_data + * @var array $confirmed_pledge_ids */ ?> @@ -17,7 +19,18 @@ use WP_User, WP_Post; ID, 96, 'blank', "{$user->login}'s avatar" ); ?> -

Pledged 10 hours a week across two organizations

+

+ +

@@ -28,50 +41,68 @@ use WP_User, WP_Post; - - post_parent ); ?> - ID, 'pledge-logo' ); ?> - post_title ); ?> +
+ + post_parent ); ?> -
- +
+
+ ID, 'pledge-logo' ); ?> +
-

- post_status ) : ?> - +

+ + post_title ); ?> + - + post_status ) : ?> +

+ post_date ) ) + ) ); ?> +

+ +
- +
+ + - post_status ) : ?> -

- You confirmed this pledge on {date} -

+ post_status ) : ?> + - + - + - -

- + post_status ) : ?> + - + + + + + +
+
+ + +