Pledges: Move has_existing_pledge function

This commit is contained in:
Kelly Dwan 2019-11-21 16:29:41 -05:00
parent 0754ae0b28
commit d9382a975a
No known key found for this signature in database
GPG key ID: 8BA5575F3D11575D
2 changed files with 45 additions and 45 deletions

View file

@ -219,49 +219,6 @@ function get_form_submission() {
return $result; return $result;
} }
/**
* Check a key value against existing pledges to see if one already exists.
*
* @param string $key The value to match against other pledges.
* @param string $key_type The type of value being matched. `email` or `domain`.
* @param int $current_pledge_id Optional. The post ID of the pledge to compare against others.
*
* @return bool
*/
function has_existing_pledge( $key, $key_type, int $current_pledge_id = 0 ) {
$args = array(
'post_type' => Pledge\CPT_ID,
'post_status' => array( 'draft', 'pending', 'publish' ),
);
switch ( $key_type ) {
case 'email':
$args['meta_query'] = array(
array(
'key' => PledgeMeta\META_PREFIX . 'org-pledge-email',
'value' => $key,
),
);
break;
case 'domain':
$args['meta_query'] = array(
array(
'key' => PledgeMeta\META_PREFIX . 'org-domain',
'value' => $key,
),
);
break;
}
if ( $current_pledge_id ) {
$args['exclude'] = array( $current_pledge_id );
}
$matching_pledge = get_posts( $args );
return ! empty( $matching_pledge );
}
/** /**
* 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.
* *
@ -335,7 +292,7 @@ function check_invalid_submission( $submission ) {
Pledge\CPT_ID Pledge\CPT_ID
); );
if ( has_existing_pledge( $email, 'email' ) ) { if ( Pledge\has_existing_pledge( $email, 'email' ) ) {
return new WP_Error( return new WP_Error(
'existing_pledge_email', 'existing_pledge_email',
__( 'This email address is already connected to an existing pledge.', 'wporg-5ftf' ) __( 'This email address is already connected to an existing pledge.', 'wporg-5ftf' )
@ -344,7 +301,7 @@ function check_invalid_submission( $submission ) {
$domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] ); $domain = PledgeMeta\get_normalized_domain_from_url( $submission['org-url'] );
if ( has_existing_pledge( $domain, 'domain' ) ) { if ( Pledge\has_existing_pledge( $domain, 'domain' ) ) {
return new WP_Error( return new WP_Error(
'existing_pledge_domain', 'existing_pledge_domain',
__( 'A pledge already exists for this domain.', 'wporg-5ftf' ) __( 'A pledge already exists for this domain.', 'wporg-5ftf' )

View file

@ -260,6 +260,49 @@ function filter_query( $query ) {
$query->set( 'posts_per_page', 100 ); $query->set( 'posts_per_page', 100 );
} }
/**
* Check a key value against existing pledges to see if one already exists.
*
* @param string $key The value to match against other pledges.
* @param string $key_type The type of value being matched. `email` or `domain`.
* @param int $current_pledge_id Optional. The post ID of the pledge to compare against others.
*
* @return bool
*/
function has_existing_pledge( $key, $key_type, int $current_pledge_id = 0 ) {
$args = array(
'post_type' => CPT_ID,
'post_status' => array( 'draft', 'pending', 'publish' ),
);
switch ( $key_type ) {
case 'email':
$args['meta_query'] = array(
array(
'key' => META_PREFIX . 'org-pledge-email',
'value' => $key,
),
);
break;
case 'domain':
$args['meta_query'] = array(
array(
'key' => META_PREFIX . 'org-domain',
'value' => $key,
),
);
break;
}
if ( $current_pledge_id ) {
$args['exclude'] = array( $current_pledge_id );
}
$matching_pledge = get_posts( $args );
return ! empty( $matching_pledge );
}
/** /**
* Enqueue assets for front-end management. * Enqueue assets for front-end management.
* *