Pledge Meta: Add cron to update all cached pledge data.

Fixes #71
This commit is contained in:
Ian Dunn 2019-10-31 21:41:35 -05:00
parent 87eb8ec43a
commit 2686c9873b
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB

View file

@ -14,10 +14,12 @@ defined( 'WPINC' ) || die();
const META_PREFIX = FiveForTheFuture\PREFIX . '_';
add_action( 'init', __NAMESPACE__ . '\register_pledge_meta' );
add_action( 'init', __NAMESPACE__ . '\schedule_cron_jobs' );
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_cached_pledge_data', 10, 3 );
add_action( 'transition_post_status', __NAMESPACE__ . '\maybe_update_cached_pledge_data', 10, 3 );
add_action( 'update_all_cached_pledged_data', __NAMESPACE__. '\update_all_cached_pledged_data' );
// 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 );
@ -126,6 +128,39 @@ function register_pledge_meta() {
}
}
/**
* Schedule cron jobs.
*
* This needs to run on the `init` action, because Cavalcade isn't fully loaded before that, and events
* wouldn't be scheduled.
*
* @see https://dotorg.trac.wordpress.org/changeset/15351/
*/
function schedule_cron_jobs() {
if ( ! wp_next_scheduled( 'update_all_cached_pledged_data' ) ) {
wp_schedule_event( time(), 'hourly', 'update_all_cached_pledged_data' );
}
}
/**
* Regularly update the cached data for all pledges.
*
* Outside of this cron job, it's only updated when a contributor post changes status, but it's possible for
* a contributor to edit their profile's # of hours at any time. If we didn't regularly check and update it,
* then it could be permanently out of date.
*/
function update_all_cached_pledged_data() {
$pledges = get_posts( array(
'post_type' => Pledge\CPT_ID,
'post_status' => 'publish',
'numberposts' => 1000,
) );
foreach ( $pledges as $pledge ) {
update_cached_pledge_data( $pledge->ID );
}
}
/**
* Adds meta boxes for the custom post type.
*
@ -321,9 +356,7 @@ function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value )
}
/**
* 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.
* Update the cached pledge data when a contributor post changes statuses.
*
* Note that contributor posts should always be trashed instead of deleted completely when a contributor is
* removed from a pledge.
@ -334,7 +367,7 @@ function update_generated_meta( $meta_id, $object_id, $meta_key, $_meta_value )
*
* @return void
*/
function update_cached_pledge_data( $new_status, $old_status, WP_Post $post ) {
function maybe_update_cached_pledge_data( $new_status, $old_status, WP_Post $post ) {
if ( Contributor\CPT_ID !== get_post_type( $post ) ) {
return;
}
@ -346,14 +379,24 @@ function update_cached_pledge_data( $new_status, $old_status, WP_Post $post ) {
$pledge = get_post( $post->post_parent );
if ( $pledge instanceof WP_Post ) {
$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_cached_pledge_data( $pledge->ID );
}
}
/**
* Update the cached data for the given pledge.
*
* This is saved so that it can be easily queried against, and also to make stats calculations easier.
*
* @param $pledge_id
*/
function update_cached_pledge_data( $pledge_id ) {
$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'] );
}
/**
* Check that an array contains values for all required keys.
*