/**
  * download function.
  *
  * @access public
  *
  * @param array $atts
  *
  * @return string
  */
 public function download($atts, $content = '')
 {
     global $dlm_download;
     extract(shortcode_atts(array('id' => '', 'autop' => false, 'template' => dlm_get_default_download_template(), 'version_id' => '', 'version' => ''), $atts));
     // Make id filterable
     $id = apply_filters('dlm_shortcode_download_id', $id);
     // Check id
     if (empty($id)) {
         return;
     }
     // Allow third party extensions to hijack shortcode
     $hijacked_content = apply_filters('dlm_shortcode_download_content', '', $id, $atts);
     // If there's hijacked content, return it and be done with it
     if ('' !== $hijacked_content) {
         return $hijacked_content;
     }
     // If we have content, wrap in a link only
     if ($content) {
         $download = new DLM_Download($id);
         if ($download->exists()) {
             if (!empty($version)) {
                 $version_id = $dlm_download->get_version_id($version);
             }
             if (isset($version_id) && 0 != $version_id) {
                 $dlm_download->set_version($version_id);
             }
             return '<a href="' . $download->get_the_download_link() . '">' . $content . '</a>';
         } else {
             return '[' . __('Download not found', 'download-monitor') . ']';
         }
     } else {
         ob_start();
         $downloads = new WP_Query(array('post_type' => 'dlm_download', 'posts_per_page' => 1, 'no_found_rows' => 1, 'post_status' => 'publish', 'p' => $id));
         if ($downloads->have_posts()) {
             while ($downloads->have_posts()) {
                 $downloads->the_post();
                 if (!empty($version)) {
                     $version_id = $dlm_download->get_version_id($version);
                 }
                 if (isset($version_id) && 0 != $version_id) {
                     $dlm_download->set_version($version_id);
                 }
                 // Template handler
                 $template_handler = new DLM_Template_Handler();
                 $template_handler->get_template_part('content-download', $template, '', array('dlm_download' => new DLM_Download(get_the_ID())));
             }
         } else {
             echo '[' . __('Download not found', 'download-monitor') . ']';
         }
         wp_reset_postdata();
         if ('true' === $autop || true === $autop) {
             return wpautop(ob_get_clean());
         } else {
             return ob_get_clean();
         }
     }
 }
 /**
  * download function.
  *
  * @access public
  *
  * @param array $atts
  *
  * @return string
  */
 public function download($atts, $content = '')
 {
     // extract shortcode atts
     extract(shortcode_atts(array('id' => '', 'autop' => false, 'template' => dlm_get_default_download_template(), 'version_id' => '', 'version' => ''), $atts));
     // Make id filterable
     $id = apply_filters('dlm_shortcode_download_id', $id);
     // Check id
     if (empty($id)) {
         return;
     }
     // Allow third party extensions to hijack shortcode
     $hijacked_content = apply_filters('dlm_shortcode_download_content', '', $id, $atts);
     // If there's hijacked content, return it and be done with it
     if ('' !== $hijacked_content) {
         return $hijacked_content;
     }
     // shortcode output
     $output = '';
     // create download object
     $download = new DLM_Download($id);
     // check if download exists
     if ($download->exists()) {
         // check if version is set
         if (!empty($version)) {
             $version_id = $download->get_version_id($version);
         }
         // check if version ID is set
         if (isset($version_id) && 0 != $version_id) {
             $download->set_version($version_id);
         }
         // if we have content, wrap in a link only
         if ($content) {
             $output = '<a href="' . $download->get_the_download_link() . '">' . $content . '</a>';
         } else {
             // template handler
             $template_handler = new DLM_Template_Handler();
             // buffer
             ob_start();
             // load template
             $template_handler->get_template_part('content-download', $template, '', array('dlm_download' => $download));
             // get output
             $output = ob_get_clean();
             // check if we need to wpautop()
             if ('true' === $autop || true === $autop) {
                 $output = wpautop($output);
             }
         }
     } else {
         $output = '[' . __('Download not found', 'download-monitor') . ']';
     }
     // reset post data
     wp_reset_postdata();
     return $output;
 }
 /**
  * download function.
  *
  * @access public
  * @param mixed $atts
  * @return void
  */
 public function download($atts, $content = '')
 {
     global $download_monitor, $dlm_download;
     extract(shortcode_atts(array('id' => '', 'autop' => false, 'template' => dlm_get_default_download_template(), 'version_id' => '', 'version' => ''), $atts));
     $id = apply_filters('dlm_shortcode_download_id', $id);
     if (empty($id)) {
         return;
     }
     // If we have content, wrap in a link only
     if ($content) {
         $download = new DLM_Download($id);
         if ($download->exists()) {
             if ($version) {
                 $version_id = $dlm_download->get_version_id($version);
             }
             if ($version_id) {
                 $dlm_download->set_version($version_id);
             }
             return '<a href="' . $download->get_the_download_link() . '">' . $content . '</a>';
         } else {
             return '[' . __('Download not found', 'download_monitor') . ']';
         }
     } else {
         ob_start();
         $downloads = new WP_Query(array('post_type' => 'dlm_download', 'posts_per_page' => 1, 'no_found_rows' => 1, 'post_status' => 'publish', 'p' => $id));
         if ($downloads->have_posts()) {
             while ($downloads->have_posts()) {
                 $downloads->the_post();
                 if ($version) {
                     $version_id = $dlm_download->get_version_id($version);
                 }
                 if ($version_id) {
                     $dlm_download->set_version($version_id);
                 }
                 $download_monitor->get_template_part('content-download', $template);
             }
         } else {
             echo '[' . __('Download not found', 'download_monitor') . ']';
         }
         wp_reset_postdata();
         if ($autop === 'true' || $autop === true) {
             return wpautop(ob_get_clean());
         } else {
             return ob_get_clean();
         }
     }
 }
 /**
  * column_default function.
  *
  * @access public
  *
  * @param mixed $log
  * @param mixed $column_name
  *
  * @return void
  */
 function column_default($log, $column_name)
 {
     switch ($column_name) {
         case 'status':
             switch ($log->download_status) {
                 case 'failed':
                     $download_status = '<span class="failed" title="' . esc_attr($log->download_status_message) . '">&nbsp;</span>';
                     break;
                 case 'redirected':
                     $download_status = '<span class="redirected" title="' . esc_attr($log->download_status_message) . '">&nbsp;</span>';
                     break;
                 default:
                     $download_status = '<span class="completed" title="' . __('Download Complete', 'download-monitor') . '">&nbsp;</span>';
                     break;
             }
             return $download_status;
             break;
         case 'date':
             return '<time title="' . date_i18n(get_option('date_format') . ' @ ' . get_option('time_format'), strtotime($log->download_date)) . '"">' . sprintf(__('%s ago', 'download-monitor'), human_time_diff(strtotime($log->download_date), current_time('timestamp'))) . '</time>';
             break;
         case 'download':
             $download = new DLM_Download($log->download_id);
             $download->set_version($log->version_id);
             if (!$download->exists()) {
                 $download_string = sprintf(__('Download #%d (no longer exists)', 'download-monitor'), $log->download_id);
             } else {
                 $download_string = '<a href="' . admin_url('post.php?post=' . $download->id . '&action=edit') . '">';
                 $download_string .= '#' . $download->id . ' &ndash; ' . $download->get_the_title();
                 $download_string .= '</a>';
             }
             if ($log->version) {
                 $download_string .= ' (' . sprintf(__('v%s', 'download-monitor'), $log->version) . ')';
             }
             return $download_string;
             break;
         case 'file':
             $download = new DLM_Download($log->download_id);
             $download->set_version($log->version_id);
             if ($download->exists() && $download->get_the_filename()) {
                 $download_string = '<code>' . $download->get_the_filename() . '</code>';
             } else {
                 $download_string = '&ndash;';
             }
             return $download_string;
             break;
         case 'user':
             if ($log->user_id) {
                 $user = get_user_by('id', $log->user_id);
             }
             if (!isset($user) || !$user) {
                 $user_string = __('Non-member', 'download-monitor');
             } else {
                 $user_string = '<a href="' . admin_url('user-edit.php?user_id=' . $user->ID) . '">';
                 $user_string .= $user->user_login . ' &ndash; ';
                 $user_string .= '<a href="mailto:' . $user->user_email . '">';
                 $user_string .= $user->user_email;
                 $user_string .= '</a>';
             }
             return $user_string;
             break;
         case 'user_ip':
             return '<a href="http://whois.arin.net/rest/ip/' . $log->user_ip . '" target="_blank">' . $log->user_ip . '</a>';
             break;
         case 'user_ua':
             $ua = $this->uaparser->parse($log->user_agent);
             return $ua->toFullString;
             break;
     }
 }
 /**
  * export_logs function
  */
 public function export_logs()
 {
     global $wpdb;
     if (empty($_GET['dlm_download_logs'])) {
         return;
     }
     $filter_status = isset($_REQUEST['filter_status']) ? sanitize_text_field($_REQUEST['filter_status']) : '';
     $filter_month = !empty($_REQUEST['filter_month']) ? sanitize_text_field($_REQUEST['filter_month']) : '';
     $items = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->download_log}\n\t\t    \tWHERE type = 'download'\n\t\t    \t" . ($filter_status ? "AND download_status = '%s'" : "%s") . "\n\t            " . ($filter_month ? "AND download_date >= '%s'" : "%s") . "\n\t            " . ($filter_month ? "AND download_date <= '%s'" : "%s") . "\n\t\t    \tORDER BY download_date DESC", $filter_status ? $filter_status : "", $filter_month ? date('Y-m-01', strtotime($filter_month)) : "", $filter_month ? date('Y-m-t', strtotime($filter_month)) : ""));
     $rows = array();
     $row = array();
     $row[] = __('Download ID', 'download_monitor');
     $row[] = __('Version ID', 'download_monitor');
     $row[] = __('Filename', 'download_monitor');
     $row[] = __('User ID', 'download_monitor');
     $row[] = __('User Login', 'download_monitor');
     $row[] = __('User Email', 'download_monitor');
     $row[] = __('User IP', 'download_monitor');
     $row[] = __('User Agent', 'download_monitor');
     $row[] = __('Date', 'download_monitor');
     $row[] = __('Status', 'download_monitor');
     $rows[] = '"' . implode('","', $row) . '"';
     if (!empty($items)) {
         foreach ($items as $item) {
             $row = array();
             $row[] = $item->download_id;
             $row[] = $item->version_id;
             $download = new DLM_Download($item->download_id);
             $download->set_version($item->version_id);
             if ($download->exists() && $download->get_the_filename()) {
                 $row[] = $download->get_the_filename();
             } else {
                 $row[] = '-';
             }
             $row[] = $item->user_id;
             if ($item->user_id) {
                 $user = get_user_by('id', $item->user_id);
             }
             if (!isset($user) || !$user) {
                 $row[] = '-';
                 $row[] = '-';
             } else {
                 $row[] = $user->user_login;
                 $row[] = $user->user_email;
             }
             $row[] = $item->user_ip;
             $row[] = $item->user_agent;
             $row[] = $item->download_date;
             $row[] = $item->download_status . ($item->download_status_message ? ' - ' : '') . $item->download_status_message;
             $rows[] = '"' . implode('","', $row) . '"';
         }
     }
     $log = implode("\n", $rows);
     header("Content-type: text/csv");
     header("Content-Disposition: attachment; filename=download_log.csv");
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
     header("Content-Length: " . strlen($log));
     echo $log;
     exit;
 }
function pmprodlm_shortcode_download_content($content, $download_id, $atts)
{
    global $current_user;
    if (empty($atts['template']) && function_exists('pmpro_hasMembershipLevel')) {
        if (!pmpro_has_membership_access($download_id)) {
            $dlm_download = new DLM_Download($download_id);
            if ($dlm_download->exists()) {
                $download_membership_levels = pmprodlm_getDownloadLevels($dlm_download);
                $content .= '<a href="';
                if (count($download_membership_levels[0]) > 1) {
                    $content .= pmpro_url('levels');
                } else {
                    $content .= pmpro_url("checkout", "?level=" . $download_membership_levels[0][0], "https");
                }
                $content .= '">' . $dlm_download->get_the_title() . '</a>';
                $content .= ' (' . __('Membership Required', 'pmprodlm') . ': ' . $download_membership_levels[1] . ')';
                $content = apply_filters("pmprodlm_shortcode_download_content_filter", $content);
            } else {
                $content = '[' . __('Download not found', 'download-monitor') . ']';
            }
        }
    }
    return $content;
}