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

@ -260,6 +260,49 @@ function filter_query( $query ) {
$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.
*