/**
  * @usage Fetch link/page template and return generated html
  * @param $template
  * @param $vars
  * @param string $type
  * @return mixed|string|void
  */
 public static function fetchTemplate($template, $vars, $type = 'link')
 {
     if (!is_array($vars) && is_int($vars) && $vars > 0) {
         $vars = array('ID' => $vars);
     }
     if (!isset($vars['ID']) || intval($vars['ID']) < 1) {
         return '';
     }
     $default['link'] = 'link-template-default.php';
     $default['page'] = 'page-template-default.php';
     if (!isset($vars['formatted'])) {
         $pack = new \WPDM\Package($vars['ID']);
         $pack->Prepare();
         $vars = $pack->PackageData;
     }
     if ($template == '') {
         if (!isset($vars['page_template'])) {
             $vars['page_template'] = 'page-template-default.php';
         }
         $template = $type == 'page' ? $vars['page_template'] : $vars['template'];
     }
     if ($template == '') {
         $template = $default[$type];
     }
     $templates = maybe_unserialize(get_option("_fm_" . $type . "_templates", true));
     if (isset($templates[$template]) && isset($templates[$template]['content'])) {
         $template = $templates[$template]['content'];
     } else {
         if (!strpos(strip_tags($template), "]")) {
             $ltpldir = get_stylesheet_directory() . '/download-manager/' . $type . '-templates/';
             if (!file_exists($ltpldir) || !file_exists($ltpldir . $template)) {
                 $ltpldir = WPDM_BASE_DIR . '/tpls/' . $type . '-templates/';
             }
             if (file_exists(TEMPLATEPATH . '/' . $template)) {
                 $template = file_get_contents(TEMPLATEPATH . '/' . $template);
             } else {
                 if (file_exists($ltpldir . $template)) {
                     $template = file_get_contents($ltpldir . $template);
                 } else {
                     if (file_exists($ltpldir . $template . '.php')) {
                         $template = file_get_contents($ltpldir . $template . '.php');
                     } else {
                         if (file_exists($ltpldir . $type . "-template-" . $template . '.php')) {
                             $template = file_get_contents($ltpldir . $type . "-template-" . $template . '.php');
                         }
                     }
                 }
             }
         }
     }
     preg_match_all("/\\[cf ([^\\]]+)\\]/", $template, $cfmatches);
     preg_match_all("/\\[thumb_([0-9]+)x([0-9]+)\\]/", $template, $matches);
     preg_match_all("/\\[thumb_url_([0-9]+)x([0-9]+)\\]/", $template, $umatches);
     preg_match_all("/\\[thumb_gallery_([0-9]+)x([0-9]+)\\]/", $template, $gmatches);
     preg_match_all("/\\[excerpt_([0-9]+)\\]/", $template, $xmatches);
     preg_match_all("/\\[pdf_thumb_([0-9]+)x([0-9]+)\\]/", $template, $pmatches);
     $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($vars['ID']), 'full');
     $vars['preview'] = $thumb['0'];
     $pdf = isset($vars['files'][0]) ? $vars['files'][0] : '';
     $ext = explode(".", $pdf);
     $ext = end($ext);
     // Parse [pdf_thumb] tag in link/page template
     if (strpos($template, 'pdf_thumb')) {
         if ($ext == 'pdf') {
             $vars['pdf_thumb'] = "<img alt='{$vars['title']}' src='" . wpdm_pdf_thumbnail($pdf, $vars['ID']) . "' />";
         } else {
             $vars['pdf_thumb'] = $vars['preview'] != '' ? "<img alt='{$vars['title']}' src='{$vars['preview']}' />" : "";
         }
     }
     // Parse [pdf_thumb_WxH] tag in link/page template
     foreach ($pmatches[0] as $nd => $scode) {
         $keys[] = $scode;
         $imsrc = wpdm_dynamic_thumb(wpdm_pdf_thumbnail($pdf, $vars['ID']), array($pmatches[1][$nd], $pmatches[2][$nd]));
         $values[] = $imsrc != '' ? "<img src='" . $imsrc . "' alt='{$vars['title']}' />" : '';
     }
     // Parse [file_type] tag in link/page template
     if (strpos($template, 'file_type')) {
         $vars['file_types'] = self::fileTypes($vars['ID'], false);
         $vars['file_type_icons'] = self::fileTypes($vars['ID']);
     }
     foreach ($matches[0] as $nd => $scode) {
         $keys[] = $scode;
         $imsrc = wpdm_dynamic_thumb($vars['preview'], array($matches[1][$nd], $matches[2][$nd]));
         $values[] = $vars['preview'] != '' ? "<img src='" . $imsrc . "' alt='{$vars['title']}' />" : '';
     }
     foreach ($umatches[0] as $nd => $scode) {
         $keys[] = $scode;
         $values[] = $vars['preview'] != '' ? wpdm_dynamic_thumb($vars['preview'], array($umatches[1][$nd], $umatches[2][$nd])) : '';
     }
     foreach ($gmatches[0] as $nd => $scode) {
         $keys[] = $scode;
         $values[] = wpdm_get_additional_preview_images($vars, $gmatches[1][$nd], $gmatches[2][$nd]);
     }
     foreach ($xmatches[0] as $nd => $scode) {
         $keys[] = $scode;
         $ss = substr(strip_tags($vars['description']), 0, intval($xmatches[1][$nd]));
         $tmp = explode(" ", substr(strip_tags($vars['description']), intval($xmatches[1][$nd])));
         $bw = array_shift($tmp);
         $ss .= $bw;
         $values[] = $ss . '...';
     }
     if ($type == 'page' && (strpos($template, '[similar_downloads]') || strpos($vars['description'], '[similar_downloads]'))) {
         $vars['similar_downloads'] = wpdm_similar_packages($vars, 5);
     }
     if (strpos($template, 'doc_preview')) {
         $vars['doc_preview'] = self::docPreview($vars);
     }
     // If need to re-process any data before fetch template
     $vars = apply_filters("wdm_before_fetch_template", $vars);
     foreach ($vars as $key => $value) {
         if (!is_array($value)) {
             $keys[] = "[{$key}]";
             $values[] = $value;
         }
     }
     $loginform = wpdm_login_form(array('redirect' => get_permalink($vars['ID'])));
     $hide_all_message = get_option('__wpdm_login_form', 0) == 1 ? $loginform : stripcslashes(str_replace(array("[loginform]", "[this_url]"), array($loginform, get_permalink($vars['ID'])), get_option('wpdm_login_msg')));
     if ($vars['download_link'] == 'blocked' && $type == 'link') {
         return "";
     }
     if ($vars['download_link'] == 'blocked' && $type == 'page') {
         return get_option('wpdm_permission_msg');
     }
     if ($vars['download_link'] == 'loginform' && $type == 'link') {
         return "";
     }
     if ($vars['download_link'] == 'loginform' && $type == 'page') {
         return $hide_all_message;
     }
     return @str_replace($keys, $values, @stripcslashes($template));
 }
Ejemplo n.º 2
0
/**
 * @param $pid
 * @param $w
 * @param $h
 * @param bool $echo
 * @return string
 * @usage Generates thumbnail html from PDF file attached with a Package. [ From v4.1.3 ]
 */
function wpdm_pdf_preview($pid, $w, $h, $echo = true)
{
    $post = get_post($pid);
    $files = get_post_meta($pid, '__wpdm_files', true);
    $pdf = $files[0];
    $ext = explode(".", $pdf);
    $ext = end($ext);
    $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($pid), 'full');
    $preview = $thumb['0'];
    if ($ext == 'pdf') {
        $pdf_thumb = wpdm_pdf_thumbnail($pdf, $pid);
    } else {
        $pdf_thumb = $preview;
    }
    $imsrc = wpdm_dynamic_thumb($pdf_thumb, array($w, $h));
    if (!$echo) {
        return "<img src='{$imsrc}' alt='{$post->post_title}'/>";
    }
    echo "<img src='{$imsrc}' alt='{$post->post_title}'/>";
}