/**
 * Display Total Downloadable Files
 * 
 * Displays the total number of downloadable files.
 *
 * @since   1.3
 */
function dedo_shortcode_ddownload_files($atts)
{
    global $dedo_options;
    // Attributes
    extract(shortcode_atts(array('format' => true, 'cache' => true), $atts, 'ddownload_files'));
    // Supply correct boolean for format and cache
    $format = in_array($format, array('true', 'yes')) ? true : false;
    $cache = in_array($cache, array('true', 'yes')) ? true : false;
    // First check for cached data
    $key = 'dedo_shortcode_files';
    $dedo_cache = new DEDO_Cache($key);
    if (true == $cache && false !== ($cached_data = $dedo_cache->get())) {
        $total_files = $cached_data;
    } else {
        // No cached data, retrieve file count
        $total_files = wp_count_posts('dedo_download');
        $total_files = $total_files->publish;
    }
    // Save to cache
    if (true == $cache) {
        $dedo_cache->set($total_files);
    }
    // Format number
    if ($format) {
        $total_files = number_format_i18n($total_files);
    }
    // Apply filters and return
    return apply_filters('dedo_shortcode_ddownload_files', $total_files);
}
 /**
  * Get Popular Downloads
  *
  * Get popular downloads and order by download count.
  * If days supplied use statistics table, else use download meta.
  *
  * Selecting by days is slow. Use responsibly!
  * 
  * @access public
  * @since 1.4
  * @return array
  */
 function get_popular_downloads($args = array())
 {
     global $wpdb;
     // Parse arguments with defaults
     extract(wp_parse_args($args, array('days' => false, 'limit' => 5, 'cache' => true)));
     // Create cache key
     $key = 'dedo_popular_days' . absint($days) . 'limit' . absint($limit);
     $dedo_cache = new DEDO_Cache($key);
     // Check for cached data
     if (true == $cache && false !== ($cached_data = $dedo_cache->get())) {
         return $cached_data;
     }
     // Days set, convet to start date and use statistics table
     if ($days) {
         $start_date = $this->convert_days_date($days);
         $sql = $wpdb->prepare("\n\t\t\t\tSELECT {$wpdb->ddownload_statistics}.post_id AS ID, COUNT( {$wpdb->ddownload_statistics}.ID ) AS downloads\n\t\t\t\tFROM {$wpdb->ddownload_statistics}\n\t\t\t\tWHERE {$wpdb->ddownload_statistics}.status = %s\n\t\t\t\t\tAND {$wpdb->ddownload_statistics}.date >= %s\n\t\t\t\tGROUP BY {$wpdb->ddownload_statistics}.post_id\n\t\t\t\tORDER BY downloads DESC\n\t\t\t\tLIMIT %d\n\t\t\t", 'success', $start_date, $limit);
         $result = $wpdb->get_results($sql, ARRAY_A);
         // Get title for each download
         foreach ($result as $key2 => $value) {
             $result[$key2]['title'] = get_the_title($result[$key2]['ID']);
         }
     } else {
         $sql = $wpdb->prepare("\n\t\t\t\tSELECT {$wpdb->posts}.ID AS ID, {$wpdb->posts}.post_title AS title, {$wpdb->postmeta}.meta_value AS downloads\n\t\t\t\tFROM {$wpdb->posts}\n\t\t\t\tLEFT JOIN {$wpdb->postmeta}\n\t\t\t\t\tON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id\n\t\t\t\tWHERE {$wpdb->posts}.post_type = %s\n\t\t\t\t\tAND {$wpdb->posts}.post_status = %s\n\t\t\t\t\tAND meta_key = %s\n\t\t\t\t\tAND meta_value > 0\n\t\t\t\tORDER BY CAST( {$wpdb->postmeta}.meta_value AS unsigned ) DESC\n\t\t\t\tLIMIT %d\n\t\t\t", 'dedo_download', 'publish', '_dedo_file_count', $limit);
         $result = $wpdb->get_results($sql, ARRAY_A);
     }
     // Save to cache
     if (true == $cache) {
         $dedo_cache->set($result);
     }
     return $result;
 }