/** * 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 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; } } }
/** * Show Download Sales Graph * * @access public * @since 1.0 * @return void */ function edd_show_download_sales_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"]}); // sales chart google.setOnLoadCallback(drawSalesChart); function drawSalesChart() { var data = new google.visualization.DataTable(); data.addColumn('string', '<?php echo edd_get_label_plural(); ?> '); data.addColumn('number', '<?php _e("Sales", "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_sales_stats($download->ID); ?> , ], <?php } ?> ]); var options = { title: "<?php echo sprintf(__('%s Performance in Sales', 'edd'), edd_get_label_singular()); ?> ", colors:['#a3bcd3'], fontSize: 12, backgroundColor: '<?php echo $bgcolor; ?> ' }; var chart = new google.visualization.ColumnChart(document.getElementById('sales_chart_div')); chart.draw(data, options); } </script> <div id="sales_chart_div"></div> <?php echo ob_get_clean(); } }
/** * Displays selected download above table and message if no download is selected * * @param $id * * @return string */ function eddlc_display_filtered_download($id) { if (edd_get_download_sales_stats($id)) { $sales = edd_get_download_sales_stats($id); $title = get_the_title($id); $message = '<strong>Selected download: </strong>' . $title . '<br/><strong>Sales: </strong>' . $sales; } elseif ($id) { $message = 'No valid download selected.'; } else { $message = 'No download selected.'; } $output = '<div class="postbox"><div class="inside">' . $message . '</div></div>'; return $output; }
/** * 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; } } }
/** * Purchases Remaining * * @since 1.0.1 * @param array $atts Arguments to pass to the shortcode * @global object $post The object related to this post * @return void */ function edd_purchase_limit_remaining_shortcode($atts) { global $post; $scope = edd_get_option('edd_purchase_limit_scope') ? edd_get_option('edd_purchase_limit_scope') : 'site-wide'; $sold_out_label = edd_get_option('edd_purchase_limit_sold_out_label') ? edd_get_option('edd_purchase_limit_sold_out_label') : __('Sold Out', 'edd-purchase-limit'); $defaults = array('download_id' => $post->ID); $atts = wp_parse_args($atts, $defaults); $max_purchases = edd_pl_get_file_purchase_limit($atts['download_id']); if ($scope == 'site-wide' && $max_purchases) { $purchases = edd_get_download_sales_stats($atts['download_id']); } elseif ($scope == 'per-user' && $max_purchases) { $purchases = edd_pl_get_user_purchase_count(get_current_user_id(), $atts['download_id']); } if ($purchases < $max_purchases) { $purchases_left = $max_purchases - $purchases; return '<span class="edd_purchases_left">' . $purchases_left . '</span>'; } else { return '<span class="edd_purchases_left edd_sold_out">' . $sold_out_label . '</span>'; } }
/** * 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; } } }
/** * 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') . ': <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') . ': ' . '<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&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 }
/** * 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') . ': <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') . ': ' . '<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 }
/** * Decrement the sale count by one * * @since 2.2 * @param int $quantity The quantity to decrease by * @return int|false */ public function decrease_sales($quantity = 1) { $sales = edd_get_download_sales_stats($this->ID); // Only decrease if not already zero if ($sales > 0) { $quantity = absint($quantity); $total_sales = $sales - $quantity; if ($this->update_meta('_edd_download_sales', $total_sales)) { $this->sales = $total_sales; return $this->sales; } } return false; }
/** * Given a User ID, return the markup for the list of user's products that earn commissions * * @param integer $user_id The User ID to get the commissioned products for * @return string HTML markup for the list of products */ function eddc_user_product_list($user_id = 0) { $user_id = empty($user_id) ? get_current_user_id() : $user_id; if (empty($user_id)) { return; } $products = eddc_get_download_ids_of_user($user_id); if (empty($products)) { return; } $header_text = __('Your Products', 'eddc'); if ($user_id != get_current_user_id()) { $user_info = get_userdata($user_id); $header_text = sprintf(__('%s\'s Products', 'eddc'), $user_info->display_name); } ob_start(); ?> <div id="edd_commissioned_products"> <h3 class="edd_commissioned_products_header"><?php echo $header_text; ?> </h3> <table id="edd_commissioned_products_table"> <thead> <tr> <?php do_action('edd_commissioned_products_head_row_begin'); ?> <th class="edd_commissioned_item"><?php _e('Item', 'eddc'); ?> </th> <th class="edd_commissioned_sales"><?php _e('Sales', 'eddc'); ?> </th> <?php do_action('edd_commissioned_products_head_row_end'); ?> </tr> </thead> <tbody> <?php if (!empty($products)) { ?> <?php foreach ($products as $product) { if (!get_post($product)) { continue; } ?> <tr class="edd_user_commission_row"> <?php do_action('edd_commissioned_products_row_begin', $product, $user_id); ?> <td class="edd_commissioned_item"><?php echo get_the_title($product); ?> </td> <td class="edd_commissioned_sales"><?php echo edd_get_download_sales_stats($product); ?> </td> <?php do_action('edd_commissioned_products_row_end', $product, $user_id); ?> </tr> <?php } ?> <?php } else { ?> <tr class="edd_commissioned_products_row_empty"> <td colspan="4"><?php _e('No item', 'eddc'); ?> </td> </tr> <?php } ?> </tbody> </table> </div> <?php return ob_get_clean(); }
/** * 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; }
public function product_list_sales_esc($product_id) { $sales = esc_html(edd_get_download_sales_stats($product_id)); $sales = apply_filters('fes_product_list_title', $sales, $product_id); return $sales; }
public function get_all_products($user_id = false, $status = array('draft', 'pending', 'publish', 'trash', 'future', 'private')) { global $wpdb; global $current_user; if (!$user_id) { $user_id = $current_user->ID; } $vendor_products = array(); $vendor_products = get_posts(array('nopaging' => true, 'author' => $user_id, 'orderby' => 'title', 'post_type' => 'download', 'post_status' => $status, 'order' => 'ASC')); if (empty($vendor_products)) { return false; } foreach ($vendor_products as $product) { $data[] = array('ID' => $product->ID, 'title' => $product->post_title, 'status' => $product->post_status, 'url' => esc_url(admin_url('post.php?post=' . $product->ID . '&action=edit')), 'sales' => edd_get_download_sales_stats($product->ID)); } $data = $this->array_msort($data, array('status' => SORT_ASC, 'title' => SORT_ASC, 'sales' => SORT_DESC, 'ID' => SORT_ASC, 'url' => SORT_ASC)); $data = apply_filters('fes_get_all_products', $data, $user_id); return $data; }
/** * 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'); } }
function eddc_user_product_list() { $user_id = get_current_user_id(); $products = eddc_get_download_ids_of_user($user_id); if (!is_user_logged_in()) { return; } if (empty($products)) { return; } ob_start(); ?> <div id="edd_commissioned_products"> <h3 class="edd_commissioned_products_header"><?php _e('Your Items', 'eddc'); ?> </h3> <table id="edd_commissioned_products_table"> <thead> <tr> <?php do_action('edd_commissioned_products_head_row_begin'); ?> <th class="edd_commissioned_item"><?php _e('Item', 'eddc'); ?> </th> <th class="edd_commissioned_sales"><?php _e('Sales', 'eddc'); ?> </th> <?php do_action('edd_commissioned_products_head_row_end'); ?> </tr> </thead> <tbody> <?php if (!empty($products)) { ?> <?php foreach ($products as $product) { if (!get_post($product)) { continue; } ?> <tr class="edd_user_commission_row"> <?php do_action('edd_commissioned_products_row_begin', $product, $user_id); ?> <td class="edd_commissioned_item"><?php echo get_the_title($product); ?> </td> <td class="edd_commissioned_sales"><?php echo edd_get_download_sales_stats($product); ?> </td> <?php do_action('edd_commissioned_products_row_end', $product, $user_id); ?> </tr> <?php } ?> <?php } else { ?> <tr class="edd_commissioned_products_row_empty"> <td colspan="4"><?php _e('No item', 'eddc'); ?> </td> </tr> <?php } ?> </tbody> </table> </div> <?php return ob_get_clean(); }
/** * Gets the downloads for the current view * * @access public * @since 0.1 * @return array $data Array of all the relevant downloads */ function get_downloads() { $data = array(); $selected_download = $this->get_filtered_download(); if ($selected_download) { $customers = $this->get_customers($selected_download); $download_ids = $this->get_all_downloads_for_customers($customers); $selected_sales = edd_get_download_sales_stats($selected_download); if ($download_ids) { $count = array_count_values($download_ids); $args = array('post_type' => 'download', 'post__in' => $download_ids, 'posts_per_page' => $this->per_page, 'paged' => $this->get_paged()); $downloads = new WP_Query($args); if ($downloads->have_posts()) { while ($downloads->have_posts()) { $downloads->the_post(); $id = get_the_ID(); $rate = number_format(100 * $count[$id] / $selected_sales, 2) . '%'; $data[] = array('ID' => $id, 'download' => $id, 'sales' => $count[$id], 'likelihood' => $rate); } } } } usort($data, array($this, 'sort_downloads')); return $data; }
$html_price .= '<ul>'; foreach ($prices as $price_id => $price) { $html_price .= '<li>' . $price['name'] . ': ' . $price['amount'] . '</li>'; //is the name of the price } $html_price .= '</ul>'; } $html_content .= $html_price; } $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());
/** * 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' => '— ' . $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 Sales * * Retreives the average monthly sales for a specific download * * @access public * @since 1.3 * @return float */ function edd_get_average_monthly_download_sales($download_id) { $sales = edd_get_download_sales_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 $sales / $months; } return $sales; }
/** * widget function. * * @see WP_Widget * @access public * @param array $args * @param array $instance * @return void */ function widget($args, $instance) { if ($this->get_cached_widget($args)) { return; } ob_start(); global $post; extract($args); $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } ?> <div class="item-stats"> <div class="item-stat"> <?php echo edd_get_download_sales_stats($post->ID); ?> <small><?php echo _n('Purchase', 'Purchases', edd_get_download_sales_stats($post->ID), 'mashshare-edd'); ?> </small> </div> <?php if (!class_exists('EDD_Reviews')) { ?> <div class="item-stat"> <a href="#comments"> <?php echo get_comments_number(); ?> <small><?php echo _n('Comment', 'Comments', get_comments_number(), 'mashshare-edd'); ?> </small> </a> </div> <?php } else { ?> <div class="item-stat"> <?php edd_reviews()->average_rating(); ?> <small><?php _e('Average Rating', 'mashshare-edd'); ?> </small> </div> <?php } ?> </div> <?php echo $after_widget; $content = ob_get_clean(); echo $content; $this->cache_widget($args, $content); }
/** * 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; }
/** * 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); } }
/** * Decrease Purchase Count * * Decreases the sale count of a download. Primarily for when a purchase is refunded. * * @access public * @since 1.0.8.1 * @return void */ function edd_decrease_purchase_count($download_id) { $sales = edd_get_download_sales_stats($download_id); if ($sales > 0) { // only decrease if not already zero $sales = $sales - 1; } if (update_post_meta($download_id, '_edd_download_sales', $sales)) { return $sales; } return false; }
function rehub_edd_show_download_sales() { if (rehub_option('rehub_framework_edd_counter') == '1') { echo '<p>'; echo edd_get_download_sales_stats(get_the_ID()) . ' sales'; echo '<br/>'; echo sumobi_edd_get_download_count(get_the_ID()) . ' downloads'; echo '</p>'; } }
/** * Ensure cart quantities are OK * * @since 1.0.0 * @return void */ function edd_pl_checkout_errors($valid_data, $posted) { global $edd_prices_sold_out; $cart = edd_get_cart_contents(); $scope = edd_get_option('edd_purchase_limit_scope') ? edd_get_option('edd_purchase_limit_scope') : 'site-wide'; $errors = array(); foreach ($cart as $item) { if (edd_has_variable_prices($item['id'])) { if (edd_pl_is_item_sold_out($item['id'], $item['options']['price_id'], false, false)) { $errors[] = array('id' => $item['id'], 'price' => $item['options']['price_id'], 'type' => 'soldout', 'avail' => null); } } else { $max_purchases = edd_pl_get_file_purchase_limit($item['id']); if ($scope == 'site-wide') { $purchases = edd_get_download_sales_stats($item['id']); if ($max_purchases && $purchases >= $max_purchases || !empty($edd_prices_sold_out)) { $errors[] = array('id' => $item['id'], 'price' => null, 'type' => 'soldout', 'avail' => null); } } else { if (is_user_logged_in()) { $purchases = edd_pl_get_user_purchase_count(get_current_user_id(), $item['id']); if ($max_purchases && $purchases >= $max_purchases || !empty($edd_prices_sold_out)) { $errors[] = array('id' => $item['id'], 'price' => null, 'type' => 'soldout', 'avail' => null); } } } } if (edd_item_in_cart($item['id'])) { if (edd_has_variable_prices($item['id'])) { $max_purchases = edd_pl_get_file_purchase_limit($item['id'], null, $item['options']['price_id']); $purchases = edd_pl_get_file_purchases($item['id'], $item['options']['price_id']); } if ($max_purchases > 0) { $cart_qty = edd_get_cart_item_quantity($item['id']); $total = $purchases + $cart_qty; if ($total > $max_purchases) { $errors[] = array('id' => $item['id'], 'price' => edd_has_variable_prices($item['id']) ? $item['options']['price_id'] : null, 'type' => 'toomany', 'avail' => $max_purchases - $purchases); } } } } if (count($errors) > 0) { foreach ($errors as $error) { $product = get_post($error['id']); if ($error['type'] == 'soldout') { edd_set_error('purchase_limit_reached', sprintf(__('The %s "%s" is sold out!', 'edd-purchase-limit'), strtolower(edd_get_label_singular()), $product->post_title)); } elseif ($error['type'] == 'toomany') { edd_set_error('purchase_limit_exceeded', sprintf(_n('There is only %s available for the %s "%s"!', 'There are only %s available for the %s "%s"!', $error['avail'], 'edd-purchase-limit'), $error['avail'], strtolower(edd_get_label_singular()), $product->post_title)); } } } }
/** * 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; ?> — <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'); }
/** * 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 sales for a specific download * * @since 1.3 * @param int $download_id Download ID * @return float $sales Average monthly sales */ function edd_get_average_monthly_download_sales($download_id = 0) { $sales = edd_get_download_sales_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) { $sales = $sales / $months; } return $sales; }
/** * 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'); }