/**
* Retrieve payments from the database
*
* @access public
* @since 1.5
*/
public function get_payments($args = array())
{
global $wpdb;
$defaults = array('number' => 20, 'offset' => 0, 'subscription' => 0, 'user_id' => 0, 'date' => array(), 'fields' => false, 's' => '');
$args = wp_parse_args($args, $defaults);
$where = '';
// payments for a specific subscription level
if (!empty($args['subscription'])) {
$where .= "WHERE `subscription`= '{$args['subscription']}' ";
}
// payments for specific users
if (!empty($args['user_id'])) {
if (is_array($args['user_id'])) {
$user_ids = implode(',', $args['user_id']);
} else {
$user_ids = intval($args['user_id']);
}
if (!empty($args['subscription'])) {
$where .= "`user_id` IN( {$user_ids} ) ";
} else {
$where .= "WHERE `user_id` IN( {$user_ids} ) ";
}
}
// Setup the date query
if (!empty($args['date']) && is_array($args['date'])) {
$day = !empty($args['date']['day']) ? absint($args['date']['day']) : null;
$month = !empty($args['date']['month']) ? absint($args['date']['month']) : null;
$year = !empty($args['date']['year']) ? absint($args['date']['year']) : null;
$date_where = '';
$date_where .= !is_null($year) ? $year . " = YEAR ( date ) " : '';
if (!is_null($month)) {
$date_where = $month . " = MONTH ( date ) AND " . $date_where;
}
if (!is_null($day)) {
$date_where = $day . " = DAY ( date ) AND " . $date_where;
}
if (!empty($args['user_id']) || !empty($args['subscription'])) {
$where .= "AND (" . $date_where . ")";
} else {
$where .= "WHERE ( " . $date_where . " ) ";
}
}
// Fields to return
if ($args['fields']) {
$fields = $args['fields'];
} else {
$fields = '*';
}
if (!empty($args['s'])) {
if (empty($where)) {
$where = "WHERE ";
} else {
$where = " AND ";
}
// Search by email
if (is_email($args['s'])) {
$user = get_user_by('email', $args['s']);
$where .= "`user_id`={$user->ID} ";
} else {
$levels_db = new RCP_Levels();
// Search by subscription key
if (strlen($args['s']) == 32) {
$where .= "`subscription_key`= '{$args['s']}' ";
} elseif ($levels_db->get_level_by('name', $args['s'])) {
// Matching subscription level found so search for payments with this level
$where .= "`subscription`= '{$args['s']}' ";
} else {
$where .= "`transaction_id`='{$args['s']}' ";
}
}
}
$payments = $wpdb->get_results($wpdb->prepare("SELECT {$fields} FROM " . $this->db_name . " {$where}ORDER BY id DESC LIMIT %d,%d;", absint($args['offset']), absint($args['number'])));
return $payments;
}