/**
 * Render Donwload Columns
 *
 * Render the custom columns content.
 *
 * @access      private
 * @since       1.0 
 * @return      void
*/
function edd_render_download_columns($column_name, $post_id)
{
    if (get_post_type($post_id) == 'download') {
        $sales = edd_get_download_sales_stats($post_id);
        $earnings = edd_get_download_earnings_stats($post_id);
        $color = get_post_meta($post_id, '_edd_purchase_color', true);
        $color = $color ? $color : 'blue';
        $purchase_text = get_post_meta($post_id, '_edd_purchase_text', true);
        $purchase_text = $purchase_text && '' !== $purchase_text ? $purchase_text : __('Purchase', 'edd');
        switch ($column_name) {
            case 'download_category':
                echo get_the_term_list($post_id, 'download_category', '', ', ', '');
                break;
            case 'download_tag':
                echo get_the_term_list($post_id, 'download_tag', '', ', ', '');
                break;
            case 'price':
                echo edd_price($post_id, false);
                if (!edd_has_variable_prices($post_id)) {
                    echo '<input type="hidden" class="downloadprice-' . $post_id . '" value="' . edd_get_download_price($post_id) . '" />';
                }
                break;
            case 'sales':
                echo $sales;
                break;
            case 'earnings':
                echo edd_currency_filter($earnings);
                break;
            case 'shortcode':
                echo '[purchase_link id="' . absint($post_id) . '" text="' . esc_html($purchase_text) . '" style="button" color="' . esc_attr($color) . '"]';
                break;
        }
    }
}
/**
 * Render Download Columns
 *
 * @since 1.0
 * @param string $column_name Column name
 * @param int $post_id Download (Post) ID
 * @return void
 */
function edd_render_download_columns($column_name, $post_id)
{
    if (get_post_type($post_id) == 'download') {
        global $edd_options;
        $style = isset($edd_options['button_style']) ? $edd_options['button_style'] : 'button';
        $color = isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'blue';
        $purchase_text = !empty($edd_options['add_to_cart_text']) ? $edd_options['add_to_cart_text'] : __('Purchase', 'edd');
        switch ($column_name) {
            case 'download_category':
                echo get_the_term_list($post_id, 'download_category', '', ', ', '');
                break;
            case 'download_tag':
                echo get_the_term_list($post_id, 'download_tag', '', ', ', '');
                break;
            case 'price':
                if (edd_has_variable_prices($post_id)) {
                    echo edd_price_range($post_id);
                } else {
                    echo edd_price($post_id, false);
                    echo '<input type="hidden" class="downloadprice-' . $post_id . '" value="' . edd_get_download_price($post_id) . '" />';
                }
                break;
            case 'sales':
                echo edd_get_download_sales_stats($post_id);
                break;
            case 'earnings':
                echo edd_currency_filter(edd_format_amount(edd_get_download_earnings_stats($post_id)));
                break;
            case 'shortcode':
                echo '[purchase_link id="' . absint($post_id) . '" text="' . esc_html($purchase_text) . '" style="' . $style . '" color="' . esc_attr($color) . '"]';
                break;
        }
    }
}
/**
 * Render Download Columns
 *
 * @since 1.0
 * @param string $column_name Column name
 * @param int $post_id Download (Post) ID
 * @return void
 */
function edd_render_download_columns($column_name, $post_id)
{
    if (get_post_type($post_id) == 'download') {
        global $edd_options;
        $style = isset($edd_options['button_style']) ? $edd_options['button_style'] : 'button';
        $color = isset($edd_options['checkout_color']) ? $edd_options['checkout_color'] : 'blue';
        $color = $color == 'inherit' ? '' : $color;
        $purchase_text = !empty($edd_options['add_to_cart_text']) ? $edd_options['add_to_cart_text'] : __('Purchase', 'edd');
        switch ($column_name) {
            case 'download_category':
                echo get_the_term_list($post_id, 'download_category', '', ', ', '');
                break;
            case 'download_tag':
                echo get_the_term_list($post_id, 'download_tag', '', ', ', '');
                break;
            case 'price':
                if (edd_has_variable_prices($post_id)) {
                    echo edd_price_range($post_id);
                } else {
                    echo edd_price($post_id, false);
                    echo '<input type="hidden" class="downloadprice-' . $post_id . '" value="' . edd_get_download_price($post_id) . '" />';
                }
                break;
            case 'sales':
                if (current_user_can('view_product_stats', $post_id)) {
                    echo '<a href="' . esc_url(admin_url('edit.php?post_type=download&page=edd-reports&tab=logs&view=sales&download=' . $post_id)) . '">';
                    echo edd_get_download_sales_stats($post_id);
                    echo '</a>';
                } else {
                    echo '-';
                }
                break;
            case 'earnings':
                if (current_user_can('view_product_stats', $post_id)) {
                    echo '<a href="' . esc_url(admin_url('edit.php?post_type=download&page=edd-reports&view=downloads&download-id=' . $post_id)) . '">';
                    echo edd_currency_filter(edd_format_amount(edd_get_download_earnings_stats($post_id)));
                    echo '</a>';
                } else {
                    echo '-';
                }
                break;
            case 'shortcode':
                echo '[purchase_link id="' . absint($post_id) . '" text="' . esc_html($purchase_text) . '" style="' . $style . '" color="' . esc_attr($color) . '"]';
                break;
        }
    }
}
/**
 * Render Download Columns
 *
 * @since 1.0
 * @param string $column_name Column name
 * @param int $post_id Download (Post) ID
 * @return void
 */
function edd_render_download_columns($column_name, $post_id)
{
    if (get_post_type($post_id) == 'download') {
        switch ($column_name) {
            case 'download_category':
                echo get_the_term_list($post_id, 'download_category', '', ', ', '');
                break;
            case 'download_tag':
                echo get_the_term_list($post_id, 'download_tag', '', ', ', '');
                break;
            case 'price':
                if (edd_has_variable_prices($post_id)) {
                    echo edd_price_range($post_id);
                } else {
                    echo edd_price($post_id, false);
                    echo '<input type="hidden" class="downloadprice-' . $post_id . '" value="' . edd_get_download_price($post_id) . '" />';
                }
                break;
            case 'sales':
                if (current_user_can('view_product_stats', $post_id)) {
                    echo '<a href="' . esc_url(admin_url('edit.php?post_type=download&page=edd-reports&tab=logs&view=sales&download=' . $post_id)) . '">';
                    echo edd_get_download_sales_stats($post_id);
                    echo '</a>';
                } else {
                    echo '-';
                }
                break;
            case 'earnings':
                if (current_user_can('view_product_stats', $post_id)) {
                    echo '<a href="' . esc_url(admin_url('edit.php?post_type=download&page=edd-reports&view=downloads&download-id=' . $post_id)) . '">';
                    echo edd_currency_filter(edd_format_amount(edd_get_download_earnings_stats($post_id)));
                    echo '</a>';
                } else {
                    echo '-';
                }
                break;
        }
    }
}
/**
 * Show Download Earnings Graph
 *
 * @access      public
 * @since       1.0 
 * @return      void
*/
function edd_show_download_earnings_graph($bgcolor = 'white')
{
    $downloads = get_posts(array('post_type' => 'download', 'posts_per_page' => -1));
    if ($downloads) {
        ob_start();
        ?>
	    <script type="text/javascript">
	   	 google.load("visualization", "1", {packages:["corechart"]});
			// earnings chart	
		    google.setOnLoadCallback(drawEarningsChart);
		    function drawEarningsChart() {
		        var data = new google.visualization.DataTable();
		        data.addColumn('string', '<?php 
        _e("Download", "edd");
        ?>
');
		        data.addColumn('number', '<?php 
        _e("Earnings", "edd");
        ?>
');
		        data.addRows([
					<?php 
        foreach ($downloads as $download) {
            ?>
		          		['<?php 
            echo html_entity_decode(get_the_title($download->ID), ENT_COMPAT, 'UTF-8');
            ?>
', 
							<?php 
            echo edd_get_download_earnings_stats($download->ID);
            ?>
						],
					<?php 
        }
        ?>
		        ]);

		        var options = {
		          	title: "<?php 
        echo sprintf(__('%s Performance in Earnings', 'edd'), edd_get_label_singular());
        ?>
",
					colors:['#a3bcd3'],
					fontSize: 12,
					backgroundColor: '<?php 
        echo $bgcolor;
        ?>
'
		        };

		        var chart = new google.visualization.ColumnChart(document.getElementById('earnings_chart_div'));
		        chart.draw(data, options);
		    }
	    </script>
		<div id="earnings_chart_div"></div>
		<?php 
        echo ob_get_clean();
    }
}
 /**
  * Decrease the earnings by the given amount
  *
  * @since 2.2
  * @param integer $amount
  * @return float|false
  */
 public function decrease_earnings($amount)
 {
     $earnings = edd_get_download_earnings_stats($this->ID);
     if ($earnings > 0) {
         // Only decrease if greater than zero
         $new_amount = $earnings - (double) $amount;
         if ($this->update_meta('_edd_download_earnings', $new_amount)) {
             $this->earnings = $new_amount;
             return $this->earnings;
         }
     }
     return false;
 }
/**
 * Generate PDF Reports
 *
 * Generates PDF report on sales and earnings for all downloads for the current year.
 *
 * @since 1.1.4.0
 * @param string $data
 * @uses edd_pdf
 * @author Sunny Ratilal
 */
function edd_generate_pdf($data)
{
    if (!current_user_can('view_shop_reports')) {
        wp_die(__('You do not have permission to generate PDF sales reports', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403));
    }
    if (!wp_verify_nonce($_GET['_wpnonce'], 'edd_generate_pdf')) {
        wp_die(__('Nonce verification failed', 'easy-digital-downloads'), __('Error', 'easy-digital-downloads'), array('response' => 403));
    }
    require_once EDD_PLUGIN_DIR . '/includes/libraries/fpdf/fpdf.php';
    require_once EDD_PLUGIN_DIR . '/includes/libraries/fpdf/edd_pdf.php';
    $daterange = date_i18n(get_option('date_format'), mktime(0, 0, 0, 1, 1, date('Y'))) . ' ' . utf8_decode(__('to', 'easy-digital-downloads')) . ' ' . date_i18n(get_option('date_format'));
    $pdf = new edd_pdf();
    $pdf->AddPage('L', 'A4');
    $pdf->SetTitle(utf8_decode(__('Sales and earnings reports for the current year for all products', 'easy-digital-downloads')));
    $pdf->SetAuthor(utf8_decode(__('Easy Digital Downloads', 'easy-digital-downloads')));
    $pdf->SetCreator(utf8_decode(__('Easy Digital Downloads', 'easy-digital-downloads')));
    $pdf->Image(EDD_PLUGIN_URL . 'assets/images/edd-logo-pdf.png', 205, 10);
    $pdf->SetMargins(8, 8, 8);
    $pdf->SetX(8);
    $pdf->SetFont('Helvetica', '', 16);
    $pdf->SetTextColor(50, 50, 50);
    $pdf->Cell(0, 3, utf8_decode(__('Sales and earnings reports for the current year for all products', 'easy-digital-downloads')), 0, 2, 'L', false);
    $pdf->SetFont('Helvetica', '', 13);
    $pdf->Ln();
    $pdf->SetTextColor(150, 150, 150);
    $pdf->Cell(0, 6, utf8_decode(__('Date Range: ', 'easy-digital-downloads')) . $daterange, 0, 2, 'L', false);
    $pdf->Ln();
    $pdf->SetTextColor(50, 50, 50);
    $pdf->SetFont('Helvetica', '', 14);
    $pdf->Cell(0, 10, utf8_decode(__('Table View', 'easy-digital-downloads')), 0, 2, 'L', false);
    $pdf->SetFont('Helvetica', '', 12);
    $pdf->SetFillColor(238, 238, 238);
    $pdf->Cell(70, 6, utf8_decode(__('Product Name', 'easy-digital-downloads')), 1, 0, 'L', true);
    $pdf->Cell(30, 6, utf8_decode(__('Price', 'easy-digital-downloads')), 1, 0, 'L', true);
    $category_labels = edd_get_taxonomy_labels('download_category');
    $pdf->Cell(50, 6, utf8_decode($category_labels['name']), 1, 0, 'L', true);
    $pdf->Cell(50, 6, utf8_decode(__('Tags', 'easy-digital-downloads')), 1, 0, 'L', true);
    $pdf->Cell(45, 6, utf8_decode(__('Number of Sales', 'easy-digital-downloads')), 1, 0, 'L', true);
    $pdf->Cell(35, 6, utf8_decode(__('Earnings to Date', 'easy-digital-downloads')), 1, 1, 'L', true);
    $year = date('Y');
    $downloads = get_posts(array('post_type' => 'download', 'year' => $year, 'posts_per_page' => -1));
    if ($downloads) {
        $pdf->SetWidths(array(70, 30, 50, 50, 45, 35));
        foreach ($downloads as $download) {
            $pdf->SetFillColor(255, 255, 255);
            $title = utf8_decode(get_the_title($download->ID));
            if (edd_has_variable_prices($download->ID)) {
                $prices = edd_get_variable_prices($download->ID);
                $first = $prices[0]['amount'];
                $last = array_pop($prices);
                $last = $last['amount'];
                if ($first < $last) {
                    $min = $first;
                    $max = $last;
                } else {
                    $min = $last;
                    $max = $first;
                }
                $price = html_entity_decode(edd_currency_filter(edd_format_amount($min)) . ' - ' . edd_currency_filter(edd_format_amount($max)));
            } else {
                $price = html_entity_decode(edd_currency_filter(edd_get_download_price($download->ID)));
            }
            $categories = get_the_term_list($download->ID, 'download_category', '', ', ', '');
            $categories = $categories ? strip_tags($categories) : '';
            $tags = get_the_term_list($download->ID, 'download_tag', '', ', ', '');
            $tags = $tags ? strip_tags($tags) : '';
            $sales = edd_get_download_sales_stats($download->ID);
            $earnings = html_entity_decode(edd_currency_filter(edd_get_download_earnings_stats($download->ID)));
            if (function_exists('iconv')) {
                // Ensure characters like euro; are properly converted. See GithuB issue #472 and #1570
                $price = iconv('UTF-8', 'windows-1252', utf8_encode($price));
                $earnings = iconv('UTF-8', 'windows-1252', utf8_encode($earnings));
            }
            $pdf->Row(array($title, $price, $categories, $tags, $sales, $earnings));
        }
    } else {
        $pdf->SetWidths(array(280));
        $title = utf8_decode(sprintf(__('No %s found.', 'easy-digital-downloads'), edd_get_label_plural()));
        $pdf->Row(array($title));
    }
    $pdf->Ln();
    $pdf->SetTextColor(50, 50, 50);
    $pdf->SetFont('Helvetica', '', 14);
    $pdf->Cell(0, 10, utf8_decode(__('Graph View', 'easy-digital-downloads')), 0, 2, 'L', false);
    $pdf->SetFont('Helvetica', '', 12);
    $image = html_entity_decode(urldecode(edd_draw_chart_image()));
    $image = str_replace(' ', '%20', $image);
    $pdf->SetX(25);
    $pdf->Image($image . '&file=.png');
    $pdf->Ln(7);
    $pdf->Output(apply_filters('edd_sales_earnings_pdf_export_filename', 'edd-report-' . date_i18n('Y-m-d')) . '.pdf', 'D');
}
Beispiel #8
0
/**
 * Render Stats Meta Box
 *
 * @since 1.0
 * @global array $post Contains all the download data
 * @return void
 */
function edd_render_stats_meta_box()
{
    global $post;
    if (!current_user_can('view_product_stats', $post->ID)) {
        return;
    }
    $earnings = edd_get_download_earnings_stats($post->ID);
    $sales = edd_get_download_sales_stats($post->ID);
    ?>

	<p>
		<strong class="label"><?php 
    _e('Sales:', 'edd');
    ?>
</strong>
		<span><?php 
    echo $sales;
    ?>
 &mdash; <a href="<?php 
    echo admin_url('/edit.php?page=edd-reports&view=sales&post_type=download&tab=logs&download=' . $post->ID);
    ?>
"><?php 
    _e('View Sales Log', 'edd');
    ?>
</a></span>
	</p>

	<p>
		<strong class="label"><?php 
    _e('Earnings:', 'edd');
    ?>
</strong>
		<span><?php 
    echo edd_currency_filter(edd_format_amount($earnings));
    ?>
</span>
	</p>

	<hr />

	<p class="file-download-log">
		<span><a href="<?php 
    echo admin_url('edit.php?page=edd-reports&view=file_downloads&post_type=download&tab=logs&download=' . $post->ID);
    ?>
"><?php 
    _e('View File Download Log', 'edd');
    ?>
</a></span><br/>
	</p>
	<p>
		<span><a href="<?php 
    echo admin_url('edit.php?post_type=download&page=edd-reports&view=downloads&download-id=' . $post->ID);
    ?>
"><?php 
    _e('View Detailed Earnings Report', 'edd');
    ?>
</a></span>
	</p>
<?php 
    do_action('edd_stats_meta_box');
}
/**
 * Generate PDF Reports
 *
 * Generates PDF report on sales and earnings for all downloads for the current year.
 *
 * @since 1.1.4.0
 * @param string $data
 * @uses edd_pdf
 * @author Sunny Ratilal
 */
function edd_generate_pdf($data)
{
    $edd_pdf_reports_nonce = $_GET['_wpnonce'];
    if (wp_verify_nonce($edd_pdf_reports_nonce, 'edd_generate_pdf')) {
        require_once EDD_PLUGIN_DIR . '/includes/libraries/fpdf/fpdf.php';
        require_once EDD_PLUGIN_DIR . '/includes/libraries/fpdf/edd_pdf.php';
        $daterange = date_i18n(get_option('date_format'), mktime(0, 0, 0, 1, 1, date('Y'))) . ' ' . utf8_decode(__('to', 'edd')) . ' ' . date_i18n(get_option('date_format'));
        $pdf = new edd_pdf();
        $pdf->AddPage('L', 'A4');
        $pdf->SetTitle(utf8_decode(__('Sales and earnings reports for the current year for all products', 'edd')));
        $pdf->SetAuthor(utf8_decode(__('Easy Digital Downloads', 'edd')));
        $pdf->SetCreator(utf8_decode(__('Easy Digital Downloads', 'edd')));
        $pdf->Image(EDD_PLUGIN_URL . 'assets/images/edd-logo.png', 205, 10);
        $pdf->SetMargins(8, 8, 8);
        $pdf->SetX(8);
        $pdf->SetFont('Helvetica', '', 16);
        $pdf->SetTextColor(50, 50, 50);
        $pdf->Cell(0, 3, utf8_decode(__('Sales and earnings reports for the current year for all products', 'edd')), 0, 2, 'L', false);
        $pdf->SetFont('Helvetica', '', 13);
        $pdf->Ln();
        $pdf->SetTextColor(150, 150, 150);
        $pdf->Cell(0, 6, utf8_decode(__('Date Range: ', 'edd')) . $daterange, 0, 2, 'L', false);
        $pdf->Ln();
        $pdf->SetTextColor(50, 50, 50);
        $pdf->SetFont('Helvetica', '', 14);
        $pdf->Cell(0, 10, utf8_decode(__('Table View', 'edd')), 0, 2, 'L', false);
        $pdf->SetFont('Helvetica', '', 12);
        $pdf->SetFillColor(238, 238, 238);
        $pdf->Cell(70, 6, utf8_decode(__('Product Name', 'edd')), 1, 0, 'L', true);
        $pdf->Cell(30, 6, utf8_decode(__('Price', 'edd')), 1, 0, 'L', true);
        $pdf->Cell(50, 6, utf8_decode(__('Categories', 'edd')), 1, 0, 'L', true);
        $pdf->Cell(50, 6, utf8_decode(__('Tags', 'edd')), 1, 0, 'L', true);
        $pdf->Cell(45, 6, utf8_decode(__('Number of Sales', 'edd')), 1, 0, 'L', true);
        $pdf->Cell(35, 6, utf8_decode(__('Earnings to Date', 'edd')), 1, 1, 'L', true);
        $year = date('Y');
        $downloads = get_posts(array('post_type' => 'download', 'year' => $year, 'posts_per_page' => -1));
        if ($downloads) {
            $pdf->SetWidths(array(70, 30, 50, 50, 45, 35));
            foreach ($downloads as $download) {
                $pdf->SetFillColor(255, 255, 255);
                $title = utf8_decode(get_the_title($download->ID));
                if (edd_has_variable_prices($download->ID)) {
                    $prices = edd_get_variable_prices($download->ID);
                    $first = $prices[0]['amount'];
                    $last = array_pop($prices);
                    $last = $last['amount'];
                    if ($first < $last) {
                        $min = $first;
                        $max = $last;
                    } else {
                        $min = $last;
                        $max = $first;
                    }
                    $price = html_entity_decode(edd_currency_filter(edd_format_amount($min)) . ' - ' . edd_currency_filter(edd_format_amount($max)));
                } else {
                    $price = html_entity_decode(edd_currency_filter(edd_get_download_price($download->ID)));
                }
                $categories = get_the_term_list($download->ID, 'download_category', '', ', ', '');
                $categories = $categories ? strip_tags($categories) : '';
                $tags = get_the_term_list($download->ID, 'download_tag', '', ', ', '');
                $tags = $tags ? strip_tags($tags) : '';
                $sales = edd_get_download_sales_stats($download->ID);
                $link = get_permalink($download->ID);
                $earnings = html_entity_decode(edd_currency_filter(edd_get_download_earnings_stats($download->ID)));
                $pdf->Row(array($title, $price, $categories, $tags, $sales, $earnings));
            }
        } else {
            $pdf->SetWidths(array(280));
            $title = utf8_decode(sprintf(__('No %s found.', 'edd'), edd_get_label_plural()));
            $pdf->Row(array($title));
        }
        $pdf->Ln();
        $pdf->SetTextColor(50, 50, 50);
        $pdf->SetFont('Helvetica', '', 14);
        $pdf->Cell(0, 10, utf8_decode(__('Graph View', 'edd')), 0, 2, 'L', false);
        $pdf->SetFont('Helvetica', '', 12);
        $image = html_entity_decode(urldecode(edd_draw_chart_image()));
        $image = str_replace(' ', '%20', $image);
        $pdf->SetX(25);
        $pdf->Image($image . '&file=.png');
        $pdf->Ln(7);
        $pdf->Output('edd-report-' . date_i18n('Y-m-d') . '.pdf', 'D');
    }
}
 /**
  * Retrieve all report data for Downloads
  *
  * @access      private
  * @since       1.3
  * @return      array
  */
 function reports_data()
 {
     $reports_data = array();
     $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'title';
     $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
     $report_args = array('post_type' => 'download', 'post_status' => 'publish', 'order' => $order, 'posts_per_page' => $this->per_page, 'paged' => $this->get_paged());
     switch ($orderby) {
         case 'title':
             $report_args['orderby'] = 'title';
             break;
         case 'sales':
             $report_args['orderby'] = 'meta_value_num';
             $report_args['meta_key'] = '_edd_download_sales';
             break;
         case 'earnings':
             $report_args['orderby'] = 'meta_value_num';
             $report_args['meta_key'] = '_edd_download_earnings';
             break;
     }
     $downloads = get_posts($report_args);
     if ($downloads) {
         foreach ($downloads as $download) {
             $reports_data[] = array('ID' => $download->ID, 'title' => get_the_title($download->ID), 'sales' => edd_get_download_sales_stats($download->ID), 'earnings' => edd_get_download_earnings_stats($download->ID), 'average_sales' => edd_get_average_monthly_download_sales($download->ID), 'average_earnings' => edd_get_average_monthly_download_earnings($download->ID));
         }
     }
     return $reports_data;
 }
     $html_content .= '</div>';
 } elseif ($item_key == 'edd_sales_stats') {
     $html_content .= '<div class="element element_' . $item_id . ' ' . $item_key . '"  >';
     $is_download = get_post_type(get_the_ID());
     $active_plugins = get_option('active_plugins');
     if (in_array('easy-digital-downloads/easy-digital-downloads.php', (array) $active_plugins) && $is_download == 'download') {
         $sales_stats = edd_get_download_sales_stats(get_the_ID());
         $html_content .= $sales_stats;
     }
     $html_content .= '</div>';
 } elseif ($item_key == 'edd_earnings_stats') {
     $html_content .= '<div class="element element_' . $item_id . ' ' . $item_key . '"  >';
     $is_download = get_post_type(get_the_ID());
     $active_plugins = get_option('active_plugins');
     if (in_array('easy-digital-downloads/easy-digital-downloads.php', (array) $active_plugins) && $is_download == 'download') {
         $earnings_stats = edd_get_download_earnings_stats(get_the_ID());
         $html_content .= $earnings_stats;
     }
     $html_content .= '</div>';
 } elseif ($item_key == 'edd_add_to_cart') {
     $html_content .= '<div class="element element_' . $item_id . ' ' . $item_key . '"  >';
     $is_download = get_post_type(get_the_ID());
     $active_plugins = get_option('active_plugins');
     if (in_array('easy-digital-downloads/easy-digital-downloads.php', (array) $active_plugins) && $is_download == 'download') {
         $purchase_link = do_shortcode('[purchase_link id="' . get_the_ID() . '" text="' . __('Add to Cart', 'post_grid_textdomain') . '" style="button"]');
         $html_content .= $purchase_link;
     }
     $html_content .= '</div>';
 } elseif ($item_key == 'edd_categories') {
     $html_content .= '<div class="element element_' . $item_id . ' ' . $item_key . '"  >';
     $is_download = get_post_type(get_the_ID());
 /**
  * Build all the reports data
  *
  * @access public
  * @since  2.4
  * @return array $reports_data All the data for customer reports
  */
 public function reports_data()
 {
     $cached_reports = get_transient('edd_earnings_by_category_data');
     if (false !== $cached_reports) {
         $reports_data = $cached_reports;
     } else {
         $reports_data = array();
         $term_args = array('parent' => 0, 'hierarchical' => 0);
         $categories = get_terms('download_category', $term_args);
         foreach ($categories as $category_id => $category) {
             $category_slugs = array($category->slug);
             $child_args = array('parent' => $category->term_id, 'hierarchical' => 0);
             $child_terms = get_terms('download_category', $child_args);
             if (!empty($child_terms)) {
                 foreach ($child_terms as $child_term) {
                     $category_slugs[] = $child_term->slug;
                 }
             }
             $download_args = array('post_type' => 'download', 'posts_per_page' => -1, 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'download_category', 'field' => 'slug', 'terms' => $category_slugs)));
             $downloads = get_posts($download_args);
             $sales = 0;
             $earnings = 0.0;
             $avg_sales = 0;
             $avg_earnings = 0.0;
             foreach ($downloads as $download) {
                 $sales += edd_get_download_sales_stats($download);
                 $earnings += edd_get_download_earnings_stats($download);
                 $avg_sales += edd_get_average_monthly_download_sales($download);
                 $avg_earnings += edd_get_average_monthly_download_earnings($download);
             }
             $avg_sales = round($avg_sales / count($downloads));
             $avg_earnings = round($avg_earnings / count($downloads), edd_currency_decimal_filter());
             $reports_data[] = array('ID' => $category->term_id, 'label' => $category->name, 'total_sales' => edd_format_amount($sales, false), 'total_sales_raw' => $sales, 'total_earnings' => edd_currency_filter(edd_format_amount($earnings)), 'total_earnings_raw' => $earnings, 'avg_sales' => edd_format_amount($avg_sales, false), 'avg_earnings' => edd_currency_filter(edd_format_amount($avg_earnings)), 'is_child' => false);
             if (!empty($child_terms)) {
                 foreach ($child_terms as $child_term) {
                     $child_args = array('post_type' => 'download', 'posts_per_page' => -1, 'fields' => 'ids', 'tax_query' => array(array('taxonomy' => 'download_category', 'field' => 'slug', 'terms' => $child_term->slug)));
                     $child_downloads = get_posts($child_args);
                     $child_sales = 0;
                     $child_earnings = 0.0;
                     $child_avg_sales = 0;
                     $child_avg_earnings = 0.0;
                     foreach ($child_downloads as $child_download) {
                         $child_sales += edd_get_download_sales_stats($child_download);
                         $child_earnings += edd_get_download_earnings_stats($child_download);
                         $child_avg_sales += edd_get_average_monthly_download_sales($child_download);
                         $child_avg_earnings += edd_get_average_monthly_download_earnings($child_download);
                     }
                     $child_avg_sales = round($child_avg_sales / count($child_downloads));
                     $child_avg_earnings = round($child_avg_earnings / count($child_downloads), edd_currency_decimal_filter());
                     $reports_data[] = array('ID' => $child_term->term_id, 'label' => '&#8212; ' . $child_term->name, 'total_sales' => edd_format_amount($child_sales, false), 'total_sales_raw' => $child_sales, 'total_earnings' => edd_currency_filter(edd_format_amount($child_earnings)), 'total_earnings_raw' => $child_earnings, 'avg_sales' => edd_format_amount($child_avg_sales, false), 'avg_earnings' => edd_currency_filter(edd_format_amount($child_avg_earnings)), 'is_child' => true);
                 }
             }
         }
         set_transient('edd_earnings_by_category_data', $reports_data, HOUR_IN_SECONDS / 4);
     }
     return $reports_data;
 }
/**
 * Average Earnings
 *
 * Retreives the average monthly earnings for a specific download
 *
 * @access      public
 * @since       1.3
 * @return      float
*/
function edd_get_average_monthly_download_earnings($download_id)
{
    $earnings = edd_get_download_earnings_stats($download_id);
    $release_date = get_post_field('post_date', $download_id);
    $diff = abs(time() - strtotime($release_date));
    $years = floor($diff / (365 * 60 * 60 * 24));
    // number of years since publication
    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
    // number of months since publication
    if ($months > 0) {
        return $earnings / $months;
    }
    return $earnings;
}
/**
 * Render Stats Meta Box
 *
 * @access      private
 * @since       1.0 
 * @return      void
*/
function edd_render_stats_meta_box()
{
    global $post;
    $earnings = edd_get_download_earnings_stats($post->ID);
    $sales = edd_get_download_sales_stats($post->ID);
    echo '<table class="form-table">';
    echo '<tr>';
    echo '<th style="width:20%">' . __('Sales:', 'edd') . '</th>';
    echo '<td class="edd_download_stats">';
    echo $sales;
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<th style="width:20%">' . __('Earnings:', 'edd') . '</th>';
    echo '<td class="edd_download_stats">';
    echo edd_currency_filter($earnings);
    echo '</td>';
    echo '</tr>';
    do_action('edd_stats_meta_box');
    echo '</table>';
}
/**
 * Decrease Earnings
 *
 * Decreases the total earnings of a download. Primarily for when a purchase is refunded.
 *
 * @access      public
 * @since       1.0.8.1 
 * @return      void
*/
function edd_decrease_earnings($download_id, $amount)
{
    $earnings = edd_get_download_earnings_stats($download_id);
    if ($earnings > 0) {
        // only decrease if greater than zero
        $earnings = $earnings - $amount;
    }
    if (update_post_meta($download_id, '_edd_download_earnings', $earnings)) {
        return $earnings;
    }
    return false;
}
 /**
  * Process Get Stats API Request
  *
  * @author Daniel J Griffiths
  * @since 1.5
  *
  * @global object $wpdb Used to query the database using the WordPress
  *
  * @param array $args Arguments provided by API Request
  *
  * @return array
  */
 public function get_stats($args = array())
 {
     $defaults = array('type' => null, 'product' => null, 'date' => null, 'startdate' => null, 'enddate' => null);
     $args = wp_parse_args($args, $defaults);
     $dates = $this->get_dates($args);
     $stats = array();
     $earnings = array('earnings' => array());
     $sales = array('sales' => array());
     $error = array();
     if (!user_can($this->user_id, 'view_shop_reports') && !$this->override) {
         return $stats;
     }
     if ($args['type'] == 'sales') {
         if ($args['product'] == null) {
             if ($args['date'] == null) {
                 $sales = $this->get_default_sales_stats();
             } elseif ($args['date'] === 'range') {
                 // Return sales for a date range
                 // Ensure the end date is later than the start date
                 if ($args['enddate'] < $args['startdate']) {
                     $error['error'] = __('The end date must be later than the start date!', 'easy-digital-downloads');
                 }
                 // Ensure both the start and end date are specified
                 if (empty($args['startdate']) || empty($args['enddate'])) {
                     $error['error'] = __('Invalid or no date range specified!', 'easy-digital-downloads');
                 }
                 $total = 0;
                 // Loop through the years
                 $y = $dates['year'];
                 while ($y <= $dates['year_end']) {
                     if ($dates['year'] == $dates['year_end']) {
                         $month_start = $dates['m_start'];
                         $month_end = $dates['m_end'];
                     } elseif ($y == $dates['year'] && $dates['year_end'] > $dates['year']) {
                         $month_start = $dates['m_start'];
                         $month_end = 12;
                     } elseif ($y == $dates['year_end']) {
                         $month_start = 1;
                         $month_end = $dates['m_end'];
                     } else {
                         $month_start = 1;
                         $month_end = 12;
                     }
                     $i = $month_start;
                     while ($i <= $month_end) {
                         if ($i == $dates['m_start']) {
                             $d = $dates['day_start'];
                         } else {
                             $d = 1;
                         }
                         if ($i == $dates['m_end']) {
                             $num_of_days = $dates['day_end'];
                         } else {
                             $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                         }
                         while ($d <= $num_of_days) {
                             $sale_count = edd_get_sales_by_date($d, $i, $y);
                             $date_key = date('Ymd', strtotime($y . '/' . $i . '/' . $d));
                             if (!isset($sales['sales'][$date_key])) {
                                 $sales['sales'][$date_key] = 0;
                             }
                             $sales['sales'][$date_key] += $sale_count;
                             $total += $sale_count;
                             $d++;
                         }
                         $i++;
                     }
                     $y++;
                 }
                 $sales['totals'] = $total;
             } else {
                 if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') {
                     $sales_count = 0;
                     // Loop through the months
                     $month = $dates['m_start'];
                     while ($month <= $dates['m_end']) {
                         $sales_count += edd_get_sales_by_date(null, $month, $dates['year']);
                         $month++;
                     }
                     $sales['sales'][$args['date']] = $sales_count;
                 } else {
                     $sales['sales'][$args['date']] = edd_get_sales_by_date($dates['day'], $dates['m_start'], $dates['year']);
                 }
             }
         } elseif ($args['product'] == 'all') {
             $products = get_posts(array('post_type' => 'download', 'nopaging' => true));
             $i = 0;
             foreach ($products as $product_info) {
                 $sales['sales'][$i] = array($product_info->post_name => edd_get_download_sales_stats($product_info->ID));
                 $i++;
             }
         } else {
             if (get_post_type($args['product']) == 'download') {
                 $product_info = get_post($args['product']);
                 $sales['sales'][0] = array($product_info->post_name => edd_get_download_sales_stats($args['product']));
             } else {
                 $error['error'] = sprintf(__('Product %s not found!', 'easy-digital-downloads'), $args['product']);
             }
         }
         if (!empty($error)) {
             return $error;
         }
         return $sales;
     } elseif ($args['type'] == 'earnings') {
         if ($args['product'] == null) {
             if ($args['date'] == null) {
                 $earnings = $this->get_default_earnings_stats();
             } elseif ($args['date'] === 'range') {
                 // Return sales for a date range
                 // Ensure the end date is later than the start date
                 if ($args['enddate'] < $args['startdate']) {
                     $error['error'] = __('The end date must be later than the start date!', 'easy-digital-downloads');
                 }
                 // Ensure both the start and end date are specified
                 if (empty($args['startdate']) || empty($args['enddate'])) {
                     $error['error'] = __('Invalid or no date range specified!', 'easy-digital-downloads');
                 }
                 $total = (double) 0.0;
                 // Loop through the years
                 $y = $dates['year'];
                 if (!isset($earnings['earnings'])) {
                     $earnings['earnings'] = array();
                 }
                 while ($y <= $dates['year_end']) {
                     if ($dates['year'] == $dates['year_end']) {
                         $month_start = $dates['m_start'];
                         $month_end = $dates['m_end'];
                     } elseif ($y == $dates['year'] && $dates['year_end'] > $dates['year']) {
                         $month_start = $dates['m_start'];
                         $month_end = 12;
                     } elseif ($y == $dates['year_end']) {
                         $month_start = 1;
                         $month_end = $dates['m_end'];
                     } else {
                         $month_start = 1;
                         $month_end = 12;
                     }
                     $i = $month_start;
                     while ($i <= $month_end) {
                         if ($i == $dates['m_start']) {
                             $d = $dates['day_start'];
                         } else {
                             $d = 1;
                         }
                         if ($i == $dates['m_end']) {
                             $num_of_days = $dates['day_end'];
                         } else {
                             $num_of_days = cal_days_in_month(CAL_GREGORIAN, $i, $y);
                         }
                         while ($d <= $num_of_days) {
                             $earnings_stat = edd_get_earnings_by_date($d, $i, $y);
                             $date_key = date('Ymd', strtotime($y . '/' . $i . '/' . $d));
                             if (!isset($earnings['earnings'][$date_key])) {
                                 $earnings['earnings'][$date_key] = 0;
                             }
                             $earnings['earnings'][$date_key] += $earnings_stat;
                             $total += $earnings_stat;
                             $d++;
                         }
                         $i++;
                     }
                     $y++;
                 }
                 $earnings['totals'] = $total;
             } else {
                 if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') {
                     $earnings_count = (double) 0.0;
                     // Loop through the months
                     $month = $dates['m_start'];
                     while ($month <= $dates['m_end']) {
                         $earnings_count += edd_get_earnings_by_date(null, $month, $dates['year']);
                         $month++;
                     }
                     $earnings['earnings'][$args['date']] = $earnings_count;
                 } else {
                     $earnings['earnings'][$args['date']] = edd_get_earnings_by_date($dates['day'], $dates['m_start'], $dates['year']);
                 }
             }
         } elseif ($args['product'] == 'all') {
             $products = get_posts(array('post_type' => 'download', 'nopaging' => true));
             $i = 0;
             foreach ($products as $product_info) {
                 $earnings['earnings'][$i] = array($product_info->post_name => edd_get_download_earnings_stats($product_info->ID));
                 $i++;
             }
         } else {
             if (get_post_type($args['product']) == 'download') {
                 $product_info = get_post($args['product']);
                 $earnings['earnings'][0] = array($product_info->post_name => edd_get_download_earnings_stats($args['product']));
             } else {
                 $error['error'] = sprintf(__('Product %s not found!', 'easy-digital-downloads'), $args['product']);
             }
         }
         if (!empty($error)) {
             return $error;
         }
         return $earnings;
     } elseif ($args['type'] == 'customers') {
         if (version_compare($edd_version, '2.3', '<') || !edd_has_upgrade_completed('upgrade_customer_payments_association')) {
             global $wpdb;
             $stats = array();
             $count = $wpdb->get_col("SELECT COUNT(DISTINCT meta_value) FROM {$wpdb->postmeta} WHERE meta_key = '_edd_payment_user_email'");
             $stats['customers']['total_customers'] = $count[0];
             return $stats;
         } else {
             $customers = new EDD_DB_Customers();
             $stats['customers']['total_customers'] = $customers->count();
             return $stats;
         }
     } elseif (empty($args['type'])) {
         $stats = array_merge($stats, $this->get_default_sales_stats());
         $stats = array_merge($stats, $this->get_default_earnings_stats());
         return array('stats' => $stats);
     }
 }
 function column_default($item, $column_name)
 {
     switch ($column_name) {
         case 'name':
             return '<a href="' . admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=edit') . '">' . $item->first_name . ' ' . $item->last_name . ' (' . $item->user_login . ')</a>';
             break;
         case 'view':
             return $item->display_name;
             break;
         case 'actions':
             $admin_actions = array();
             if (user_can($item->ID, 'fes_is_admin') || user_can($item->ID, 'frontend_vendor')) {
                 $admin_actions['view'] = array('action' => 'view', 'name' => __('View', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=edit'));
                 $admin_actions['revoke'] = array('action' => 'delete', 'name' => __('Revoke', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=revoke_vendor'));
                 $admin_actions['suspend'] = array('action' => 'suspend', 'name' => __('Suspend', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=suspend_vendor'));
             }
             if (user_can($item->ID, 'pending_vendor')) {
                 $admin_actions['view'] = array('action' => 'view', 'name' => __('View', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=edit'));
                 $admin_actions['approve'] = array('action' => 'approve', 'name' => __('Approve', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=approve_vendor'));
                 $admin_actions['decline'] = array('action' => 'delete', 'name' => __('Decline', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=decline_vendor'));
             }
             if (user_can($item->ID, 'suspended_vendor')) {
                 $admin_actions['view'] = array('action' => 'view', 'name' => __('View', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=edit'));
                 $admin_actions['revoke'] = array('action' => 'delete', 'name' => __('Revoke', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=revoke_vendor'));
                 $admin_actions['unsuspend'] = array('action' => 'unsuspend', 'name' => __('Unsuspend', 'edd_fes'), 'url' => admin_url('admin.php?page=fes-vendors&vendor=' . $item->ID . '&action=unsuspend_vendor'));
             }
             $admin_actions = apply_filters('fes_admin_actions', $admin_actions, $item);
             foreach ($admin_actions as $action) {
                 $image = isset($action['image_url']) ? $action['image_url'] : fes_plugin_url . 'assets/img/icons/' . $action['action'] . '.png';
                 printf('<a class="button tips" href="%s" data-tip="%s"><img src="%s" alt="%s" width="14" /></a>', esc_url($action['url']), esc_attr($action['name']), esc_attr($image), esc_attr($action['name']));
             }
             break;
             break;
         case 'products':
             $posts = new WP_Query();
             $posts->query(array('posts_per_page' => -1, 'author' => $item->ID, 'post_type' => 'download'));
             return sizeof($posts->posts);
             break;
         case 'status':
             if (user_can($item, 'pending_vendor')) {
                 echo '<span class="download-status pending-review">' . __('Pending', 'edd_fes') . '</span>';
             } else {
                 if (user_can($item, 'fes_is_admin') || user_can($item, 'frontend_vendor')) {
                     echo '<span class="download-status published">' . __('Approved', 'edd_fes') . '</span>';
                 } else {
                     if (user_can($item, 'suspended_vendor')) {
                         echo '<span class="download-status future">' . __('Suspended', 'edd_fes') . '</span>';
                     } else {
                         return 'ERROR: WP_Query is misbehaving!';
                     }
                 }
             }
             break;
         case 'date':
             return date_i18n(get_option('date_format'), strtotime($item->user_registered));
             break;
         case 'sales':
             global $wpdb;
             global $current_user;
             $vendor_products = array();
             $vendor_products = get_posts(array('nopaging' => true, 'orderby' => 'title', 'post_type' => 'download', 'post_status' => 'publish', 'author' => $item->ID, 'order' => 'ASC'));
             if (empty($vendor_products)) {
                 return edd_currency_filter(edd_format_amount(0));
             }
             $sales = 0;
             foreach ($vendor_products as $product) {
                 $sales = $sales + edd_get_download_earnings_stats($product->ID);
             }
             return edd_currency_filter(edd_format_amount($sales));
             break;
         default:
             return print_r($item, true);
             //Show the whole array for troubleshooting purposes
             break;
     }
 }
 /**
  * Build all the reports data
  *
  * @access public
  * @since 1.5
  * @return array $reports_data All the data for customer reports
  */
 public function reports_data()
 {
     $reports_data = array();
     $downloads = $this->products->posts;
     if ($downloads) {
         foreach ($downloads as $download) {
             $reports_data[] = array('ID' => $download, 'title' => get_the_title($download), 'sales' => edd_get_download_sales_stats($download), 'earnings' => edd_get_download_earnings_stats($download), 'average_sales' => edd_get_average_monthly_download_sales($download), 'average_earnings' => edd_get_average_monthly_download_earnings($download));
         }
     }
     return $reports_data;
 }
 /**
  * Process Get Stats API Request
  *
  * @access public
  * @author Daniel J Griffiths
  * @since 1.5
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @param array $args Arguments provided by API Request
  */
 public function get_stats($args = array())
 {
     $defaults = array('type' => null, 'product' => null, 'date' => null, 'startdate' => null, 'enddate' => null);
     $args = wp_parse_args($args, $defaults);
     $dates = $this->get_dates($args);
     if ($args['type'] == 'sales') {
         if ($args['product'] == null) {
             if ($args['date'] == null) {
                 $sales = $this->get_default_sales_stats();
             } elseif ($args['date'] === 'range') {
                 // Return sales for a date range
                 // Ensure the end date is later than the start date
                 if ($args['enddate'] < $args['startdate']) {
                     $error['error'] = __('The end date must be later than the start date!', 'edd');
                 }
                 // Ensure both the start and end date are specified
                 if (empty($args['startdate']) || empty($args['enddate'])) {
                     $error['error'] = __('Invalid or no date range specified!', 'edd');
                 }
                 $total = 0;
                 // Loop through the years
                 $year = $dates['year'];
                 while ($year <= $dates['year_end']) {
                     // Loop through the months
                     $month = $dates['m_start'];
                     while ($month <= $dates['m_end']) {
                         // Loop through the days
                         $day = $month > $dates['m_start'] ? 1 : $dates['day_start'];
                         $days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
                         while ($day <= $days_in_month) {
                             $sale_count = edd_get_sales_by_date($day, $month, $year);
                             $sales['sales'][date('Ymd', strtotime($year . '/' . $month . '/' . $day))] = $sale_count;
                             $total += $sale_count;
                             $day++;
                         }
                         $month++;
                     }
                     $year++;
                 }
                 $sales['totals'] = $total;
             } else {
                 if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') {
                     $sales_count = 0;
                     // Loop through the months
                     $month = $dates['m_start'];
                     while ($month <= $dates['m_end']) {
                         $sales_count += edd_get_sales_by_date(null, $month, $dates['year']);
                         $month++;
                     }
                     $sales['sales'][$args['date']] = $sales_count;
                 } else {
                     $sales['sales'][$args['date']] = edd_get_sales_by_date($dates['day'], $dates['m_start'], $dates['year']);
                 }
             }
         } elseif ($args['product'] == 'all') {
             $products = get_posts(array('post_type' => 'download', 'nopaging' => true));
             $i = 0;
             foreach ($products as $product_info) {
                 $sales['sales'][$i] = array($product_info->post_name => edd_get_download_sales_stats($product_info->ID));
                 $i++;
             }
         } else {
             if (get_post_type($args['product']) == 'download') {
                 $product_info = get_post($args['product']);
                 $sales['sales'][0] = array($product_info->post_name => edd_get_download_sales_stats($args['product']));
             } else {
                 $error['error'] = sprintf(__('Product %s not found!', 'edd'), $args['product']);
             }
         }
         if (!empty($error)) {
             return $error;
         }
         return $sales;
     } elseif ($args['type'] == 'earnings') {
         if ($args['product'] == null) {
             if ($args['date'] == null) {
                 $earnings = $this->get_default_earnings_stats();
             } elseif ($args['date'] === 'range') {
                 // Return sales for a date range
                 // Ensure the end date is later than the start date
                 if ($args['enddate'] < $args['startdate']) {
                     $error['error'] = __('The end date must be later than the start date!', 'edd');
                 }
                 // Ensure both the start and end date are specified
                 if (empty($args['startdate']) || empty($args['enddate'])) {
                     $error['error'] = __('Invalid or no date range specified!', 'edd');
                 }
                 $total = (double) 0.0;
                 // Loop through the years
                 $year = $dates['year'];
                 while ($year <= $dates['year_end']) {
                     // Loop through the months
                     $month = $dates['m_start'];
                     while ($month <= $dates['m_end']) {
                         // Loop through the days
                         $day = $month > $dates['m_start'] ? 1 : $dates['day_start'];
                         $days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
                         while ($day <= $days_in_month) {
                             $sale_count = edd_get_earnings_by_date($day, $month, $year);
                             $earnings['earnings'][date('Ymd', strtotime($year . '/' . $month . '/' . $day))] = $sale_count;
                             $total += $sale_count;
                             $day++;
                         }
                         $month++;
                     }
                     $year++;
                 }
                 $earnings['totals'] = $total;
             } else {
                 if ($args['date'] == 'this_quarter' || $args['date'] == 'last_quarter') {
                     $earnings_count = (double) 0.0;
                     // Loop through the months
                     $month = $dates['m_start'];
                     while ($month <= $dates['m_end']) {
                         $earnings_count += edd_get_earnings_by_date(null, $month, $dates['year']);
                         $month++;
                     }
                     $earnings['earnings'][$args['date']] = $earnings_count;
                 } else {
                     $earnings['earnings'][$args['date']] = edd_get_earnings_by_date($dates['day'], $dates['m_start'], $dates['year']);
                 }
             }
         } elseif ($args['product'] == 'all') {
             $products = get_posts(array('post_type' => 'download', 'nopaging' => true));
             $i = 0;
             foreach ($products as $product_info) {
                 $earnings['earnings'][$i] = array($product_info->post_name => edd_get_download_earnings_stats($product_info->ID));
                 $i++;
             }
         } else {
             if (get_post_type($args['product']) == 'download') {
                 $product_info = get_post($args['product']);
                 $earnings['earnings'][0] = array($product_info->post_name => edd_get_download_earnings_stats($args['product']));
             } else {
                 $error['error'] = sprintf(__('Product %s not found!', 'edd'), $args['product']);
             }
         }
         if (!empty($error)) {
             return $error;
         }
         return $earnings;
     } elseif ($args['type'] == 'customers') {
         global $wpdb;
         $stats = array();
         $count = $wpdb->get_col("SELECT COUNT(DISTINCT meta_value) FROM {$wpdb->postmeta} WHERE meta_key = '_edd_payment_user_email'");
         $stats['customers']['total_customers'] = $count[0];
         return $stats;
     } elseif (empty($args['type'])) {
         $stats = array();
         $stats = array_merge($stats, $this->get_default_sales_stats());
         $stats = array_merge($stats, $this->get_default_earnings_stats());
         return array('stats' => $stats);
     }
 }
/**
 * Retrieves the average monthly earnings for a specific download
 *
 * @since 1.3
 * @param int $download_id Download ID
 * @return float $earnings Average monthly earnings
 */
function edd_get_average_monthly_download_earnings($download_id = 0)
{
    $earnings = edd_get_download_earnings_stats($download_id);
    $release_date = get_post_field('post_date', $download_id);
    $diff = abs(current_time('timestamp') - strtotime($release_date));
    $months = floor($diff / (30 * 60 * 60 * 24));
    // Number of months since publication
    if ($months > 0) {
        $earnings = $earnings / $months;
    }
    return $earnings < 0 ? 0 : $earnings;
}
/**
 * Render Stats Meta Box
 *
 * @since 1.0
 * @global array $post Contains all the download data
 * @return void
 */
function edd_render_stats_meta_box()
{
    global $post;
    $earnings = edd_get_download_earnings_stats($post->ID);
    $sales = edd_get_download_sales_stats($post->ID);
    echo '<table class="form-table">';
    echo '<tr>';
    echo '<th style="width: 20%">' . __('Sales:', 'edd') . '</th>';
    echo '<td class="edd_download_stats">';
    echo $sales . '&nbsp;&ndash;&nbsp;<a href="' . admin_url('/edit.php?page=edd-reports&view=sales&post_type=download&tab=logs&download=' . $post->ID) . '">' . __('View Sales Log', 'edd') . '</a>';
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<th style="width: 30%">' . __('Earnings:', 'edd') . '</th>';
    echo '<td class="edd_download_stats">';
    echo edd_currency_filter(edd_format_amount($earnings));
    echo '</td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td colspan="2" class="edd_download_stats">';
    echo '<a href="' . admin_url('/edit.php?page=edd-reports&view=file_downloads&post_type=download&tab=logs&download=' . $post->ID) . '">' . __('View File Download Log', 'edd') . '</a>';
    echo '</td>';
    echo '</tr>';
    do_action('edd_stats_meta_box');
    echo '</table>';
}