'tk_invite_codes', 'posts_per_page' => 1, 'no_found_rows' => true, 'update_post_term_cache' => false, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- meta_query is required to look up an invite code by its hash; result is bounded to 1 post. 'meta_query' => array( array( 'key' => 'tk_all_in_one_invite_code', 'value' => $code, 'compare' => '=', ), ), ); $query = new WP_Query( $args ); // If have posts means we have a valid code. if ( $query->have_posts() ) { while ( $query->have_posts() ) : $query->the_post(); // IF the code is multi uyse check if the use limit have being reached. $all_in_one_invite_codes_options = get_post_meta( get_the_ID(), 'all_in_one_invite_codes_options', true ); $is_multiple_use = isset( $all_in_one_invite_codes_options['multiple_use'] ) ? true : false; if ( $is_multiple_use ) { $code_amount = isset( $all_in_one_invite_codes_options['generate_codes'] ) ? intval( $all_in_one_invite_codes_options['generate_codes'] ) : 0; if ( $code_amount <= 0 ) { $all_in_one_invite_codes_options['generate_codes'] = 0; update_post_meta( get_the_ID(), 'all_in_one_invite_codes_options', $all_in_one_invite_codes_options ); $result['error'] = __( 'Use limit reached for this multi-use invite code.', 'all-in-one-invite-codes' ); return $result; } } else { // IF the status is set this code is not free to use and was already used before or got deactivated. if ( ! all_in_one_invite_codes_is_valide( get_the_ID() ) ) { $result['error'] = __( 'This invite code has already been used or has been deactivated.', 'all-in-one-invite-codes' ); return $result; } else { // Alright, this is a valid code let us check if the code options are also valid for this user $all_in_one_invite_codes_options = get_post_meta( get_the_ID(), 'all_in_one_invite_codes_options', true ); // Check if the code is assigned to an email address and if the email address of the code is the same trying to register. if ( isset( $all_in_one_invite_codes_options['email'] ) ) { if ( ! empty( $all_in_one_invite_codes_options['email'] ) && strtolower( $all_in_one_invite_codes_options['email'] ) != strtolower( $user_email ) ) { $result['error'] = __( 'Email address does not belong to this invite code.', 'all-in-one-invite-codes' ); return $result; } } // Check if the invite code propose type match the one registered. if ( isset( $all_in_one_invite_codes_options['type'] ) ) { if ( ! empty( $all_in_one_invite_codes_options['type'] ) && $all_in_one_invite_codes_options['type'] != 'any' ) { // Check if the code propose is for an especific type if ( strtolower( $all_in_one_invite_codes_options['type'] ) != $type ) { $result['error'] = sprintf( /* translators: %s: page type / purpose for which the invite code was issued. */ __( 'This invite code can\'t be applied on this page; it is for the %s page only.', 'all-in-one-invite-codes' ), $all_in_one_invite_codes_options['type'] ); return $result; } } } } } endwhile; } else { // Error, something went wrong there where no code found $result['error'] = __( 'Invite code does not exist.', 'all-in-one-invite-codes' ); return $result; } return true; }