/**
  * Tie into the heartbeat and append our stats
  *
  * @access public
  * @since 1.8
  * @return array
  */
 public static function heartbeat_received($response, $data)
 {
     if (!current_user_can('view_shop_reports')) {
         return $response;
         // Only modify heartbeat if current user can view show reports
     }
     // Make sure we only run our query if the edd_heartbeat key is present
     if (isset($data['edd_heartbeat']) && $data['edd_heartbeat'] == 'dashboard_summary') {
         // Instantiate the stats class
         $stats = new EDD_Payment_Stats();
         $earnings = edd_get_total_earnings();
         // Send back the number of complete payments
         $response['edd-total-payments'] = edd_format_amount(edd_get_total_sales(), false);
         $response['edd-total-earnings'] = html_entity_decode(edd_currency_filter(edd_format_amount($earnings)), ENT_COMPAT, 'UTF-8');
         $response['edd-payments-month'] = edd_format_amount($stats->get_sales(0, 'this_month', false, array('publish', 'revoked')), false);
         $response['edd-earnings-month'] = html_entity_decode(edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'this_month'))), ENT_COMPAT, 'UTF-8');
         $response['edd-payments-today'] = edd_format_amount($stats->get_sales(0, 'today', false, array('publish', 'revoked')), false);
         $response['edd-earnings-today'] = html_entity_decode(edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'today'))), ENT_COMPAT, 'UTF-8');
     }
     return $response;
 }
 /**
  * Generate the default sales stats returned by the 'stats' endpoint
  *
  * @access private
  * @since 1.5.3
  * @return array default sales statistics
  */
 private function get_default_sales_stats()
 {
     // Default sales return
     $sales = array();
     $sales['sales']['today'] = $this->stats->get_sales(0, 'today');
     $sales['sales']['current_month'] = $this->stats->get_sales(0, 'this_month');
     $sales['sales']['last_month'] = $this->stats->get_sales(0, 'last_month');
     $sales['sales']['totals'] = edd_get_total_sales();
     return $sales;
 }
/**
 * Loads the dashboard sales widget via ajax
 *
 * @since 2.1
 * @return void
 */
function edd_load_dashboard_sales_widget()
{
    if (!current_user_can(apply_filters('edd_dashboard_stats_cap', 'view_shop_reports'))) {
        die;
    }
    $stats = new EDD_Payment_Stats();
    ?>
	<div class="edd_dashboard_widget">
		<div class="table table_left table_current_month">
			<table>
				<thead>
					<tr>
						<td colspan="2"><?php 
    _e('Current Month', 'edd');
    ?>
</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="first t monthly_earnings"><?php 
    _e('Earnings', 'edd');
    ?>
</td>
						<td class="b b-earnings"><?php 
    echo edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'this_month')));
    ?>
</td>
					</tr>
					<tr>
						<?php 
    $monthly_sales = $stats->get_sales(0, 'this_month', false, array('publish', 'revoked'));
    ?>
						<td class="first t monthly_sales"><?php 
    echo _n('Sale', 'Sales', $monthly_sales, 'edd');
    ?>
</td>
						<td class="b b-sales"><?php 
    echo $monthly_sales;
    ?>
</td>
					</tr>
				</tbody>
			</table>
			<table>
				<thead>
					<tr>
						<td colspan="2"><?php 
    _e('Last Month', 'edd');
    ?>
</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="first t earnings"><?php 
    echo __('Earnings', 'edd');
    ?>
</td>
						<td class="b b-last-month-earnings"><?php 
    echo edd_currency_filter(edd_format_amount($stats->get_earnings(0, 'last_month')));
    ?>
</td>
					</tr>
					<tr>
						<td class="first t sales">
							<?php 
    $last_month_sales = $stats->get_sales(0, 'last_month', false, array('publish', 'revoked'));
    ?>
							<?php 
    echo _n('Sale', 'Sales', $last_month_sales, 'edd');
    ?>
						</td>
						<td class="b b-last-month-sales">
							<?php 
    echo $last_month_sales;
    ?>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="table table_right table_today">
			<table>
				<thead>
					<tr>
						<td colspan="2">
							<?php 
    _e('Today', 'edd');
    ?>
						</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="t sales"><?php 
    _e('Earnings', 'edd');
    ?>
</td>
						<td class="last b b-earnings">
							<?php 
    $earnings_today = $stats->get_earnings(0, 'today', false);
    ?>
							<?php 
    echo edd_currency_filter(edd_format_amount($earnings_today));
    ?>
						</td>
					</tr>
					<tr>
						<td class="t sales">
							<?php 
    _e('Sales', 'edd');
    ?>
						</td>
						<td class="last b b-sales">
							<?php 
    $sales_today = $stats->get_sales(0, 'today', false, array('publish', 'revoked'));
    ?>
							<?php 
    echo edd_format_amount($sales_today, false);
    ?>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div class="table table_right table_totals">
			<table>
				<thead>
					<tr>
						<td colspan="2"><?php 
    _e('Totals', 'edd');
    ?>
</td>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="t earnings"><?php 
    _e('Total Earnings', 'edd');
    ?>
</td>
						<td class="last b b-earnings"><?php 
    echo edd_currency_filter(edd_format_amount(edd_get_total_earnings()));
    ?>
</td>
					</tr>
					<tr>
						<td class="t sales"><?php 
    _e('Total Sales', 'edd');
    ?>
</td>
						<td class="last b b-sales"><?php 
    echo edd_format_amount(edd_get_total_sales(), false);
    ?>
</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div style="clear: both"></div>
		<?php 
    do_action('edd_sales_summary_widget_after_stats', $stats);
    ?>
		<?php 
    $p_query = new EDD_Payments_Query(array('number' => 5, 'status' => 'publish'));
    $payments = $p_query->get_payments();
    if ($payments) {
        ?>
		<div class="table recent_purchases">
			<table>
				<thead>
					<tr>
						<td colspan="2">
							<?php 
        _e('Recent Purchases', 'edd');
        ?>
							<a href="<?php 
        echo admin_url('edit.php?post_type=download&page=edd-payment-history');
        ?>
">&nbsp;&ndash;&nbsp;<?php 
        _e('View All', 'edd');
        ?>
</a>
						</td>
					</tr>
				</thead>
				<tbody>
					<?php 
        foreach ($payments as $payment) {
            ?>
						<tr>
							<td class="edd_order_label">
								<a href="<?php 
            echo add_query_arg('id', $payment->ID, admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details'));
            ?>
" title="<?php 
            printf(__('Purchase Details for Payment #%s', 'edd'), $payment->ID);
            ?>
 ">
									<?php 
            echo get_the_title($payment->ID);
            ?>
									&mdash; <?php 
            echo $payment->user_info['email'];
            ?>
								</a>
								<?php 
            if ($payment->user_info['id'] > 0) {
                $user = get_user_by('id', $payment->user_info['id']);
                if ($user) {
                    echo "(" . $user->data->user_login . ")";
                }
            }
            ?>
							</td>
							<td class="edd_order_price">
								<a href="<?php 
            echo add_query_arg('id', $payment->ID, admin_url('edit.php?post_type=download&page=edd-payment-history&view=view-order-details'));
            ?>
" title="<?php 
            printf(__('Purchase Details for Payment #%s', 'edd'), $payment->ID);
            ?>
 ">
									<span class="edd_price_label"><?php 
            echo edd_currency_filter(edd_format_amount($payment->total), edd_get_payment_currency_code($payment->ID));
            ?>
</span>
								</a>
							</td>
						</tr>
						<?php 
        }
        // End foreach
        ?>
				</tbody>
			</table>
		</div>
		<?php 
    }
    // End if
    ?>
		<?php 
    do_action('edd_sales_summary_widget_after_purchases', $payments);
    ?>
	</div>
	<?php 
    die;
}
/**
 * Sales Summary Dashboard Widget
 *
 * Builds and renders the Sales Summary dashboard widget. This widget displays
 * the current month's sales and earnings, total sales and earnings best selling
 * downloads as well as recent purchases made on your EDD Store.
 *
 * @author Sunny Ratilal
 * @since 1.2.2
 * @return void
 */
function edd_dashboard_sales_widget()
{
    $top_selling_args = array('post_type' => 'download', 'posts_per_page' => 1, 'post_status' => 'publish', 'meta_key' => '_edd_download_sales', 'meta_compare' => '>', 'meta_value' => 0, 'orderby' => 'meta_value_num', 'update_post_term_cache' => false, 'order' => 'DESC');
    $top_selling = get_posts($top_selling_args);
    ?>
	<div class="edd_dashboard_widget">
		<div class="table table_left table_current_month">
			<p class="sub"><?php 
    _e('Current Month', 'edd');
    ?>
</p>
			<table>
				<tbody>
					<tr class="first">
						<td class="first b"><?php 
    echo edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, date('n'), date('Y'))));
    ?>
</td>
						<td class="t monthly_earnings"><?php 
    _e('Earnings', 'edd');
    ?>
</td>
					</tr>
					<tr>
						<?php 
    $monthly_sales = edd_get_sales_by_date(null, date('n'), date('Y'));
    ?>
						<td class="first b"><?php 
    echo $monthly_sales;
    ?>
</td>
						<td class="t monthly_sales"><?php 
    echo _n('Sale', 'Sales', $monthly_sales, 'edd');
    ?>
</td>
					</tr>
				</tbody>
			</table>
			<p class="label_heading"><?php 
    _e('Last Month', 'edd');
    ?>
</p>
			<?php 
    $previous_month = date('n') == 1 ? 12 : date('n') - 1;
    $previous_year = $previous_month == 12 ? date('Y') - 1 : date('Y');
    ?>
			<div>
				<?php 
    echo __('Earnings', 'edd') . ':&nbsp;<span class="edd_price_label">' . edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, $previous_month, $previous_year))) . '</span>';
    ?>
			</div>
			<div>
				<?php 
    $last_month_sales = edd_get_sales_by_date(null, $previous_month, $previous_year);
    ?>
				<?php 
    echo _n('Sale', 'Sales', $last_month_sales, 'edd') . ':&nbsp;' . '<span class="edd_price_label">' . $last_month_sales . '</span>';
    ?>
			</div>
		</div>
		<div class="table table_right table_totals">
			<p class="sub"><?php 
    _e('Totals', 'edd');
    ?>
</p>
			<table>
				<tbody>
					<tr class="first">
						<td class="b b-earnings"><?php 
    echo edd_currency_filter(edd_format_amount(edd_get_total_earnings()));
    ?>
</td>
						<td class="last t earnings"><?php 
    _e('Total Earnings', 'edd');
    ?>
</td>
					</tr>
					<tr>
						<td class="b b-sales"><?php 
    echo edd_get_total_sales();
    ?>
</td>
						<td class="last t sales"><?php 
    _e('Total Sales', 'edd');
    ?>
</td>
					</tr>
				</tbody>
			</table>
			<?php 
    if ($top_selling) {
        foreach ($top_selling as $list) {
            ?>
					<p class="lifetime_best_selling label_heading"><?php 
            _e('Lifetime Best Selling', 'edd');
            ?>
</p>
					<p><span class="lifetime_best_selling_label"><?php 
            echo edd_get_download_sales_stats($list->ID);
            ?>
</span> <a href="<?php 
            echo get_permalink($list->ID);
            ?>
"><?php 
            echo get_the_title($list->ID);
            ?>
</a></p>
			<?php 
        }
    }
    ?>
		</div>
		<div style="clear: both"></div>
		<?php 
    $payments = edd_get_payments(array('number' => 5, 'mode' => 'live', 'orderby' => 'post_date', 'order' => 'DESC', 'user' => null, 'status' => 'publish', 'meta_key' => null, 'fields' => 'ids'));
    if ($payments) {
        ?>
		<p class="edd_dashboard_widget_subheading"><?php 
        _e('Recent Purchases', 'edd');
        ?>
</p>
		<div class="table recent_purchases">
			<table>
				<tbody>
					<?php 
        foreach ($payments as $payment) {
            $payment_meta = edd_get_payment_meta($payment);
            ?>
					<tr>
						<td>
							<?php 
            echo get_the_title($payment);
            ?>
 - (<?php 
            echo $payment_meta['email'];
            ?>
) - <span class="edd_price_label"><?php 
            echo edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment)));
            ?>
</span> - <a href="<?php 
            echo add_query_arg('id', $payment, admin_url('edit.php?post_type=download&page=edd-payment-history&edd-action=view-order-details'));
            ?>
" title="<?php 
            printf(__('Purchase Details for Payment #%s', 'edd'), $payment);
            ?>
 "><?php 
            _e('View Order Details', 'edd');
            ?>
</a>
						</td>
					</tr>
					<?php 
        }
        // End foreach
        ?>
				</tbody>
			</table>
		</div>
		<?php 
    }
    // End if
    ?>
	</div>
	<?php 
}
 /**
  * Generate the default sales stats returned by the 'stats' endpoint
  *
  * @access private
  * @since 1.5.3
  * @return array default sales statistics
  */
 private function get_default_sales_stats()
 {
     // Default sales return
     $previous_month = date('n') == 1 ? 12 : date('n') - 1;
     $previous_year = date('n') == 1 ? date('Y') - 1 : date('Y');
     $sales['sales']['current_month'] = edd_get_sales_by_date(null, date('n'), date('Y'));
     $sales['sales']['last_month'] = edd_get_sales_by_date(null, $previous_month, $previous_year);
     $sales['sales']['totals'] = edd_get_total_sales();
     return $sales;
 }
/**
 * Sales Summary Dashboard Widget
 *
 * @access      private
 * @author      Sunny Ratilal
 * @since       1.2.2
*/
function edd_dashboard_sales_widget()
{
    $top_selling_args = array('post_type' => 'download', 'posts_per_page' => 1, 'post_status' => 'publish', 'meta_key' => '_edd_download_sales', 'meta_compare' => '>', 'meta_value' => 0, 'orderby' => 'meta_value_num', 'cache_results' => false, 'update_post_term_cache' => false, 'no_found_rows' => true, 'order' => 'DESC');
    $top_selling = get_posts($top_selling_args);
    ?>
	<div class="table table_current_month">
		<p class="sub"><?php 
    _e('Current Month', 'edd');
    ?>
</p>
		<table>
			<tbody>
				<tr class="first">
					<td class="first b"><?php 
    echo edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, date('n'), date('Y'))));
    ?>
</td>
					<td class="t monthly_earnings"><?php 
    _e('Earnings', 'edd');
    ?>
</td>
				</tr>
				<tr>
					<?php 
    $monthly_sales = edd_get_sales_by_date(null, date('n'), date('Y'));
    ?>
					<td class="first b"><?php 
    echo $monthly_sales;
    ?>
</td>
					<td class="t monthly_sales"><?php 
    echo _n('Sale', 'Sales', $monthly_sales, 'edd');
    ?>
</td>
				</tr>
			</tbody>
		</table>
		<p class="label_heading"><?php 
    _e('Last Month', 'edd');
    ?>
</p>
		<div>
			<?php 
    echo __('Earnings', 'edd') . ':&nbsp;<span class="edd_price_label">' . edd_currency_filter(edd_format_amount(edd_get_earnings_by_date(null, date('n') - 1, date('Y')))) . '</span>';
    ?>
		</div>
		<div>
			<?php 
    $last_month_sales = edd_get_sales_by_date(null, date('n') - 1, date('Y'));
    ?>
			<?php 
    echo _n('Sale', 'Sales', $last_month_sales, 'edd') . ':&nbsp;' . '<span class="edd_price_label">' . $last_month_sales . '</span>';
    ?>
		</div>
	</div>
	<div class="table table_totals">
		<p class="sub"><?php 
    _e('Totals', 'edd');
    ?>
</p>
		<table>
			<tbody>
				<tr class="first">
					<td class="b b-earnings"><?php 
    echo edd_currency_filter(edd_format_amount(edd_get_total_earnings()));
    ?>
</td>
					<td class="last t earnings"><?php 
    _e('Total Earnings', 'edd');
    ?>
</td>
				</tr>
				<tr>
					<td class="b b-sales"><?php 
    echo edd_get_total_sales();
    ?>
</td>
					<td class="last t sales"><?php 
    _e('Total Sales', 'edd');
    ?>
</td>
				</tr>
			</tbody>
		</table>
		<?php 
    if ($top_selling) {
        foreach ($top_selling as $list) {
            ?>
				<p class="lifetime_best_selling label_heading"><?php 
            _e('Lifetime Best Selling', 'edd');
            ?>
</p>
				<p><span class="lifetime_best_selling_label"><?php 
            echo edd_get_download_sales_stats($list->ID);
            ?>
</span> <a href="<?php 
            echo get_permalink($list->ID);
            ?>
"><?php 
            echo get_the_title($list->ID);
            ?>
</a></p>
		<?php 
        }
    }
    ?>
	</div>
	<div style="clear: both"></div>
	<?php 
    $payments = edd_get_payments(array('number' => 5, 'mode' => 'live', 'orderby' => 'post_date', 'order' => 'DESC', 'user' => null, 'status' => 'publish', 'meta_key' => null));
    if ($payments) {
        ?>
	<p class="edd_dashboard_widget_subheading"><?php 
        _e('Recent Purchases', 'edd');
        ?>
</p>
	<div class="table recent_purchases">
		<table>
			<tbody>
				<?php 
        foreach ($payments as $payment) {
            $payment_meta = edd_get_payment_meta($payment->ID);
            ?>
				<tr>
					<td><?php 
            echo get_the_title($payment->ID);
            ?>
 - (<?php 
            echo $payment_meta['email'];
            ?>
) - <span class="edd_price_label"><?php 
            echo edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment->ID)));
            ?>
</span> - <a href="#TB_inline?width=640&amp;inlineId=purchased-files-<?php 
            echo $payment->ID;
            ?>
" class="thickbox" title="<?php 
            printf(__('Purchase Details for Payment #%s', 'edd'), $payment->ID);
            ?>
 "><?php 
            _e('View Order Details', 'edd');
            ?>
</a>
						<div id="purchased-files-<?php 
            echo $payment->ID;
            ?>
" style="display:none;">
							<?php 
            $cart_items = edd_get_payment_meta_cart_details($payment->ID);
            if (empty($cart_items) || !$cart_items) {
                $cart_items = maybe_unserialize($payment_meta['downloads']);
            }
            ?>
							<h4><?php 
            echo _n(__('Purchased File', 'edd'), __('Purchased Files', 'edd'), count($cart_items));
            ?>
</h4>
							<ul class="purchased-files-list">
							<?php 
            if ($cart_items) {
                foreach ($cart_items as $key => $cart_item) {
                    echo '<li>';
                    $id = isset($payment_meta['cart_details']) ? $cart_item['id'] : $cart_item;
                    $price_override = isset($payment_meta['cart_details']) ? $cart_item['price'] : null;
                    $user_info = edd_get_payment_meta_user_info($payment->ID);
                    $price = edd_get_download_final_price($id, $user_info, $price_override);
                    echo '<a href="' . admin_url('post.php?post=' . $id . '&action=edit') . '" target="_blank">' . get_the_title($id) . '</a>';
                    echo ' - ';
                    if (isset($cart_items[$key]['item_number'])) {
                        $price_options = $cart_items[$key]['item_number']['options'];
                        if (isset($price_options['price_id'])) {
                            echo edd_get_price_option_name($id, $price_options['price_id']);
                            echo ' - ';
                        }
                    }
                    echo edd_currency_filter(edd_format_amount($price));
                    echo '</li>';
                }
            }
            ?>
							</ul>
							<?php 
            $payment_date = strtotime($payment->post_date);
            ?>
							<p><?php 
            echo __('Date and Time:', 'edd') . ' ' . date_i18n(get_option('date_format'), $payment_date) . ' ' . date_i18n(get_option('time_format'), $payment_date);
            ?>
							<p><?php 
            echo __('Discount used:', 'edd') . ' ';
            if (isset($user_info['discount']) && $user_info['discount'] != 'none') {
                echo $user_info['discount'];
            } else {
                _e('none', 'edd');
            }
            ?>
							<p><?php 
            echo __('Total:', 'edd') . ' ' . edd_currency_filter(edd_format_amount(edd_get_payment_amount($payment->ID)));
            ?>
</p>

							<div class="purcase-personal-details">
								<h4><?php 
            _e('Buyer\'s Personal Details:', 'edd');
            ?>
</h4>
								<ul>
									<li><?php 
            echo __('Name:', 'edd') . ' ' . $user_info['first_name'] . ' ' . $user_info['last_name'];
            ?>
</li>
									<li><?php 
            echo __('Email:', 'edd') . ' ' . $payment_meta['email'];
            ?>
</li>
									<?php 
            do_action('edd_payment_personal_details_list', $payment_meta, $user_info);
            ?>
								</ul>
							</div>
							<?php 
            $gateway = edd_get_payment_gateway($payment->ID);
            if ($gateway) {
                ?>
							<div class="payment-method">
								<h4><?php 
                _e('Payment Method:', 'edd');
                ?>
</h4>
								<span class="payment-method-name"><?php 
                echo edd_get_gateway_admin_label($gateway);
                ?>
</span>
							</div>
							<?php 
            }
            ?>
							<div class="purchase-key-wrap">
								<h4><?php 
            _e('Purchase Key', 'edd');
            ?>
</h4>
								<span class="purchase-key"><?php 
            echo $payment_meta['key'];
            ?>
</span>
							</div>
							<p><a id="edd-close-purchase-details" class="button-secondary" onclick="tb_remove();" title="<?php 
            _e('Close', 'edd');
            ?>
"><?php 
            _e('Close', 'edd');
            ?>
</a></p>
						</div>
					</td>
				</tr>
				<?php 
        }
        // end foreach
        ?>
			</tbody>
		</table>
	</div>
	<?php 
    }
    // end if
}