mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-04-21 02:23:43 +03:00
Contributors: Prevent adding to a pledge twice.
This syncs `r17275-dotorg` to the canonical Git repo.
This commit is contained in:
parent
5c0b79fa86
commit
a545168ef2
|
@ -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',
|
||||||
|
|
|
@ -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,
|
||||||
|
|
Loading…
Reference in a new issue