/**
 * Converts old sale and file download logs to new logging system
 *
 * @since 1.3.1
 * @uses WP_Query
 * @uses EDD_Logging
 * @return void
 */
function edd_v131_upgrades()
{
    if (get_option('edd_logs_upgraded')) {
        return;
    }
    if (version_compare(get_option('edd_version'), '1.3', '>=')) {
        return;
    }
    ignore_user_abort(true);
    if (!edd_is_func_disabled('set_time_limit') && !ini_get('safe_mode')) {
        set_time_limit(0);
    }
    $args = array('post_type' => 'download', 'posts_per_page' => -1, 'post_status' => 'publish');
    $query = new WP_Query($args);
    $downloads = $query->get_posts();
    if ($downloads) {
        $edd_log = new EDD_Logging();
        foreach ($downloads as $download) {
            // Convert sale logs
            $sale_logs = edd_get_download_sales_log($download->ID, false);
            if ($sale_logs) {
                foreach ($sale_logs['sales'] as $sale) {
                    $log_data = array('post_parent' => $download->ID, 'post_date' => $sale['date'], 'log_type' => 'sale');
                    $log_meta = array('payment_id' => $sale['payment_id']);
                    $log = $edd_log->insert_log($log_data, $log_meta);
                }
            }
            // Convert file download logs
            $file_logs = edd_get_file_download_log($download->ID, false);
            if ($file_logs) {
                foreach ($file_logs['downloads'] as $log) {
                    $log_data = array('post_parent' => $download->ID, 'post_date' => $log['date'], 'log_type' => 'file_download');
                    $log_meta = array('user_info' => $log['user_info'], 'file_id' => $log['file_id'], 'ip' => $log['ip']);
                    $log = $edd_log->insert_log($log_data, $log_meta);
                }
            }
        }
    }
    add_option('edd_logs_upgraded', '1');
}
/**
 * Render Download Log Meta Box
 *
 * @access      private
 * @since       1.0 
 * @return      void
*/
function edd_render_download_log_meta_box()
{
    global $post;
    $per_page = 10;
    if (isset($_GET['edd_log_page'])) {
        $page = intval($_GET['edd_log_page']);
        $offset = $per_page * ($page - 1);
        $download_log = edd_get_file_download_log($post->ID, true, $per_page, $offset);
    } else {
        $page = 1;
        $download_log = edd_get_file_download_log($post->ID, true);
    }
    $files = edd_get_download_files($post->ID);
    echo '<table class="form-table">';
    echo '<tr>';
    echo '<th style="width:20%"><strong>' . __('Download Log', 'edd') . '</strong></th>';
    echo '<td colspan="4" class="edd_download_stats">';
    _e('Each time a file is downloaded, it is recorded below.', 'edd');
    echo '</td>';
    echo '</tr>';
    if ($download_log) {
        foreach ($download_log['downloads'] as $file_download) {
            $user_id = isset($file_download['user_info']['id']) ? $file_download['user_info']['id'] : 0;
            $user_data = get_userdata($user_id);
            if ($user_data) {
                $name = $user_data->display_name;
            } else {
                $name = $file_download['user_info']['email'];
            }
            $file_name = $files[$file_download['file_id']]['name'];
            echo '<tr>';
            echo '<td class="edd_download_sales_log">';
            echo '<strong>' . __('Date:', 'edd') . '</strong> ' . $file_download['date'];
            echo '</td>';
            echo '<td class="edd_download_sales_log">';
            echo '<strong>' . __('Downloaded by:', 'edd') . '</strong> ' . $name;
            echo '</td>';
            echo '<td class="edd_download_sales_log">';
            echo '<strong>' . __('IP Address:', 'edd') . '</strong> ' . $file_download['ip'];
            echo '</td>';
            echo '<td colspan="2" class="edd_download_sales_log">';
            echo '<strong>' . __('File: ', 'edd') . '</strong> ' . $file_name;
            echo '</td>';
            echo '</tr>';
            do_action('edd_download_log__meta_box');
        }
        // endforeach
    } else {
        echo '<tr>';
        echo '<td colspan=4" class="edd_download_sales_log">';
        echo __('No file downloads yet yet', 'edd');
        echo '</td>';
        echo '</tr>';
    }
    echo '</table>';
    $total_log_entries = $download_log['number'];
    $total_pages = ceil($total_log_entries / $per_page);
    if ($total_pages > 1) {
        echo '<div class="tablenav">';
        echo '<div class="tablenav-pages alignright">';
        $base = 'post.php?post=' . $post->ID . '&action=edit%_%';
        echo paginate_links(array('base' => $base, 'format' => '&edd_log_page=%#%', 'prev_text' => '&laquo; ' . __('Previous', 'edd'), 'next_text' => __('Next', 'edd') . ' &raquo;', 'total' => $total_pages, 'current' => $page, 'end_size' => 1, 'mid_size' => 5, 'add_fragment' => '#edd_file_download_log'));
        echo '</div>';
        echo '</div><!--end .tablenav-->';
    }
}