AddOns.php
5 years ago
Categories.php
3 years ago
Packages.php
1 week ago
Settings.php
5 months ago
Stats.php
4 years ago
Templates.php
2 years ago
Welcome.php
6 months ago
Templates.php
248 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDM\Admin\Menu; |
| 4 | |
| 5 | |
| 6 | use WPDM\__\__; |
| 7 | use WPDM\__\Crypt; |
| 8 | use WPDM\__\Template; |
| 9 | |
| 10 | class Templates |
| 11 | { |
| 12 | |
| 13 | function __construct() |
| 14 | { |
| 15 | add_filter('init', array($this, 'livePreview'), 99 ); |
| 16 | add_filter('wdm_before_fetch_template', array($this, 'introduceCustomTags'), 99, 3 ); |
| 17 | //add_action('admin_init', array($this, 'Save')); |
| 18 | add_action('wp_ajax_template_preview', array($this, 'preview')); |
| 19 | add_action('wp_ajax_wpdm_save_email_template', array($this, 'saveEmailTemplate')); |
| 20 | add_action('wp_ajax_update_template_status', array($this, 'updateTemplateStatus')); |
| 21 | add_action('wp_ajax_wpdm_save_email_setting', array($this, 'saveEmailSetting')); |
| 22 | add_action('wp_ajax_connect_template_server', array($this, 'templateServer')); |
| 23 | add_action('admin_menu', array($this, 'menu')); |
| 24 | } |
| 25 | |
| 26 | function livePreview(){ |
| 27 | if(wpdm_query_var('template_preview') !== '') { |
| 28 | __::isAuthentic('_tplnonce', NONCE_KEY, WPDM_ADMIN_CAP); |
| 29 | add_filter( 'show_admin_bar', '__return_false' ); |
| 30 | $page_template = Template::locate('template-preview.php', WPDM_SRC_DIR.'__/views'); |
| 31 | $type = wpdm_query_var('_type'); |
| 32 | global $post; |
| 33 | if(wpdm_query_var('pid')) |
| 34 | $package = get_post(wpdm_query_var('pid'), ARRAY_A); |
| 35 | else { |
| 36 | $package = get_posts(array('post_type' => 'wpdmpro', 'posts_per_page' => 1, 'post_status' => 'publish')); |
| 37 | $package = (array)$package[0]; |
| 38 | } |
| 39 | $template = Crypt::decrypt(wpdm_query_var('template_preview')); |
| 40 | $template = stripslashes_deep(str_replace(array("\r", "\n"), "", html_entity_decode(urldecode($template)))); |
| 41 | $output = wpdm_fetch_template($template, $package, $type); |
| 42 | $template = "<div class='w3eden' style='max-width: 900px;margin: 20px auto !important;padding: 40px;'>{$output}</div><script> jQuery(function($) { var body = document.body, html = document.documentElement; var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); window.parent.wpdmifh(height); });</script>"; |
| 43 | include $page_template; |
| 44 | die(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | function menu() |
| 49 | { |
| 50 | add_submenu_page('edit.php?post_type=wpdmpro', __( "Templates ‹ Download Manager" , "download-manager" ), __( "Templates" , "download-manager" ), WPDM_MENU_ACCESS_CAP, 'templates', array($this, 'UI')); |
| 51 | } |
| 52 | |
| 53 | function UI(){ |
| 54 | $ttype = isset($_GET['_type']) ? wpdm_query_var('_type') : 'link'; |
| 55 | |
| 56 | if (isset($_GET['task']) && ($_GET['task'] == 'EditTemplate' || $_GET['task'] == 'NewTemplate')) |
| 57 | Templates::editor(); |
| 58 | else if (isset($_GET['task']) && $_GET['task'] == 'EditEmailTemplate') |
| 59 | Templates::emailEditor(); |
| 60 | else |
| 61 | Templates::Show(); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | public static function editor(){ |
| 66 | include wpdm_admin_tpl_path("templates/template-editor.php"); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | public static function emailEditor(){ |
| 71 | include wpdm_admin_tpl_path("templates/email-template-editor.php"); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | public static function Show(){ |
| 76 | WPDM()->packageTemplate->covertAll()->covertAll('page'); |
| 77 | include wpdm_admin_tpl_path('templates/templates.php'); |
| 78 | } |
| 79 | |
| 80 | function saveEmailTemplate(){ |
| 81 | if (isset($_POST['email_template'])) { |
| 82 | __::isAuthentic('__setnonce', WPDM_PRI_NONCE, WPDM_ADMIN_CAP); |
| 83 | $email_template = wpdm_query_var('email_template', array('validate' => array('subject' => '', 'message' => 'escs', 'from_name' => '', 'from_email' => ''))); |
| 84 | update_option("__wpdm_etpl_".wpdm_query_var('id'), $email_template, false); |
| 85 | wp_send_json(array('success' => true, 'message' => 'Email Template Saved!')); |
| 86 | //header("location: edit.php?post_type=wpdmpro&page=templates&_type=$ttype"); |
| 87 | die(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @usage Preview link/page template |
| 93 | */ |
| 94 | function preview() |
| 95 | { |
| 96 | |
| 97 | $wposts = array(); |
| 98 | __::isAuthentic('_tplnonce', WPDM_PUB_NONCE, WPDM_ADMIN_CAP); |
| 99 | $template = isset($_REQUEST['template'])?wpdm_query_var('template', 'escs'):''; |
| 100 | $type = wpdm_query_var("_type"); |
| 101 | $css = wpdm_query_var("css","txt"); |
| 102 | |
| 103 | |
| 104 | $args=array( |
| 105 | 'post_type' => 'wpdmpro', |
| 106 | 'posts_per_page' => 1 |
| 107 | ); |
| 108 | |
| 109 | $wposts = get_posts( $args ); |
| 110 | $template = stripslashes($template); |
| 111 | $template = urlencode($template); |
| 112 | $tplnonce = wp_create_nonce(NONCE_KEY); |
| 113 | $template_enc = Crypt::encrypt($template); |
| 114 | $preview_link = home_url("/?template_preview={$template_enc}&_type={$type}&_tplnonce={$tplnonce}"); |
| 115 | |
| 116 | if(count($wposts)==0) $html = "<div class='w3eden'><div class='col-md-12'><div class='alert alert-info'>".__( "No package found! Please create at least 1 package to see template preview" , "download-manager" )."</div> </div></div>"; |
| 117 | else |
| 118 | $html = "<a class='btn btn-link btn-block' href='{$preview_link}' target='_blank'><i class='fa fa-external-link-alt'></i> Preview in a new window</a><iframe id='templateiframe' src='{$preview_link}' style='border: 0;width: 100%;height: 200px;overflow: hidden'></iframe><script>function wpdmifh( h ){ jQuery('#templateiframe').height(h); }</script>"; |
| 119 | |
| 120 | echo $html; |
| 121 | die(); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | public static function dropdown($params, $activeOnly = false) |
| 126 | { |
| 127 | extract($params); |
| 128 | $type = isset($type) && in_array($type, array('link', 'page', 'email')) ? esc_attr($type) : 'link'; |
| 129 | $tplstatus = maybe_unserialize(get_option("_fm_{$type}_template_status")); |
| 130 | |
| 131 | $xactivetpls = array(); |
| 132 | $activetpls = array(); |
| 133 | if(is_array($tplstatus)) { |
| 134 | foreach ($tplstatus as $tpl => $active) { |
| 135 | if (!$active) |
| 136 | $xactivetpls[] = $tpl; |
| 137 | else |
| 138 | $activetpls[] = $tpl; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | $ttpldir = get_stylesheet_directory() . '/download-manager/' . $type . '-templates/'; |
| 143 | $ttpls = array(); |
| 144 | if(file_exists($ttpldir)) { |
| 145 | $ttpls = scandir($ttpldir); |
| 146 | array_shift($ttpls); |
| 147 | array_shift($ttpls); |
| 148 | } |
| 149 | |
| 150 | $ltpldir = WPDM()->package->templateDir . $type . '-templates/'; |
| 151 | |
| 152 | $ctpls = scandir($ltpldir); |
| 153 | array_shift($ctpls); |
| 154 | array_shift($ctpls); |
| 155 | |
| 156 | foreach($ctpls as $ind => $tpl){ |
| 157 | $ctpls[$ind] = $ltpldir.$tpl; |
| 158 | } |
| 159 | |
| 160 | foreach($ttpls as $tpl){ |
| 161 | if(!in_array($ltpldir.$tpl, $ctpls)) { |
| 162 | $ctpls[] = $ttpldir . $tpl; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | //$custom_templates = maybe_unserialize(get_option("_fm_{$type}_templates", array())); |
| 167 | $custom_templates = WPDM()->packageTemplate->getCustomTemplates($type); |
| 168 | $custom_templates = array_reverse($custom_templates); |
| 169 | $name = isset($name)?$name:$type.'_template'; |
| 170 | $css = isset($css)?"style='$css'":''; |
| 171 | $id = isset($id)?$id:uniqid(); |
| 172 | $default = $type == 'link'?'link-template-default.php':'page-template-default.php'; |
| 173 | $xdf = str_replace(".php", "", $default); |
| 174 | $html = ""; |
| 175 | if(is_array($xactivetpls) && count($xactivetpls) > 0) |
| 176 | $default = (in_array($xdf, $xactivetpls) || in_array($default, $xactivetpls)) && isset($activetpls[0])?$activetpls[0]:$default; |
| 177 | |
| 178 | $html .= "<select name='$name' id='$id' class='form-control template {$type}_template' {$css}><option value='$default'>Select ".ucfirst($type)." Template</option>"; |
| 179 | $data = array(); |
| 180 | if(is_array($custom_templates)) { |
| 181 | foreach ($custom_templates as $id => $template) { |
| 182 | if(!$activeOnly || ($activeOnly && (!isset($tplstatus[$id]) || $tplstatus[$id] == 1))) { |
| 183 | $data[$id] = $template['title']; |
| 184 | $eselected = isset($selected) && $selected == $id ? 'selected=selected' : ''; |
| 185 | $html .= "<option value='{$id}' {$eselected}>{$template['title']}</option>"; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | foreach ($ctpls as $ctpl) { |
| 190 | $ind = str_replace(".php", "", basename($ctpl)); |
| 191 | if(!$activeOnly || ($activeOnly && (!isset($tplstatus[$ind]) || $tplstatus[$ind] == 1))) { |
| 192 | $tmpdata = file_get_contents($ctpl); |
| 193 | $regx = "/WPDM.*Template[\s]*:([^\-\->]+)/"; |
| 194 | if (preg_match($regx, $tmpdata, $matches)) { |
| 195 | $data[basename($ctpl)] = $matches[1]; |
| 196 | $eselected = isset($selected) && $selected == basename($ctpl) ? 'selected=selected' : ''; |
| 197 | |
| 198 | $html .= "<option value='" . basename($ctpl) . "' {$eselected}>{$matches[1]}</option>"; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | $html .= "</select>"; |
| 203 | |
| 204 | return isset($data_type) && $data_type == 'ARRAY'? $data : $html; |
| 205 | } |
| 206 | |
| 207 | function saveEmailSetting(){ |
| 208 | __::isAuthentic('__sesnonce', WPDM_PRI_NONCE, WPDM_ADMIN_CAP, true); |
| 209 | |
| 210 | update_option('__wpdm_email_template', wpdm_query_var('__wpdm_email_template'), false); |
| 211 | $email_settings = wpdm_query_var('__wpdm_email_setting', array('validate' => array('logo' => 'url', 'banner' => 'url', 'youtube' => 'url', 'twitter' => 'url', 'facebook' => 'url', 'footer_text' => 'txts'))); |
| 212 | update_option('__wpdm_email_setting', $email_settings, false); |
| 213 | die("Done!"); |
| 214 | } |
| 215 | |
| 216 | function updateTemplateStatus(){ |
| 217 | __::isAuthentic('tsnonce', WPDM_PRI_NONCE, WPDM_ADMIN_CAP); |
| 218 | $type = wpdm_query_var('type', 'txt'); |
| 219 | $tpldata = maybe_unserialize(get_option("_fm_{$type}_template_status")); |
| 220 | $tpldata[wpdm_query_var('template')] = wpdm_query_var('status', 'int'); |
| 221 | update_option("_fm_{$type}_template_status", $tpldata, false); |
| 222 | echo "OK"; |
| 223 | die(); |
| 224 | } |
| 225 | |
| 226 | function introduceCustomTags($vars, $template, $type){ |
| 227 | $upload_dir = wp_upload_dir(); |
| 228 | $upload_dir = $upload_dir['basedir']; |
| 229 | $tags_dir = $upload_dir.'/wpdm-custom-tags/'; |
| 230 | if(!file_exists($tags_dir)) mkdir($tags_dir, 0755, true); |
| 231 | $custom_tags = scandir($tags_dir); |
| 232 | foreach ($custom_tags as $custom_tag) { |
| 233 | if (strstr($custom_tag, '.tag')) { |
| 234 | $content = file_get_contents($tags_dir . $custom_tag); |
| 235 | $custom_tag = str_replace(".tag", "", $custom_tag); |
| 236 | $vars[$custom_tag] = stripslashes($content); |
| 237 | } |
| 238 | } |
| 239 | return $vars; |
| 240 | } |
| 241 | |
| 242 | function templateServer(){ |
| 243 | wpdmpro_required(); |
| 244 | die(); |
| 245 | } |
| 246 | |
| 247 | } |
| 248 |