Pledge List: Show hours because it's a more meaningful statistic.

This commit is contained in:
Ian Dunn 2019-10-31 16:23:57 -05:00
parent 7273057950
commit 33d3bc1933
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB
5 changed files with 28 additions and 18 deletions

View file

@ -6,9 +6,7 @@
namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta; namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta;
use WordPressDotOrg\FiveForTheFuture; use WordPressDotOrg\FiveForTheFuture;
use WordPressDotOrg\FiveForTheFuture\Pledge; use WordPressDotOrg\FiveForTheFuture\{ Pledge, PledgeForm, Contributor, XProfile };
use WordPressDotOrg\FiveForTheFuture\PledgeForm;
use WordPressDotOrg\FiveForTheFuture\Contributor;
use WP_Post, WP_Error; use WP_Post, WP_Error;
defined( 'WPINC' ) || die(); defined( 'WPINC' ) || die();
@ -19,7 +17,7 @@ add_action( 'init', __NAMESPACE__ . '\register_pledge_meta' );
add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' ); add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' );
add_action( 'save_post', __NAMESPACE__ . '\save_pledge', 10, 2 ); add_action( 'save_post', __NAMESPACE__ . '\save_pledge', 10, 2 );
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' ); add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_action( 'transition_post_status', __NAMESPACE__ . '\update_confirmed_contributor_count', 10, 3 ); add_action( 'transition_post_status', __NAMESPACE__ . '\update_cached_pledge_data', 10, 3 );
// Both hooks must be used because `updated` doesn't fire if the post meta didn't previously exist. // Both hooks must be used because `updated` doesn't fire if the post meta didn't previously exist.
add_action( 'updated_postmeta', __NAMESPACE__ . '\update_generated_meta', 10, 4 ); add_action( 'updated_postmeta', __NAMESPACE__ . '\update_generated_meta', 10, 4 );
@ -76,6 +74,11 @@ function get_pledge_meta_config( $context = 'all' ) {
'sanitize_callback' => 'absint', 'sanitize_callback' => 'absint',
'show_in_rest' => false, 'show_in_rest' => false,
), ),
'pledge-total-hours' => array(
'single' => true,
'sanitize_callback' => 'absint',
'show_in_rest' => false,
),
); );
switch ( $context ) { switch ( $context ) {
@ -318,7 +321,9 @@ function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value )
} }
/** /**
* Update the cached count of confirmed contributors for a pledge when a contributor post changes statuses. * Update cached pledge data when a contributor post changes statuses.
*
* This is saved so that it can be easily queried against, and also to make stats calculations easier.
* *
* Note that contributor posts should always be trashed instead of deleted completely when a contributor is * Note that contributor posts should always be trashed instead of deleted completely when a contributor is
* removed from a pledge. * removed from a pledge.
@ -329,7 +334,7 @@ function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value )
* *
* @return void * @return void
*/ */
function update_confirmed_contributor_count( $new_status, $old_status, WP_Post $post ) { function update_cached_pledge_data( $new_status, $old_status, WP_Post $post ) {
if ( Contributor\CPT_ID !== get_post_type( $post ) ) { if ( Contributor\CPT_ID !== get_post_type( $post ) ) {
return; return;
} }
@ -341,9 +346,11 @@ function update_confirmed_contributor_count( $new_status, $old_status, WP_Post $
$pledge = get_post( $post->post_parent ); $pledge = get_post( $post->post_parent );
if ( $pledge instanceof WP_Post ) { if ( $pledge instanceof WP_Post ) {
$confirmed_contributors = Contributor\get_pledge_contributors( $pledge->ID, 'publish' ); $pledge_data = XProfile\get_aggregate_contributor_data_for_pledge( $pledge->ID );
update_post_meta( $pledge->ID, META_PREFIX . 'pledge-confirmed-contributors', $pledge_data['contributors'] );
update_post_meta( $pledge->ID, META_PREFIX . 'pledge-total-hours', $pledge_data['hours'] );
update_post_meta( $pledge->ID, META_PREFIX . 'pledge-confirmed-contributors', count( $confirmed_contributors ) );
} }
} }

View file

@ -225,6 +225,7 @@ function filter_query( $query ) {
} }
$contributor_count_key = META_PREFIX . 'pledge-confirmed-contributors'; $contributor_count_key = META_PREFIX . 'pledge-confirmed-contributors';
$hours_count_key = META_PREFIX . 'pledge-total-hours';
// Set up meta queries to include the "valid pledge" check, added to both search and pledge archive requests. // Set up meta queries to include the "valid pledge" check, added to both search and pledge archive requests.
$meta_queries = (array) $query->get( 'meta_query' ); $meta_queries = (array) $query->get( 'meta_query' );
@ -246,14 +247,15 @@ function filter_query( $query ) {
// Archives should only show pledges with contributors. // Archives should only show pledges with contributors.
$query->set( 'meta_query', $meta_queries ); $query->set( 'meta_query', $meta_queries );
$order = isset( $_GET['order'] ) ? $_GET['order'] : ''; $order = isset( $_GET['order'] ) ? $_GET['order'] : '';
switch ( $order ) { switch ( $order ) {
case 'alphabetical': case 'alphabetical':
$query->set( 'orderby', 'name' ); $query->set( 'orderby', 'name' );
$query->set( 'order', 'ASC' ); $query->set( 'order', 'ASC' );
break; break;
case 'contributors': case 'hours':
$query->set( 'meta_key', $contributor_count_key ); $query->set( 'meta_key', $hours_count_key );
$query->set( 'orderby', 'meta_value_num' ); $query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' ); $query->set( 'order', 'DESC' );
break; break;

View file

@ -76,7 +76,7 @@ function get_aggregate_contributor_data_for_pledge( $pledge_id ) {
break; break;
case 30: // Teams. case 30: // Teams.
$value = maybe_unserialize( $item['value'] ); $value = (array) maybe_unserialize( $item['value'] );
$carry['teams'] = array_merge( $carry['teams'], $value ); $carry['teams'] = array_merge( $carry['teams'], $value );
break; break;
} }

View file

@ -37,8 +37,8 @@ get_header(); ?>
<option value="alphabetical" <?php selected( $pledge_order, 'alphabetical' ); ?>> <option value="alphabetical" <?php selected( $pledge_order, 'alphabetical' ); ?>>
<?php esc_html_e( 'Alphabetical', 'wordpressorg' ); ?> <?php esc_html_e( 'Alphabetical', 'wordpressorg' ); ?>
</option> </option>
<option value="contributors" <?php selected( $pledge_order, 'contributors' ); ?>> <option value="hours" <?php selected( $pledge_order, 'hours' ); ?>>
<?php esc_html_e( 'Total Contributors', 'wordpressorg' ); ?> <?php esc_html_e( 'Total Hours', 'wordpressorg' ); ?>
</option> </option>
</select> </select>
<span class="screen-reader-text"> <span class="screen-reader-text">

View file

@ -5,9 +5,9 @@
namespace WordPressdotorg\Five_for_the_Future\Theme; namespace WordPressdotorg\Five_for_the_Future\Theme;
use WordPressDotOrg\FiveForTheFuture\Contributor; use WordPressDotOrg\FiveForTheFuture\{Contributor, PledgeMeta };
use WordPressDotOrg\FiveForTheFuture\PledgeMeta;
$pledge = get_post();
$data = array(); $data = array();
foreach ( PledgeMeta\get_pledge_meta_config() as $key => $config ) { foreach ( PledgeMeta\get_pledge_meta_config() as $key => $config ) {
@ -15,7 +15,6 @@ foreach ( PledgeMeta\get_pledge_meta_config() as $key => $config ) {
} }
$contributors = Contributor\get_pledge_contributors( get_the_ID() ); $contributors = Contributor\get_pledge_contributors( get_the_ID() );
$count = count( $contributors );
$allowed_html = array_merge( $allowed_html = array_merge(
wp_kses_allowed_html( 'data' ), wp_kses_allowed_html( 'data' ),
@ -36,12 +35,14 @@ $content = apply_filters( 'the_content', $data['org-description'] );
$content = strip_tags( $content ); $content = strip_tags( $content );
$content = wp_trim_words( $content, 55, $more ); $content = wp_trim_words( $content, 55, $more );
$total_hours = $pledge->{ PledgeMeta\META_PREFIX . 'pledge-total-hours' };
$contributor_title = sprintf( $contributor_title = sprintf(
esc_html( esc_html(
_n( '%1$s has pledged %2$d contributor', '%1$s has pledged %2$d contributors', $count, 'wordpressorg' ) _n( '%1$s has pledged %2$d hour', '%1$s has pledged %2$d hours', $total_hours, 'wordpressorg' )
), ),
wp_kses_post( get_the_title() ), wp_kses_post( get_the_title() ),
intval( $count ) intval( $total_hours )
); );
?> ?>