From 461278f5255c914eafc957851a60e36c4fe9d41c Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Mon, 28 Oct 2019 12:26:18 -0700 Subject: [PATCH] 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. --- plugins/wporg-5ftf/includes/contributor.php | 42 +++++++++++++++------ plugins/wporg-5ftf/includes/pledge-form.php | 4 +- plugins/wporg-5ftf/includes/pledge-log.php | 24 ++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index e4ba7c6..875dc5f 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -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 ); } /** diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index 3173efe..bd47193 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -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( diff --git a/plugins/wporg-5ftf/includes/pledge-log.php b/plugins/wporg-5ftf/includes/pledge-log.php index 6a88a76..3b569e6 100644 --- a/plugins/wporg-5ftf/includes/pledge-log.php +++ b/plugins/wporg-5ftf/includes/pledge-log.php @@ -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: %s', + implode( ', ', $contributors ) + ), + $results, + get_current_user_id() + ); +}