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

@ -2,7 +2,7 @@
namespace WordPressDotOrg\FiveForTheFuture\Contributor; namespace WordPressDotOrg\FiveForTheFuture\Contributor;
use WordPressDotOrg\FiveForTheFuture; use WordPressDotOrg\FiveForTheFuture;
use WordPressDotOrg\FiveForTheFuture\Pledge; use WordPressDotOrg\FiveForTheFuture\{ Pledge, XProfile };
use WP_Error, WP_Post, WP_User; use WP_Error, WP_Post, WP_User;
defined( 'WPINC' ) || die(); defined( 'WPINC' ) || die();
@ -249,16 +249,25 @@ function get_contributor_user_objects( array $contributor_posts ) {
*/ */
function render_my_pledges() { function render_my_pledges() {
$user = wp_get_current_user(); $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' ) ); $pledge_url = get_permalink( get_page_by_path( 'for-organizations' ) );
$success_message = process_my_pledges_form();
$contributor_posts = get_posts( array( $contributor_posts = get_posts( array(
'title' => $user->user_login,
'post_type' => CPT_ID, 'post_type' => CPT_ID,
'post_title' => $user->user_login,
'post_status' => array( 'pending', 'publish' ), 'post_status' => array( 'pending', 'publish' ),
'numberposts' => 100, '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(); ob_start();
require FiveForTheFuture\get_views_path() . 'list-my-pledges.php'; require FiveForTheFuture\get_views_path() . 'list-my-pledges.php';
return ob_get_clean(); return ob_get_clean();

View file

@ -4,6 +4,15 @@ namespace WordPressDotOrg\FiveForTheFuture\XProfile;
use WordPressDotOrg\FiveForTheFuture\Contributor; use WordPressDotOrg\FiveForTheFuture\Contributor;
use wpdb; 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(); defined( 'WPINC' ) || die();
/** /**
@ -20,13 +29,6 @@ defined( 'WPINC' ) || die();
function get_xprofile_contribution_data( array $user_ids ) { function get_xprofile_contribution_data( array $user_ids ) {
global $wpdb; 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( $sql = $wpdb->prepare(
' '
SELECT user_id, field_id, value SELECT user_id, field_id, value
@ -35,7 +37,7 @@ function get_xprofile_contribution_data( array $user_ids ) {
AND field_id IN ( %2$s ) AND field_id IN ( %2$s )
', ',
implode( ', ', array_map( 'absint', $user_ids ) ), 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. 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; 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;
}

View file

@ -9,6 +9,8 @@ use WP_User, WP_Post;
* @var WP_Post $contributor_post * @var WP_Post $contributor_post
* @var string $success_message * @var string $success_message
* @var string $pledge_url * @var string $pledge_url
* @var array $profile_data
* @var array $confirmed_pledge_ids
*/ */
?> ?>
@ -17,7 +19,18 @@ use WP_User, WP_Post;
<?php echo get_avatar( $user->ID, 96, 'blank', "{$user->login}'s avatar" ); ?> <?php echo get_avatar( $user->ID, 96, 'blank', "{$user->login}'s avatar" ); ?>
<p>Pledged 10 hours a week across two organizations</p> <?php // todo pull from profiles. ?> <p>
<?php echo esc_html( sprintf(
_n(
'Pledged %1$s hours a week across %2$s organization',
'Pledged %1$s hours a week across %2$s organizations',
count( $confirmed_pledge_ids ),
'wporg-5ftf'
),
$profile_data['hours_per_week'],
count( $confirmed_pledge_ids )
) ); ?>
</p>
<?php if ( $success_message ) : ?> <?php if ( $success_message ) : ?>
<div class="notice notice-success notice-alt"> <div class="notice notice-success notice-alt">
@ -28,16 +41,35 @@ use WP_User, WP_Post;
<?php endif; ?> <?php endif; ?>
<?php if ( $contributor_posts ) : ?> <?php if ( $contributor_posts ) : ?>
<div class="fftf_pledges">
<?php foreach ( $contributor_posts as $contributor_post ) : ?> <?php foreach ( $contributor_posts as $contributor_post ) : ?>
<?php $pledge = get_post( $contributor_post->post_parent ); ?> <?php $pledge = get_post( $contributor_post->post_parent ); ?>
<div class="fftf_pledge">
<div class="pledge-logo-container">
<?php echo get_the_post_thumbnail( $pledge->ID, 'pledge-logo' ); ?> <?php echo get_the_post_thumbnail( $pledge->ID, 'pledge-logo' ); ?>
<?php echo esc_html( $pledge->post_title ); ?> </div>
<div class="pledge-title">
<a href="<?php echo esc_url( get_permalink( $pledge->ID ) ); ?>">
<?php echo esc_html( $pledge->post_title ); ?>
</a>
<?php if ( 'publish' === $contributor_post->post_status ) : ?>
<p>
<?php esc_html_e( sprintf(
__( 'You confirmed this pledge on %s', 'wporg-5ftf' ),
date( get_option( 'date_format' ), strtotime( $contributor_post->post_date ) )
) ); ?>
</p>
<?php endif; ?>
</div>
<div class="pledge-actions">
<form action="" method="post"> <form action="" method="post">
<input type="hidden" name="contributor_post_id" value="<?php echo esc_attr( $contributor_post->ID ); ?>" /> <input type="hidden" name="contributor_post_id" value="<?php echo esc_attr( $contributor_post->ID ); ?>" />
<p>
<?php if ( 'pending' === $contributor_post->post_status ) : ?> <?php if ( 'pending' === $contributor_post->post_status ) : ?>
<?php wp_nonce_field( 'join_decline_organization' ); ?> <?php wp_nonce_field( 'join_decline_organization' ); ?>
@ -55,10 +87,6 @@ use WP_User, WP_Post;
/> />
<?php elseif ( 'publish' === $contributor_post->post_status ) : ?> <?php elseif ( 'publish' === $contributor_post->post_status ) : ?>
<p>
You confirmed this pledge on {date}
</p>
<?php wp_nonce_field( 'leave_organization' ); ?> <?php wp_nonce_field( 'leave_organization' ); ?>
<input <input
@ -68,10 +96,13 @@ use WP_User, WP_Post;
/> />
<?php endif; ?> <?php endif; ?>
</p>
</form> </form>
</div>
</div>
<?php endforeach; ?> <?php endforeach; ?>
</div>
<?php else : ?> <?php else : ?>