Contributors: Prevent adding to a pledge twice.

This syncs `r17275-dotorg` to the canonical Git repo.
This commit is contained in:
Corey McKrill 2022-03-17 15:07:30 -07:00 committed by Ian Dunn
parent 5c0b79fa86
commit a545168ef2
No known key found for this signature in database
GPG key ID: 99B971B50343CBCB
2 changed files with 32 additions and 5 deletions

View file

@ -455,16 +455,27 @@ function process_my_pledges_form() {
* Ensure each item in a list of usernames is valid and corresponds to a user. * 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 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. * @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(); $invalid_contributors = array();
$duplicate_contributors = array();
$sanitized_contributors = array(); $sanitized_contributors = array();
$contributors = str_replace( '@', '', $contributors ); $contributors = str_replace( '@', '', $contributors );
$contributors = explode( ',', $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 ) { foreach ( $contributors as $wporg_username ) {
$sanitized_username = sanitize_user( $wporg_username ); $sanitized_username = sanitize_user( $wporg_username );
$user = get_user_by( 'login', $sanitized_username ); $user = get_user_by( 'login', $sanitized_username );
@ -474,16 +485,21 @@ function parse_contributors( $contributors ) {
} }
if ( $user instanceof WP_User ) { 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; $sanitized_contributors[] = $user->user_login;
} else { } else {
$invalid_contributors[] = $wporg_username; $invalid_contributors[] = $wporg_username;
} }
} }
if ( ! empty( $invalid_contributors ) ) {
/* translators: Used between sponsor names in a list, there is a space after the comma. */ /* translators: Used between sponsor names in a list, there is a space after the comma. */
$item_separator = _x( ', ', 'list item separator', 'wporg-5ftf' ); $item_separator = _x( ', ', 'list item separator', 'wporg-5ftf' );
if ( ! empty( $invalid_contributors ) ) {
return new WP_Error( return new WP_Error(
'invalid_contributor', 'invalid_contributor',
sprintf( 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 ) ) { if ( empty( $sanitized_contributors ) ) {
return new WP_Error( return new WP_Error(
'contributor_required', 'contributor_required',

View file

@ -55,7 +55,7 @@ function manage_contributors_handler() {
case 'add-contributor': case 'add-contributor':
$pledge = get_post( $pledge_id ); $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 ) ) { if ( is_wp_error( $new_contributors ) ) {
wp_die( wp_json_encode( array( wp_die( wp_json_encode( array(
'success' => false, 'success' => false,