/** * @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)); }
/** * @usage Organize package data using all available variable * @param $vars * @return mixed */ function wpdm_setup_package_data($vars) { if (isset($vars['formatted'])) { return $vars; } if (!isset($vars['ID'])) { return $vars; } $pack = new \WPDM\Package($vars['ID']); $pack->Prepare(); return $pack->PackageData; }