mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-04-22 11:03:43 +03:00
Pledges: Move parse_contributors
function
This commit is contained in:
parent
a99b3b478e
commit
b898dc23f3
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue