From a545168ef25ef7a8309431329c8ed8920d9d6e9f Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Thu, 17 Mar 2022 15:07:30 -0700 Subject: [PATCH] Contributors: Prevent adding to a pledge twice. This syncs `r17275-dotorg` to the canonical Git repo. --- plugins/wporg-5ftf/includes/contributor.php | 35 ++++++++++++++++++--- plugins/wporg-5ftf/includes/endpoints.php | 2 +- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index ec0ae88..0008059 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -455,16 +455,27 @@ function process_my_pledges_form() { * Ensure each item in a list of usernames is valid and corresponds to a user. * * @param string $contributors A comma-separated list of username strings. + * @param int $pledge_id Optional. The ID of an existing pledge post that contributors are being added to. * * @return array|WP_Error An array of sanitized wporg usernames on success. Otherwise WP_Error. */ -function parse_contributors( $contributors ) { +function parse_contributors( $contributors, $pledge_id = null ) { $invalid_contributors = array(); + $duplicate_contributors = array(); $sanitized_contributors = array(); $contributors = str_replace( '@', '', $contributors ); $contributors = explode( ',', $contributors ); + $existing_usernames = array(); + if ( $pledge_id ) { + $pledge_contributors = get_pledge_contributors( $pledge_id, 'all' ); + $existing_usernames = wp_list_pluck( + $pledge_contributors['publish'] + $pledge_contributors['pending'], + 'post_title' + ); + } + foreach ( $contributors as $wporg_username ) { $sanitized_username = sanitize_user( $wporg_username ); $user = get_user_by( 'login', $sanitized_username ); @@ -474,16 +485,21 @@ function parse_contributors( $contributors ) { } if ( $user instanceof WP_User ) { + if ( in_array( $user->user_login, $existing_usernames, true ) ) { + $duplicate_contributors[] = $user->user_login; + continue; + } + $sanitized_contributors[] = $user->user_login; } else { $invalid_contributors[] = $wporg_username; } } - if ( ! empty( $invalid_contributors ) ) { - /* translators: Used between sponsor names in a list, there is a space after the comma. */ - $item_separator = _x( ', ', 'list item separator', 'wporg-5ftf' ); + /* translators: Used between sponsor names in a list, there is a space after the comma. */ + $item_separator = _x( ', ', 'list item separator', 'wporg-5ftf' ); + if ( ! empty( $invalid_contributors ) ) { return new WP_Error( 'invalid_contributor', sprintf( @@ -494,6 +510,17 @@ function parse_contributors( $contributors ) { ); } + if ( ! empty( $duplicate_contributors ) ) { + return new WP_Error( + 'duplicate_contributor', + sprintf( + /* translators: %s is a list of usernames. */ + __( 'The following contributor usernames are already associated with this pledge: %s', 'wporg-5ftf' ), + implode( $item_separator, $duplicate_contributors ) + ) + ); + } + if ( empty( $sanitized_contributors ) ) { return new WP_Error( 'contributor_required', diff --git a/plugins/wporg-5ftf/includes/endpoints.php b/plugins/wporg-5ftf/includes/endpoints.php index e6dea41..f51601c 100644 --- a/plugins/wporg-5ftf/includes/endpoints.php +++ b/plugins/wporg-5ftf/includes/endpoints.php @@ -55,7 +55,7 @@ function manage_contributors_handler() { case 'add-contributor': $pledge = get_post( $pledge_id ); - $new_contributors = Contributor\parse_contributors( $_POST['contributors'] ); + $new_contributors = Contributor\parse_contributors( $_POST['contributors'], $pledge->ID ); if ( is_wp_error( $new_contributors ) ) { wp_die( wp_json_encode( array( 'success' => false,