Last active
January 4, 2016 20:49
-
-
Save supercleanse/8676730 to your computer and use it in GitHub Desktop.
Determine if a user is active or not for a specific product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Returns true if a specified user is active for a specified product ... false if the user isn't active */ | |
function is_user_active_for_product( $user_id, $product_id ) { | |
global $wpdb; | |
$mepr_db = new MeprDb(); | |
$sql = "SELECT COUNT(*) " . | |
"FROM {$mepr_db->transactions} " . | |
"WHERE user_id=%d " . | |
"AND product_id=%d " . | |
"AND ( expires_at='0000-00-00 00:00:00' " . | |
"OR expires_at > NOW() )"; | |
$sql = $wpdb->prepare( $sql, $user_id, $product_id ); | |
$active_transaction_count = $wpdb->get_var($sql); | |
return !empty($active_transaction_count); | |
} | |
/** Returns an array of user ids of active users for a given product */ | |
function active_users_for_product( $product_id ) { | |
global $wpdb; | |
$mepr_db = new MeprDb(); | |
$sql = "SELECT user_id " . | |
"FROM {$mepr_db->transactions} " . | |
"WHERE product_id=%d " . | |
"AND ( expires_at='0000-00-00 00:00:00' " . | |
"OR expires_at > NOW() )"; | |
$sql = $wpdb->prepare( $sql, $product_id ); | |
$user_ids = $wpdb->get_col( $sql ); | |
return $user_ids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment