diff --git a/plugins/wporg-5ftf/includes/contributor.php b/plugins/wporg-5ftf/includes/contributor.php index 8d9b618..8bc5177 100644 --- a/plugins/wporg-5ftf/includes/contributor.php +++ b/plugins/wporg-5ftf/includes/contributor.php @@ -402,3 +402,58 @@ function process_my_pledges_form() { return $message; } + +/** + * 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. + * + * @return array|WP_Error An array of sanitized wporg usernames on success. Otherwise WP_Error. + */ +function parse_contributors( $contributors ) { + $invalid_contributors = array(); + $sanitized_contributors = array(); + + $contributors = str_replace( '@', '', $contributors ); + $contributors = explode( ',', $contributors ); + + foreach ( $contributors as $wporg_username ) { + $sanitized_username = sanitize_user( $wporg_username ); + $user = get_user_by( 'login', $sanitized_username ); + + if ( ! $user instanceof WP_User ) { + $user = get_user_by( 'slug', $sanitized_username ); + } + + if ( $user instanceof WP_User ) { + $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' ); + + return new WP_Error( + 'invalid_contributor', + sprintf( + /* translators: %s is a list of usernames. */ + __( 'The following contributor usernames are not valid: %s', 'wporg-5ftf' ), + implode( $item_separator, $invalid_contributors ) + ) + ); + } + + if ( empty( $sanitized_contributors ) ) { + return new WP_Error( + 'contributor_required', + __( 'The pledge must have at least one contributor username.', 'wporg-5ftf' ) + ); + } + + $sanitized_contributors = array_unique( $sanitized_contributors ); + + return $sanitized_contributors; +} diff --git a/plugins/wporg-5ftf/includes/endpoints.php b/plugins/wporg-5ftf/includes/endpoints.php index 427059a..ffe9c85 100644 --- a/plugins/wporg-5ftf/includes/endpoints.php +++ b/plugins/wporg-5ftf/includes/endpoints.php @@ -5,7 +5,7 @@ namespace WordPressDotOrg\FiveForTheFuture\Endpoints; -use WordPressDotOrg\FiveForTheFuture\{ Auth, Contributor, Email, PledgeForm }; +use WordPressDotOrg\FiveForTheFuture\{ Auth, Contributor, Email }; use const WordPressDotOrg\FiveForTheFuture\PledgeMeta\META_PREFIX; add_action( 'wp_ajax_manage-contributors', __NAMESPACE__ . '\manage_contributors_handler' ); @@ -54,7 +54,7 @@ function manage_contributors_handler() { case 'add-contributor': $pledge = get_post( $pledge_id ); - $new_contributors = PledgeForm\parse_contributors( $_POST['contributors'] ); + $new_contributors = Contributor\parse_contributors( $_POST['contributors'] ); if ( is_wp_error( $new_contributors ) ) { wp_die( wp_json_encode( [ 'success' => false, diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index a7cf3c9..9fd1170 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -70,7 +70,7 @@ function process_form_new() { return $has_error; } - $contributors = parse_contributors( $submission['pledge-contributors'] ); + $contributors = Contributor\parse_contributors( $submission['pledge-contributors'] ); if ( is_wp_error( $contributors ) ) { return $contributors; } @@ -219,61 +219,6 @@ function get_form_submission() { return $result; } -/** - * 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. - * - * @return array|WP_Error An array of sanitized wporg usernames on success. Otherwise WP_Error. - */ -function parse_contributors( $contributors ) { - $invalid_contributors = array(); - $sanitized_contributors = array(); - - $contributors = str_replace( '@', '', $contributors ); - $contributors = explode( ',', $contributors ); - - foreach ( $contributors as $wporg_username ) { - $sanitized_username = sanitize_user( $wporg_username ); - $user = get_user_by( 'login', $sanitized_username ); - - if ( ! $user instanceof WP_User ) { - $user = get_user_by( 'slug', $sanitized_username ); - } - - if ( $user instanceof WP_User ) { - $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' ); - - return new WP_Error( - 'invalid_contributor', - sprintf( - /* translators: %s is a list of usernames. */ - __( 'The following contributor usernames are not valid: %s', 'wporg-5ftf' ), - implode( $item_separator, $invalid_contributors ) - ) - ); - } - - if ( empty( $sanitized_contributors ) ) { - return new WP_Error( - 'contributor_required', - __( 'The pledge must have at least one contributor username.', 'wporg-5ftf' ) - ); - } - - $sanitized_contributors = array_unique( $sanitized_contributors ); - - return $sanitized_contributors; -} - /** * Check the submission for valid data. *