diff --git a/plugins/wporg-5ftf/includes/pledge-meta.php b/plugins/wporg-5ftf/includes/pledge-meta.php index f37868a..9f8a4a9 100755 --- a/plugins/wporg-5ftf/includes/pledge-meta.php +++ b/plugins/wporg-5ftf/includes/pledge-meta.php @@ -6,9 +6,7 @@ namespace WordPressDotOrg\FiveForTheFuture\PledgeMeta; use WordPressDotOrg\FiveForTheFuture; -use WordPressDotOrg\FiveForTheFuture\Pledge; -use WordPressDotOrg\FiveForTheFuture\PledgeForm; -use WordPressDotOrg\FiveForTheFuture\Contributor; +use WordPressDotOrg\FiveForTheFuture\{ Pledge, PledgeForm, Contributor, XProfile }; use WP_Post, WP_Error; defined( 'WPINC' ) || die(); @@ -19,7 +17,7 @@ add_action( 'init', __NAMESPACE__ . '\register_pledge_meta' ); add_action( 'admin_init', __NAMESPACE__ . '\add_meta_boxes' ); add_action( 'save_post', __NAMESPACE__ . '\save_pledge', 10, 2 ); 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. add_action( 'updated_postmeta', __NAMESPACE__ . '\update_generated_meta', 10, 4 ); @@ -76,6 +74,11 @@ function get_pledge_meta_config( $context = 'all' ) { 'sanitize_callback' => 'absint', 'show_in_rest' => false, ), + 'pledge-total-hours' => array( + 'single' => true, + 'sanitize_callback' => 'absint', + 'show_in_rest' => false, + ), ); 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 * removed from a pledge. @@ -329,7 +334,7 @@ function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value ) * * @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 ) ) { return; } @@ -341,9 +346,11 @@ function update_confirmed_contributor_count( $new_status, $old_status, WP_Post $ $pledge = get_post( $post->post_parent ); 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 ) ); } } diff --git a/plugins/wporg-5ftf/includes/pledge.php b/plugins/wporg-5ftf/includes/pledge.php index dfd390b..5ab4148 100755 --- a/plugins/wporg-5ftf/includes/pledge.php +++ b/plugins/wporg-5ftf/includes/pledge.php @@ -225,6 +225,7 @@ function filter_query( $query ) { } $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. $meta_queries = (array) $query->get( 'meta_query' ); @@ -246,14 +247,15 @@ function filter_query( $query ) { // Archives should only show pledges with contributors. $query->set( 'meta_query', $meta_queries ); $order = isset( $_GET['order'] ) ? $_GET['order'] : ''; + switch ( $order ) { case 'alphabetical': $query->set( 'orderby', 'name' ); $query->set( 'order', 'ASC' ); break; - case 'contributors': - $query->set( 'meta_key', $contributor_count_key ); + case 'hours': + $query->set( 'meta_key', $hours_count_key ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'order', 'DESC' ); break; diff --git a/plugins/wporg-5ftf/includes/xprofile.php b/plugins/wporg-5ftf/includes/xprofile.php index 946e46a..2f31a6e 100644 --- a/plugins/wporg-5ftf/includes/xprofile.php +++ b/plugins/wporg-5ftf/includes/xprofile.php @@ -76,7 +76,7 @@ function get_aggregate_contributor_data_for_pledge( $pledge_id ) { break; case 30: // Teams. - $value = maybe_unserialize( $item['value'] ); + $value = (array) maybe_unserialize( $item['value'] ); $carry['teams'] = array_merge( $carry['teams'], $value ); break; } diff --git a/themes/wporg-5ftf/archive-5ftf_pledge.php b/themes/wporg-5ftf/archive-5ftf_pledge.php index 087c7f4..2f76727 100644 --- a/themes/wporg-5ftf/archive-5ftf_pledge.php +++ b/themes/wporg-5ftf/archive-5ftf_pledge.php @@ -37,8 +37,8 @@ get_header(); ?> - diff --git a/themes/wporg-5ftf/template-parts/content-5ftf_pledge.php b/themes/wporg-5ftf/template-parts/content-5ftf_pledge.php index 0f8e565..1d49a13 100644 --- a/themes/wporg-5ftf/template-parts/content-5ftf_pledge.php +++ b/themes/wporg-5ftf/template-parts/content-5ftf_pledge.php @@ -5,9 +5,9 @@ namespace WordPressdotorg\Five_for_the_Future\Theme; -use WordPressDotOrg\FiveForTheFuture\Contributor; -use WordPressDotOrg\FiveForTheFuture\PledgeMeta; +use WordPressDotOrg\FiveForTheFuture\{Contributor, PledgeMeta }; +$pledge = get_post(); $data = array(); 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() ); -$count = count( $contributors ); $allowed_html = array_merge( wp_kses_allowed_html( 'data' ), @@ -36,12 +35,14 @@ $content = apply_filters( 'the_content', $data['org-description'] ); $content = strip_tags( $content ); $content = wp_trim_words( $content, 55, $more ); +$total_hours = $pledge->{ PledgeMeta\META_PREFIX . 'pledge-total-hours' }; + $contributor_title = sprintf( 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() ), - intval( $count ) + intval( $total_hours ) ); ?>