Log: Record when contributors are added to a pledge

This also updates the function that creates new contributor posts. It
replaces `create_new_contributor` with `add_pledge_contributors`. This
way we can capture a batch of contributor additions all in one event by
collecting all the `wp_insert_post` results in an array and including
it as an action parameter.
This commit is contained in:
Corey McKrill 2019-10-28 12:26:18 -07:00
parent a41ecd7665
commit 461278f525
No known key found for this signature in database
GPG key ID: C2C0746F7BF17E38
3 changed files with 55 additions and 15 deletions

View file

@ -123,22 +123,40 @@ function populate_list_table_columns( $column, $post_id ) {
}
/**
* Create a contributor post as a child of a pledge post.
* Add one or more contributors to a pledge.
*
* @param string $wporg_username
* @param int $pledge_id
* Note that this does not validate whether a contributor's wporg username exists in the system.
*
* @return int|WP_Error Post ID on success. Otherwise WP_Error.
* @param int $pledge_id The post ID of the pledge.
* @param array $contributors Array of contributor wporg usernames.
*
* @return void
*/
function create_new_contributor( $wporg_username, $pledge_id ) {
$args = array(
'post_type' => CPT_ID,
'post_title' => sanitize_user( $wporg_username ),
'post_parent' => $pledge_id,
'post_status' => 'pending',
);
function add_pledge_contributors( $pledge_id, $contributors ) {
$results = array();
return wp_insert_post( $args, true );
foreach ( $contributors as $wporg_username ) {
$args = array(
'post_type' => CPT_ID,
'post_title' => sanitize_user( $wporg_username ),
'post_parent' => $pledge_id,
'post_status' => 'pending',
);
$result = wp_insert_post( $args, true );
$results[ $wporg_username ] = ( is_wp_error( $result ) ) ? $result->get_error_code() : $result;
}
/**
* Action: Fires when one or more contributors are added to a pledge.
*
* @param int $pledge_id The post ID of the pledge.
* @param array $contributors Array of contributor wporg usernames.
* @param array $results Associative array, key is wporg username, value is post ID on success,
* or an error code on failure.
*/
do_action( FiveForTheFuture\PREFIX . '_add_pledge_contributors', $pledge_id, $contributors, $results );
}
/**

View file

@ -90,9 +90,7 @@ function process_form_new() {
return $new_pledge_id;
}
foreach ( $contributors as $wporg_username ) {
Contributor\create_new_contributor( $wporg_username, $new_pledge_id );
}
Contributor\add_pledge_contributors( $new_pledge_id, $contributors );
// Attach logo to the pledge.
wp_update_post( array(

View file

@ -17,6 +17,7 @@ add_action( 'save_post_' . Pledge\CPT_ID, __NAMESPACE__ . '\capture_save_post',
add_action( 'updated_postmeta', __NAMESPACE__ . '\capture_updated_postmeta', 99, 4 );
add_action( 'added_post_meta', __NAMESPACE__ . '\capture_added_post_meta', 99, 4 );
add_action( 'transition_post_status', __NAMESPACE__ . '\capture_transition_post_status', 99, 3 );
add_action( FiveForTheFuture\PREFIX . '_add_pledge_contributors', __NAMESPACE__ . '\capture_add_pledge_contributors', 99, 3 );
/**
* Adds a meta box for the log on the custom post type.
@ -234,3 +235,26 @@ function capture_transition_post_status( $new_status, $old_status, WP_Post $post
get_current_user_id()
);
}
/**
* Record a log for the event of contributors being added to a pledge.
*
* @param int $pledge_id The post ID of the pledge.
* @param array $contributors Array of contributor wporg usernames.
* @param array $results Associative array, key is wporg username, value is post ID on success,
* or an error code on failure.
*
* @return void
*/
function capture_add_pledge_contributors( $pledge_id, $contributors, $results ) {
add_log_entry(
$pledge_id,
'contributors_added',
sprintf(
'Contributors added: <code>%s</code>',
implode( '</code>, <code>', $contributors )
),
$results,
get_current_user_id()
);
}