From d9382a975a7adf88a723e06547c3d09ab64eb868 Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Thu, 21 Nov 2019 16:29:41 -0500 Subject: [PATCH] Pledges: Move `has_existing_pledge` function --- plugins/wporg-5ftf/includes/pledge-form.php | 47 +-------------------- plugins/wporg-5ftf/includes/pledge.php | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/plugins/wporg-5ftf/includes/pledge-form.php b/plugins/wporg-5ftf/includes/pledge-form.php index c17f32f..a7cf3c9 100755 --- a/plugins/wporg-5ftf/includes/pledge-form.php +++ b/plugins/wporg-5ftf/includes/pledge-form.php @@ -219,49 +219,6 @@ function get_form_submission() { 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. * @@ -335,7 +292,7 @@ function check_invalid_submission( $submission ) { Pledge\CPT_ID ); - if ( has_existing_pledge( $email, 'email' ) ) { + if ( Pledge\has_existing_pledge( $email, 'email' ) ) { return new WP_Error( 'existing_pledge_email', __( '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'] ); - if ( has_existing_pledge( $domain, 'domain' ) ) { + if ( Pledge\has_existing_pledge( $domain, 'domain' ) ) { return new WP_Error( 'existing_pledge_domain', __( 'A pledge already exists for this domain.', 'wporg-5ftf' ) diff --git a/plugins/wporg-5ftf/includes/pledge.php b/plugins/wporg-5ftf/includes/pledge.php index 5c2aa12..353f615 100755 --- a/plugins/wporg-5ftf/includes/pledge.php +++ b/plugins/wporg-5ftf/includes/pledge.php @@ -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. *