Skip to content

Instantly share code, notes, and snippets.

@supercleanse
Last active January 4, 2016 20:49
Show Gist options
  • Save supercleanse/8676730 to your computer and use it in GitHub Desktop.
Save supercleanse/8676730 to your computer and use it in GitHub Desktop.
Determine if a user is active or not for a specific product
<?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