/**
  * Process Get Customers API Request
  *
  * @access public
  * @since 1.5
  * @author Daniel J Griffiths
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @param int $customer Customer ID
  * @return array $customers Multidimensional array of the customers
  */
 public function get_customers($customer = null)
 {
     if ($customer == null) {
         global $wpdb;
         $paged = $this->get_paged();
         $per_page = $this->per_page();
         $offset = $per_page * ($paged - 1);
         $customer_list_query = $wpdb->get_col("SELECT DISTINCT meta_value FROM {$wpdb->postmeta} where meta_key = '_edd_payment_user_email' ORDER BY meta_id DESC LIMIT {$per_page} OFFSET {$offset}");
         $customer_count = 0;
         foreach ($customer_list_query as $customer_email) {
             $customer_info = get_user_by('email', $customer_email);
             if ($customer_info) {
                 // Customer with registered account
                 $customers['customers'][$customer_count]['info']['id'] = $customer_info->ID;
                 $customers['customers'][$customer_count]['info']['username'] = $customer_info->user_login;
                 $customers['customers'][$customer_count]['info']['display_name'] = $customer_info->display_name;
                 $customers['customers'][$customer_count]['info']['first_name'] = $customer_info->user_firstname;
                 $customers['customers'][$customer_count]['info']['last_name'] = $customer_info->user_lastname;
                 $customers['customers'][$customer_count]['info']['email'] = $customer_info->user_email;
             } else {
                 // Guest customer
                 $customers['customers'][$customer_count]['info']['id'] = -1;
                 $customers['customers'][$customer_count]['info']['username'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['display_name'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['first_name'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['last_name'] = __('Guest', 'edd');
                 $customers['customers'][$customer_count]['info']['email'] = $customer_email;
             }
             $customers['customers'][$customer_count]['stats']['total_purchases'] = edd_count_purchases_of_customer($customer_email);
             $customers['customers'][$customer_count]['stats']['total_spent'] = edd_purchase_total_of_user($customer_email);
             $customers['customers'][$customer_count]['stats']['total_downloads'] = edd_count_file_downloads_of_user($customer_email);
             $customer_count++;
         }
     } else {
         if (is_numeric($customer)) {
             $customer_info = get_userdata($customer);
         } else {
             $customer_info = get_user_by('email', $customer);
         }
         if ($customer_info && edd_has_purchases($customer_info->ID)) {
             $customers['customers'][0]['info']['id'] = $customer_info->ID;
             $customers['customers'][0]['info']['username'] = $customer_info->user_login;
             $customers['customers'][0]['info']['display_name'] = $customer_info->display_name;
             $customers['customers'][0]['info']['first_name'] = $customer_info->user_firstname;
             $customers['customers'][0]['info']['last_name'] = $customer_info->user_lastname;
             $customers['customers'][0]['info']['email'] = $customer_info->user_email;
             $customers['customers'][0]['stats']['total_purchases'] = edd_count_purchases_of_customer($customer);
             $customers['customers'][0]['stats']['total_spent'] = edd_purchase_total_of_user($customer);
             $customers['customers'][0]['stats']['total_downloads'] = edd_count_file_downloads_of_user($customer);
         } else {
             $error['error'] = sprintf(__('Customer %s not found!', 'edd'), $customer);
             return $error;
         }
     }
     return $customers;
 }
/**
 * Check to see if a user has access to a post/page
 *
 * @since       2.0
 * @param       int $user_id The ID of the user to check
 * @param       array $restricted_to The array of downloads for a post/page
 * @param       int $post_id The ID of the object we are viewing
 * @return      array $return An array containing the status and optional message
 */
function edd_cr_user_can_access($user_id = false, $restricted_to, $post_id = false)
{
    $has_access = false;
    $restricted_count = count($restricted_to);
    $products = array();
    // If no user is given, use the current user
    if (!$user_id) {
        $user_id = get_current_user_id();
    }
    // bbPress specific checks. Moderators can see everything
    if (class_exists('bbPress') && current_user_can('moderate')) {
        $has_access = true;
    }
    // Admins have full access
    if (current_user_can('manage_options')) {
        $has_access = true;
    }
    // The post author can always access
    if ($post_id && current_user_can('edit_post', $post_id)) {
        $has_access = true;
    }
    if ($restricted_to && !$has_access) {
        foreach ($restricted_to as $item => $data) {
            if (empty($data['download'])) {
                $has_access = true;
            }
            // The author of a download always has access
            if ((int) get_post_field('post_author', $data['download']) === (int) $user_id && is_user_logged_in()) {
                $has_access = true;
                break;
            }
            // If restricted to any customer and user has purchased something
            if ('any' === $data['download'] && edd_has_purchases($user_id) && is_user_logged_in()) {
                $has_access = true;
                break;
            } elseif ('any' === $data['download']) {
                $products[0] = __('any product', 'edd-cr');
                $has_access = false;
                break;
            }
            // Check for variable prices
            if (!$has_access) {
                if (edd_has_variable_prices($data['download'])) {
                    if (strtolower($data['price_id']) !== 'all' && !empty($data['price_id'])) {
                        $products[] = '<a href="' . get_permalink($data['download']) . '">' . get_the_title($data['download']) . ' - ' . edd_get_price_option_name($data['download'], $data['price_id']) . '</a>';
                        if (edd_has_user_purchased($user_id, $data['download'], $data['price_id'])) {
                            $has_access = true;
                        }
                    } else {
                        $products[] = '<a href="' . get_permalink($data['download']) . '">' . get_the_title($data['download']) . '</a>';
                        if (edd_has_user_purchased($user_id, $data['download'])) {
                            $has_access = true;
                        }
                    }
                } else {
                    $products[] = '<a href="' . get_permalink($data['download']) . '">' . get_the_title($data['download']) . '</a>';
                    if (is_user_logged_in() && edd_has_user_purchased($user_id, $data['download'])) {
                        $has_access = true;
                    }
                }
            }
        }
        if ($has_access == false) {
            if ($restricted_count > 1) {
                $message = __('This content is restricted to buyers of:', 'edd-cr');
                if (!empty($products)) {
                    $message .= '<ul>';
                    foreach ($products as $id => $product) {
                        $message .= '<li>' . $product . '</li>';
                    }
                    $message .= '</ul>';
                }
            } else {
                $message = sprintf(__('This content is restricted to buyers of %s.', 'edd-cr'), $products[0]);
            }
        }
        if (isset($message)) {
            $return['message'] = $message;
        } else {
            $return['message'] = __('This content is restricted to buyers.', 'edd-cr');
        }
    } else {
        // Just in case we're checking something unrestricted...
        $has_access = true;
    }
    // Allow plugins to modify the restriction requirements
    $has_access = apply_filters('edd_cr_user_can_access', $has_access, $user_id, $restricted_to);
    $return['status'] = $has_access;
    return $return;
}