WordPress Download Manager - Version 3.2.34

Version Description

  • 2022.01.11 =
  • Fixed data sanitization issue on stats page
  • Optimized json api calls
Download this release

Release Info

Developer codename065
Plugin Icon 128x128 WordPress Download Manager
Version 3.2.34
Comparing to
See all releases

Code changes from version 3.2.33 to 3.2.34

download-manager.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: https://www.wpdownloadmanager.com/purchases/
5
  Description: Manage, Protect and Track file downloads, and sell digital products from your WordPress site. A complete digital asset management solution.
6
  Author: W3 Eden, Inc.
7
  Author URI: https://www.wpdownloadmanager.com/
8
- Version: 3.2.33
9
  Text Domain: download-manager
10
  Domain Path: /languages
11
  */
@@ -39,7 +39,7 @@ use WPDM\Widgets\WidgetController;
39
 
40
  global $WPDM;
41
 
42
- define('WPDM_VERSION','3.2.33');
43
 
44
  define('WPDM_TEXT_DOMAIN','download-manager');
45
 
5
  Description: Manage, Protect and Track file downloads, and sell digital products from your WordPress site. A complete digital asset management solution.
6
  Author: W3 Eden, Inc.
7
  Author URI: https://www.wpdownloadmanager.com/
8
+ Version: 3.2.34
9
  Text Domain: download-manager
10
  Domain Path: /languages
11
  */
39
 
40
  global $WPDM;
41
 
42
+ define('WPDM_VERSION','3.2.34');
43
 
44
  define('WPDM_TEXT_DOMAIN','download-manager');
45
 
readme.txt CHANGED
@@ -5,7 +5,7 @@ Tags: download manager, document management, file manager, digital store, ecomme
5
  Requires at least: 5.3
6
  Tested up to: 5.8
7
  License: GPLv2 or later
8
- Stable tag: 3.2.32
9
 
10
 
11
  This File Management & Digital Store plugin will help you to control file downloads & sell digital products from your WP site.
@@ -181,7 +181,11 @@ Check download stats and get a push notification when someone downloads, install
181
 
182
  == Changelog ==
183
 
184
- = 3.2.33 - 2021.12.21 =
 
 
 
 
185
  * Codebase optimization
186
  * Changed name from `WordPress Download Manager` to `Download Manager`
187
 
5
  Requires at least: 5.3
6
  Tested up to: 5.8
7
  License: GPLv2 or later
8
+ Stable tag: 3.2.34
9
 
10
 
11
  This File Management & Digital Store plugin will help you to control file downloads & sell digital products from your WP site.
181
 
182
  == Changelog ==
183
 
184
+ = 3.2.34 - 2022.01.11 =
185
+ * Fixed data sanitization issue on stats page
186
+ * Optimized json api calls
187
+
188
+ = 3.2.33 - 2022.01.11 =
189
  * Codebase optimization
190
  * Changed name from `WordPress Download Manager` to `Download Manager`
191
 
src/Admin/Menu/Packages.php CHANGED
@@ -60,7 +60,7 @@ class Packages
60
  delete_post_meta($post, $k);
61
 
62
  }
63
-
64
  foreach ($_POST['file'] as $meta_key => $meta_value) {
65
  $key_name = "__wpdm_" . $meta_key;
66
  if($meta_key == 'package_dir' && $meta_value != '') {
60
  delete_post_meta($post, $k);
61
 
62
  }
63
+
64
  foreach ($_POST['file'] as $meta_key => $meta_value) {
65
  $key_name = "__wpdm_" . $meta_key;
66
  if($meta_key == 'package_dir' && $meta_value != '') {
src/Admin/views/metaboxes/attach-file/upload-file.php CHANGED
@@ -13,7 +13,7 @@ $afile = wpdm_valueof($afiles, 0);
13
  <?php
14
  $filesize = "<em style='color: darkred'>( ".__("attached file is missing/deleted",'download-manager')." )</em>";
15
  $afile = trim($afile);
16
- $mfz = get_post_meta(get_the_ID(), '__wpdm_package_size', true);
17
  $url = false;
18
  if($afile !=''){
19
  if(substr_count($afile, "://") > 0){
@@ -37,7 +37,7 @@ $afile = wpdm_valueof($afiles, 0);
37
  <a href="#" id="dcf" title="Delete Current File" class="pull-right" style="font-size:24px">
38
  <i class="fa fa-trash color-red"></i>
39
  </a>
40
- <div class="media-body"><strong><?php echo basename($afile); ?></strong><br><span class="text-success"><?php echo (double)$mfz && !$url ?$mfz:$filesize; ?></span></div>
41
  </div>
42
 
43
  <?php } else echo "<span style='font-weight:bold;color:#ddd'>". __('No file uploaded yet!', 'download-manager')."</span>"; ?>
13
  <?php
14
  $filesize = "<em style='color: darkred'>( ".__("attached file is missing/deleted",'download-manager')." )</em>";
15
  $afile = trim($afile);
16
+
17
  $url = false;
18
  if($afile !=''){
19
  if(substr_count($afile, "://") > 0){
37
  <a href="#" id="dcf" title="Delete Current File" class="pull-right" style="font-size:24px">
38
  <i class="fa fa-trash color-red"></i>
39
  </a>
40
+ <div class="media-body"><strong><?php echo basename($afile); ?></strong><br><span class="text-success"><?php echo $filesize; ?></span></div>
41
  </div>
42
 
43
  <?php } else echo "<span style='font-weight:bold;color:#ddd'>". __('No file uploaded yet!', 'download-manager')."</span>"; ?>
src/Admin/views/stats/history.php CHANGED
@@ -22,8 +22,8 @@ $page_no = wpdm_query_var('page_no', 'int') ?: 1;
22
  */
23
  $from_date_string = sanitize_text_field($from_date_string);
24
  $to_date_string = sanitize_text_field($to_date_string);
25
- $user_ids = array_map('sanitize_text_field', $user_ids);
26
- $package_ids = array_map('sanitize_text_field', $package_ids);
27
 
28
  /**
29
  * Selected/Initial Values for the fields
22
  */
23
  $from_date_string = sanitize_text_field($from_date_string);
24
  $to_date_string = sanitize_text_field($to_date_string);
25
+ $user_ids = \WPDM\__\__::sanitize_array($user_ids, 'int');
26
+ $package_ids = \WPDM\__\__::sanitize_array($package_ids, 'int');
27
 
28
  /**
29
  * Selected/Initial Values for the fields
src/Package/PackageController.php CHANGED
@@ -12,272 +12,314 @@ use WPDM\__\Query;
12
  use WPDM\__\Session;
13
  use WPDM\__\TempStorage;
14
 
15
- class PackageController extends PackageTemplate
16
- {
17
-
18
- public $ID;
19
- public $package;
20
- public $shortCodes;
21
- public $packageData = [];
22
- public $files = [];
23
- public $restAPI;
24
- public $templateDir;
25
-
26
-
27
- function __construct($ID = null)
28
- {
29
- parent::__construct();
30
-
31
- $this->templateDir = __DIR__.'/views/';
32
- $this->shortCodes = new Shortcodes();
33
- new Hooks();
34
-
35
- new RestAPI();
36
-
37
- global $post;
38
- if (!$ID && is_object($post) && $post->post_type == 'wpdmpro') $ID = $post->ID;
39
- $this->ID = $ID;
40
-
41
- }
42
-
43
- function prepare($ID = null, $template = null, $template_type = 'page')
44
- {
45
- global $post;
46
-
47
- if (!$ID) $ID = $this->ID;
48
- if (!$ID && isset($post->ID) && get_post_type($post) == 'wpdmpro') $ID = $post->ID;
49
- if (!$ID) {
50
- $this->packageData = array('error' => __("ID missing!", "download-manager"));
51
- return $this;
52
- }
53
-
54
- if (!is_object($post) || $post->ID != $ID) {
55
- $_post = get_post($ID);
56
- if (get_class($_post) != 'WP_Post') return new \WP_Error('broke', __("Invalid Package ID!", "download-manager"));
57
- $post_vars = (array)$_post;
58
- } else
59
- $post_vars = (array)$post;
60
-
61
- $ID = $post_vars['ID'];
62
-
63
- $post_vars['title'] = stripcslashes($post_vars['post_title']);
64
- $post_vars['description'] = stripcslashes($post_vars['post_content']);
65
- $post_vars['description'] = wpautop(stripslashes($post_vars['description']));
66
-
67
- if (!has_shortcode($post_vars['description'], 'wpdm_package'))
68
- $post_vars['description'] = do_shortcode(stripslashes($post_vars['description']));
69
-
70
- $post_vars['excerpt'] = wpautop(stripcslashes(wpdm_escs($post_vars['post_excerpt'])));
71
- $author = get_user_by('id', $post_vars['post_author']);
72
- if (is_object($author)) $post_vars['author_name'] = $author->display_name;
73
- $post_vars['author_profile_url'] = get_author_posts_url($post_vars['post_author']);
74
- if (is_object($author)) $post_vars['avatar_url'] = get_avatar_url($author->user_email);
75
- if (is_object($author)) $post_vars['avatar'] = get_avatar($author->user_email, 96);
76
- $post_vars['author_package_count'] = count_user_posts($post_vars['post_author'], "wpdmpro");
77
-
78
- $template_tags = $this->parseTemplate($template, $ID, $template_type);
79
-
80
- //Featured Image
81
- $post_vars['preview'] = $post_vars['thumb'] = "";
82
- if (has_post_thumbnail($ID)) {
83
- $src = wp_get_attachment_image_src(get_post_thumbnail_id($ID), 'full', false);
84
- $post_vars['preview'] = $src['0'];
85
- $post_vars['featured_image'] = get_the_post_thumbnail($ID, 'full');
86
-
87
- if (in_array('thumb', $template_tags))
88
- $post_vars['thumb'] = get_the_post_thumbnail($ID);
89
- }
90
- if (in_array('create_date', $template_tags))
91
- $post_vars['create_date'] = get_the_date('', $ID);
92
-
93
- if (in_array('update_date', $template_tags))
94
- $post_vars['update_date'] = date_i18n(get_option('date_format'), strtotime($post_vars['post_modified']));
95
-
96
- if (in_array('categories', $template_tags))
97
- $post_vars['categories'] = get_the_term_list($ID, 'wpdmcategory', '', ', ', '');
98
-
99
- if (in_array('category', $template_tags)) {
100
- $cats = get_the_terms($ID, 'wpdmcategory');
101
- if (is_array($cats) && count($cats) > 0)
102
- $post_vars['category'] = $cats[0]->name;
103
- }
104
-
105
- $post_vars['comment_count'] = wp_count_comments($ID)->approved;
106
-
107
- if (in_array('cats_class', $template_tags)) {
108
- $cats = wp_get_post_terms($ID, 'wpdmcategory');
109
- $post_vars['cats_class'] = "";
110
- foreach ($cats as $cat) {
111
- $post_vars['cats_class'] .= " wpdmcat-{$cat->id} {$cat->slug}";
112
- }
113
- }
114
-
115
- $data = $this->metaData($post_vars['ID']);
116
-
117
- if (is_array($data)) {
118
- $post_vars = array_merge($data, $post_vars);
119
- }
120
-
121
- if (!isset($post_vars['files']) || !is_array($post_vars['files']))
122
- $post_vars['files'] = self::getFiles($ID, true);
123
-
124
- if (in_array('file_count', $template_tags))
125
- $post_vars['file_count'] = count($post_vars['files']);
126
-
127
- if (in_array('play_button', $template_tags))
128
- $post_vars['play_button'] = self::audioPlayer($post_vars);
129
-
130
-
131
- $post_vars['link_label'] = isset($post_vars['link_label']) ? esc_attr($post_vars['link_label']) : esc_attr__("Download", "download-manager");
132
- $post_vars['page_url'] = get_permalink($post_vars['ID']);
133
- $post_vars['page_link'] = "<a href='" . $post_vars['page_url'] . "'>{$post_vars['title']}</a>";
134
- $post_vars['page_url_qr'] = "<img class='wpdm-qr-code wpdm-qr-code{$post_vars['ID']}' style='max-width: 250px' src='http://chart.googleapis.com/chart?cht=qr&chs=450x450&choe=UTF-8&chld=H|0&chl={$post_vars['page_url']}' alt='{$post_vars['title']}' />";
135
-
136
-
137
- if (!isset($post_vars['btnclass']))
138
- $post_vars['btnclass'] = wpdm_download_button_style(null, $ID);
139
-
140
- if (in_array('tags', $template_tags)) {
141
- $tags = wp_get_post_terms($post_vars['ID'], 'wpdmtag');
142
- $taghtml = "";
143
- if (is_array($tags)) {
144
- foreach ($tags as $tag) {
145
- $taghtml .= "<a class='btn btn-secondary btn-xs' style='margin:0 5px 5px 0' href=\""
146
- . get_tag_link($tag->term_id)
147
- . "\"><i class='fa fa-tag'></i> &nbsp; " . $tag->name . "</a> &nbsp;";
148
- }
149
- }
150
- $post_vars['tags'] = $taghtml;
151
- }
152
-
153
- if (in_array('file_ext', $template_tags)) {
154
- $post_vars['files'] = isset($post_vars['files']) && is_array($post_vars['files']) ? $post_vars['files'] : self::getFiles($ID);
155
- if (count($post_vars['files']) > 1) $post_vars['file_ext'] = 'zip';
156
- if (is_array($post_vars['files']) && count($post_vars['files']) == 1) {
157
- $tmpdata = $post_vars['files'];
158
- $tmpdata = array_shift($tmpdata);
159
- $tmpdata = explode(".", $tmpdata);
160
- $post_vars['file_ext'] = end($tmpdata);
161
- }
162
- }
163
-
164
- if (in_array('file_size', $template_tags))
165
- $post_vars['file_size'] = self::Size($post_vars['ID']);
166
-
167
- if (in_array('audio_player_single', $template_tags))
168
- $post_vars['audio_player_single'] = self::audioPlayer($post_vars, true);
169
-
170
- if (array_intersect($template_tags, array('youtube_player', 'youtube_thumb_0', 'youtube_thumb_1', 'youtube_thumb_2', 'youtube_thumb_3'))) {
171
- $post_vars['files'] = isset($post_vars['files']) && is_array($post_vars['files']) ? $post_vars['files'] : self::getFiles($ID);
172
- $tmplfile = $post_vars['files'];
173
- $tmpfile = is_array($tmplfile) && count($tmplfile) > 0 ? array_shift($tmplfile) : '';
174
- if (strpos($tmpfile, 'youtu')) {
175
- if (preg_match('/youtu\.be\/([^\/]+)/', $tmpfile, $match))
176
- $vid = $match[1];
177
- else if (preg_match('/watch\?v=([^\/]+)/', $tmpfile, $match))
178
- $vid = $match[1];
179
- if (isset($vid)) {
180
- $post_vars['youtube_thumb_0'] = '<img src="https://img.youtube.com/vi/' . $vid . '/0.jpg" alt="Thumb 0" />';
181
- $post_vars['youtube_thumb_1'] = '<img src="https://img.youtube.com/vi/' . $vid . '/1.jpg" alt="Thumb 1" />';
182
- $post_vars['youtube_thumb_2'] = '<img src="https://img.youtube.com/vi/' . $vid . '/2.jpg" alt="Thumb 2" />';
183
- $post_vars['youtube_thumb_3'] = '<img src="https://img.youtube.com/vi/' . $vid . '/3.jpg" alt="Thumb 3" />';
184
- $post_vars['youtube_player'] = '<iframe width="1280" height="720" src="https://www.youtube.com/embed/' . $vid . '" frameborder="0" allowfullscreen></iframe>';
185
- }
186
- }
187
- }
188
-
189
- if (array_intersect(array('file_type_icon', 'icon'), $template_tags)) {
190
- $post_vars['files'] = isset($post_vars['files']) && is_array($post_vars['files']) ? $post_vars['files'] : self::getFiles($ID);
191
- if (is_array($post_vars['files'])) {
192
- $tifn = $ifn = @end($post_vars['files']);
193
- $ifn = @explode('.', $ifn);
194
- $ifn = @end($ifn);
195
- if (strlen($ifn) > 4 && strstr($tifn, "://"))
196
- $ifn = 'web';
197
- if (strlen($ifn) > 4 && !strstr($tifn, "://"))
198
- $ifn = 'unknown';
199
- } else
200
- $ifn = 'unknown';
201
-
202
- $post_vars['file_type_icon'] = '<img class="wpdm_icon" alt="' . __("Icon", "download-manager") . '" src="' . FileSystem::fileTypeIcon((@count($post_vars['files']) <= 1 ? $ifn : 'zip')) . '" />';
203
-
204
- if (!isset($post_vars['icon']) || $post_vars['icon'] == '') {
205
- $post_vars['icon'] = $post_vars['file_type_icon'];
206
- } else if (!strpos($post_vars['icon'], '://'))
207
- $post_vars['icon'] = '<img class="wpdm_icon" alt="' . __("Icon", "download-manager") . '" src="' . plugins_url(str_replace('download-manager/file-type-icons/', 'download-manager/assets/file-type-icons/', $post_vars['icon'])) . '" />';
208
- else if (!strpos($post_vars['icon'], ">"))
209
- $post_vars['icon'] = '<img class="wpdm_icon" alt="' . __("Icon", "download-manager") . '" src="' . str_replace('download-manager/file-type-icons/', 'download-manager/assets/file-type-icons/', $post_vars['icon']) . '" />';
210
-
211
- }
212
-
213
-
214
- $post_vars['link_label'] = apply_filters('wpdm_button_image', $post_vars['link_label'], $post_vars, $template_type);
215
-
216
- $post_vars['link_label'] = $post_vars['link_label'] ? $post_vars['link_label'] : __("Download", "download-manager");
217
-
218
- $post_vars['download_url'] = $this->getDownloadURL($post_vars['ID']);
219
- $post_vars['download_link_popup'] =
220
- $post_vars['download_link_extended'] =
221
- $post_vars['download_link'] = (int)get_option('__wpdm_mask_dlink', 1) == 1 ? "<a class='wpdm-download-link download-on-click {$post_vars['btnclass']}' rel='nofollow' href='#' data-downloadurl=\"{$post_vars['download_url']}\">{$post_vars['link_label']}</a>" : "<a class='wpdm-download-link {$post_vars['btnclass']}' rel='nofollow' href='{$post_vars['download_url']}'>{$post_vars['link_label']}</a>";
222
-
223
- $limit_over = 0;
224
- $alert_size = ($template_type == 'link') ? 'alert-sm' : '';
225
-
226
- $loginmsg = Messages::login_required($post_vars['ID']);
227
-
228
- //Download limit is over
229
- if (self::userDownloadLimitExceeded($post_vars['ID'])) {
230
- $limit_over = 1;
231
- $limit_msg = Messages::download_limit_exceeded($post_vars['ID']);
232
- $post_vars['download_url'] = '#';
233
- $post_vars['link_label'] = $limit_msg;
234
- $post_vars['download_link_popup'] =
235
- $post_vars['download_link_extended'] =
236
- $post_vars['download_link'] = $post_vars['link_label'];
237
- } //Item is expired
238
- else if (isset($post_vars['expire_date']) && $post_vars['expire_date'] != "" && strtotime($post_vars['expire_date']) < time()) {
239
- $post_vars['download_url'] = '#';
240
- $post_vars['link_label'] = __("Download was expired on", "download-manager") . " " . date_i18n(get_option('date_format') . " h:i A", strtotime($post_vars['expire_date']));
241
- $post_vars['download_link'] =
242
- $post_vars['download_link_extended'] =
243
- $post_vars['download_link_popup'] = "<div class='alert alert-warning {$alert_size}' data-title='" . __("DOWNLOAD ERROR:", "download-manager") . "'>{$post_vars['link_label']}</div>";
244
- } //Not available yet
245
- else if (isset($post_vars['publish_date']) && $post_vars['publish_date'] != '' && strtotime($post_vars['publish_date']) > time()) {
246
- $post_vars['download_url'] = '#';
247
- $post_vars['link_label'] = __("Download will be available from ", "download-manager") . " " . date_i18n(get_option('date_format') . " h:i A", strtotime($post_vars['publish_date']));
248
- $post_vars['download_link'] =
249
- $post_vars['download_link_extended'] =
250
- $post_vars['download_link_popup'] = "<div class='alert alert-warning {$alert_size}' data-title='" . __("DOWNLOAD ERROR:", "download-manager") . "'>{$post_vars['link_label']}</div>";
251
- } //User is not allowed
252
- else if (is_user_logged_in() && !self::userCanAccess($post_vars['ID'])) {
253
- $post_vars['download_url'] = '#';
254
- $post_vars['link_label'] = stripslashes(get_option('__wpdm_permission_denied_msg'));
255
- $post_vars['link_label'] = $post_vars['link_label'] !== '' ? $post_vars['link_label'] : __('You are not allowed to download.', 'download-manager');
256
- $post_vars['download_link'] =
257
- $post_vars['download_link_extended'] =
258
- $post_vars['download_link_popup'] = "<div class='alert alert-danger {$alert_size}' data-title='" . __("DOWNLOAD ERROR:", "download-manager") . "'>{$post_vars['link_label']}</div>";
259
- } //User is not logged in and guest access is not allowed
260
- else if (!is_user_logged_in() && !self::userCanAccess($post_vars['ID'])) {
261
- $packurl = get_permalink($post_vars['ID']);
262
- $loginform = WPDM()->user->login->form(array('redirect' => $packurl));
263
- $post_vars['download_url'] = WPDM()->user->login->url($_SERVER['REQUEST_URI']);
264
- $post_vars['download_link'] =
265
- $post_vars['download_link_extended'] =
266
- $post_vars['download_link_popup'] = stripcslashes(str_replace(array("[loginform]", "[this_url]", "[package_url]"), array($loginform, $_SERVER['REQUEST_URI'], $packurl), $loginmsg));
267
- $post_vars['download_link'] =
268
- $post_vars['download_link_extended'] =
269
- $post_vars['download_link_popup'] = get_option('__wpdm_login_form', 0) == 1 ? $loginform : $post_vars['download_link'];
270
- } //PACKAGE is locked
271
- else if ($this->isLocked($post_vars['ID'])) {
272
- $post_vars['download_url'] = '#';
273
- $post_vars['download_link'] = $post_vars['download_link_popup'] = "<a href='#unlock' class='wpdm-download-link wpdm-download-locked {$post_vars['btnclass']}' data-package='{$post_vars['ID']}'>{$post_vars['link_label']}</a>"; //self::activeLocks($post_vars);
274
- $post_vars['download_link_extended'] = self::activeLocks($post_vars['ID'], array('embed' => 1));
275
- //$post_vars['download_link_popup'] = self::activeLocks($post_vars, array('popstyle' => 'popup'));
276
- }
277
-
278
- if (isset($data['terms_lock']) && $data['terms_lock'] != 0 && (!function_exists('wpdmpp_effective_price') || wpdmpp_effective_price($post_vars['ID']) == 0) && $limit_over == 0 && self::userCanAccess($post_vars['ID'])) {
279
- //$data['terms_conditions'] = wpautop(strip_tags($data['terms_conditions'], "<p><br><a><strong><b><i>"));
280
- /*$data['terms_conditions'] = wpautop(wpdm_escs(get_post_meta($post_vars['ID'], '__wpdm_terms_conditions', true)));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  $data['terms_title'] = wpdm_escs(get_post_meta($post_vars['ID'], '__wpdm_terms_title', true));
282
  $data['terms_title'] = !isset($data['terms_title']) || $data['terms_title'] == '' ? __("Terms and Conditions", 'download-manager') : sanitize_text_field($data['terms_title']);
283
  $data['terms_check_label'] = !isset($data['terms_check_label']) || $data['terms_check_label'] == '' ? __("I Agree With Terms and Conditions", 'download-manager') : sanitize_text_field($data['terms_check_label']);
@@ -287,685 +329,820 @@ class PackageController extends PackageTemplate
287
  //$data['terms_conditions'] = wpautop(strip_tags($data['terms_conditions'], "<p><br><a><strong><b><i>"));
288
  $data['terms_conditions'] = wpautop(wpdm_escs($data['terms_conditions']));*/
289
 
290
- $terms_page = (int)get_post_meta($ID, '__wpdm_terms_page', true);
291
- $terms_title = get_post_meta($ID, '__wpdm_terms_title', true);
292
- $terms_conditions = get_post_meta($ID, '__wpdm_terms_conditions', true);
293
- if ($terms_page) {
294
- $terms_page = get_post($terms_page);
295
- $terms_title = $terms_page->post_title;
296
- $terms_conditions = $terms_page->post_content;
297
- }
298
- $terms_title = $terms_title !== '' ? $terms_title : __("Terms and Conditions", 'download-manager');
299
- $terms_check_label = get_post_meta($ID, '__wpdm_terms_check_label', true);
300
- $terms_check_label = $terms_check_label ? $terms_check_label : __("I Agree With Terms and Conditions", 'download-manager');
301
-
302
- $data['terms_title'] = $terms_title;
303
- $data['terms_check_label'] = $terms_check_label;
304
- $data['terms_conditions'] = $terms_conditions;
305
-
306
- if (!self::isLocked($post_vars)) {
307
- $post_vars['download_link_popup'] = $post_vars['download_link'] = "<a href='#unlock' class='wpdm-download-link wpdm-download-locked {$post_vars['btnclass']}' data-package='{$post_vars['ID']}'>{$post_vars['link_label']}</a>";
308
- }
309
-
310
- $post_vars['download_link_extended'] = "<div class='panel panel-default card card terms-panel' style='margin: 0'><div class='panel-heading card-header'>{$terms_title}</div><div class='panel-body card-body' style='max-height: 200px;overflow: auto'>{$terms_conditions}</div><div class='panel-footer card-footer'><label><input data-pid='{$post_vars['ID']}' class='wpdm-checkbox terms_checkbox terms_checkbox_{$post_vars['ID']}' type='checkbox' onclick='jQuery(\".download_footer_{$post_vars['ID']}\").slideToggle();'> {$terms_check_label}</label></div><div class='panel-footer card-footer download_footer_{$post_vars['ID']}' style='display:none;'>{$post_vars['download_link_extended']}</div></div><script>jQuery(function($){ $('#wpdm-filelist-{$post_vars['ID']} .btn.inddl, #xfilelist .btn.inddl').attr('disabled', 'disabled'); });</script>";
311
-
312
- }
313
-
314
- if (!isset($post_vars['formatted'])) $post_vars['formatted'] = 0;
315
- ++$post_vars['formatted'];
316
-
317
- $post_vars['__template'] = $template;
318
- $post_vars['__template_type'] = $template_type;
319
- $post_vars = apply_filters('wpdm_after_prepare_package_data', $post_vars, $template_type);
320
-
321
- $this->packageData = $post_vars;
322
-
323
- foreach ($post_vars as $key => $val) {
324
- try {
325
- if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key))
326
- $this->{$key} = $val;
327
- } catch (\Exception $e) {
328
- }
329
- }
330
- return $this;
331
- }
332
-
333
- /**
334
- * @usage Get all or any specific package info
335
- * @param $ID
336
- * @param null $meta
337
- * @return mixed
338
- */
339
- public static function get($ID, $meta = null)
340
- {
341
- $ID = (int)$ID;
342
- if ($ID == 0) return null;
343
- if ($meta != null)
344
- return get_post_meta($ID, "__wpdm_" . $meta, true);
345
- $p = new PackageController();
346
- $package = $p->prepare($ID);
347
- return $package->packageData;
348
- }
349
-
350
- /**
351
- * @usage Verify single file download option
352
- * @param null $ID
353
- * @return mixed
354
- */
355
- public function isSingleFileDownloadAllowed($ID = null)
356
- {
357
- global $post;
358
- if (!$ID && $post->post_type == 'wpdmpro') $ID = $post->ID;
359
- $global = get_option('__wpdm_individual_file_download', 1);
360
- $package = get_post_meta($ID, '__wpdm_individual_file_download', true);
361
- $effective = $package == -1 || $package == '' ? $global : $package;
362
- return apply_filters("wpdm_is_single_file_download_allowed", $effective, $ID);
363
- }
364
-
365
- /**
366
- * @param $id
367
- * @usage Returns the user roles who has access to specified package
368
- * @return array|mixed
369
- */
370
- public function allowedRoles($id)
371
- {
372
- $roles = get_post_meta($id, '__wpdm_access', true);
373
- $roles = maybe_unserialize($roles);
374
-
375
- $cats = get_the_terms($id, 'wpdmcategory');
376
- if (!is_array($roles)) $roles = array();
377
- if (is_array($cats)) {
378
- foreach ($cats as $cat) {
379
- $croles = WPDM()->categories->getAllowedRoles($cat->term_id);
380
- $roles = array_merge($roles, $croles);
381
- }
382
- }
383
-
384
- $roles = array_unique($roles);
385
-
386
- $roles = apply_filters("wpdm_allowed_roles", $roles, $id);
387
- if (!is_array($roles)) $roles = array();
388
- return $roles;
389
- }
390
-
391
- /**
392
- * @usage Check if a package is locked or public
393
- * @param $id
394
- * @return bool
395
- */
396
- public function isLocked($package = null)
397
- {
398
- if(!$package) $package = $this->ID;
399
- if (!is_array($package) && (int)$package > 0) {
400
- $id = $package;
401
- $package = array();
402
- $package['ID'] = $id;
403
- $package['email_lock'] = get_post_meta($id, '__wpdm_email_lock', true);
404
- $package['password_lock'] = get_post_meta($id, '__wpdm_password_lock', true);
405
- $package['gplusone_lock'] = get_post_meta($id, '__wpdm_gplusone_lock', true);
406
- $package['tweet_lock'] = get_post_meta($id, '__wpdm_tweet_lock', true);
407
- $package['twitterfollow_lock'] = get_post_meta($id, '__wpdm_twitterfollow_lock', true);
408
- $package['facebooklike_lock'] = get_post_meta($id, '__wpdm_facebooklike_lock', true);
409
- $package['linkedin_lock'] = get_post_meta($id, '__wpdm_linkedin_lock', true);
410
- $package['captcha_lock'] = get_post_meta($id, '__wpdm_captcha_lock', true);
411
- } else
412
- $id = $package['ID'];
413
- $lock = '';
414
- $package = apply_filters('wpdm_custom_data', $package, $id);
415
- if (isset($package['email_lock']) && (int)$package['email_lock'] == 1) $lock = 'locked';
416
- if (isset($package['password_lock']) && (int)$package['password_lock'] == 1) $lock = 'locked';
417
- if (isset($package['gplusone_lock']) && (int)$package['gplusone_lock'] == 1) $lock = 'locked';
418
- if (isset($package['tweet_lock']) && (int)$package['tweet_lock'] == 1) $lock = 'locked';
419
- if (isset($package['twitterfollow_lock']) && (int)$package['twitterfollow_lock'] == 1) $lock = 'locked';
420
- if (isset($package['facebooklike_lock']) && (int)$package['facebooklike_lock'] == 1) $lock = 'locked';
421
- if (isset($package['linkedin_lock']) && (int)$package['linkedin_lock'] == 1) $lock = 'locked';
422
- if (isset($package['captcha_lock']) && (int)$package['captcha_lock'] == 1) $lock = 'locked';
423
-
424
- if ($lock !== 'locked')
425
- $lock = apply_filters('wpdm_check_lock', $lock, $id);
426
- return ($lock === 'locked');
427
- }
428
-
429
- /**
430
- * Check if a package with the given $ID is password protected
431
- * @param $ID
432
- * @return bool
433
- */
434
- function isPasswordProtected($ID)
435
- {
436
- $locked = (int)get_post_meta($ID, '__wpdm_password_lock', true);
437
- $password = get_post_meta($ID, '__wpdm_password', true);
438
- return $locked && $password ? $password : false;
439
- }
440
-
441
- /**
442
- * @usage Check if current user has access to package or category
443
- * @param $id
444
- * @param string $type
445
- *
446
- * @return bool
447
- */
448
- public function userCanAccess($ID = null, $type = 'package')
449
- {
450
- global $current_user;
451
- $ID = $ID ? $ID : $this->ID;
452
- if(!$ID) return false;
453
- if ($type == 'package')
454
- $roles = $this->allowedRoles($ID);
455
- else $roles = WPDM()->categories->getAllowedRoles($ID);
456
-
457
- if (!is_array($roles)) $roles = array();
458
- $matched = is_user_logged_in() ? array_intersect($current_user->roles, $roles) : array();
459
-
460
- if ($type === 'category' && count($roles) == 0) return true;
461
- if (in_array('guest', $roles)) return true;
462
- if (count($matched) > 0) return true;
463
-
464
- return false;
465
-
466
- }
467
-
468
- /**
469
- * @usage Check if current user has access to package or category based on roles
470
- * @param $id
471
- * @param string $type
472
- *
473
- * @return bool
474
- */
475
- public function userHasAccess($user_id, $ID, $type = 'package')
476
- {
477
-
478
- $user = get_user_by('ID', $user_id);
479
-
480
- if ($type == 'package')
481
- $roles = $this->allowedRoles($ID);
482
- else $roles = WPDM()->categories->getAllowedRoles($ID);
483
-
484
- if (!is_array($roles)) $roles = array();
485
- $matched = is_user_logged_in() ? array_intersect($user->roles, $roles) : array();
486
- if ($type === 'category' && count($roles) == 0) return true;
487
- if (in_array('guest', $roles)) return true;
488
- if (count($matched) > 0) return true;
489
- return false;
490
-
491
- }
492
-
493
- /**
494
- * @usage Check user's download limit
495
- * @param $ID
496
- * @return bool
497
- */
498
- public static function userDownloadLimitExceeded($ID)
499
- {
500
- global $current_user;
501
-
502
- if (is_user_logged_in())
503
- $index = $current_user->ID;
504
- else
505
- $index = $_SERVER['REMOTE_ADDR'];
506
-
507
- $stock = (int)get_post_meta($ID, '__wpdm_quota', true);
508
- $download_count = (int)get_post_meta($ID, '__wpdm_download_count', true);
509
-
510
- $user_download_count = WPDM()->downloadHistory->userDownloadCount($ID);
511
-
512
- $download_limit_per_user = (int)get_post_meta($ID, '__wpdm_download_limit_per_user', true);
513
-
514
- if ($download_limit_per_user > 0 && $user_download_count >= $download_limit_per_user) return apply_filters("wpdm_user_download_limit_exceeded", true, $ID);
515
-
516
- $exceeded = false;
517
-
518
- if ($stock > 0 && $download_count >= $stock) $exceeded = true;;
519
-
520
- $exceeded = apply_filters("wpdm_user_download_limit_exceeded", $exceeded, $ID);
521
-
522
- return $exceeded;
523
- }
524
-
525
- function userDownloadCount($ID)
526
- {
527
- global $wpdb;
528
- //$wpdb->
529
- }
530
-
531
- /**
532
- * @usage Check if user is can download this package
533
- * @param $ID
534
- * @return bool
535
- */
536
- public function userCanDownload($ID)
537
- {
538
- return $this->userCanAccess($ID) && !$this->userDownloadLimitExceeded($ID);
539
- }
540
-
541
- /**
542
- * @usage Count files in a package
543
- * @param $id
544
- * @return int
545
- */
546
- public function fileCount($ID)
547
- {
548
-
549
- $count = count($this->getFiles($ID, true));
550
- return $count;
551
-
552
- }
553
-
554
- /**
555
- * @usage Get list of attached files & all files inside attached dir with a package
556
- * @param $ID
557
- * @return array|mixed
558
- */
559
- public function getFiles($ID, $include_dir = false)
560
- {
561
- $files = maybe_unserialize(get_post_meta($ID, '__wpdm_files', true));
562
- if (!$files || !is_array($files)) $files = array();
563
- foreach ($files as &$file) {
564
- $file = trim($file);
565
- }
566
- if ($include_dir) {
567
- $package_dir = get_post_meta($ID, '__wpdm_package_dir', true);
568
- $package_dir = file_exists($package_dir) ? $package_dir : Crypt::decrypt($package_dir);
569
- if ($package_dir != '') {
570
- $package_dir = trailingslashit($package_dir);
571
- $package_dir = realpath($package_dir);
572
- if($package_dir) {
573
- $dfiles = FileSystem::scanDir($package_dir, true, true, '', true);
574
- $files += $dfiles;
575
- }
576
- }
577
- }
578
- $files = apply_filters("wpdm_get_files", $files, $ID);
579
-
580
- return $files;
581
- }
582
-
583
- /**
584
- * @usage Create zip from attached files
585
- * @param $ID
586
- * @return mixed|string|\WP_Error
587
- */
588
- public function zip($ID)
589
- {
590
- $files = $this->getFiles($ID);
591
- $zipped = get_post_meta($ID, "__wpdm_zipped_file", true);
592
- if (count($files) > 0) {
593
- if ($zipped == '' || !file_exists($zipped)) {
594
- $zipped = UPLOAD_DIR . sanitize_file_name(get_the_title($ID)) . '-' . $ID . '.zip';
595
- $zipped = FileSystem::zipFiles($files, $zipped);
596
- return $zipped;
597
- }
598
- }
599
- return new \WP_Error(404, __("No File Attached!", "download-manager"));
600
- }
601
-
602
- /**
603
- * @usage Calculate package size
604
- * @param $ID
605
- * @param bool|false $recalculate
606
- * @return bool|float|int|mixed|string
607
- */
608
- public function size($ID, $recalculate = false)
609
- {
610
-
611
- if (get_post_type($ID) != 'wpdmpro') return false;
612
-
613
- $size = get_post_meta($ID, '__wpdm_package_size', true);
614
-
615
- if ($size != "" && !$recalculate) return $size;
616
-
617
- $files = $this->getFiles($ID);
618
-
619
- $size = 0;
620
- if (is_array($files)) {
621
- foreach ($files as $f) {
622
- $f = trim($f);
623
- if (__::is_url($f)) continue;
624
- if (file_exists($f))
625
- $size += @filesize($f);
626
- else
627
- $size += @filesize(UPLOAD_DIR . $f);
628
- }
629
- }
630
-
631
- update_post_meta($ID, '__wpdm_package_size_b', $size);
632
- $size = $size / 1024;
633
- if ($size > 1024) $size = number_format($size / 1024, 2) . ' MB';
634
- else $size = number_format($size, 2) . ' KB';
635
- update_post_meta($ID, '__wpdm_package_size', $size);
636
- return $size;
637
- }
638
-
639
- /**
640
- * @usage Generate play button for link template
641
- * @param $package
642
- * @param bool $return
643
- * @param $style
644
- * @return mixed|string|void
645
- */
646
- public function audioPlayer($package, $return = true, $style = 'primary btn-lg wpdm-btn-play-lg')
647
- {
648
-
649
- $audiohtml = "";
650
-
651
- if (!is_array($package['files']) || count($package['files']) == 0) return;
652
- $audios = array();
653
- $nonce = wp_create_nonce($_SERVER['REQUEST_URI']);
654
- $audio = $audx = null;
655
- foreach ($package['files'] as $index => $file) {
656
- $realpath = file_exists($file) ? $file : UPLOAD_DIR . $file;
657
- $filetype = wp_check_filetype($realpath);
658
- $tmpvar = explode('/', $filetype['type']);
659
- if ($tmpvar[0] == 'audio') {
660
- $audio = $file;
661
- $audx = $index;
662
- break;
663
- }
664
- }
665
-
666
- if ($audio != null) {
667
- $song = FileSystem::mediaURL($package['ID'], $audx, basename($audio)); //home_url("/?wpdmdl={$package['ID']}&ind=".$audx."&play=".basename($audio));
668
- $audiohtml = "<button data-player='wpdm-audio-player' data-song='{$song}' class='btn btn-{$style} wpdm-btn-play'><i class='fa fa-play'></i></button>";
669
- $audiohtml = apply_filters("wpdm_audio_play_button", $audiohtml, $song, $package, 0);
670
- }
671
-
672
- if ($return)
673
- return $audiohtml;
674
-
675
- echo $audiohtml;
676
-
677
- }
678
-
679
- /**
680
- * @param $ID
681
- * @param $files
682
- * @param int $width
683
- * @return string
684
- */
685
- public static function videoPlayer($ID, $files = null, $width = 800)
686
- {
687
-
688
- if (!$files)
689
- $files = WPDM()->package->getFiles($ID);
690
-
691
- $videos = array();
692
- foreach ($files as $index => $file) {
693
- $realpath = file_exists($file) ? $file : UPLOAD_DIR . $file;
694
- $filetype = wp_check_filetype($realpath);
695
- $tmpvar = explode('/', $filetype['type']);
696
- if ($tmpvar[0] == 'video') {
697
- $videos[] = $file;
698
- $vidx[] = $index;
699
- }
700
-
701
- }
702
-
703
- $videothumbs = "";
704
- $mpvs = get_post_meta($ID, '__wpdm_additional_previews', true);
705
- $mmv = 0;
706
-
707
- if (is_array($mpvs) && count($mpvs) > 1 && count($videos) > 1) {
708
-
709
- foreach ($mpvs as $i => $mpv) {
710
- if ($mmv < count($videos)) {
711
- //$url = self::expirableDownloadLink($ID, 3);
712
- $ind = $vidx[$i]; //\WPDM_Crypt::Encrypt($videos[$mmv]);
713
- //$video = $url . "&ind={$ind}&play=" . basename($videos[$mmv]);
714
- $video = FileSystem::mediaURL($ID, $ind, wpdm_basename($videos[$mmv]));
715
-
716
- $videothumbs .= "<a href='#' data-video='{$video}' class='__wpdm_playvideo'><img class='thumbnail' src='" . wpdm_dynamic_thumb($mpv, array(64, 64)) . "'/></a>";
717
- }
718
- $mmv++;
719
- }
720
- }
721
-
722
- $player_html = '';
723
- if (count($videos) > 0) {
724
- //$url = self::expirableDownloadLink($ID, 10);
725
- $ind = $vidx[0]; //\WPDM_Crypt::Encrypt($videos[0]);
726
- //$video = $url . "&ind={$ind}&play=" . basename($videos[0]);
727
- $video = FileSystem::mediaURL($ID, $ind, wpdm_basename($videos[0]));
728
-
729
- $player_html = "<video id='__wpdm_videoplayer' class='thumbnail' width=\"{$width}\" controls controlsList='nodownload'><source src=\"{$video}\" type=\"video/mp4\">Your browser does not support HTML5 video.</video><div class='videothumbs'>{$videothumbs}</div>";
730
- //if(!WPDM()->package->userCanAccess($ID)) $player_html = \WPDM\__\Messages::Error(stripslashes(get_option('wpdm_permission_msg')), -1);
731
- }
732
-
733
- $player_html = apply_filters("wpdm_video_player_html", $player_html, $ID, $file, $width);
734
- return $player_html;
735
- }
736
-
737
- /**
738
- * @param $ID
739
- * @param $files
740
- * @param int $width
741
- * @param int $height
742
- * @return mixed|string
743
- */
744
- public function productPreview($ID, $files = null, $width = 800, $height = 600)
745
- {
746
- if (!$files)
747
- $files = WPDM()->package->getFiles($ID);
748
- $tfiles = $files;
749
- $keys = array_keys($files);
750
- $ind = $keys[0];
751
- $file = array_shift($tfiles);
752
- $realpath = file_exists($file) ? $file : UPLOAD_DIR . $file;
753
- $filetype = wp_check_filetype($realpath);
754
- $type = explode('/', $filetype['type']);
755
- $type = $type[1];
756
- switch ($type) {
757
- case 'mpeg':
758
- case 'mp4':
759
- return self::videoPlayer($ID, $files, $width);
760
- case 'png':
761
- case 'jpg':
762
- case 'jpeg':
763
- return "<img src='" . FileSystem::imageThumbnail($realpath, $width, $height) . "' alt='" . get_the_title($ID) . "'/>";
764
- case 'pdf':
765
- $url = self::expirableDownloadLink($ID, 1, 300);
766
- //$ind = \WPDM_Crypt::Encrypt($file);
767
- $url .= "&ind={$ind}";
768
- return FileSystem::docViewer($url, $ID, 'pdf');
769
- default:
770
- return '';
771
- }
772
- }
773
-
774
- /**
775
- * @usage Get All Custom Data of a Package
776
- * @param $pid
777
- * @return array
778
- */
779
- public function metaData($ID)
780
- {
781
- global $wpdb;
782
-
783
- $metaValidate = ['__wpdm_icon' => 'txt', '__wpdm_view_count' => 'int', '__wpdm_download_count' => 'int', '__wpdm_link_label' => 'txt', '__wpdm_version' => 'txt', '__wpdm_quota' => 'int', '__wpdm_access' => 'array', '__wpdm_package_size' => 'txt', '__wpdm_download_limit_per_user' => 'int', '__wpdm_terms_lock' => 'int', '__wpdm_publish_date' => 'txt', '__wpdm_expire_date' => 'txt'];
784
- $metaKeys = array_keys($metaValidate);
785
- $metaData = $wpdb->get_results("select * from {$wpdb->prefix}postmeta where meta_key in ('" . implode('\',\'', $metaKeys) . "') and post_id = '{$ID}'");
786
-
787
- $data = [];
788
- if (is_array($metaData)) {
789
- foreach ($metaData as $metaDataRow) {
790
- $key = str_replace("__wpdm_", "", $metaDataRow->meta_key);
791
- if($key === 'publish_date') {
792
- $key = 'avail_date';
793
- $metaDataRow->meta_value = wp_date(get_option('date_format')." ".get_option('time_format'), strtotime($metaDataRow->meta_value));
794
- }
795
- if($key === 'expire_date') {
796
- $metaDataRow->meta_value = wp_date(get_option('date_format')." ".get_option('time_format'), strtotime($metaDataRow->meta_value));
797
- }
798
- //if($metaValidate[$metaDataRow->meta_key] === 'array' && !is_array($metaDataRow->meta_value)) $metaDataRow->meta_value = unserialize($metaDataRow->meta_value);
799
- //else
800
- // $metaDataRow->meta_value = __::sanitize_var($metaDataRow->meta_value, $metaValidate[$metaDataRow->meta_key]);
801
- $data[$key] = wpdm_sanitize_var(maybe_unserialize($metaDataRow->meta_value), $metaValidate[$metaDataRow->meta_key]);
802
- }
803
- }
804
- unset($metaData);
805
- $data = apply_filters('wpdm_custom_data', $data, $ID);
806
- if (!is_array($data)) $data = [];
807
-
808
- return $data;
809
- }
810
-
811
- /**
812
- * @usage Generate download link of a package
813
- * @param $package
814
- * @param int $embed
815
- * @param array $extras
816
- * @return string
817
- */
818
- function prepareDownloadLink(&$package, $embed = 0, $extras = array())
819
- {
820
- global $wpdb, $current_user, $wpdm_download_icon, $wpdm_download_lock_icon, $btnclass;
821
- if (is_array($extras))
822
- extract($extras);
823
- $data = '';
824
-
825
- $package['link_url'] = home_url('/?download=1&');
826
- $package['link_label'] = !isset($package['link_label']) || $package['link_label'] == '' ? __("Download", "download-manager") : $package['link_label'];
827
-
828
- //Change link label using a button image
829
- $template_type = isset($template_type) ? $template_type : 'link';
830
- $package['link_label'] = apply_filters('wpdm_button_image', $package['link_label'], $package, $template_type);
831
-
832
-
833
- $package['download_url'] = wpdm_download_url($package);
834
- if (wpdm_is_download_limit_exceed($package['ID'])) {
835
- $limit_msg = Messages::download_limit_exceeded($package['ID']);
836
- $package['download_url'] = '#';
837
- $package['link_label'] = $limit_msg;
838
- }
839
- if (isset($package['expire_date']) && $package['expire_date'] != "" && strtotime($package['expire_date']) < time()) {
840
- $package['download_url'] = '#';
841
- $package['link_label'] = apply_filters("wpdm_download_expired_message", __("Download was expired on", "download-manager") . " " . date_i18n(get_option('date_format') . " h:i A", strtotime($package['expire_date'])), $package);
842
- $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<a href='#'>{$package['link_label']}</a>";
843
- return "<div class='alert alert-warning'><b>" . __("Download:", "download-manager") . "</b><br/>{$package['link_label']}</div>";
844
- }
845
-
846
- if (isset($package['publish_date']) && $package['publish_date'] != '' && strtotime($package['publish_date']) > time()) {
847
- $package['download_url'] = '#';
848
- $package['link_label'] = apply_filters("wpdm_download_availability_message", __("Download will be available from ", "download-manager") . " " . date_i18n(get_option('date_format') . " h:i A", strtotime($package['publish_date'])), $package);
849
- $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<a href='#'>{$package['link_label']}</a>";
850
- return "<div class='alert alert-warning'><b>" . __("Download:", "download-manager") . "</b><br/>{$package['link_label']}</div>";
851
- }
852
-
853
- $link_label = isset($package['link_label']) ? $package['link_label'] : __("Download", "download-manager");
854
-
855
- $package['access'] = wpdm_allowed_roles($package['ID']);
856
-
857
- if ($package['download_url'] != '#')
858
- $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<a class='wpdm-download-link {$btnclass}' rel='nofollow' href='#' onclick=\"location.href='{$package['download_url']}';return false;\"><i class='$wpdm_download_icon'></i>{$link_label}</a>";
859
- else
860
- $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<div class='alert alert-warning'><b>" . __("Download:", "download-manager") . "</b><br/>{$link_label}</div>";
861
- $caps = array_keys($current_user->caps);
862
- $role = array_shift($caps);
863
-
864
- $matched = (is_array(@maybe_unserialize($package['access'])) && is_user_logged_in()) ? array_intersect($current_user->roles, @maybe_unserialize($package['access'])) : array();
865
-
866
- $skiplink = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
867
 
868
- if (is_user_logged_in() && count($matched) <= 0 && !@in_array('guest', @maybe_unserialize($package['access']))) {
869
- $package['download_url'] = "#";
870
- $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = stripslashes(get_option('__wpdm_permission_denied_msg'));
871
- $package = apply_filters('download_link', $package);
872
- if (get_option('_wpdm_hide_all', 0) == 1) {
873
- $package['download_link'] = $package['download_link_extended'] = 'blocked';
874
- }
875
- return $package['download_link'];
876
- }
877
- if (!@in_array('guest', @maybe_unserialize($package['access'])) && !is_user_logged_in()) {
878
-
879
- $loginform = wpdm_login_form(array('redirect' => get_permalink($package['ID'])));
880
- if (get_option('_wpdm_hide_all', 0) == 1) return 'loginform';
881
- $package['download_url'] = $vars['download_link_extended'] = $vars['download_link_popup'] = home_url('/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
882
- $loginmsg = Messages::login_required($package['ID']);
883
- $package['download_link'] = stripcslashes(str_replace(array("[loginform]", "[this_url]", "[package_url]"), array($loginform, $_SERVER['REQUEST_URI'], get_permalink($package['ID'])), $loginmsg));
884
- return get_option('__wpdm_login_form', 0) == 1 ? $loginform : $package['download_link'];
885
-
886
- }
887
-
888
- $package = apply_filters('download_link', $package);
889
 
890
- $unqid = uniqid();
891
 
892
- if (!isset($package['quota']) || (isset($package['quota']) && $package['quota'] > 0 && $package['quota'] > $package['download_count']) || $package['quota'] == 0) {
893
- $lock = 0;
894
 
895
- if (isset($package['password_lock']) && (int)$package['password_lock'] == 1 && $package['password'] != '') {
896
- $lock = 'locked';
897
- $data = PackageLocks::AskPassword($package);
898
- }
899
 
900
 
901
- $sociallock = "";
902
 
903
- if (isset($package['email_lock']) && (int)$package['email_lock'] == 1) {
904
- $data .= PackageLocks::AskEmail($package);
905
- $lock = 'locked';
906
- }
907
 
908
- if (isset($package['linkedin_lock']) && (int)$package['linkedin_lock'] == 1) {
909
- $lock = 'locked';
910
- $sociallock .= PackageLocks::LinkedInShare($package);
911
 
912
- }
913
 
914
- if (isset($package['twitterfollow_lock']) && (int)$package['twitterfollow_lock'] == 1) {
915
- $lock = 'locked';
916
- $sociallock .= PackageLocks::TwitterFollow($package);
917
 
918
- }
919
 
920
- if (isset($package['gplusone_lock']) && (int)$package['gplusone_lock'] == 1) {
921
- $lock = 'locked';
922
- $sociallock .= PackageLocks::GooglePlusOne($package, true);
923
 
924
- }
925
 
926
- if (isset($package['tweet_lock']) && (int)$package['tweet_lock'] == 1) {
927
- $lock = 'locked';
928
- $sociallock .= PackageLocks::Tweet($package);
929
 
930
- }
931
 
932
- if (isset($package['facebooklike_lock']) && (int)$package['facebooklike_lock'] == 1) {
933
- $lock = 'locked';
934
- $sociallock .= PackageLocks::FacebookLike($package, true);
935
 
936
- }
937
 
938
 
939
- if (isset($package['captcha_lock']) && (int)$package['captcha_lock'] == 1) {
940
- $lock = 'locked';
941
- $sociallock .= PackageLocks::reCaptchaLock($package, true);
942
 
943
- }
944
 
945
- $extralocks = '';
946
- $extralocks = apply_filters("wpdm_download_lock", $extralocks, $package);
947
 
948
- if (is_array($extralocks) && $extralocks['lock'] === 'locked') {
949
 
950
- if (isset($extralocks['type']) && $extralocks['type'] == 'social')
951
- $sociallock .= $extralocks['html'];
952
- else
953
- $data .= $extralocks['html'];
 
954
 
955
- $lock = 'locked';
956
- }
957
 
958
- if ($sociallock != "") {
959
- $data .= "<div class='panel panel-default card card-default'><div class='panel-heading card-header'>" . __("Download", "download-manager") . "</div><div class='panel-body card-body wpdm-social-locks text-center'>{$sociallock}</div></div>";
960
- }
961
 
962
- if ($lock === 'locked') {
963
- $popstyle = isset($popstyle) && in_array($popstyle, array('popup', 'pop-over')) ? $popstyle : 'pop-over';
964
- if ($embed == 1)
965
- $adata = "</strong>{$data}";
966
- else {
967
- //$dataattrs = $popstyle == 'pop-over'? 'data-title="'.__( "Download" , "download-manager" ).' ' . $package['title'] . '"' : 'data-toggle="modal" data-target="#pkg_' . $package['ID'] . "_" . $unqid . '"';
968
- $adata = '<a href="#pkg_' . $package['ID'] . "_" . $unqid . '" data-trigger="manual" data-package="' . $package['ID'] . '" class="wpdm-download-link wpdm-download-locked ' . $popstyle . ' ' . $btnclass . '"><i class=\'' . $wpdm_download_lock_icon . '\'></i>' . $package['link_label'] . '</a>';
 
 
 
969
 
970
  // if ($popstyle == 'pop-over') {
971
  // if(!get_option('__wpdm_ajax_popup', false))
@@ -974,418 +1151,480 @@ class PackageController extends PackageTemplate
974
  // else
975
  // $adata .= '<div class="modal fade" role="modal" id="pkg_' . $package['ID'] . "_" . $unqid . '"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><strong style="margin:0px;font-size:12pt">' . __('Download') . '</strong></div><div class="modal-body">' . $data . '</div><div class="modal-footer text-right"><button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button></div></div></div></div>';
976
 
977
- }
978
-
979
- $data = $adata;
980
- }
981
- if ($lock !== 'locked') {
982
-
983
- $data = $package['download_link'];
984
-
985
-
986
- }
987
- } else {
988
- $data = Messages::download_limit_exceeded($package['ID']);
989
- }
990
-
991
-
992
- //return str_replace(array("\r","\n"),"",$data);
993
- return $data;
994
-
995
- }
996
-
997
- private static function activeLocks($ID, $params = array('embed' => 0, 'popstyle' => 'pop-over'))
998
- {
999
-
1000
- $embed = isset($params['embed']) ? $params['embed'] : 0;
1001
- $template_type = isset($params['template_type']) ? $params['template_type'] : 'link';
1002
- $popstyle = isset($params['popstyle']) ? $params['popstyle'] : 'pop-over';
1003
-
1004
- $package = array(
1005
- 'ID' => $ID,
1006
- 'password' => get_post_meta($ID, '__wpdm_password', true),
1007
- 'password_lock' => get_post_meta($ID, '__wpdm_password_lock', true),
1008
- 'email_lock' => get_post_meta($ID, '__wpdm_email_lock', true),
1009
- 'linkedin_lock' => get_post_meta($ID, '__wpdm_linkedin_lock', true),
1010
- 'twitterfollow_lock' => get_post_meta($ID, '__wpdm_twitterfollow_lock', true),
1011
- 'gplusone_lock' => get_post_meta($ID, '__wpdm_gplusone_lock', true),
1012
- 'tweet_lock' => get_post_meta($ID, '__wpdm_tweet_lock', true),
1013
- 'facebooklike_lock' => get_post_meta($ID, '__wpdm_facebooklike_lock', true),
1014
- 'captcha_lock' => get_post_meta($ID, '__wpdm_captcha_lock', true),
1015
- );
1016
-
1017
- $package = apply_filters('wpdm_before_apply_locks', $package);
1018
- $lock = $data = "";
1019
- $unqid = uniqid();
1020
-
1021
- if (isset($package['password_lock']) && (int)$package['password_lock'] == 1 && $package['password'] != '') {
1022
- $lock = 'locked';
1023
- $data = PackageLocks::AskPassword($package);
1024
- }
1025
-
1026
- $sociallock = "";
1027
-
1028
- if (isset($package['email_lock']) && (int)$package['email_lock'] == 1) {
1029
- $data .= PackageLocks::AskEmail($package);
1030
- $lock = 'locked';
1031
- }
1032
-
1033
- if (isset($package['linkedin_lock']) && (int)$package['linkedin_lock'] == 1) {
1034
- $lock = 'locked';
1035
- $sociallock .= PackageLocks::LinkedInShare($package);
1036
-
1037
- }
1038
-
1039
- if (isset($package['twitterfollow_lock']) && (int)$package['twitterfollow_lock'] == 1) {
1040
- $lock = 'locked';
1041
- $sociallock .= PackageLocks::TwitterFollow($package);
1042
-
1043
- }
1044
-
1045
- if (isset($package['gplusone_lock']) && (int)$package['gplusone_lock'] == 1) {
1046
- $lock = 'locked';
1047
- $sociallock .= PackageLocks::GooglePlusOne($package, true);
1048
-
1049
- }
1050
-
1051
- if (isset($package['tweet_lock']) && (int)$package['tweet_lock'] == 1) {
1052
- $lock = 'locked';
1053
- $sociallock .= PackageLocks::Tweet($package);
1054
-
1055
- }
1056
-
1057
- if (isset($package['facebooklike_lock']) && (int)$package['facebooklike_lock'] == 1) {
1058
- $lock = 'locked';
1059
- $sociallock .= PackageLocks::FacebookLike($package, true);
1060
-
1061
- }
1062
-
1063
-
1064
- $extralocks = '';
1065
- $extralocks = apply_filters("wpdm_download_lock", $extralocks, $package);
1066
-
1067
- if (is_array($extralocks) && $extralocks['lock'] === 'locked') {
1068
-
1069
- if (isset($extralocks['type']) && $extralocks['type'] == 'social')
1070
- $sociallock .= $extralocks['html'];
1071
- else
1072
- $data .= $extralocks['html'];
1073
-
1074
- $lock = 'locked';
1075
- }
1076
 
1077
- if ($sociallock !== "") {
1078
- $socdesc = get_option('_wpdm_social_lock_panel_desc', '');
1079
- $socdesc = $socdesc !== '' ? "<p>{$socdesc}</p>" : "";
1080
- $data .= "<div class='panel panel-default card'><div class='panel-heading card-header'>" . get_option('_wpdm_social_lock_panel_title', 'Like or Share to Download') . "</div><div class='panel-body card-body wpdm-social-locks text-center'>{$socdesc}{$sociallock}</div></div>";
1081
- }
1082
 
 
1083
 
1084
- if (isset($package['captcha_lock']) && (int)$package['captcha_lock'] == 1) {
1085
- $lock = 'locked';
1086
- $captcha = PackageLocks::reCaptchaLock($package, true);
1087
- $data .= $captcha; //"<div class='panel panel-default card'><div class='panel-heading card-header'>" . __("Verify CAPTCHA to Download", "download-manager") . "</div><div class='panel-body card-body wpdm-social-locks text-center'>{$captcha}</div></div>";
1088
- }
1089
 
1090
- if ($lock === 'locked') {
1091
- $popstyle = isset($popstyle) && in_array($popstyle, array('popup', 'pop-over')) ? $popstyle : 'pop-over';
1092
- if ($embed == 1)
1093
- $adata = "</strong>{$data}";
1094
- else {
1095
- $link_label = get_post_meta($ID, '__wpdm_link_label', true);
1096
- $link_label = trim($link_label) ? $link_label : __("Download", "download-manager");
1097
- $style = wpdm_download_button_style(($template_type === 'page'), $ID);
1098
- $style = isset($params['btnclass']) && $params['btnclass'] !== '' ? $params['btnclass'] : $style;
1099
- $adata = '<a href="#pkg_' . $ID . "_" . $unqid . '" data-package="' . $ID . '" data-trigger="manual" class="wpdm-download-link wpdm-download-locked ' . $style . '">' . $link_label . '</a>';
1100
 
1101
- }
1102
 
1103
- $data = $adata;
1104
- }
1105
- return $data;
1106
- }
1107
-
1108
-
1109
- /**
1110
- * @usage Generate download link of a package
1111
- * @param $package
1112
- * @param int $embed
1113
- * @param array $extras
1114
- * @return string
1115
- */
1116
- public function downloadLink($ID, $embed = 0, $extras = array())
1117
- {
1118
- global $wpdb, $current_user, $wpdm_download_icon, $wpdm_download_lock_icon;
1119
- if (is_array($extras))
1120
- extract($extras);
1121
- $data = '';
1122
 
1123
- $template_type = isset($extras['template_type']) ? $extras['template_type'] : 'page';
1124
 
1125
- //$package = self::get($ID);
1126
 
1127
- $link_label = get_post_meta($ID, '__wpdm_link_label', true);
1128
- $link_label = (trim($link_label) === '') ? __("Download", "download-manager") : $link_label;
 
1129
 
1130
- $template_type = isset($template_type) ? $template_type : 'link';
1131
- $link_label = apply_filters("wpdm_link_label", $link_label, $ID, $template_type);
 
 
 
 
 
 
 
 
 
 
1132
 
1133
- $loginmsg = Messages::login_required($ID);
 
 
1134
 
1135
- $download_url = $this->getDownloadURL($ID);
 
 
 
1136
 
1137
- $limit_over = 0;
1138
- $alert_size = ($template_type == 'link') ? 'alert-sm' : '';
1139
 
1140
- if (self::userDownloadLimitExceeded($ID)) {
1141
- $limit_msg = Messages::download_limit_exceeded($ID);
1142
- $download_url = '#';
1143
- $link_label = $limit_msg;
1144
- $limit_over = 1;
1145
- }
1146
 
1147
- $expired = get_post_meta($ID, '__wpdm_expire_date', true);
1148
- $publish = get_post_meta($ID, '__wpdm_publish_date', true);
 
1149
 
1150
- if ($expired !== "" && strtotime($expired) < time()) {
1151
- $download_url = '#';
1152
- $link_label = __("Download was expired on", "download-manager") . " " . date_i18n(get_option('date_format') . " h:i A", strtotime($expired));
1153
- return "<div class='alert alert-warning {$alert_size}' data-title='" . __("DOWNLOAD ERROR:", "download-manager") . "'>{$link_label}</div>";
1154
- }
1155
 
1156
- if ($publish !== "" && strtotime($publish) > time()) {
1157
- $download_url = '#';
1158
- $link_label = __("Download will be available from ", "download-manager") . " " . date_i18n(get_option('date_format') . " h:i A", strtotime($publish));
1159
- return "<div class='alert alert-warning {$alert_size}' data-title='" . __("DOWNLOAD ERROR:", "download-manager") . "'>{$link_label}</div>";
1160
- }
1161
 
 
1162
 
1163
- $style = wpdm_download_button_style(($template_type === 'page'), $ID);
1164
- $style = isset($btnclass) && $btnclass !== '' ? $btnclass : $style;
 
1165
 
1166
- if ($download_url != '#')
1167
- $download_link = $download_link_extended = $download_link_popup = (int)get_option('__wpdm_mask_dlink', 1) === 1 ? "<a class='wpdm-download-link download-on-click {$style}' rel='nofollow' href='#' data-downloadurl=\"{$download_url}\">{$link_label}</a>" : "<a class='wpdm-download-link {$style}' rel='nofollow' href='{$download_url}'>{$link_label}</a>";
1168
- //$download_link = $download_link_extended = $download_link_popup = (int)get_option('__wpdm_mask_dlink', 1) === 1 ? "<a class='wpdm-download-link {$style}' rel='nofollow' href='#' onclick=\"location.href='{$download_url}';return false;\">{$link_label}</a>" : "<a class='wpdm-download-link {$style}' rel='nofollow' href='{$download_url}'>{$link_label}</a>";
1169
- else
1170
- $download_link = "<div class='alert alert-warning {$alert_size}' data-title='" . __("DOWNLOAD ERROR:", "download-manager") . "'>{$link_label}</div>";
1171
 
 
 
 
1172
 
1173
- $access = self::allowedRoles($ID);
1174
- if (!is_array($access)) $access = array();
1175
- $matched = (is_array($access) && is_user_logged_in()) ? array_intersect($current_user->roles, $access) : array();
1176
- if (!$matched) $matched = [];
1177
 
1178
- $skiplink = 0;
 
 
1179
 
1180
- //User does't have permission to download
1181
- if (is_user_logged_in() && count($matched) <= 0 && !@in_array('guest', $access)) {
1182
- $download_url = "#";
1183
- $download_link = $download_link_extended = $download_link_popup = Messages::permission_denied($ID);
1184
- if (get_option('_wpdm_hide_all', 0) == 1) {
1185
- $download_link = $download_link_extended = $download_link_popup = 'blocked';
1186
- }
1187
- return $download_link;
1188
- }
1189
 
1190
- //Login is required to download
1191
- if (!@in_array('guest', $access) && !is_user_logged_in()) {
1192
 
1193
- $loginform = WPDM()->user->login->form(array('redirect' => $_SERVER['REQUEST_URI']));
1194
- if (get_option('_wpdm_hide_all', 0) == 1) {
1195
- $hide_all_message = get_option('__wpdm_login_form', 0) == 1 ? $loginform : stripcslashes(str_replace(array("[loginform]", "[this_url]", "[package_url]"), array($loginform, $_SERVER['REQUEST_URI'], get_permalink($ID)), $loginmsg));
1196
- if ($template_type == 'link')
1197
- return "<a href='" . wpdm_login_url($_SERVER['REQUEST_URI']) . "' class='btn btn-danger'>" . __("Login", "download-manager") . "</a>";
1198
- else
1199
- return $hide_all_message;
1200
- }
1201
- $download_url = wpdm_login_url($_SERVER['REQUEST_URI']);
1202
- $download_link = $download_link_extended = $download_link_popup = stripcslashes(str_replace(array("[loginform]", "[this_url]", "[package_url]"), array($loginform, $_SERVER['REQUEST_URI'], get_permalink($ID)), $loginmsg));
1203
- return get_option('__wpdm_login_form', 0) == 1 ? $loginform : $download_link;
1204
 
1205
- }
1206
 
1207
- //$package = apply_filters('wpdm_before_apply_locks', $package);
1208
- //$package = apply_filters('wpdm_after_prepare_package_data', $package);
1209
-
1210
- $unqid = uniqid();
1211
- $stock_limit = (int)get_post_meta($ID, '__wpdm_quota', true);
1212
- $download_count = (int)get_post_meta($ID, '__wpdm_download_count', true);
1213
- if ($stock_limit > $download_count || $stock_limit == 0) {
1214
- $lock = 0;
1215
-
1216
- $extras['embed'] = $embed;
1217
- $data = self::activeLocks($ID, $extras);
1218
-
1219
- $terms_lock = (int)get_post_meta($ID, '__wpdm_terms_lock', true);
1220
- $terms_page = (int)get_post_meta($ID, '__wpdm_terms_page', true);
1221
- $terms_title = get_post_meta($ID, '__wpdm_terms_title', true);
1222
- $terms_conditions = get_post_meta($ID, '__wpdm_terms_conditions', true);
1223
- if ($terms_page) {
1224
- $terms_page = get_post($terms_page);
1225
- $terms_title = $terms_page->post_title;
1226
- $terms_conditions = $terms_page->post_content;
1227
- }
1228
- $terms_check_label = get_post_meta($ID, '__wpdm_terms_check_label', true);
1229
- if ($terms_lock !== 0 && (!function_exists('wpdmpp_effective_price') || wpdmpp_effective_price($ID) == 0)) {
1230
- if (!self::isLocked($ID) && !$embed) {
1231
- $data = "<a href='#unlock' class='wpdm-download-link wpdm-download-locked {$style}' data-package='{$ID}'>{$link_label}</a>";
1232
- } else {
1233
- $data = $data ? $data : $download_link;
1234
- }
1235
- if ($embed == 1)
1236
- $data = "<div class='panel panel-default card terms-panel' style='margin: 0 0 10px 0'><div class='panel-heading card-header'>{$terms_title}</div><div class='panel-body card-body' style='max-height: 200px;overflow: auto'>{$terms_conditions}</div><div class='panel-footer card-footer'><label><input data-pid='{$ID}' class='wpdm-checkbox terms_checkbox terms_checkbox_{$ID}' type='checkbox'> {$terms_check_label}</label></div><div class='panel-footer card-footer bg-white download_footer_{$ID}' style='display:none;'>{$data}</div></div><script>jQuery(function($){ $('#wpdm-filelist-{$ID} .btn.inddl, #xfilelist .btn.inddl').attr('disabled', 'disabled'); });</script>";
1237
 
1238
- }
 
1239
 
1240
- if ($data != "") {
1241
- $data = apply_filters('wpdm_download_link', $data, $extras + array('ID' => $ID, 'id' => $ID));
1242
- return $data;
1243
- }
 
1244
 
1245
 
1246
- $data = $download_link;
 
 
 
 
1247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1248
 
1249
- } else {
1250
- $data = "<button class='btn btn-danger btn-block' type='button' disabled='disabled' data-title='DOWNLOAD ERROR:'>" . __("Limit Over!", "download-manager") . "</button>";
1251
- }
1252
- if ($data == 'loginform') return WPDM()->user->login->form();
1253
- $data = apply_filters('wpdm_download_link', $data, $extras + array('ID' => $ID, 'id' => $ID));
1254
- return $data;
1255
-
1256
- }
1257
-
1258
- /**
1259
- * @usage Generate download url for public/open downloads, the url will not work for the packages with lock option
1260
- * @param $ID
1261
- * @param $ext
1262
- * @return string
1263
- */
1264
- public function getDownloadURL($ID, $ext = array())
1265
- {
1266
- if (self::isLocked($ID) && !Session::get('__wpdm_unlocked_' . $ID)) return '#locked';
1267
- if (!is_array($ext)) $ext = [];
1268
- $ext['wpdmdl'] = $ID;
1269
- $ext['refresh'] = uniqid() . time();
1270
- $permalink = get_permalink($ID);
1271
- $permalink = apply_filters("wpdm_download_url_base", $permalink, $ID);
1272
- $download_url = add_query_arg($ext, $permalink);
1273
- $flat = (int)get_option('__wpdm_flat_download_url', 0);
1274
- $code = json_encode($ext);
1275
- $code = base64_encode($code);
1276
- $code = rtrim($code, '=');
1277
- $filename = isset($ext['filename']) ? $ext['filename'] : '';
1278
- if (isset($ext['ind']) && $filename == '') {
1279
- $files = $this->getFiles($ID);
1280
- $filename = wpdm_basename($files[$ext['ind']]);
1281
- }
1282
- if (!isset($ext['ind']) && $filename == '') {
1283
- $files = $this->getFiles($ID);
1284
- if (count($files) > 1)
1285
- $filename = sanitize_file_name(get_the_title($ID)) . ".zip";
1286
- else {
1287
- $filename = array_shift($files);
1288
- $filename = wpdm_basename($filename);
1289
- }
1290
 
1291
- }
1292
- if ($flat) $download_url = home_url("/wpdmdl/{$ID}-{$code}/{$filename}");
1293
- return $download_url;
1294
- }
1295
-
1296
- /**
1297
- * @param $ID
1298
- * @return false|string
1299
- */
1300
- public function getMasterDownloadURL($ID)
1301
- {
1302
- $package_url = get_permalink($ID);
1303
- $params['wpdmdl'] = $ID;
1304
- $params['masterkey'] = get_post_meta($ID, '__wpdm_masterkey', true);
1305
- $download_url = add_query_arg($params, $package_url);
1306
- return $download_url;
1307
- }
1308
-
1309
- /**
1310
- * @param $ID
1311
- * @param $Key
1312
- * @return bool
1313
- */
1314
- public function validateMasterKey($ID, $Key)
1315
- {
1316
- if ($Key === '') return false;
1317
- $masterKey = get_post_meta($ID, '__wpdm_masterkey', true);
1318
- if ($masterKey === '') return false;
1319
- if ($masterKey === $Key) return true;
1320
- return false;
1321
- }
1322
-
1323
- /**
1324
- * @param $ID
1325
- * @param int $usageLimit
1326
- * @param int $expirePeriod seconds
1327
- * @return string
1328
- */
1329
- function expirableDownloadLink($ID, $usageLimit = 10, $expirePeriod = 999999, $sessionOnly = true)
1330
- {
1331
- $key = uniqid();
1332
- $exp = array('use' => $usageLimit, 'expire' => time() + $expirePeriod);
1333
- if (!$sessionOnly)
1334
- update_post_meta($ID, "__wpdmkey_" . $key, $exp);
1335
- else
1336
- TempStorage::set("__wpdmkey_{$key}", $exp, time() + $expirePeriod);
1337
- //Session::set( '__wpdm_unlocked_'.$ID , 1 );
1338
- //$download_url = $this->getDownloadURL($ID, "_wpdmkey={$key}");
1339
- $permalink = get_permalink($ID);
1340
- $permalink = apply_filters("wpdm_download_url_base", $permalink, $ID);
1341
- $download_url = add_query_arg(array("wpdmdl" => $ID, "_wpdmkey" => $key), $permalink);
1342
- return $download_url;
1343
- }
1344
-
1345
- /**
1346
- * @param $ID
1347
- * @param int $usageLimit
1348
- * @param int $expirePeriod seconds
1349
- * @return string
1350
- */
1351
- static function expirableDownloadPage($ID, $usageLimit = 10, $expirePeriod = 604800, $sessionOnly = true)
1352
- {
1353
- $key = uniqid();
1354
- $exp = array('use' => $usageLimit, 'expire' => time() + $expirePeriod);
1355
- if (!$sessionOnly)
1356
- update_post_meta($ID, "__wpdmkey_" . $key, $exp);
1357
- else
1358
- TempStorage::set("__wpdmkey_{$key}", $exp, time() + $expirePeriod);
1359
- $download_page_key = Crypt::encrypt(array('pid' => $ID, 'key' => $key));
1360
- $download_page = home_url("wpdm-download/{$download_page_key}");
1361
- return $download_page;
1362
- }
1363
-
1364
-
1365
- /**
1366
- * @usage Fetch link/page template and return generated html
1367
- * @param $template
1368
- * @param $vars
1369
- * @param string $type
1370
- * @return mixed|string
1371
- */
1372
- public function fetchTemplate($template, $vars, $type = 'link')
1373
- {
1374
- if (!is_array($vars) && is_int($vars) && $vars > 0) $vars = array('ID' => $vars);
1375
- if (!isset($vars['ID']) || intval($vars['ID']) < 1) return '';
1376
 
1377
- $loginmsg = Messages::login_required($vars['ID']);
 
1378
 
1379
- if (!is_user_logged_in() && count(self::allowedRoles($vars['ID'])) >= 0 && !self::userCanAccess($vars['ID'])) {
1380
- $loginform = wpdm_login_form(array('redirect' => get_permalink($vars['ID'])));
1381
- $hide_all_message = get_option('__wpdm_login_form', 0) == 1 ? $loginform : stripcslashes(str_replace(array("[loginform]", "[this_url]", "[package_url]"), array($loginform, $_SERVER['REQUEST_URI'], get_permalink($vars['ID'])), $loginmsg));
1382
- if (get_option('_wpdm_hide_all', 0) == 1) return $type == 'page' ? $hide_all_message : '';
1383
- }
1384
 
1385
- if (is_user_logged_in() && !self::userCanAccess($vars['ID']) && get_option('_wpdm_hide_all', 0) == 1) return $type != 'page' ? "" : get_option('__wpdm_permission_denied_msg', __("You are not allowed to download this item!", "download-manager"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1386
 
 
 
 
 
1387
 
1388
- /*$default['link'] = 'link-template-default.php';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1389
  $default['page'] = 'page-template-default.php';
1390
 
1391
 
@@ -1438,186 +1677,228 @@ class PackageController extends PackageTemplate
1438
 
1439
  }*/
1440
 
1441
- $ret = $this->prepare($vars['ID'], $template, $type);
1442
- if (!is_wp_error($ret))
1443
- $vars = $this->packageData;
1444
- else
1445
- return '';
1446
-
1447
- // Get template content
1448
- $template = $this->getTemplateContent($template, $vars['ID'], $type);
1449
-
1450
- if (isset($vars['__loginform_only']) && $vars['__loginform_only'] != '') return $vars['__loginform_only'];
1451
-
1452
- preg_match_all("/\[cf ([^\]]+)\]/", $template, $cfmatches);
1453
- preg_match_all("/\[thumb_([0-9]+)x([0-9]+)\]/", $template, $matches);
1454
- preg_match_all("/\[thumb_url_([0-9]+)x([0-9]+)\]/", $template, $umatches);
1455
- preg_match_all("/\[thumb_gallery_([0-9]+)x([0-9]+)\]/", $template, $gmatches);
1456
- preg_match_all("/\[excerpt_([0-9]+)\]/", $template, $xmatches);
1457
- preg_match_all("/\[pdf_thumb_([0-9]+)x([0-9]+)\]/", $template, $pmatches);
1458
- preg_match_all("/\[txt=([^\]]+)\]/", $template, $txtmatches);
1459
- preg_match_all("/\[hide_empty:([^\]]+)\]/", $template, $hematches);
1460
- preg_match_all("/\[video_player_([0-9]+)\]/", $template, $vdmatches);
1461
- preg_match_all("/\[product_preview_([0-9]+)x([0-9]+)\]/", $template, $ppmatches);
1462
- preg_match_all("/\[file_list_extended_([0-9]+)x([0-9]+)x([0-9]+)\]/", $template, $flematches);
1463
- preg_match_all("/\[image_gallery_([0-9]+)x([0-9]+)x([0-9]+)\]/", $template, $igematches);
1464
-
1465
-
1466
- //$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($vars['ID']), 'full');
1467
- //$vars['preview'] = $thumb['0'];
1468
- //$vars['featured_image'] = ($vars['preview'] != '')?"<img src='{$vars['preview']}' alt='{$vars['title']}' />":"";
1469
-
1470
- $tfiles = $vars['files'];
1471
- $pdf = is_array($tfiles) ? array_shift($tfiles) : "";
1472
- $ext = FileSystem::fileExt($pdf);
1473
-
1474
- // [video_player_...]
1475
- foreach ($vdmatches[0] as $nd => $scode) {
1476
- $scode = str_replace(array('[', ']'), '', $scode);
1477
- $vars[$scode] = self::videoPlayer($vars['ID'], $vars['files'], $vdmatches[1][$nd]);
1478
- }
1479
-
1480
- //Replace all file list extended tags
1481
- foreach ($flematches[0] as $nd => $scode) {
1482
- $scode = str_replace(array("[", "]"), "", $scode);
1483
- $vars[$scode] = FileList::Box($vars, $flematches[1][$nd], $flematches[2][$nd], $flematches[3][$nd]);
1484
- }
1485
-
1486
- //Replace all image gallery tags
1487
- foreach ($igematches[0] as $nd => $scode) {
1488
- $scode = str_replace(array("[", "]"), "", $scode);
1489
- $vars[$scode] = FileList::imageGallery($vars, $igematches[1][$nd], $igematches[2][$nd], $igematches[3][$nd]);
1490
- }
1491
-
1492
-
1493
- //Replace all txt variables
1494
- foreach ($txtmatches[0] as $nd => $scode) {
1495
- $scode = str_replace(array('[', ']'), '', $scode);
1496
- $vars[$scode] = __($txtmatches[1][$nd], "download-manager");
1497
- }
1498
-
1499
- // Parse [pdf_thumb] tag in link/page template
1500
- if (strpos($template, 'pdf_thumb')) {
1501
- if ($ext == 'pdf') {
1502
- $pdf_preview = FileSystem::pdfThumbnail($pdf, $vars['ID']);
1503
- $vars['pdf_thumb'] = "<img alt='{$vars['title']}' src='" . $pdf_preview . "' />";
1504
- $vars['pdf_thumb_url'] = $pdf_preview;
1505
- $vars['pdf_name'] = str_replace(["pdf", "PDF"], "", wp_basename($pdf));
1506
- }
1507
- else $vars['pdf_thumb'] = $vars['preview'] != '' ? "<img alt='{$vars['title']}' src='{$vars['preview']}' />" : "";
1508
- }
1509
-
1510
- // Parse [pdf_thumb_WxH] tag in link/page template
1511
- foreach ($pmatches[0] as $nd => $scode) {
1512
- $imsrc = wpdm_dynamic_thumb(FileSystem::pdfThumbnail($pdf, $vars['ID']), array($pmatches[1][$nd], $pmatches[2][$nd]));
1513
- $scode = str_replace(array("[", "]"), "", $scode);
1514
- $vars[$scode] = $imsrc != '' ? "<img src='" . $imsrc . "' alt='{$vars['title']}' />" : '';
1515
- }
1516
-
1517
- // Parse [file_type] tag in link/page template
1518
- if (strpos($template, 'file_type')) {
1519
- $vars['file_types'] = self::fileTypes($vars['ID'], false);
1520
- if (is_array($vars['file_types']))
1521
- $vars['file_types'] = implode(", ", $vars['file_types']);
1522
- $vars['file_type_icons'] = self::fileTypes($vars['ID']);
1523
- }
1524
-
1525
- $crop = get_option('__wpdm_crop_thumbs', true);
1526
-
1527
- // [thumb_WxH]
1528
- foreach ($matches[0] as $nd => $scode) {
1529
- $imsrc = wpdm_dynamic_thumb($vars['preview'], array($matches[1][$nd], $matches[2][$nd]), $crop);
1530
- $scode = str_replace(array("[", "]"), "", $scode);
1531
- $vars[$scode] = $vars['preview'] != '' ? "<img class='wpdm-thumb wpdm-thumb-{$matches[1][$nd]}x{$matches[2][$nd]} wpdm-thumb-{$vars['ID']}' src='" . $imsrc . "' alt='{$vars['title']}' />" : '';
1532
- }
1533
-
1534
- // [thumb_url...]
1535
- foreach ($umatches[0] as $nd => $scode) {
1536
- $scode = str_replace(array("[", "]"), "", $scode);
1537
- $vars[$scode] = $vars['preview'] != '' ? wpdm_dynamic_thumb($vars['preview'], array($umatches[1][$nd], $umatches[2][$nd]), $crop) : '';
1538
- }
1539
-
1540
- // [thumb_gallery...]
1541
- foreach ($gmatches[0] as $nd => $scode) {
1542
- $scode = str_replace(array("[", "]"), "", $scode);
1543
- $vars[$scode] = $this->additionalPreviewImages($vars, $gmatches[1][$nd], $gmatches[2][$nd]);
1544
- }
1545
-
1546
- // [product_preview...]
1547
- foreach ($ppmatches[0] as $nd => $scode) {
1548
- $scode = str_replace(array("[", "]"), "", $scode);
1549
- $vars[$scode] = self::productPreview($vars['ID'], $vars['files'], $ppmatches[1][$nd], $ppmatches[2][$nd]);
1550
- }
1551
-
1552
- // [excerpt_...]
1553
- foreach ($xmatches[0] as $nd => $scode) {
1554
- $ss = substr(strip_tags($vars['description']), 0, intval($xmatches[1][$nd]));
1555
- $tmp = explode(" ", substr(strip_tags($vars['description']), intval($xmatches[1][$nd])));
1556
- $bw = array_shift($tmp);
1557
- $ss .= $bw;
1558
- $scode = str_replace(array("[", "]"), "", $scode);
1559
- $vars[$scode] = $ss . '...';
1560
- }
1561
-
1562
- if ($type == 'page' && (strpos($template, '[similar_downloads]') || strpos($vars['description'], '[similar_downloads]')))
1563
- $vars['similar_downloads'] = $this->similarPackages($vars, 6);
1564
-
1565
- if (strpos($template, 'doc_preview'))
1566
- $vars['doc_preview'] = self::docPreview($vars);
1567
-
1568
- if (substr_count($template, 'video_preview_modal'))
1569
- $vars['video_preview_modal'] = self::videoPreviewModal($vars, $type);
1570
-
1571
-
1572
- $vars['fav_button'] = self::favBtn($vars['ID']);
1573
- $vars['fav_button_sm'] = self::favBtn($vars['ID'], array('size' => 'btn-sm', 'a2f_label' => "<i class='fa fa-heart'></i> &nbsp; " . __("Add to favourite", "download-manager"), 'rff_label' => "<i class='fa fa-heart'></i> &nbsp; " . __("Remove from favourite", "download-manager")));
1574
- $vars['fav_button_ico_sm'] = self::favBtn($vars['ID'], array('size' => 'btn-sm', 'a2f_label' => "<i class='far fa-heart'></i>", 'rff_label' => "<i class='fas fa-heart'></i>"));
1575
- $vars['fav_button_ico'] = self::favBtn($vars['ID'], array('size' => '', 'a2f_label' => "<i class='fa fa-heart'></i>", 'rff_label' => "<i class='fa fa-heart'></i>"));
1576
-
1577
-
1578
- // If need to re-process any data before fetch template
1579
- $vars['__template_type'] = $type;
1580
- $vars = apply_filters("wdm_before_fetch_template", $vars, $template, $type);
1581
-
1582
- foreach ($hematches[0] as $index => $hide_empty) {
1583
- $hide_empty = str_replace(array('[', ']'), '', $hide_empty);
1584
- if (!isset($vars[$hematches[1][$index]]) || ($vars[$hematches[1][$index]] == '' || $vars[$hematches[1][$index]] == '0'))
1585
- $vars[$hide_empty] = 'wpdm_hide wpdm_remove_empty';
1586
- else
1587
- $value[$hide_empty] = '';
1588
- }
1589
-
1590
-
1591
- $keys = array();
1592
- $values = array();
1593
-
1594
- foreach ($vars as $key => $value) {
1595
- if (!is_array($value) && !is_object($value)) {
1596
- $keys[] = "[$key]";
1597
- $values[] = $value;
1598
- }
1599
- }
1600
-
1601
-
1602
- $loginform = wpdm_login_form(array('redirect' => get_permalink($vars['ID'])));
1603
- $hide_all_message = get_option('__wpdm_login_form', 0) == 1 ? $loginform : stripcslashes(str_replace(array("[loginform]", "[this_url]", "[package_url]"), array($loginform, $_SERVER['REQUEST_URI'], get_permalink($vars['ID'])), $loginmsg));
1604
-
1605
- if ($vars['download_link'] == 'blocked' && $type == 'link') return "";
1606
- if ($vars['download_link'] == 'blocked' && $type == 'page') return get_option('__wpdm_permission_denied_msg');
1607
- if ($vars['download_link'] == 'loginform' && $type == 'link') return "";
1608
- if ($vars['download_link'] == 'loginform' && $type == 'page') return $hide_all_message;
1609
-
1610
-
1611
- $template = str_replace($keys, $values, @stripcslashes($template));
1612
-
1613
- $template = apply_filters("wpdm_after_fetch_template", $template, $vars);
1614
-
1615
- //wp_reset_query();
1616
- //wp_reset_postdata();
1617
- return $template;
1618
- }
1619
-
1620
- /*public static function parseTemplate($template, $post, $type = 'link')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1621
  {
1622
 
1623
  if (!strpos(strip_tags($template), "]")) {
@@ -1662,550 +1943,640 @@ class PackageController extends PackageTemplate
1662
  return $template;
1663
  }*/
1664
 
1665
- /**
1666
- * @usage Find attached files types with a package
1667
- * @param $ID
1668
- * @param bool|true $img
1669
- * @return array|string
1670
- */
1671
- public static function fileTypes($ID, $img = true, $size = 16)
1672
- {
1673
- $files = maybe_unserialize(get_post_meta($ID, '__wpdm_files', true));
1674
- $ext = array();
1675
- if (is_array($files)) {
1676
- foreach ($files as $f) {
1677
- $f = trim($f);
1678
- $ext[] = FileSystem::fileExt($f);
1679
- }
1680
- }
1681
-
1682
- $ext = array_unique($ext);
1683
- $exico = '';
1684
- foreach ($ext as $exi) {
1685
- $exico .= "<img alt='{$exi}' title='{$exi}' class='ttip' style='width:{$size}px;height:{$size}px;' src='" . FileSystem::fileTypeIcon($exi) . "' /> ";
1686
- }
1687
- if ($img) return $exico;
1688
- return $ext;
1689
- }
1690
-
1691
-
1692
- /**
1693
- * @param $package
1694
- * @return string
1695
- * @usage Generate Google Doc Preview
1696
- */
1697
- public function docPreview($package)
1698
- {
1699
-
1700
- //$files = $package['files'];
1701
- $files = $this->getFiles($package['ID']);
1702
- if (!is_array($files)) return "";
1703
- $ind = -1;
1704
- $fext = '';
1705
- foreach ($files as $i => $sfile) {
1706
- $ifile = $sfile;
1707
- $sfile = explode(".", $sfile);
1708
- $fext = end($sfile);
1709
- if (in_array(end($sfile), array('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'))) {
1710
- $ind = $i;
1711
- break;
1712
- }
1713
- }
1714
-
1715
- if ($ind == -1) return "";
1716
- $ext = count($files) > 1 ? 'ind=' . $ind : '';
1717
- $params = array('ind' => $ind, 'filename' => $files[$ind]);
1718
- $url = $this->getDownloadURL($package['ID'], $params);
1719
- $url .= "&open=1";
1720
- if (strpos($ifile, "://")) $url = $ifile;
1721
- $doc_preview_html = FileSystem::docViewer($url, $package['ID'], $fext);
1722
- $doc_preview_html = apply_filters('wpdm_doc_preview', $doc_preview_html, $package, $url, $fext);
1723
- return $doc_preview_html;
1724
- }
1725
-
1726
- /**
1727
- * Get additional preview images
1728
- * @param $file
1729
- * @param $w
1730
- * @param $h
1731
- * @return string
1732
- */
1733
- function additionalPreviewImages($file, $w, $h){
1734
-
1735
- $file['additional_previews'] = maybe_unserialize(get_post_meta($file['ID'],'__wpdm_additional_previews', true));
1736
- $k = 0;
1737
- $img = '';
1738
- $id = uniqid();
1739
- if($file['additional_previews']){
1740
- foreach($file['additional_previews'] as $p){
1741
- ++$k;
1742
- if( is_numeric($p) )
1743
- $img .= "<a href='".wp_get_attachment_url($p)."' id='more_previews_a_{$k}' class='more_previews_a imgpreview wpdm-lightbox' data-lightbox-gallery='gallery_{$id}' rel='previews'><img id='more_previews_{$k}' class='more_previews img-rounded' src='". wpdm_dynamic_thumb( get_attached_file( $p ), array($w, $h)) ."'/></a>";
1744
- else
1745
- $img .= "<a href='{$p}' id='more_previews_a_{$k}' class='more_previews_a imgpreview wpdm-lightbox' data-lightbox-gallery='gallery_{$id}' rel='previews' ><img id='more_previews_{$k}' class='more_previews img-rounded' src='". wpdm_dynamic_thumb($p, array($w, $h)) ."'/></a>";
1746
- }}
1747
- $js = ""; // "<script>jQuery(function($){ $('a.more_previews_a').nivoLightbox(); });</script>";
1748
- return $img.$js;
1749
- }
1750
-
1751
- /**
1752
- * @usage Generates view preview modal link of the given package
1753
- * @param $package
1754
- * @param int $embed
1755
- * @param array $extras
1756
- * @return string
1757
- */
1758
- public function videoPreviewModal($package, $template_type = 'link')
1759
- {
1760
- if (is_int($package)) $package = get_post($package, ARRAY_A);
1761
- $link_label = wpdm_valueof($package, 'link_label');
1762
- $link_label = $link_label ? $link_label : $this->linkLabel($package['ID']);
1763
- $style = wpdm_download_button_style($template_type === 'page', $package['ID']);
1764
- $files = isset($package['files']) ? $package['files'] : $this->getFiles($package['ID']);
1765
- $video = "";
1766
- foreach ($files as $file) {
1767
- if (substr_count($file, 'youtu.be') || substr_count($file, 'youtube.com') || substr_count($file, 'vimeo.com')) {
1768
- $video = $file;
1769
- }
1770
- }
1771
- $link = "<a class='wpdm-lightbox {$style}' href='{$video}'>{$link_label}</a>";
1772
- return $link;
1773
-
1774
- }
1775
-
1776
- /**
1777
- * Get package link label
1778
- * @param $ID
1779
- * @return mixed|string|void
1780
- */
1781
- public function linkLabel($ID)
1782
- {
1783
- $link_label = get_post_meta($ID, '__wpdm_link_label', true);
1784
- $link_label = esc_attr($link_label);
1785
- $link_label = $link_label ? $link_label : __("Download", "download-manager");
1786
- return $link_label;
1787
- }
1788
-
1789
- /**
1790
- * @usage Create New Package
1791
- * @param $data
1792
- * @return mixed
1793
- */
1794
- public static function create($package_data)
1795
- {
1796
-
1797
- if (isset($package_data['post_type']))
1798
- unset($package_data['post_type']);
1799
-
1800
- $package_data_core = array(
1801
- 'post_title' => '',
1802
- 'post_content' => '',
1803
- 'post_excerpt' => '',
1804
- 'post_status' => 'publish',
1805
- 'post_type' => 'wpdmpro',
1806
- 'post_author' => get_current_user_id(),
1807
- 'ping_status' => get_option('default_ping_status'),
1808
- 'post_parent' => 0,
1809
- 'menu_order' => 0,
1810
- 'to_ping' => '',
1811
- 'pinged' => '',
1812
- 'post_password' => '',
1813
- 'guid' => '',
1814
- 'post_content_filtered' => '',
1815
- 'import_id' => 0
1816
- );
1817
-
1818
- $package_data_meta = array(
1819
- 'files' => array(),
1820
- 'fileinfo' => array(),
1821
- 'package_dir' => '',
1822
- 'link_label' => __("Download", "download-manager"),
1823
- 'download_count' => 0,
1824
- 'view_count' => 0,
1825
- 'version' => '1.0.0',
1826
- 'stock' => 0,
1827
- 'package_size' => 0,
1828
- 'package_size_b' => 0,
1829
- 'access' => '',
1830
- 'individual_file_download' => -1,
1831
- 'cache_zip' => -1,
1832
- 'template' => 'link-template-panel.php',
1833
- 'page_template' => 'page-template-1col-flat.php',
1834
- 'password_lock' => '0',
1835
- 'facebook_lock' => '0',
1836
- 'gplusone_lock' => '0',
1837
- 'linkedin_lock' => '0',
1838
- 'tweet_lock' => '0',
1839
- 'email_lock' => '0',
1840
- 'icon' => '',
1841
- 'import_id' => 0
1842
- );
1843
-
1844
- foreach ($package_data_core as $key => &$value) {
1845
- $value = isset($package_data[$key]) ? $package_data[$key] : $package_data_core[$key];
1846
- }
1847
-
1848
- if (!isset($package_data['ID']))
1849
- $post_id = wp_insert_post($package_data_core);
1850
- else {
1851
- $post_id = $package_data['ID'];
1852
- $package_data_core['ID'] = $post_id;
1853
- wp_update_post($package_data_core);
1854
- }
1855
-
1856
- foreach ($package_data_meta as $key => $value) {
1857
- $value = isset($package_data[$key]) ? $package_data[$key] : $package_data_meta[$key];
1858
- update_post_meta($post_id, '__wpdm_' . $key, $value);
1859
- }
1860
-
1861
- if (isset($package_data['cats']))
1862
- wp_set_post_terms($post_id, $package_data['cats'], 'wpdmcategory');
1863
-
1864
- if (isset($package_data['featured_image'])) {
1865
-
1866
- $wp_filetype = wp_check_filetype(wp_basename($package_data['featured_image']), null);
1867
-
1868
- if (__::is_url($package_data['featured_image'])) {
1869
- $upload_dir = wp_upload_dir();
1870
- $file_path = $upload_dir['path'] . '/' . sanitize_file_name($package_data['post_title']) . '.' . $wp_filetype['ext'];
1871
- //$data = remote_get($package_data['featured_image']);
1872
- //$ret = copy($package_data['featured_image'], $file_path);
1873
- //if(!$ret) wpdmdd($package_data['featured_image']);
1874
- file_put_contents($file_path, wpdm_remote_get($package_data['featured_image']));
1875
- $package_data['featured_image'] = $file_path;
1876
- }
1877
-
1878
- $mime_type = '';
1879
-
1880
- if (isset($wp_filetype['type']) && $wp_filetype['type'])
1881
- $mime_type = $wp_filetype['type'];
1882
- unset($wp_filetype);
1883
- $attachment = array(
1884
- 'post_mime_type' => $mime_type,
1885
- 'post_parent' => $post_id,
1886
- 'post_title' => wp_basename($package_data['featured_image']),
1887
- 'post_status' => 'inherit'
1888
- );
1889
- $attachment_id = wp_insert_attachment($attachment, $package_data['featured_image'], $post_id);
1890
- unset($attachment);
1891
-
1892
- if (!is_wp_error($attachment_id)) {
1893
- $attachment_data = wp_generate_attachment_metadata($attachment_id, $package_data['featured_image']);
1894
- wp_update_attachment_metadata($attachment_id, $attachment_data);
1895
- unset($attachment_data);
1896
- set_post_thumbnail($post_id, $attachment_id);
1897
- }
1898
- }
1899
-
1900
- return $post_id;
1901
- }
1902
-
1903
- /**
1904
- * @param $id
1905
- * @param array $atfb
1906
- * @return string
1907
- */
1908
- public static function favBtn($id, $atfb = array(), $count = true)
1909
- {
1910
- if (empty($atfb))
1911
- $atfb = array('size' => '', 'a2f_label' => "<i class='fa fa-heart'></i> &nbsp;" . __("Add to favourite", "download-manager"), 'rff_label' => "<i class='fa fa-heart'></i> &nbsp; " . __("Remove from favourite", "download-manager"));
1912
- $atfb = apply_filters("wpdm_fav_btn", $atfb, $id);
1913
- $myfavs = maybe_unserialize(get_user_meta(get_current_user_id(), '__wpdm_favs', true));
1914
- $ufavs = maybe_unserialize(get_post_meta($id, '__wpdm_favs', true));
1915
- $pfc = is_array($ufavs) ? count($ufavs) : 0;
1916
- $btnclass = is_array($myfavs) && in_array($id, $myfavs) ? 'btn-danger' : 'btn-secondary';
1917
- $label = is_array($myfavs) && in_array($id, $myfavs) ? $atfb['rff_label'] : $atfb['a2f_label'];
1918
- extract($atfb);
1919
- if ($count)
1920
- return "<div class='btn-group'><button type='button' data-alabel=\"{$atfb['a2f_label']}\" data-rlabel=\"{$atfb['rff_label']}\" data-package='{$id}' class='btn btn-wpdm-a2f {$btnclass} {$size} btn-simple'>{$label}</button><button class='btn btn-secondary btn-simple {$size}' disabled='disabled'>{$pfc}</button></div>";
1921
- else
1922
- return "<button type='button' data-alabel=\"{$atfb['a2f_label']}\" data-rlabel=\"{$atfb['rff_label']}\" data-package='{$id}' class='btn btn-wpdm-a2f {$btnclass} {$size} btn-simple'>{$label}</button>";
1923
-
1924
- }
1925
-
1926
- /**
1927
- * Shows favourite count
1928
- * @param $id
1929
- * @return int
1930
- */
1931
- public static function favCount($id)
1932
- {
1933
- $ufavs = maybe_unserialize(get_post_meta($id, '__wpdm_favs', true));
1934
- $pfc = is_array($ufavs) ? count($ufavs) : 0;
1935
- return $pfc;
1936
- }
1937
-
1938
- /**
1939
- * @param $ID
1940
- * @param $emails
1941
- * @param string $names
1942
- * @param int $usageLimit
1943
- * @param int $expireTime
1944
- * @usage mail package link to specified email address
1945
- * @since 4.7.4
1946
- */
1947
- static function emailDownloadLink($ID, $emails, $names = '', $usageLimit = 3, $expireTime = 604800)
1948
- {
1949
- if (!is_array($emails)) $emails = explode(",", $emails);
1950
- if (!is_array($names)) $names = explode(",", $names);
1951
- $title = get_the_title($ID);
1952
- $banner = get_the_post_thumbnail_url($ID, array(600, 400));
1953
- $logo = get_site_icon_url();
1954
- foreach ($emails as $index => $email) {
1955
- $download_link = WPDM()->package->expirableDownloadLink($ID, $usageLimit, $expireTime);
1956
- $download_page_link = WPDM()->package->expirableDownloadPage($ID, $usageLimit, $expireTime);
1957
- $params = array(
1958
- 'to_email' => $email,
1959
- 'name' => isset($names[$index]) ? $names[$index] : '',
1960
- 'package_name' => $title,
1961
- 'download_url' => $download_link,
1962
- 'download_page_url' => $download_page_link,
1963
- 'img_logo' => $logo,
1964
- 'banner' => $banner
1965
- );
1966
- \WPDM\__\Email::send("email-lock", $params);
1967
- }
1968
- }
1969
-
1970
- /**
1971
- * Check if specified link or page template have the tag
1972
- * @param null $template
1973
- * @param $tag
1974
- * @return bool|string
1975
- */
1976
- static function templateHasTag($template = null, $tag = '')
1977
- {
1978
- if (!$template) return true;
1979
- else if (is_string($tag)) return substr_count($template, "[{$tag}]");
1980
- else if (is_array($tag)) {
1981
- foreach ($tag as $t) {
1982
- if (substr_count($template, "[{$t}]")) return true;
1983
- }
1984
- }
1985
- return false;
1986
-
1987
- }
1988
-
1989
- /**
1990
- * Returns package icon
1991
- * @param $ID
1992
- * @return string
1993
- */
1994
- static function icon($ID, $html = false, $class = '')
1995
- {
1996
- $icon = get_post_meta($ID, '__wpdm_icon', true);
1997
- if ($icon == '') {
1998
- $file_types = WPDM()->package->fileTypes($ID, false);
1999
- if (count($file_types)) {
2000
- if (count($file_types) == 1) {
2001
- $tmpavar = $file_types;
2002
- $ext = $tmpvar = array_shift($tmpavar);
2003
- } else
2004
- $ext = 'zip';
2005
- } else
2006
- $ext = "unknown";
2007
- if ($ext === '') $ext = 'wpdm';
2008
- $icon = FileSystem::fileTypeIcon($ext);
2009
- }
2010
- if ($html) $icon = "<img src='{$icon}' alt='Icon' class='$class' />";
2011
- return apply_filters("wpdm_package_icon", $icon, $ID);
2012
- }
2013
-
2014
- /**
2015
- * Create a copy of a given package
2016
- * @param $ID
2017
- * @return int|\WP_Error
2018
- */
2019
- static function copy($ID, $author = null, $new_meta = array())
2020
- {
2021
- $old_pack = (array)get_post($ID);
2022
- $package = array(
2023
- 'post_title' => $old_pack['post_title'],
2024
- 'post_content' => $old_pack['post_content'],
2025
- 'post_status' => $old_pack['post_status'],
2026
- 'comment_status' => $old_pack['comment_status'],
2027
- 'ping_status' => $old_pack['ping_status'],
2028
- 'post_type' => 'wpdmpro'
2029
- );
2030
-
2031
- if ($author)
2032
- $package['post_author'] = $author;
2033
- $new_ID = wp_insert_post($package);
2034
-
2035
- $meta = get_post_meta($ID);
2036
-
2037
- foreach ($meta as $key => $value) {
2038
- foreach ($value as $v) {
2039
- update_post_meta($new_ID, $key, maybe_unserialize($v));
2040
- }
2041
- }
2042
- if (is_array($new_meta)) {
2043
- foreach ($new_meta as $key => $value) {
2044
- update_post_meta($new_ID, $key, maybe_unserialize($value));
2045
- }
2046
- }
2047
- return $new_ID;
2048
- }
2049
-
2050
- static function dummy()
2051
- {
2052
- $package = array(
2053
- 'post_title' => __('Sample Package', 'download-manager'),
2054
- 'post_content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. ',
2055
- 'excerpt' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s',
2056
- 'post_status' => 'publish',
2057
- 'download_link' => '<a href="#">Download</a>',
2058
- 'download_link_extended' => '<a href="#">Download</a>',
2059
- );
2060
- return $package;
2061
- }
2062
-
2063
- /**
2064
- * @param null $ID
2065
- * @return array Additional preview image urls
2066
- */
2067
- function additionalPreviews($ID = null)
2068
- {
2069
- $ID = $ID ? $ID : $this->ID;
2070
- if (!$ID && is_singular('wpdmpro')) $ID = get_the_ID();
2071
- if (!$ID) return array();
2072
- $additional_previews = get_post_meta($ID, '__wpdm_additional_previews', true);
2073
- $previews = array();
2074
- foreach ($additional_previews as $media_id) {
2075
- $previews[] = wp_get_attachment_url($media_id);
2076
- }
2077
- return $previews;
2078
- }
2079
-
2080
- function getThumbnail($ID, $FILEID, $size)
2081
- {
2082
- if (!$this->files)
2083
- $this->files = self::getFiles($ID, true);
2084
- $file = wpdm_valueof($this->files, $FILEID);
2085
-
2086
- $imgext = array('png', 'jpg', 'jpeg', 'gif');
2087
- $ext = FileSystem::fileExt($file);
2088
- $thumb = '';
2089
- $abspath = WPDM()->package->locateFile($file);
2090
-
2091
- if (in_array($ext, $imgext) && $abspath)
2092
- $thumb = FileSystem::imageThumbnail($abspath, $size[0], $size[1], WPDM_USE_GLOBAL, true);
2093
- else if ($ext === 'svg')
2094
- $thumb = str_replace(ABSPATH, home_url('/'), $file);
2095
- else if (strtolower($ext) === 'pdf' && class_exists('Imagick'))
2096
- $thumb = FileSystem::pdfThumbnail($file, md5($file));
2097
- else {
2098
- $thumb = FileSystem::fileTypeIcon($ext);
2099
- }
2100
-
2101
- return apply_filters("wpdm_file_thumbnail", $thumb, ['file' => $file, 'FILEID' => $FILEID, 'ID' => $ID, 'size' => $size]);
2102
- }
2103
-
2104
- function locateFile($file)
2105
- {
2106
- if (file_exists($file))
2107
- return $file;
2108
- if (file_exists(UPLOAD_DIR . $file))
2109
- return UPLOAD_DIR . $file;
2110
- return false;
2111
- }
2112
-
2113
- function addViewCount()
2114
- {
2115
- if (isset($_REQUEST['__wpdm_view_count']) && wp_verify_nonce($_REQUEST['__wpdm_view_count'], NONCE_KEY)) {
2116
-
2117
- $id = (int)($_REQUEST['id']);
2118
- $views = (int)get_post_meta($id, '__wpdm_view_count', true);
2119
- update_post_meta($id, '__wpdm_view_count', $views + 1);
2120
- wp_send_json(['views' => $views + 1]);
2121
- }
2122
- }
2123
-
2124
-
2125
- /**
2126
- * @usage Find similar packages
2127
- * @param null $package_id
2128
- * @param int $count
2129
- * @param bool|true $html
2130
- * @return array|bool|string
2131
- */
2132
- function similarPackages($package_id = null, $count = 5, $html = true)
2133
- {
2134
- $id = $package_id ? $package_id : get_the_ID();
2135
- if (is_array($package_id)) $id = $package_id['ID'];
2136
- $tags = wp_get_post_terms($id, 'wpdmtag');
2137
- $cats = wp_get_post_terms($id, 'wpdmcategory');
2138
- $posts = array();
2139
- if ($tags) {
2140
- $tag_ids = array();
2141
- foreach ($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
2142
- foreach ($cats as $individual_cat) $cat_ids[] = $individual_cat->term_id;
2143
- $args = array(
2144
- 'post_type' => 'wpdmpro',
2145
- 'tax_query' => [
2146
- [
2147
- 'taxonomy' => 'wpdmtag',
2148
- 'field' => 'id',
2149
- 'terms' => $tag_ids,
2150
- 'operator' => 'IN'
2151
- ],
2152
- [
2153
- 'taxonomy' => 'wpdmcategory',
2154
- 'field' => 'id',
2155
- 'terms' => $cat_ids,
2156
- 'operator' => 'IN'
2157
- ],
2158
- 'relation' => 'OR'
2159
- ],
2160
- 'post__not_in' => array($id),
2161
- 'posts_per_page' => $count
2162
- );
2163
-
2164
- $posts = get_posts($args);
2165
-
2166
- if (!$html) return $posts;
2167
-
2168
- $html = "";
2169
- //Filter hook to change related packages/downloads template
2170
- $template = apply_filters("wpdm_replated_package_template", "link-template-panel.php", $package_id);
2171
- $cols = apply_filters("wpdm_replated_package_columns", 6, $package_id);
2172
- foreach ($posts as $p) {
2173
-
2174
- $package['ID'] = $p->ID;
2175
- $package['post_title'] = $p->post_title;
2176
- $package['post_content'] = $p->post_content;
2177
- $package['post_excerpt'] = $p->post_excerpt;
2178
- $html .= "<div class='col-md-{$cols}'>" . wpdm_fetch_template($template, $package, 'link') . "</div>";
2179
-
2180
- }
2181
- }
2182
- if (count($posts) == 0) $html = "<div class='col-md-12'><div class='alert alert-info'>" . __("No related download found!", "download-manager") . "</div> </div>";
2183
- $html = "<div class='w3eden'><div class='row'>" . $html . "</div></div>";
2184
- wp_reset_query();
2185
- return $html;
2186
- }
2187
-
2188
- function search($keyword = '')
2189
- {
2190
- $keyword = wpdm_query_var('search') ? : $keyword;
2191
- if($keyword) {
2192
- $query = new Query();
2193
- $query->search($keyword);
2194
- if(wpdm_query_var('premium', 'int') > 0) {
2195
- $query->meta('__wpdm_base_price', 0, '>');
2196
- $query->meta_relation('AND');
2197
- }
2198
- $query->process();
2199
- $packages = $query->packages();
2200
- if(wpdm_query_var('premium', 'int') > 0 && function_exists('wpdmpp_product_license_options')) {
2201
- foreach ($packages as &$package) {
2202
- $licenses = wpdmpp_product_license_options($package->ID);
2203
- $package->licenses = count($licenses) > 0 ? $licenses : null;
2204
- }
2205
- }
2206
- wp_send_json(['total' => $query->count, 'packages' => $packages, 'q' => $query->params]);
2207
- }
2208
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2209
 
2210
 
2211
  }
12
  use WPDM\__\Session;
13
  use WPDM\__\TempStorage;
14
 
15
+ class PackageController extends PackageTemplate {
16
+
17
+ public $ID;
18
+ public $package;
19
+ public $shortCodes;
20
+ public $packageData = [];
21
+ public $files = [];
22
+ public $restAPI;
23
+ public $templateDir;
24
+
25
+
26
+ function __construct( $ID = null ) {
27
+ parent::__construct();
28
+
29
+ $this->templateDir = __DIR__ . '/views/';
30
+ $this->shortCodes = new Shortcodes();
31
+ new Hooks();
32
+
33
+ new RestAPI();
34
+
35
+ global $post;
36
+ if ( ! $ID && is_object( $post ) && $post->post_type == 'wpdmpro' ) {
37
+ $ID = $post->ID;
38
+ }
39
+ $this->ID = $ID;
40
+
41
+ }
42
+
43
+ function prepare( $ID = null, $template = null, $template_type = 'page' ) {
44
+ global $post;
45
+
46
+ if ( ! $ID ) {
47
+ $ID = $this->ID;
48
+ }
49
+ if ( ! $ID && isset( $post->ID ) && get_post_type( $post ) == 'wpdmpro' ) {
50
+ $ID = $post->ID;
51
+ }
52
+ if ( ! $ID ) {
53
+ $this->packageData = array( 'error' => __( "ID missing!", "download-manager" ) );
54
+
55
+ return $this;
56
+ }
57
+
58
+ if ( ! is_object( $post ) || $post->ID != $ID ) {
59
+ $_post = get_post( $ID );
60
+ if ( get_class( $_post ) != 'WP_Post' ) {
61
+ return new \WP_Error( 'broke', __( "Invalid Package ID!", "download-manager" ) );
62
+ }
63
+ $post_vars = (array) $_post;
64
+ } else {
65
+ $post_vars = (array) $post;
66
+ }
67
+
68
+ $ID = $post_vars['ID'];
69
+
70
+ $post_vars['title'] = stripcslashes( $post_vars['post_title'] );
71
+ $post_vars['description'] = stripcslashes( $post_vars['post_content'] );
72
+ $post_vars['description'] = wpautop( stripslashes( $post_vars['description'] ) );
73
+
74
+ if ( ! has_shortcode( $post_vars['description'], 'wpdm_package' ) ) {
75
+ $post_vars['description'] = do_shortcode( stripslashes( $post_vars['description'] ) );
76
+ }
77
+
78
+ $post_vars['excerpt'] = wpautop( stripcslashes( wpdm_escs( $post_vars['post_excerpt'] ) ) );
79
+ $author = get_user_by( 'id', $post_vars['post_author'] );
80
+ if ( is_object( $author ) ) {
81
+ $post_vars['author_name'] = $author->display_name;
82
+ }
83
+ $post_vars['author_profile_url'] = get_author_posts_url( $post_vars['post_author'] );
84
+ if ( is_object( $author ) ) {
85
+ $post_vars['avatar_url'] = get_avatar_url( $author->user_email );
86
+ }
87
+ if ( is_object( $author ) ) {
88
+ $post_vars['avatar'] = get_avatar( $author->user_email, 96 );
89
+ }
90
+ $post_vars['author_package_count'] = count_user_posts( $post_vars['post_author'], "wpdmpro" );
91
+
92
+ $template_tags = $this->parseTemplate( $template, $ID, $template_type );
93
+
94
+ //Featured Image
95
+ $post_vars['preview'] = $post_vars['thumb'] = "";
96
+ if ( has_post_thumbnail( $ID ) ) {
97
+ $src = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'full', false );
98
+ $post_vars['preview'] = $src['0'];
99
+ $post_vars['featured_image'] = get_the_post_thumbnail( $ID, 'full' );
100
+
101
+ if ( in_array( 'thumb', $template_tags ) ) {
102
+ $post_vars['thumb'] = get_the_post_thumbnail( $ID );
103
+ }
104
+ }
105
+ if ( in_array( 'create_date', $template_tags ) ) {
106
+ $post_vars['create_date'] = get_the_date( '', $ID );
107
+ }
108
+
109
+ if ( in_array( 'update_date', $template_tags ) ) {
110
+ $post_vars['update_date'] = date_i18n( get_option( 'date_format' ), strtotime( $post_vars['post_modified'] ) );
111
+ }
112
+
113
+ if ( in_array( 'categories', $template_tags ) ) {
114
+ $post_vars['categories'] = get_the_term_list( $ID, 'wpdmcategory', '', ', ', '' );
115
+ }
116
+
117
+ if ( in_array( 'category', $template_tags ) ) {
118
+ $cats = get_the_terms( $ID, 'wpdmcategory' );
119
+ if ( is_array( $cats ) && count( $cats ) > 0 ) {
120
+ $post_vars['category'] = $cats[0]->name;
121
+ }
122
+ }
123
+
124
+ $post_vars['comment_count'] = wp_count_comments( $ID )->approved;
125
+
126
+ if ( in_array( 'cats_class', $template_tags ) ) {
127
+ $cats = wp_get_post_terms( $ID, 'wpdmcategory' );
128
+ $post_vars['cats_class'] = "";
129
+ foreach ( $cats as $cat ) {
130
+ $post_vars['cats_class'] .= " wpdmcat-{$cat->id} {$cat->slug}";
131
+ }
132
+ }
133
+
134
+ $data = $this->metaData( $post_vars['ID'] );
135
+
136
+ if ( is_array( $data ) ) {
137
+ $post_vars = array_merge( $data, $post_vars );
138
+ }
139
+
140
+ if ( ! isset( $post_vars['files'] ) || ! is_array( $post_vars['files'] ) ) {
141
+ $post_vars['files'] = self::getFiles( $ID, true );
142
+ }
143
+
144
+ if ( in_array( 'file_count', $template_tags ) ) {
145
+ $post_vars['file_count'] = count( $post_vars['files'] );
146
+ }
147
+
148
+ if ( in_array( 'play_button', $template_tags ) ) {
149
+ $post_vars['play_button'] = self::audioPlayer( $post_vars );
150
+ }
151
+
152
+
153
+ $post_vars['link_label'] = isset( $post_vars['link_label'] ) ? esc_attr( $post_vars['link_label'] ) : esc_attr__( "Download", "download-manager" );
154
+ $post_vars['page_url'] = get_permalink( $post_vars['ID'] );
155
+ $post_vars['page_link'] = "<a href='" . $post_vars['page_url'] . "'>{$post_vars['title']}</a>";
156
+ $post_vars['page_url_qr'] = "<img class='wpdm-qr-code wpdm-qr-code{$post_vars['ID']}' style='max-width: 250px' src='http://chart.googleapis.com/chart?cht=qr&chs=450x450&choe=UTF-8&chld=H|0&chl={$post_vars['page_url']}' alt='{$post_vars['title']}' />";
157
+
158
+
159
+ if ( ! isset( $post_vars['btnclass'] ) ) {
160
+ $post_vars['btnclass'] = wpdm_download_button_style( null, $ID );
161
+ }
162
+
163
+ if ( in_array( 'tags', $template_tags ) ) {
164
+ $tags = wp_get_post_terms( $post_vars['ID'], 'wpdmtag' );
165
+ $taghtml = "";
166
+ if ( is_array( $tags ) ) {
167
+ foreach ( $tags as $tag ) {
168
+ $taghtml .= "<a class='btn btn-secondary btn-xs' style='margin:0 5px 5px 0' href=\""
169
+ . get_tag_link( $tag->term_id )
170
+ . "\"><i class='fa fa-tag'></i> &nbsp; " . $tag->name . "</a> &nbsp;";
171
+ }
172
+ }
173
+ $post_vars['tags'] = $taghtml;
174
+ }
175
+
176
+ if ( in_array( 'file_ext', $template_tags ) ) {
177
+ $post_vars['files'] = isset( $post_vars['files'] ) && is_array( $post_vars['files'] ) ? $post_vars['files'] : self::getFiles( $ID );
178
+ if ( count( $post_vars['files'] ) > 1 ) {
179
+ $post_vars['file_ext'] = 'zip';
180
+ }
181
+ if ( is_array( $post_vars['files'] ) && count( $post_vars['files'] ) == 1 ) {
182
+ $tmpdata = $post_vars['files'];
183
+ $tmpdata = array_shift( $tmpdata );
184
+ $tmpdata = explode( ".", $tmpdata );
185
+ $post_vars['file_ext'] = end( $tmpdata );
186
+ }
187
+ }
188
+
189
+ if ( in_array( 'file_size', $template_tags ) ) {
190
+ $post_vars['file_size'] = self::Size( $post_vars['ID'] );
191
+ }
192
+
193
+ if ( in_array( 'audio_player_single', $template_tags ) ) {
194
+ $post_vars['audio_player_single'] = self::audioPlayer( $post_vars, true );
195
+ }
196
+
197
+ if ( array_intersect( $template_tags, array(
198
+ 'youtube_player',
199
+ 'youtube_thumb_0',
200
+ 'youtube_thumb_1',
201
+ 'youtube_thumb_2',
202
+ 'youtube_thumb_3'
203
+ ) ) ) {
204
+ $post_vars['files'] = isset( $post_vars['files'] ) && is_array( $post_vars['files'] ) ? $post_vars['files'] : self::getFiles( $ID );
205
+ $tmplfile = $post_vars['files'];
206
+ $tmpfile = is_array( $tmplfile ) && count( $tmplfile ) > 0 ? array_shift( $tmplfile ) : '';
207
+ if ( strpos( $tmpfile, 'youtu' ) ) {
208
+ if ( preg_match( '/youtu\.be\/([^\/]+)/', $tmpfile, $match ) ) {
209
+ $vid = $match[1];
210
+ } else if ( preg_match( '/watch\?v=([^\/]+)/', $tmpfile, $match ) ) {
211
+ $vid = $match[1];
212
+ }
213
+ if ( isset( $vid ) ) {
214
+ $post_vars['youtube_thumb_0'] = '<img src="https://img.youtube.com/vi/' . $vid . '/0.jpg" alt="Thumb 0" />';
215
+ $post_vars['youtube_thumb_1'] = '<img src="https://img.youtube.com/vi/' . $vid . '/1.jpg" alt="Thumb 1" />';
216
+ $post_vars['youtube_thumb_2'] = '<img src="https://img.youtube.com/vi/' . $vid . '/2.jpg" alt="Thumb 2" />';
217
+ $post_vars['youtube_thumb_3'] = '<img src="https://img.youtube.com/vi/' . $vid . '/3.jpg" alt="Thumb 3" />';
218
+ $post_vars['youtube_player'] = '<iframe width="1280" height="720" src="https://www.youtube.com/embed/' . $vid . '" frameborder="0" allowfullscreen></iframe>';
219
+ }
220
+ }
221
+ }
222
+
223
+ if ( array_intersect( array( 'file_type_icon', 'icon' ), $template_tags ) ) {
224
+ $post_vars['files'] = isset( $post_vars['files'] ) && is_array( $post_vars['files'] ) ? $post_vars['files'] : self::getFiles( $ID );
225
+ if ( is_array( $post_vars['files'] ) ) {
226
+ $tifn = $ifn = @end( $post_vars['files'] );
227
+ $ifn = @explode( '.', $ifn );
228
+ $ifn = @end( $ifn );
229
+ if ( strlen( $ifn ) > 4 && strstr( $tifn, "://" ) ) {
230
+ $ifn = 'web';
231
+ }
232
+ if ( strlen( $ifn ) > 4 && ! strstr( $tifn, "://" ) ) {
233
+ $ifn = 'unknown';
234
+ }
235
+ } else {
236
+ $ifn = 'unknown';
237
+ }
238
+
239
+ $post_vars['file_type_icon'] = '<img class="wpdm_icon" alt="' . __( "Icon", "download-manager" ) . '" src="' . FileSystem::fileTypeIcon( ( @count( $post_vars['files'] ) <= 1 ? $ifn : 'zip' ) ) . '" />';
240
+
241
+ if ( ! isset( $post_vars['icon'] ) || $post_vars['icon'] == '' ) {
242
+ $post_vars['icon'] = $post_vars['file_type_icon'];
243
+ } else if ( ! strpos( $post_vars['icon'], '://' ) ) {
244
+ $post_vars['icon'] = '<img class="wpdm_icon" alt="' . __( "Icon", "download-manager" ) . '" src="' . plugins_url( str_replace( 'download-manager/file-type-icons/', 'download-manager/assets/file-type-icons/', $post_vars['icon'] ) ) . '" />';
245
+ } else if ( ! strpos( $post_vars['icon'], ">" ) ) {
246
+ $post_vars['icon'] = '<img class="wpdm_icon" alt="' . __( "Icon", "download-manager" ) . '" src="' . str_replace( 'download-manager/file-type-icons/', 'download-manager/assets/file-type-icons/', $post_vars['icon'] ) . '" />';
247
+ }
248
+
249
+ }
250
+
251
+
252
+ $post_vars['link_label'] = apply_filters( 'wpdm_button_image', $post_vars['link_label'], $post_vars, $template_type );
253
+
254
+ $post_vars['link_label'] = $post_vars['link_label'] ? $post_vars['link_label'] : __( "Download", "download-manager" );
255
+
256
+ $post_vars['download_url'] = $this->getDownloadURL( $post_vars['ID'] );
257
+ $post_vars['download_link_popup'] =
258
+ $post_vars['download_link_extended'] =
259
+ $post_vars['download_link'] = (int) get_option( '__wpdm_mask_dlink', 1 ) == 1 ? "<a class='wpdm-download-link download-on-click {$post_vars['btnclass']}' rel='nofollow' href='#' data-downloadurl=\"{$post_vars['download_url']}\">{$post_vars['link_label']}</a>" : "<a class='wpdm-download-link {$post_vars['btnclass']}' rel='nofollow' href='{$post_vars['download_url']}'>{$post_vars['link_label']}</a>";
260
+
261
+ $limit_over = 0;
262
+ $alert_size = ( $template_type == 'link' ) ? 'alert-sm' : '';
263
+
264
+ $loginmsg = Messages::login_required( $post_vars['ID'] );
265
+
266
+ //Download limit is over
267
+ if ( self::userDownloadLimitExceeded( $post_vars['ID'] ) ) {
268
+ $limit_over = 1;
269
+ $limit_msg = Messages::download_limit_exceeded( $post_vars['ID'] );
270
+ $post_vars['download_url'] = '#';
271
+ $post_vars['link_label'] = $limit_msg;
272
+ $post_vars['download_link_popup'] =
273
+ $post_vars['download_link_extended'] =
274
+ $post_vars['download_link'] = $post_vars['link_label'];
275
+ } //Item is expired
276
+ else if ( isset( $post_vars['expire_date'] ) && $post_vars['expire_date'] != "" && strtotime( $post_vars['expire_date'] ) < time() ) {
277
+ $post_vars['download_url'] = '#';
278
+ $post_vars['link_label'] = __( "Download was expired on", "download-manager" ) . " " . date_i18n( get_option( 'date_format' ) . " h:i A", strtotime( $post_vars['expire_date'] ) );
279
+ $post_vars['download_link'] =
280
+ $post_vars['download_link_extended'] =
281
+ $post_vars['download_link_popup'] = "<div class='alert alert-warning {$alert_size}' data-title='" . __( "DOWNLOAD ERROR:", "download-manager" ) . "'>{$post_vars['link_label']}</div>";
282
+ } //Not available yet
283
+ else if ( isset( $post_vars['publish_date'] ) && $post_vars['publish_date'] != '' && strtotime( $post_vars['publish_date'] ) > time() ) {
284
+ $post_vars['download_url'] = '#';
285
+ $post_vars['link_label'] = __( "Download will be available from ", "download-manager" ) . " " . date_i18n( get_option( 'date_format' ) . " h:i A", strtotime( $post_vars['publish_date'] ) );
286
+ $post_vars['download_link'] =
287
+ $post_vars['download_link_extended'] =
288
+ $post_vars['download_link_popup'] = "<div class='alert alert-warning {$alert_size}' data-title='" . __( "DOWNLOAD ERROR:", "download-manager" ) . "'>{$post_vars['link_label']}</div>";
289
+ } //User is not allowed
290
+ else if ( is_user_logged_in() && ! self::userCanAccess( $post_vars['ID'] ) ) {
291
+ $post_vars['download_url'] = '#';
292
+ $post_vars['link_label'] = stripslashes( get_option( '__wpdm_permission_denied_msg' ) );
293
+ $post_vars['link_label'] = $post_vars['link_label'] !== '' ? $post_vars['link_label'] : __( 'You are not allowed to download.', 'download-manager' );
294
+ $post_vars['download_link'] =
295
+ $post_vars['download_link_extended'] =
296
+ $post_vars['download_link_popup'] = "<div class='alert alert-danger {$alert_size}' data-title='" . __( "DOWNLOAD ERROR:", "download-manager" ) . "'>{$post_vars['link_label']}</div>";
297
+ } //User is not logged in and guest access is not allowed
298
+ else if ( ! is_user_logged_in() && ! self::userCanAccess( $post_vars['ID'] ) ) {
299
+ $packurl = get_permalink( $post_vars['ID'] );
300
+ $loginform = WPDM()->user->login->form( array( 'redirect' => $packurl ) );
301
+ $post_vars['download_url'] = WPDM()->user->login->url( $_SERVER['REQUEST_URI'] );
302
+ $post_vars['download_link'] =
303
+ $post_vars['download_link_extended'] =
304
+ $post_vars['download_link_popup'] = stripcslashes( str_replace( array(
305
+ "[loginform]",
306
+ "[this_url]",
307
+ "[package_url]"
308
+ ), array( $loginform, $_SERVER['REQUEST_URI'], $packurl ), $loginmsg ) );
309
+ $post_vars['download_link'] =
310
+ $post_vars['download_link_extended'] =
311
+ $post_vars['download_link_popup'] = get_option( '__wpdm_login_form', 0 ) == 1 ? $loginform : $post_vars['download_link'];
312
+ } //PACKAGE is locked
313
+ else if ( $this->isLocked( $post_vars['ID'] ) ) {
314
+ $post_vars['download_url'] = '#';
315
+ $post_vars['download_link'] = $post_vars['download_link_popup'] = "<a href='#unlock' class='wpdm-download-link wpdm-download-locked {$post_vars['btnclass']}' data-package='{$post_vars['ID']}'>{$post_vars['link_label']}</a>"; //self::activeLocks($post_vars);
316
+ $post_vars['download_link_extended'] = self::activeLocks( $post_vars['ID'], array( 'embed' => 1 ) );
317
+ //$post_vars['download_link_popup'] = self::activeLocks($post_vars, array('popstyle' => 'popup'));
318
+ }
319
+
320
+ if ( isset( $data['terms_lock'] ) && $data['terms_lock'] != 0 && ( ! function_exists( 'wpdmpp_effective_price' ) || wpdmpp_effective_price( $post_vars['ID'] ) == 0 ) && $limit_over == 0 && self::userCanAccess( $post_vars['ID'] ) ) {
321
+ //$data['terms_conditions'] = wpautop(strip_tags($data['terms_conditions'], "<p><br><a><strong><b><i>"));
322
+ /*$data['terms_conditions'] = wpautop(wpdm_escs(get_post_meta($post_vars['ID'], '__wpdm_terms_conditions', true)));
323
  $data['terms_title'] = wpdm_escs(get_post_meta($post_vars['ID'], '__wpdm_terms_title', true));
324
  $data['terms_title'] = !isset($data['terms_title']) || $data['terms_title'] == '' ? __("Terms and Conditions", 'download-manager') : sanitize_text_field($data['terms_title']);
325
  $data['terms_check_label'] = !isset($data['terms_check_label']) || $data['terms_check_label'] == '' ? __("I Agree With Terms and Conditions", 'download-manager') : sanitize_text_field($data['terms_check_label']);
329
  //$data['terms_conditions'] = wpautop(strip_tags($data['terms_conditions'], "<p><br><a><strong><b><i>"));
330
  $data['terms_conditions'] = wpautop(wpdm_escs($data['terms_conditions']));*/
331
 
332
+ $terms_page = (int) get_post_meta( $ID, '__wpdm_terms_page', true );
333
+ $terms_title = get_post_meta( $ID, '__wpdm_terms_title', true );
334
+ $terms_conditions = get_post_meta( $ID, '__wpdm_terms_conditions', true );
335
+ if ( $terms_page ) {
336
+ $terms_page = get_post( $terms_page );
337
+ $terms_title = $terms_page->post_title;
338
+ $terms_conditions = $terms_page->post_content;
339
+ }
340
+ $terms_title = $terms_title !== '' ? $terms_title : __( "Terms and Conditions", 'download-manager' );
341
+ $terms_check_label = get_post_meta( $ID, '__wpdm_terms_check_label', true );
342
+ $terms_check_label = $terms_check_label ? $terms_check_label : __( "I Agree With Terms and Conditions", 'download-manager' );
343
+
344
+ $data['terms_title'] = $terms_title;
345
+ $data['terms_check_label'] = $terms_check_label;
346
+ $data['terms_conditions'] = $terms_conditions;
347
+
348
+ if ( ! self::isLocked( $post_vars ) ) {
349
+ $post_vars['download_link_popup'] = $post_vars['download_link'] = "<a href='#unlock' class='wpdm-download-link wpdm-download-locked {$post_vars['btnclass']}' data-package='{$post_vars['ID']}'>{$post_vars['link_label']}</a>";
350
+ }
351
+
352
+ $post_vars['download_link_extended'] = "<div class='panel panel-default card card terms-panel' style='margin: 0'><div class='panel-heading card-header'>{$terms_title}</div><div class='panel-body card-body' style='max-height: 200px;overflow: auto'>{$terms_conditions}</div><div class='panel-footer card-footer'><label><input data-pid='{$post_vars['ID']}' class='wpdm-checkbox terms_checkbox terms_checkbox_{$post_vars['ID']}' type='checkbox' onclick='jQuery(\".download_footer_{$post_vars['ID']}\").slideToggle();'> {$terms_check_label}</label></div><div class='panel-footer card-footer download_footer_{$post_vars['ID']}' style='display:none;'>{$post_vars['download_link_extended']}</div></div><script>jQuery(function($){ $('#wpdm-filelist-{$post_vars['ID']} .btn.inddl, #xfilelist .btn.inddl').attr('disabled', 'disabled'); });</script>";
353
+
354
+ }
355
+
356
+ if ( ! isset( $post_vars['formatted'] ) ) {
357
+ $post_vars['formatted'] = 0;
358
+ }
359
+ ++ $post_vars['formatted'];
360
+
361
+ $post_vars['__template'] = $template;
362
+ $post_vars['__template_type'] = $template_type;
363
+ $post_vars = apply_filters( 'wpdm_after_prepare_package_data', $post_vars, $template_type );
364
+
365
+ $this->packageData = $post_vars;
366
+
367
+ foreach ( $post_vars as $key => $val ) {
368
+ try {
369
+ if ( preg_match( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $key ) ) {
370
+ $this->{$key} = $val;
371
+ }
372
+ } catch ( \Exception $e ) {
373
+ }
374
+ }
375
+
376
+ return $this;
377
+ }
378
+
379
+ /**
380
+ * @usage Get all or any specific package info
381
+ *
382
+ * @param $ID
383
+ * @param null $meta
384
+ *
385
+ * @return mixed
386
+ */
387
+ public static function get( $ID, $meta = null ) {
388
+ $ID = (int) $ID;
389
+ if ( $ID == 0 ) {
390
+ return null;
391
+ }
392
+ if ( $meta != null ) {
393
+ return get_post_meta( $ID, "__wpdm_" . $meta, true );
394
+ }
395
+ $p = new PackageController();
396
+ $package = $p->prepare( $ID );
397
+
398
+ return $package->packageData;
399
+ }
400
+
401
+ /**
402
+ * @usage Verify single file download option
403
+ *
404
+ * @param null $ID
405
+ *
406
+ * @return mixed
407
+ */
408
+ public function isSingleFileDownloadAllowed( $ID = null ) {
409
+ global $post;
410
+ if ( ! $ID && $post->post_type == 'wpdmpro' ) {
411
+ $ID = $post->ID;
412
+ }
413
+ $global = get_option( '__wpdm_individual_file_download', 1 );
414
+ $package = get_post_meta( $ID, '__wpdm_individual_file_download', true );
415
+ $effective = $package == - 1 || $package == '' ? $global : $package;
416
+
417
+ return apply_filters( "wpdm_is_single_file_download_allowed", $effective, $ID );
418
+ }
419
+
420
+ /**
421
+ * @param $id
422
+ *
423
+ * @usage Returns the user roles who has access to specified package
424
+ * @return array|mixed
425
+ */
426
+ public function allowedRoles( $id ) {
427
+ $roles = get_post_meta( $id, '__wpdm_access', true );
428
+ $roles = maybe_unserialize( $roles );
429
+
430
+ $cats = get_the_terms( $id, 'wpdmcategory' );
431
+ if ( ! is_array( $roles ) ) {
432
+ $roles = array();
433
+ }
434
+ if ( is_array( $cats ) ) {
435
+ foreach ( $cats as $cat ) {
436
+ $croles = WPDM()->categories->getAllowedRoles( $cat->term_id );
437
+ $roles = array_merge( $roles, $croles );
438
+ }
439
+ }
440
+
441
+ $roles = array_unique( $roles );
442
+
443
+ $roles = apply_filters( "wpdm_allowed_roles", $roles, $id );
444
+ if ( ! is_array( $roles ) ) {
445
+ $roles = array();
446
+ }
447
+
448
+ return $roles;
449
+ }
450
+
451
+ /**
452
+ * @usage Check if a package is locked or public
453
+ *
454
+ * @param $id
455
+ *
456
+ * @return bool
457
+ */
458
+ public function isLocked( $package = null ) {
459
+ if ( ! $package ) {
460
+ $package = $this->ID;
461
+ }
462
+ if ( ! is_array( $package ) && (int) $package > 0 ) {
463
+ $id = $package;
464
+ $package = array();
465
+ $package['ID'] = $id;
466
+ $package['email_lock'] = get_post_meta( $id, '__wpdm_email_lock', true );
467
+ $package['password_lock'] = get_post_meta( $id, '__wpdm_password_lock', true );
468
+ $package['gplusone_lock'] = get_post_meta( $id, '__wpdm_gplusone_lock', true );
469
+ $package['tweet_lock'] = get_post_meta( $id, '__wpdm_tweet_lock', true );
470
+ $package['twitterfollow_lock'] = get_post_meta( $id, '__wpdm_twitterfollow_lock', true );
471
+ $package['facebooklike_lock'] = get_post_meta( $id, '__wpdm_facebooklike_lock', true );
472
+ $package['linkedin_lock'] = get_post_meta( $id, '__wpdm_linkedin_lock', true );
473
+ $package['captcha_lock'] = get_post_meta( $id, '__wpdm_captcha_lock', true );
474
+ } else {
475
+ $id = $package['ID'];
476
+ }
477
+ $lock = '';
478
+ $package = apply_filters( 'wpdm_custom_data', $package, $id );
479
+ if ( isset( $package['email_lock'] ) && (int) $package['email_lock'] == 1 ) {
480
+ $lock = 'locked';
481
+ }
482
+ if ( isset( $package['password_lock'] ) && (int) $package['password_lock'] == 1 ) {
483
+ $lock = 'locked';
484
+ }
485
+ if ( isset( $package['gplusone_lock'] ) && (int) $package['gplusone_lock'] == 1 ) {
486
+ $lock = 'locked';
487
+ }
488
+ if ( isset( $package['tweet_lock'] ) && (int) $package['tweet_lock'] == 1 ) {
489
+ $lock = 'locked';
490
+ }
491
+ if ( isset( $package['twitterfollow_lock'] ) && (int) $package['twitterfollow_lock'] == 1 ) {
492
+ $lock = 'locked';
493
+ }
494
+ if ( isset( $package['facebooklike_lock'] ) && (int) $package['facebooklike_lock'] == 1 ) {
495
+ $lock = 'locked';
496
+ }
497
+ if ( isset( $package['linkedin_lock'] ) && (int) $package['linkedin_lock'] == 1 ) {
498
+ $lock = 'locked';
499
+ }
500
+ if ( isset( $package['captcha_lock'] ) && (int) $package['captcha_lock'] == 1 ) {
501
+ $lock = 'locked';
502
+ }
503
+
504
+ if ( $lock !== 'locked' ) {
505
+ $lock = apply_filters( 'wpdm_check_lock', $lock, $id );
506
+ }
507
+
508
+ return ( $lock === 'locked' );
509
+ }
510
+
511
+ /**
512
+ * Check if a package with the given $ID is password protected
513
+ *
514
+ * @param $ID
515
+ *
516
+ * @return bool
517
+ */
518
+ function isPasswordProtected( $ID ) {
519
+ $locked = (int) get_post_meta( $ID, '__wpdm_password_lock', true );
520
+ $password = get_post_meta( $ID, '__wpdm_password', true );
521
+
522
+ return $locked && $password ? $password : false;
523
+ }
524
+
525
+ /**
526
+ * @usage Check if current user has access to package or category
527
+ *
528
+ * @param $id
529
+ * @param string $type
530
+ *
531
+ * @return bool
532
+ */
533
+ public function userCanAccess( $ID = null, $type = 'package' ) {
534
+ global $current_user;
535
+ $ID = $ID ? $ID : $this->ID;
536
+ if ( ! $ID ) {
537
+ return false;
538
+ }
539
+ if ( $type == 'package' ) {
540
+ $roles = $this->allowedRoles( $ID );
541
+ } else {
542
+ $roles = WPDM()->categories->getAllowedRoles( $ID );
543
+ }
544
+
545
+ if ( ! is_array( $roles ) ) {
546
+ $roles = array();
547
+ }
548
+ $matched = is_user_logged_in() ? array_intersect( $current_user->roles, $roles ) : array();
549
+
550
+ if ( $type === 'category' && count( $roles ) == 0 ) {
551
+ return true;
552
+ }
553
+ if ( in_array( 'guest', $roles ) ) {
554
+ return true;
555
+ }
556
+ if ( count( $matched ) > 0 ) {
557
+ return true;
558
+ }
559
+
560
+ return false;
561
+
562
+ }
563
+
564
+ /**
565
+ * @usage Check if current user has access to package or category based on roles
566
+ *
567
+ * @param $id
568
+ * @param string $type
569
+ *
570
+ * @return bool
571
+ */
572
+ public function userHasAccess( $user_id, $ID, $type = 'package' ) {
573
+
574
+ $user = get_user_by( 'ID', $user_id );
575
+
576
+ if ( $type == 'package' ) {
577
+ $roles = $this->allowedRoles( $ID );
578
+ } else {
579
+ $roles = WPDM()->categories->getAllowedRoles( $ID );
580
+ }
581
+
582
+ if ( ! is_array( $roles ) ) {
583
+ $roles = array();
584
+ }
585
+ $matched = is_user_logged_in() ? array_intersect( $user->roles, $roles ) : array();
586
+ if ( $type === 'category' && count( $roles ) == 0 ) {
587
+ return true;
588
+ }
589
+ if ( in_array( 'guest', $roles ) ) {
590
+ return true;
591
+ }
592
+ if ( count( $matched ) > 0 ) {
593
+ return true;
594
+ }
595
+
596
+ return false;
597
+
598
+ }
599
+
600
+ /**
601
+ * @usage Check user's download limit
602
+ *
603
+ * @param $ID
604
+ *
605
+ * @return bool
606
+ */
607
+ public static function userDownloadLimitExceeded( $ID ) {
608
+ global $current_user;
609
+
610
+ if ( is_user_logged_in() ) {
611
+ $index = $current_user->ID;
612
+ } else {
613
+ $index = $_SERVER['REMOTE_ADDR'];
614
+ }
615
+
616
+ $stock = (int) get_post_meta( $ID, '__wpdm_quota', true );
617
+ $download_count = (int) get_post_meta( $ID, '__wpdm_download_count', true );
618
+
619
+ $user_download_count = WPDM()->downloadHistory->userDownloadCount( $ID );
620
+
621
+ $download_limit_per_user = (int) get_post_meta( $ID, '__wpdm_download_limit_per_user', true );
622
+
623
+ if ( $download_limit_per_user > 0 && $user_download_count >= $download_limit_per_user ) {
624
+ return apply_filters( "wpdm_user_download_limit_exceeded", true, $ID );
625
+ }
626
+
627
+ $exceeded = false;
628
+
629
+ if ( $stock > 0 && $download_count >= $stock ) {
630
+ $exceeded = true;
631
+ };
632
+
633
+ $exceeded = apply_filters( "wpdm_user_download_limit_exceeded", $exceeded, $ID );
634
+
635
+ return $exceeded;
636
+ }
637
+
638
+ function userDownloadCount( $ID ) {
639
+ global $wpdb;
640
+ //$wpdb->
641
+ }
642
+
643
+ /**
644
+ * @usage Check if user is can download this package
645
+ *
646
+ * @param $ID
647
+ *
648
+ * @return bool
649
+ */
650
+ public function userCanDownload( $ID ) {
651
+ return $this->userCanAccess( $ID ) && ! $this->userDownloadLimitExceeded( $ID );
652
+ }
653
+
654
+ /**
655
+ * @usage Count files in a package
656
+ *
657
+ * @param $id
658
+ *
659
+ * @return int
660
+ */
661
+ public function fileCount( $ID ) {
662
+
663
+ $count = count( $this->getFiles( $ID, true ) );
664
+
665
+ return $count;
666
+
667
+ }
668
+
669
+ /**
670
+ * @usage Get list of attached files & all files inside attached dir with a package
671
+ *
672
+ * @param $ID
673
+ *
674
+ * @return array|mixed
675
+ */
676
+ public function getFiles( $ID, $include_dir = false ) {
677
+ $files = maybe_unserialize( get_post_meta( $ID, '__wpdm_files', true ) );
678
+ if ( ! $files || ! is_array( $files ) ) {
679
+ $files = array();
680
+ }
681
+ foreach ( $files as &$file ) {
682
+ $file = trim( $file );
683
+ }
684
+ if ( $include_dir ) {
685
+ $package_dir = get_post_meta( $ID, '__wpdm_package_dir', true );
686
+ $package_dir = file_exists( $package_dir ) ? $package_dir : Crypt::decrypt( $package_dir );
687
+ if ( $package_dir != '' ) {
688
+ $package_dir = trailingslashit( $package_dir );
689
+ $package_dir = realpath( $package_dir );
690
+ if ( $package_dir ) {
691
+ $dfiles = FileSystem::scanDir( $package_dir, true, true, '', true );
692
+ $files += $dfiles;
693
+ }
694
+ }
695
+ }
696
+ $files = apply_filters( "wpdm_get_files", $files, $ID );
697
+
698
+ return $files;
699
+ }
700
+
701
+ /**
702
+ * @usage Create zip from attached files
703
+ *
704
+ * @param $ID
705
+ *
706
+ * @return mixed|string|\WP_Error
707
+ */
708
+ public function zip( $ID ) {
709
+ $files = $this->getFiles( $ID );
710
+ $zipped = get_post_meta( $ID, "__wpdm_zipped_file", true );
711
+ if ( count( $files ) > 0 ) {
712
+ if ( $zipped == '' || ! file_exists( $zipped ) ) {
713
+ $zipped = UPLOAD_DIR . sanitize_file_name( get_the_title( $ID ) ) . '-' . $ID . '.zip';
714
+ $zipped = FileSystem::zipFiles( $files, $zipped );
715
+
716
+ return $zipped;
717
+ }
718
+ }
719
+
720
+ return new \WP_Error( 404, __( "No File Attached!", "download-manager" ) );
721
+ }
722
+
723
+ /**
724
+ * @usage Calculate package size
725
+ *
726
+ * @param $ID
727
+ * @param bool|false $recalculate
728
+ *
729
+ * @return bool|float|int|mixed|string
730
+ */
731
+ public function size( $ID, $recalculate = false ) {
732
+
733
+ if ( get_post_type( $ID ) != 'wpdmpro' ) {
734
+ return false;
735
+ }
736
+
737
+ $size = get_post_meta( $ID, '__wpdm_package_size', true );
738
+
739
+ if ( $size != "" && ! $recalculate ) {
740
+ return $size;
741
+ }
742
+
743
+ $files = $this->getFiles( $ID );
744
+
745
+ $size = 0;
746
+ if ( is_array( $files ) ) {
747
+ foreach ( $files as $f ) {
748
+ $f = trim( $f );
749
+ if ( __::is_url( $f ) ) {
750
+ continue;
751
+ }
752
+ if ( file_exists( $f ) ) {
753
+ $size += @filesize( $f );
754
+ } else {
755
+ $size += @filesize( UPLOAD_DIR . $f );
756
+ }
757
+ }
758
+ }
759
+
760
+ update_post_meta( $ID, '__wpdm_package_size_b', $size );
761
+ $size = $size / 1024;
762
+ if ( $size > 1024 ) {
763
+ $size = number_format( $size / 1024, 2 ) . ' MB';
764
+ } else {
765
+ $size = number_format( $size, 2 ) . ' KB';
766
+ }
767
+ update_post_meta( $ID, '__wpdm_package_size', $size );
768
+
769
+ return $size;
770
+ }
771
+
772
+ /**
773
+ * @usage Generate play button for link template
774
+ *
775
+ * @param $package
776
+ * @param bool $return
777
+ * @param $style
778
+ *
779
+ * @return mixed|string|void
780
+ */
781
+ public function audioPlayer( $package, $return = true, $style = 'primary btn-lg wpdm-btn-play-lg' ) {
782
+
783
+ $audiohtml = "";
784
+
785
+ if ( ! is_array( $package['files'] ) || count( $package['files'] ) == 0 ) {
786
+ return;
787
+ }
788
+ $audios = array();
789
+ $nonce = wp_create_nonce( $_SERVER['REQUEST_URI'] );
790
+ $audio = $audx = null;
791
+ foreach ( $package['files'] as $index => $file ) {
792
+ $realpath = file_exists( $file ) ? $file : UPLOAD_DIR . $file;
793
+ $filetype = wp_check_filetype( $realpath );
794
+ $tmpvar = explode( '/', $filetype['type'] );
795
+ if ( $tmpvar[0] == 'audio' ) {
796
+ $audio = $file;
797
+ $audx = $index;
798
+ break;
799
+ }
800
+ }
801
+
802
+ if ( $audio != null ) {
803
+ $song = FileSystem::mediaURL( $package['ID'], $audx, basename( $audio ) ); //home_url("/?wpdmdl={$package['ID']}&ind=".$audx."&play=".basename($audio));
804
+ $audiohtml = "<button data-player='wpdm-audio-player' data-song='{$song}' class='btn btn-{$style} wpdm-btn-play'><i class='fa fa-play'></i></button>";
805
+ $audiohtml = apply_filters( "wpdm_audio_play_button", $audiohtml, $song, $package, 0 );
806
+ }
807
+
808
+ if ( $return ) {
809
+ return $audiohtml;
810
+ }
811
+
812
+ echo $audiohtml;
813
+
814
+ }
815
+
816
+ /**
817
+ * @param $ID
818
+ * @param $files
819
+ * @param int $width
820
+ *
821
+ * @return string
822
+ */
823
+ public static function videoPlayer( $ID, $files = null, $width = 800 ) {
824
+
825
+ if ( ! $files ) {
826
+ $files = WPDM()->package->getFiles( $ID );
827
+ }
828
+
829
+ $videos = array();
830
+ foreach ( $files as $index => $file ) {
831
+ $realpath = file_exists( $file ) ? $file : UPLOAD_DIR . $file;
832
+ $filetype = wp_check_filetype( $realpath );
833
+ $tmpvar = explode( '/', $filetype['type'] );
834
+ if ( $tmpvar[0] == 'video' ) {
835
+ $videos[] = $file;
836
+ $vidx[] = $index;
837
+ }
838
+
839
+ }
840
+
841
+ $videothumbs = "";
842
+ $mpvs = get_post_meta( $ID, '__wpdm_additional_previews', true );
843
+ $mmv = 0;
844
+
845
+ if ( is_array( $mpvs ) && count( $mpvs ) > 1 && count( $videos ) > 1 ) {
846
+
847
+ foreach ( $mpvs as $i => $mpv ) {
848
+ if ( $mmv < count( $videos ) ) {
849
+ //$url = self::expirableDownloadLink($ID, 3);
850
+ $ind = $vidx[ $i ]; //\WPDM_Crypt::Encrypt($videos[$mmv]);
851
+ //$video = $url . "&ind={$ind}&play=" . basename($videos[$mmv]);
852
+ $video = FileSystem::mediaURL( $ID, $ind, wpdm_basename( $videos[ $mmv ] ) );
853
+
854
+ $videothumbs .= "<a href='#' data-video='{$video}' class='__wpdm_playvideo'><img class='thumbnail' src='" . wpdm_dynamic_thumb( $mpv, array(
855
+ 64,
856
+ 64
857
+ ) ) . "'/></a>";
858
+ }
859
+ $mmv ++;
860
+ }
861
+ }
862
+
863
+ $player_html = '';
864
+ if ( count( $videos ) > 0 ) {
865
+ //$url = self::expirableDownloadLink($ID, 10);
866
+ $ind = $vidx[0]; //\WPDM_Crypt::Encrypt($videos[0]);
867
+ //$video = $url . "&ind={$ind}&play=" . basename($videos[0]);
868
+ $video = FileSystem::mediaURL( $ID, $ind, wpdm_basename( $videos[0] ) );
869
+
870
+ $player_html = "<video id='__wpdm_videoplayer' class='thumbnail' width=\"{$width}\" controls controlsList='nodownload'><source src=\"{$video}\" type=\"video/mp4\">Your browser does not support HTML5 video.</video><div class='videothumbs'>{$videothumbs}</div>";
871
+ //if(!WPDM()->package->userCanAccess($ID)) $player_html = \WPDM\__\Messages::Error(stripslashes(get_option('wpdm_permission_msg')), -1);
872
+ }
873
+
874
+ $player_html = apply_filters( "wpdm_video_player_html", $player_html, $ID, $file, $width );
875
+
876
+ return $player_html;
877
+ }
878
+
879
+ /**
880
+ * @param $ID
881
+ * @param $files
882
+ * @param int $width
883
+ * @param int $height
884
+ *
885
+ * @return mixed|string
886
+ */
887
+ public function productPreview( $ID, $files = null, $width = 800, $height = 600 ) {
888
+ if ( ! $files ) {
889
+ $files = WPDM()->package->getFiles( $ID );
890
+ }
891
+ $tfiles = $files;
892
+ $keys = array_keys( $files );
893
+ $ind = $keys[0];
894
+ $file = array_shift( $tfiles );
895
+ $realpath = file_exists( $file ) ? $file : UPLOAD_DIR . $file;
896
+ $filetype = wp_check_filetype( $realpath );
897
+ $type = explode( '/', $filetype['type'] );
898
+ $type = $type[1];
899
+ switch ( $type ) {
900
+ case 'mpeg':
901
+ case 'mp4':
902
+ return self::videoPlayer( $ID, $files, $width );
903
+ case 'png':
904
+ case 'jpg':
905
+ case 'jpeg':
906
+ return "<img src='" . FileSystem::imageThumbnail( $realpath, $width, $height ) . "' alt='" . get_the_title( $ID ) . "'/>";
907
+ case 'pdf':
908
+ $url = self::expirableDownloadLink( $ID, 1, 300 );
909
+ //$ind = \WPDM_Crypt::Encrypt($file);
910
+ $url .= "&ind={$ind}";
911
+
912
+ return FileSystem::docViewer( $url, $ID, 'pdf' );
913
+ default:
914
+ return '';
915
+ }
916
+ }
917
+
918
+ /**
919
+ * @usage Get All Custom Data of a Package
920
+ *
921
+ * @param $pid
922
+ *
923
+ * @return array
924
+ */
925
+ public function metaData( $ID ) {
926
+ global $wpdb;
927
+
928
+ $metaValidate = [
929
+ '__wpdm_icon' => 'txt',
930
+ '__wpdm_view_count' => 'int',
931
+ '__wpdm_download_count' => 'int',
932
+ '__wpdm_link_label' => 'txt',
933
+ '__wpdm_version' => 'txt',
934
+ '__wpdm_quota' => 'int',
935
+ '__wpdm_access' => 'array',
936
+ '__wpdm_package_size' => 'txt',
937
+ '__wpdm_download_limit_per_user' => 'int',
938
+ '__wpdm_terms_lock' => 'int',
939
+ '__wpdm_publish_date' => 'txt',
940
+ '__wpdm_expire_date' => 'txt'
941
+ ];
942
+ $metaKeys = array_keys( $metaValidate );
943
+ $metaData = $wpdb->get_results( "select * from {$wpdb->prefix}postmeta where meta_key in ('" . implode( '\',\'', $metaKeys ) . "') and post_id = '{$ID}'" );
944
+
945
+ $data = [];
946
+ if ( is_array( $metaData ) ) {
947
+ foreach ( $metaData as $metaDataRow ) {
948
+ $key = str_replace( "__wpdm_", "", $metaDataRow->meta_key );
949
+ if ( $key === 'publish_date' ) {
950
+ $key = 'avail_date';
951
+ $metaDataRow->meta_value = wp_date( get_option( 'date_format' ) . " " . get_option( 'time_format' ), strtotime( $metaDataRow->meta_value ) );
952
+ }
953
+ if ( $key === 'expire_date' ) {
954
+ $metaDataRow->meta_value = wp_date( get_option( 'date_format' ) . " " . get_option( 'time_format' ), strtotime( $metaDataRow->meta_value ) );
955
+ }
956
+ //if($metaValidate[$metaDataRow->meta_key] === 'array' && !is_array($metaDataRow->meta_value)) $metaDataRow->meta_value = unserialize($metaDataRow->meta_value);
957
+ //else
958
+ // $metaDataRow->meta_value = __::sanitize_var($metaDataRow->meta_value, $metaValidate[$metaDataRow->meta_key]);
959
+ $data[ $key ] = wpdm_sanitize_var( maybe_unserialize( $metaDataRow->meta_value ), $metaValidate[ $metaDataRow->meta_key ] );
960
+ }
961
+ }
962
+ unset( $metaData );
963
+ $data = apply_filters( 'wpdm_custom_data', $data, $ID );
964
+ if ( ! is_array( $data ) ) {
965
+ $data = [];
966
+ }
967
+
968
+ return $data;
969
+ }
970
+
971
+ /**
972
+ * @usage Generate download link of a package
973
+ *
974
+ * @param $package
975
+ * @param int $embed
976
+ * @param array $extras
977
+ *
978
+ * @return string
979
+ */
980
+ function prepareDownloadLink( &$package, $embed = 0, $extras = array() ) {
981
+ global $wpdb, $current_user, $wpdm_download_icon, $wpdm_download_lock_icon, $btnclass;
982
+ if ( is_array( $extras ) ) {
983
+ extract( $extras );
984
+ }
985
+ $data = '';
986
+
987
+ $package['link_url'] = home_url( '/?download=1&' );
988
+ $package['link_label'] = ! isset( $package['link_label'] ) || $package['link_label'] == '' ? __( "Download", "download-manager" ) : $package['link_label'];
989
+
990
+ //Change link label using a button image
991
+ $template_type = isset( $template_type ) ? $template_type : 'link';
992
+ $package['link_label'] = apply_filters( 'wpdm_button_image', $package['link_label'], $package, $template_type );
993
+
994
+
995
+ $package['download_url'] = wpdm_download_url( $package );
996
+ if ( wpdm_is_download_limit_exceed( $package['ID'] ) ) {
997
+ $limit_msg = Messages::download_limit_exceeded( $package['ID'] );
998
+ $package['download_url'] = '#';
999
+ $package['link_label'] = $limit_msg;
1000
+ }
1001
+ if ( isset( $package['expire_date'] ) && $package['expire_date'] != "" && strtotime( $package['expire_date'] ) < time() ) {
1002
+ $package['download_url'] = '#';
1003
+ $package['link_label'] = apply_filters( "wpdm_download_expired_message", __( "Download was expired on", "download-manager" ) . " " . date_i18n( get_option( 'date_format' ) . " h:i A", strtotime( $package['expire_date'] ) ), $package );
1004
+ $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<a href='#'>{$package['link_label']}</a>";
1005
+
1006
+ return "<div class='alert alert-warning'><b>" . __( "Download:", "download-manager" ) . "</b><br/>{$package['link_label']}</div>";
1007
+ }
1008
+
1009
+ if ( isset( $package['publish_date'] ) && $package['publish_date'] != '' && strtotime( $package['publish_date'] ) > time() ) {
1010
+ $package['download_url'] = '#';
1011
+ $package['link_label'] = apply_filters( "wpdm_download_availability_message", __( "Download will be available from ", "download-manager" ) . " " . date_i18n( get_option( 'date_format' ) . " h:i A", strtotime( $package['publish_date'] ) ), $package );
1012
+ $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<a href='#'>{$package['link_label']}</a>";
1013
+
1014
+ return "<div class='alert alert-warning'><b>" . __( "Download:", "download-manager" ) . "</b><br/>{$package['link_label']}</div>";
1015
+ }
1016
+
1017
+ $link_label = isset( $package['link_label'] ) ? $package['link_label'] : __( "Download", "download-manager" );
1018
+
1019
+ $package['access'] = wpdm_allowed_roles( $package['ID'] );
1020
+
1021
+ if ( $package['download_url'] != '#' ) {
1022
+ $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<a class='wpdm-download-link {$btnclass}' rel='nofollow' href='#' onclick=\"location.href='{$package['download_url']}';return false;\"><i class='$wpdm_download_icon'></i>{$link_label}</a>";
1023
+ } else {
1024
+ $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = "<div class='alert alert-warning'><b>" . __( "Download:", "download-manager" ) . "</b><br/>{$link_label}</div>";
1025
+ }
1026
+ $caps = array_keys( $current_user->caps );
1027
+ $role = array_shift( $caps );
1028
+
1029
+ $matched = ( is_array( @maybe_unserialize( $package['access'] ) ) && is_user_logged_in() ) ? array_intersect( $current_user->roles, @maybe_unserialize( $package['access'] ) ) : array();
1030
+
1031
+ $skiplink = 0;
1032
+
1033
+ if ( is_user_logged_in() && count( $matched ) <= 0 && ! @in_array( 'guest', @maybe_unserialize( $package['access'] ) ) ) {
1034
+ $package['download_url'] = "#";
1035
+ $package['download_link'] = $vars['download_link_extended'] = $vars['download_link_popup'] = stripslashes( get_option( '__wpdm_permission_denied_msg' ) );
1036
+ $package = apply_filters( 'download_link', $package );
1037
+ if ( get_option( '_wpdm_hide_all', 0 ) == 1 ) {
1038
+ $package['download_link'] = $package['download_link_extended'] = 'blocked';
1039
+ }
1040
+
1041
+ return $package['download_link'];
1042
+ }
1043
+ if ( ! @in_array( 'guest', @maybe_unserialize( $package['access'] ) ) && ! is_user_logged_in() ) {
1044
+
1045
+ $loginform = wpdm_login_form( array( 'redirect' => get_permalink( $package['ID'] ) ) );
1046
+ if ( get_option( '_wpdm_hide_all', 0 ) == 1 ) {
1047
+ return 'loginform';
1048
+ }
1049
+ $package['download_url'] = $vars['download_link_extended'] = $vars['download_link_popup'] = home_url( '/wp-login.php?redirect_to=' . urlencode( $_SERVER['REQUEST_URI'] ) );
1050
+ $loginmsg = Messages::login_required( $package['ID'] );
1051
+ $package['download_link'] = stripcslashes( str_replace( array(
1052
+ "[loginform]",
1053
+ "[this_url]",
1054
+ "[package_url]"
1055
+ ), array( $loginform, $_SERVER['REQUEST_URI'], get_permalink( $package['ID'] ) ), $loginmsg ) );
1056
+
1057
+ return get_option( '__wpdm_login_form', 0 ) == 1 ? $loginform : $package['download_link'];
1058
+
1059
+ }
1060
 
1061
+ $package = apply_filters( 'download_link', $package );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1062
 
1063
+ $unqid = uniqid();
1064
 
1065
+ if ( ! isset( $package['quota'] ) || ( isset( $package['quota'] ) && $package['quota'] > 0 && $package['quota'] > $package['download_count'] ) || $package['quota'] == 0 ) {
1066
+ $lock = 0;
1067
 
1068
+ if ( isset( $package['password_lock'] ) && (int) $package['password_lock'] == 1 && $package['password'] != '' ) {
1069
+ $lock = 'locked';
1070
+ $data = PackageLocks::AskPassword( $package );
1071
+ }
1072
 
1073
 
1074
+ $sociallock = "";
1075
 
1076
+ if ( isset( $package['email_lock'] ) && (int) $package['email_lock'] == 1 ) {
1077
+ $data .= PackageLocks::AskEmail( $package );
1078
+ $lock = 'locked';
1079
+ }
1080
 
1081
+ if ( isset( $package['linkedin_lock'] ) && (int) $package['linkedin_lock'] == 1 ) {
1082
+ $lock = 'locked';
1083
+ $sociallock .= PackageLocks::LinkedInShare( $package );
1084
 
1085
+ }
1086
 
1087
+ if ( isset( $package['twitterfollow_lock'] ) && (int) $package['twitterfollow_lock'] == 1 ) {
1088
+ $lock = 'locked';
1089
+ $sociallock .= PackageLocks::TwitterFollow( $package );
1090
 
1091
+ }
1092
 
1093
+ if ( isset( $package['gplusone_lock'] ) && (int) $package['gplusone_lock'] == 1 ) {
1094
+ $lock = 'locked';
1095
+ $sociallock .= PackageLocks::GooglePlusOne( $package, true );
1096
 
1097
+ }
1098
 
1099
+ if ( isset( $package['tweet_lock'] ) && (int) $package['tweet_lock'] == 1 ) {
1100
+ $lock = 'locked';
1101
+ $sociallock .= PackageLocks::Tweet( $package );
1102
 
1103
+ }
1104
 
1105
+ if ( isset( $package['facebooklike_lock'] ) && (int) $package['facebooklike_lock'] == 1 ) {
1106
+ $lock = 'locked';
1107
+ $sociallock .= PackageLocks::FacebookLike( $package, true );
1108
 
1109
+ }
1110
 
1111
 
1112
+ if ( isset( $package['captcha_lock'] ) && (int) $package['captcha_lock'] == 1 ) {
1113
+ $lock = 'locked';
1114
+ $sociallock .= PackageLocks::reCaptchaLock( $package, true );
1115
 
1116
+ }
1117
 
1118
+ $extralocks = '';
1119
+ $extralocks = apply_filters( "wpdm_download_lock", $extralocks, $package );
1120
 
1121
+ if ( is_array( $extralocks ) && $extralocks['lock'] === 'locked' ) {
1122
 
1123
+ if ( isset( $extralocks['type'] ) && $extralocks['type'] == 'social' ) {
1124
+ $sociallock .= $extralocks['html'];
1125
+ } else {
1126
+ $data .= $extralocks['html'];
1127
+ }
1128
 
1129
+ $lock = 'locked';
1130
+ }
1131
 
1132
+ if ( $sociallock != "" ) {
1133
+ $data .= "<div class='panel panel-default card card-default'><div class='panel-heading card-header'>" . __( "Download", "download-manager" ) . "</div><div class='panel-body card-body wpdm-social-locks text-center'>{$sociallock}</div></div>";
1134
+ }
1135
 
1136
+ if ( $lock === 'locked' ) {
1137
+ $popstyle = isset( $popstyle ) && in_array( $popstyle, array(
1138
+ 'popup',
1139
+ 'pop-over'
1140
+ ) ) ? $popstyle : 'pop-over';
1141
+ if ( $embed == 1 ) {
1142
+ $adata = "</strong>{$data}";
1143
+ } else {
1144
+ //$dataattrs = $popstyle == 'pop-over'? 'data-title="'.__( "Download" , "download-manager" ).' ' . $package['title'] . '"' : 'data-toggle="modal" data-target="#pkg_' . $package['ID'] . "_" . $unqid . '"';
1145
+ $adata = '<a href="#pkg_' . $package['ID'] . "_" . $unqid . '" data-trigger="manual" data-package="' . $package['ID'] . '" class="wpdm-download-link wpdm-download-locked ' . $popstyle . ' ' . $btnclass . '"><i class=\'' . $wpdm_download_lock_icon . '\'></i>' . $package['link_label'] . '</a>';
1146
 
1147
  // if ($popstyle == 'pop-over') {
1148
  // if(!get_option('__wpdm_ajax_popup', false))
1151
  // else
1152
  // $adata .= '<div class="modal fade" role="modal" id="pkg_' . $package['ID'] . "_" . $unqid . '"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><strong style="margin:0px;font-size:12pt">' . __('Download') . '</strong></div><div class="modal-body">' . $data . '</div><div class="modal-footer text-right"><button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button></div></div></div></div>';
1153
 
1154
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1155
 
1156
+ $data = $adata;
1157
+ }
1158
+ if ( $lock !== 'locked' ) {
 
 
1159
 
1160
+ $data = $package['download_link'];
1161
 
 
 
 
 
 
1162
 
1163
+ }
1164
+ } else {
1165
+ $data = Messages::download_limit_exceeded( $package['ID'] );
1166
+ }
 
 
 
 
 
 
1167
 
 
1168
 
1169
+ //return str_replace(array("\r","\n"),"",$data);
1170
+ return $data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1171
 
1172
+ }
1173
 
1174
+ private static function activeLocks( $ID, $params = array( 'embed' => 0, 'popstyle' => 'pop-over' ) ) {
1175
 
1176
+ $embed = isset( $params['embed'] ) ? $params['embed'] : 0;
1177
+ $template_type = isset( $params['template_type'] ) ? $params['template_type'] : 'link';
1178
+ $popstyle = isset( $params['popstyle'] ) ? $params['popstyle'] : 'pop-over';
1179
 
1180
+ $package = array(
1181
+ 'ID' => $ID,
1182
+ 'password' => get_post_meta( $ID, '__wpdm_password', true ),
1183
+ 'password_lock' => get_post_meta( $ID, '__wpdm_password_lock', true ),
1184
+ 'email_lock' => get_post_meta( $ID, '__wpdm_email_lock', true ),
1185
+ 'linkedin_lock' => get_post_meta( $ID, '__wpdm_linkedin_lock', true ),
1186
+ 'twitterfollow_lock' => get_post_meta( $ID, '__wpdm_twitterfollow_lock', true ),
1187
+ 'gplusone_lock' => get_post_meta( $ID, '__wpdm_gplusone_lock', true ),
1188
+ 'tweet_lock' => get_post_meta( $ID, '__wpdm_tweet_lock', true ),
1189
+ 'facebooklike_lock' => get_post_meta( $ID, '__wpdm_facebooklike_lock', true ),
1190
+ 'captcha_lock' => get_post_meta( $ID, '__wpdm_captcha_lock', true ),
1191
+ );
1192
 
1193
+ $package = apply_filters( 'wpdm_before_apply_locks', $package );
1194
+ $lock = $data = "";
1195
+ $unqid = uniqid();
1196
 
1197
+ if ( isset( $package['password_lock'] ) && (int) $package['password_lock'] == 1 && $package['password'] != '' ) {
1198
+ $lock = 'locked';
1199
+ $data = PackageLocks::AskPassword( $package );
1200
+ }
1201
 
1202
+ $sociallock = "";
 
1203
 
1204
+ if ( isset( $package['email_lock'] ) && (int) $package['email_lock'] == 1 ) {
1205
+ $data .= PackageLocks::AskEmail( $package );
1206
+ $lock = 'locked';
1207
+ }
 
 
1208
 
1209
+ if ( isset( $package['linkedin_lock'] ) && (int) $package['linkedin_lock'] == 1 ) {
1210
+ $lock = 'locked';
1211
+ $sociallock .= PackageLocks::LinkedInShare( $package );
1212
 
1213
+ }
 
 
 
 
1214
 
1215
+ if ( isset( $package['twitterfollow_lock'] ) && (int) $package['twitterfollow_lock'] == 1 ) {
1216
+ $lock = 'locked';
1217
+ $sociallock .= PackageLocks::TwitterFollow( $package );
 
 
1218
 
1219
+ }
1220
 
1221
+ if ( isset( $package['gplusone_lock'] ) && (int) $package['gplusone_lock'] == 1 ) {
1222
+ $lock = 'locked';
1223
+ $sociallock .= PackageLocks::GooglePlusOne( $package, true );
1224
 
1225
+ }
 
 
 
 
1226
 
1227
+ if ( isset( $package['tweet_lock'] ) && (int) $package['tweet_lock'] == 1 ) {
1228
+ $lock = 'locked';
1229
+ $sociallock .= PackageLocks::Tweet( $package );
1230
 
1231
+ }
 
 
 
1232
 
1233
+ if ( isset( $package['facebooklike_lock'] ) && (int) $package['facebooklike_lock'] == 1 ) {
1234
+ $lock = 'locked';
1235
+ $sociallock .= PackageLocks::FacebookLike( $package, true );
1236
 
1237
+ }
 
 
 
 
 
 
 
 
1238
 
 
 
1239
 
1240
+ $extralocks = '';
1241
+ $extralocks = apply_filters( "wpdm_download_lock", $extralocks, $package );
 
 
 
 
 
 
 
 
 
1242
 
1243
+ if ( is_array( $extralocks ) && $extralocks['lock'] === 'locked' ) {
1244
 
1245
+ if ( isset( $extralocks['type'] ) && $extralocks['type'] == 'social' ) {
1246
+ $sociallock .= $extralocks['html'];
1247
+ } else {
1248
+ $data .= $extralocks['html'];
1249
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1250
 
1251
+ $lock = 'locked';
1252
+ }
1253
 
1254
+ if ( $sociallock !== "" ) {
1255
+ $socdesc = get_option( '_wpdm_social_lock_panel_desc', '' );
1256
+ $socdesc = $socdesc !== '' ? "<p>{$socdesc}</p>" : "";
1257
+ $data .= "<div class='panel panel-default card'><div class='panel-heading card-header'>" . get_option( '_wpdm_social_lock_panel_title', 'Like or Share to Download' ) . "</div><div class='panel-body card-body wpdm-social-locks text-center'>{$socdesc}{$sociallock}</div></div>";
1258
+ }
1259
 
1260
 
1261
+ if ( isset( $package['captcha_lock'] ) && (int) $package['captcha_lock'] == 1 ) {
1262
+ $lock = 'locked';
1263
+ $captcha = PackageLocks::reCaptchaLock( $package, true );
1264
+ $data .= $captcha; //"<div class='panel panel-default card'><div class='panel-heading card-header'>" . __("Verify CAPTCHA to Download", "download-manager") . "</div><div class='panel-body card-body wpdm-social-locks text-center'>{$captcha}</div></div>";
1265
+ }
1266
 
1267
+ if ( $lock === 'locked' ) {
1268
+ $popstyle = isset( $popstyle ) && in_array( $popstyle, array(
1269
+ 'popup',
1270
+ 'pop-over'
1271
+ ) ) ? $popstyle : 'pop-over';
1272
+ if ( $embed == 1 ) {
1273
+ $adata = "</strong>{$data}";
1274
+ } else {
1275
+ $link_label = get_post_meta( $ID, '__wpdm_link_label', true );
1276
+ $link_label = trim( $link_label ) ? $link_label : __( "Download", "download-manager" );
1277
+ $style = wpdm_download_button_style( ( $template_type === 'page' ), $ID );
1278
+ $style = isset( $params['btnclass'] ) && $params['btnclass'] !== '' ? $params['btnclass'] : $style;
1279
+ $adata = '<a href="#pkg_' . $ID . "_" . $unqid . '" data-package="' . $ID . '" data-trigger="manual" class="wpdm-download-link wpdm-download-locked ' . $style . '">' . $link_label . '</a>';
1280
 
1281
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1282
 
1283
+ $data = $adata;
1284
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1285
 
1286
+ return $data;
1287
+ }
1288
 
 
 
 
 
 
1289
 
1290
+ /**
1291
+ * @usage Generate download link of a package
1292
+ *
1293
+ * @param $package
1294
+ * @param int $embed
1295
+ * @param array $extras
1296
+ *
1297
+ * @return string
1298
+ */
1299
+ public function downloadLink( $ID, $embed = 0, $extras = array() ) {
1300
+ global $wpdb, $current_user, $wpdm_download_icon, $wpdm_download_lock_icon;
1301
+ if ( is_array( $extras ) ) {
1302
+ extract( $extras );
1303
+ }
1304
+ $data = '';
1305
+
1306
+ $template_type = isset( $extras['template_type'] ) ? $extras['template_type'] : 'page';
1307
 
1308
+ //$package = self::get($ID);
1309
+
1310
+ $link_label = get_post_meta( $ID, '__wpdm_link_label', true );
1311
+ $link_label = ( trim( $link_label ) === '' ) ? __( "Download", "download-manager" ) : $link_label;
1312
 
1313
+ $template_type = isset( $template_type ) ? $template_type : 'link';
1314
+ $link_label = apply_filters( "wpdm_link_label", $link_label, $ID, $template_type );
1315
+
1316
+ $loginmsg = Messages::login_required( $ID );
1317
+
1318
+ $download_url = $this->getDownloadURL( $ID );
1319
+
1320
+ $limit_over = 0;
1321
+ $alert_size = ( $template_type == 'link' ) ? 'alert-sm' : '';
1322
+
1323
+ if ( self::userDownloadLimitExceeded( $ID ) ) {
1324
+ $limit_msg = Messages::download_limit_exceeded( $ID );
1325
+ $download_url = '#';
1326
+ $link_label = $limit_msg;
1327
+ $limit_over = 1;
1328
+ }
1329
+
1330
+ $expired = get_post_meta( $ID, '__wpdm_expire_date', true );
1331
+ $publish = get_post_meta( $ID, '__wpdm_publish_date', true );
1332
+
1333
+ if ( $expired !== "" && strtotime( $expired ) < time() ) {
1334
+ $download_url = '#';
1335
+ $link_label = __( "Download was expired on", "download-manager" ) . " " . date_i18n( get_option( 'date_format' ) . " h:i A", strtotime( $expired ) );
1336
+
1337
+ return "<div class='alert alert-warning {$alert_size}' data-title='" . __( "DOWNLOAD ERROR:", "download-manager" ) . "'>{$link_label}</div>";
1338
+ }
1339
+
1340
+ if ( $publish !== "" && strtotime( $publish ) > time() ) {
1341
+ $download_url = '#';
1342
+ $link_label = __( "Download will be available from ", "download-manager" ) . " " . date_i18n( get_option( 'date_format' ) . " h:i A", strtotime( $publish ) );
1343
+
1344
+ return "<div class='alert alert-warning {$alert_size}' data-title='" . __( "DOWNLOAD ERROR:", "download-manager" ) . "'>{$link_label}</div>";
1345
+ }
1346
+
1347
+
1348
+ $style = wpdm_download_button_style( ( $template_type === 'page' ), $ID );
1349
+ $style = isset( $btnclass ) && $btnclass !== '' ? $btnclass : $style;
1350
+
1351
+ if ( $download_url != '#' ) {
1352
+ $download_link = $download_link_extended = $download_link_popup = (int) get_option( '__wpdm_mask_dlink', 1 ) === 1 ? "<a class='wpdm-download-link download-on-click {$style}' rel='nofollow' href='#' data-downloadurl=\"{$download_url}\">{$link_label}</a>" : "<a class='wpdm-download-link {$style}' rel='nofollow' href='{$download_url}'>{$link_label}</a>";
1353
+ } //$download_link = $download_link_extended = $download_link_popup = (int)get_option('__wpdm_mask_dlink', 1) === 1 ? "<a class='wpdm-download-link {$style}' rel='nofollow' href='#' onclick=\"location.href='{$download_url}';return false;\">{$link_label}</a>" : "<a class='wpdm-download-link {$style}' rel='nofollow' href='{$download_url}'>{$link_label}</a>";
1354
+ else {
1355
+ $download_link = "<div class='alert alert-warning {$alert_size}' data-title='" . __( "DOWNLOAD ERROR:", "download-manager" ) . "'>{$link_label}</div>";
1356
+ }
1357
+
1358
+
1359
+ $access = self::allowedRoles( $ID );
1360
+ if ( ! is_array( $access ) ) {
1361
+ $access = array();
1362
+ }
1363
+ $matched = ( is_array( $access ) && is_user_logged_in() ) ? array_intersect( $current_user->roles, $access ) : array();
1364
+ if ( ! $matched ) {
1365
+ $matched = [];
1366
+ }
1367
+
1368
+ $skiplink = 0;
1369
+
1370
+ //User does't have permission to download
1371
+ if ( is_user_logged_in() && count( $matched ) <= 0 && ! @in_array( 'guest', $access ) ) {
1372
+ $download_url = "#";
1373
+ $download_link = $download_link_extended = $download_link_popup = Messages::permission_denied( $ID );
1374
+ if ( get_option( '_wpdm_hide_all', 0 ) == 1 ) {
1375
+ $download_link = $download_link_extended = $download_link_popup = 'blocked';
1376
+ }
1377
+
1378
+ return $download_link;
1379
+ }
1380
+
1381
+ //Login is required to download
1382
+ if ( ! @in_array( 'guest', $access ) && ! is_user_logged_in() ) {
1383
+
1384
+ $loginform = WPDM()->user->login->form( array( 'redirect' => $_SERVER['REQUEST_URI'] ) );
1385
+ if ( get_option( '_wpdm_hide_all', 0 ) == 1 ) {
1386
+ $hide_all_message = get_option( '__wpdm_login_form', 0 ) == 1 ? $loginform : stripcslashes( str_replace( array(
1387
+ "[loginform]",
1388
+ "[this_url]",
1389
+ "[package_url]"
1390
+ ), array( $loginform, $_SERVER['REQUEST_URI'], get_permalink( $ID ) ), $loginmsg ) );
1391
+ if ( $template_type == 'link' ) {
1392
+ return "<a href='" . wpdm_login_url( $_SERVER['REQUEST_URI'] ) . "' class='btn btn-danger'>" . __( "Login", "download-manager" ) . "</a>";
1393
+ } else {
1394
+ return $hide_all_message;
1395
+ }
1396
+ }
1397
+ $download_url = wpdm_login_url( $_SERVER['REQUEST_URI'] );
1398
+ $download_link = $download_link_extended = $download_link_popup = stripcslashes( str_replace( array(
1399
+ "[loginform]",
1400
+ "[this_url]",
1401
+ "[package_url]"
1402
+ ), array( $loginform, $_SERVER['REQUEST_URI'], get_permalink( $ID ) ), $loginmsg ) );
1403
+
1404
+ return get_option( '__wpdm_login_form', 0 ) == 1 ? $loginform : $download_link;
1405
+
1406
+ }
1407
+
1408
+ //$package = apply_filters('wpdm_before_apply_locks', $package);
1409
+ //$package = apply_filters('wpdm_after_prepare_package_data', $package);
1410
+
1411
+ $unqid = uniqid();
1412
+ $stock_limit = (int) get_post_meta( $ID, '__wpdm_quota', true );
1413
+ $download_count = (int) get_post_meta( $ID, '__wpdm_download_count', true );
1414
+ if ( $stock_limit > $download_count || $stock_limit == 0 ) {
1415
+ $lock = 0;
1416
+
1417
+ $extras['embed'] = $embed;
1418
+ $data = self::activeLocks( $ID, $extras );
1419
+
1420
+ $terms_lock = (int) get_post_meta( $ID, '__wpdm_terms_lock', true );
1421
+ $terms_page = (int) get_post_meta( $ID, '__wpdm_terms_page', true );
1422
+ $terms_title = get_post_meta( $ID, '__wpdm_terms_title', true );
1423
+ $terms_conditions = get_post_meta( $ID, '__wpdm_terms_conditions', true );
1424
+ if ( $terms_page ) {
1425
+ $terms_page = get_post( $terms_page );
1426
+ $terms_title = $terms_page->post_title;
1427
+ $terms_conditions = $terms_page->post_content;
1428
+ }
1429
+ $terms_check_label = get_post_meta( $ID, '__wpdm_terms_check_label', true );
1430
+ if ( $terms_lock !== 0 && ( ! function_exists( 'wpdmpp_effective_price' ) || wpdmpp_effective_price( $ID ) == 0 ) ) {
1431
+ if ( ! self::isLocked( $ID ) && ! $embed ) {
1432
+ $data = "<a href='#unlock' class='wpdm-download-link wpdm-download-locked {$style}' data-package='{$ID}'>{$link_label}</a>";
1433
+ } else {
1434
+ $data = $data ? $data : $download_link;
1435
+ }
1436
+ if ( $embed == 1 ) {
1437
+ $data = "<div class='panel panel-default card terms-panel' style='margin: 0 0 10px 0'><div class='panel-heading card-header'>{$terms_title}</div><div class='panel-body card-body' style='max-height: 200px;overflow: auto'>{$terms_conditions}</div><div class='panel-footer card-footer'><label><input data-pid='{$ID}' class='wpdm-checkbox terms_checkbox terms_checkbox_{$ID}' type='checkbox'> {$terms_check_label}</label></div><div class='panel-footer card-footer bg-white download_footer_{$ID}' style='display:none;'>{$data}</div></div><script>jQuery(function($){ $('#wpdm-filelist-{$ID} .btn.inddl, #xfilelist .btn.inddl').attr('disabled', 'disabled'); });</script>";
1438
+ }
1439
+
1440
+ }
1441
+
1442
+ if ( $data != "" ) {
1443
+ $data = apply_filters( 'wpdm_download_link', $data, $extras + array( 'ID' => $ID, 'id' => $ID ) );
1444
+
1445
+ return $data;
1446
+ }
1447
+
1448
+
1449
+ $data = $download_link;
1450
+
1451
+
1452
+ } else {
1453
+ $data = "<button class='btn btn-danger btn-block' type='button' disabled='disabled' data-title='DOWNLOAD ERROR:'>" . __( "Limit Over!", "download-manager" ) . "</button>";
1454
+ }
1455
+ if ( $data == 'loginform' ) {
1456
+ return WPDM()->user->login->form();
1457
+ }
1458
+ $data = apply_filters( 'wpdm_download_link', $data, $extras + array( 'ID' => $ID, 'id' => $ID ) );
1459
+
1460
+ return $data;
1461
+
1462
+ }
1463
+
1464
+ /**
1465
+ * @usage Generate download url for public/open downloads, the url will not work for the packages with lock option
1466
+ *
1467
+ * @param $ID
1468
+ * @param $ext
1469
+ *
1470
+ * @return string
1471
+ */
1472
+ public function getDownloadURL( $ID, $ext = array() ) {
1473
+ if ( self::isLocked( $ID ) && ! Session::get( '__wpdm_unlocked_' . $ID ) ) {
1474
+ return '#locked';
1475
+ }
1476
+ if ( ! is_array( $ext ) ) {
1477
+ $ext = [];
1478
+ }
1479
+ $ext['wpdmdl'] = $ID;
1480
+ $ext['refresh'] = uniqid() . time();
1481
+ $permalink = get_permalink( $ID );
1482
+ $permalink = apply_filters( "wpdm_download_url_base", $permalink, $ID );
1483
+ $download_url = add_query_arg( $ext, $permalink );
1484
+ $flat = (int) get_option( '__wpdm_flat_download_url', 0 );
1485
+ $code = json_encode( $ext );
1486
+ $code = base64_encode( $code );
1487
+ $code = rtrim( $code, '=' );
1488
+ $filename = isset( $ext['filename'] ) ? $ext['filename'] : '';
1489
+ if ( isset( $ext['ind'] ) && $filename == '' ) {
1490
+ $files = $this->getFiles( $ID );
1491
+ $filename = wpdm_basename( $files[ $ext['ind'] ] );
1492
+ }
1493
+ if ( ! isset( $ext['ind'] ) && $filename == '' ) {
1494
+ $files = $this->getFiles( $ID );
1495
+ if ( count( $files ) > 1 ) {
1496
+ $filename = sanitize_file_name( get_the_title( $ID ) ) . ".zip";
1497
+ } else {
1498
+ $filename = array_shift( $files );
1499
+ $filename = wpdm_basename( $filename );
1500
+ }
1501
+
1502
+ }
1503
+ if ( $flat ) {
1504
+ $download_url = home_url( "/wpdmdl/{$ID}-{$code}/{$filename}" );
1505
+ }
1506
+
1507
+ return $download_url;
1508
+ }
1509
+
1510
+ /**
1511
+ * @param $ID
1512
+ *
1513
+ * @return false|string
1514
+ */
1515
+ public function getMasterDownloadURL( $ID ) {
1516
+ $package_url = get_permalink( $ID );
1517
+ $params['wpdmdl'] = $ID;
1518
+ $params['masterkey'] = get_post_meta( $ID, '__wpdm_masterkey', true );
1519
+ $download_url = add_query_arg( $params, $package_url );
1520
+
1521
+ return $download_url;
1522
+ }
1523
+
1524
+ /**
1525
+ * @param $ID
1526
+ * @param $Key
1527
+ *
1528
+ * @return bool
1529
+ */
1530
+ public function validateMasterKey( $ID, $Key ) {
1531
+ if ( $Key === '' ) {
1532
+ return false;
1533
+ }
1534
+ $masterKey = get_post_meta( $ID, '__wpdm_masterkey', true );
1535
+ if ( $masterKey === '' ) {
1536
+ return false;
1537
+ }
1538
+ if ( $masterKey === $Key ) {
1539
+ return true;
1540
+ }
1541
+
1542
+ return false;
1543
+ }
1544
+
1545
+ /**
1546
+ * @param $ID
1547
+ * @param int $usageLimit
1548
+ * @param int $expirePeriod seconds
1549
+ *
1550
+ * @return string
1551
+ */
1552
+ function expirableDownloadLink( $ID, $usageLimit = 10, $expirePeriod = 999999, $sessionOnly = true ) {
1553
+ $key = uniqid();
1554
+ $exp = array( 'use' => $usageLimit, 'expire' => time() + $expirePeriod );
1555
+ if ( ! $sessionOnly ) {
1556
+ update_post_meta( $ID, "__wpdmkey_" . $key, $exp );
1557
+ } else {
1558
+ TempStorage::set( "__wpdmkey_{$key}", $exp, time() + $expirePeriod );
1559
+ }
1560
+ //Session::set( '__wpdm_unlocked_'.$ID , 1 );
1561
+ //$download_url = $this->getDownloadURL($ID, "_wpdmkey={$key}");
1562
+ $permalink = get_permalink( $ID );
1563
+ $permalink = apply_filters( "wpdm_download_url_base", $permalink, $ID );
1564
+ $download_url = add_query_arg( array( "wpdmdl" => $ID, "_wpdmkey" => $key ), $permalink );
1565
+
1566
+ return $download_url;
1567
+ }
1568
+
1569
+ /**
1570
+ * @param $ID
1571
+ * @param int $usageLimit
1572
+ * @param int $expirePeriod seconds
1573
+ *
1574
+ * @return string
1575
+ */
1576
+ static function expirableDownloadPage( $ID, $usageLimit = 10, $expirePeriod = 604800, $sessionOnly = true ) {
1577
+ $key = uniqid();
1578
+ $exp = array( 'use' => $usageLimit, 'expire' => time() + $expirePeriod );
1579
+ if ( ! $sessionOnly ) {
1580
+ update_post_meta( $ID, "__wpdmkey_" . $key, $exp );
1581
+ } else {
1582
+ TempStorage::set( "__wpdmkey_{$key}", $exp, time() + $expirePeriod );
1583
+ }
1584
+ $download_page_key = Crypt::encrypt( array( 'pid' => $ID, 'key' => $key ) );
1585
+ $download_page = home_url( "wpdm-download/{$download_page_key}" );
1586
+
1587
+ return $download_page;
1588
+ }
1589
+
1590
+
1591
+ /**
1592
+ * @usage Fetch link/page template and return generated html
1593
+ *
1594
+ * @param $template
1595
+ * @param $vars
1596
+ * @param string $type
1597
+ *
1598
+ * @return mixed|string
1599
+ */
1600
+ public function fetchTemplate( $template, $vars, $type = 'link' ) {
1601
+ if ( ! is_array( $vars ) && is_int( $vars ) && $vars > 0 ) {
1602
+ $vars = array( 'ID' => $vars );
1603
+ }
1604
+ if ( ! isset( $vars['ID'] ) || intval( $vars['ID'] ) < 1 ) {
1605
+ return '';
1606
+ }
1607
+
1608
+ $loginmsg = Messages::login_required( $vars['ID'] );
1609
+
1610
+ if ( ! is_user_logged_in() && count( self::allowedRoles( $vars['ID'] ) ) >= 0 && ! self::userCanAccess( $vars['ID'] ) ) {
1611
+ $loginform = wpdm_login_form( array( 'redirect' => get_permalink( $vars['ID'] ) ) );
1612
+ $hide_all_message = get_option( '__wpdm_login_form', 0 ) == 1 ? $loginform : stripcslashes( str_replace( array(
1613
+ "[loginform]",
1614
+ "[this_url]",
1615
+ "[package_url]"
1616
+ ), array( $loginform, $_SERVER['REQUEST_URI'], get_permalink( $vars['ID'] ) ), $loginmsg ) );
1617
+ if ( get_option( '_wpdm_hide_all', 0 ) == 1 ) {
1618
+ return $type == 'page' ? $hide_all_message : '';
1619
+ }
1620
+ }
1621
+
1622
+ if ( is_user_logged_in() && ! self::userCanAccess( $vars['ID'] ) && get_option( '_wpdm_hide_all', 0 ) == 1 ) {
1623
+ return $type != 'page' ? "" : get_option( '__wpdm_permission_denied_msg', __( "You are not allowed to download this item!", "download-manager" ) );
1624
+ }
1625
+
1626
+
1627
+ /*$default['link'] = 'link-template-default.php';
1628
  $default['page'] = 'page-template-default.php';
1629
 
1630
 
1677
 
1678
  }*/
1679
 
1680
+ $ret = $this->prepare( $vars['ID'], $template, $type );
1681
+ if ( ! is_wp_error( $ret ) ) {
1682
+ $vars = $this->packageData;
1683
+ } else {
1684
+ return '';
1685
+ }
1686
+
1687
+ // Get template content
1688
+ $template = $this->getTemplateContent( $template, $vars['ID'], $type );
1689
+
1690
+ if ( isset( $vars['__loginform_only'] ) && $vars['__loginform_only'] != '' ) {
1691
+ return $vars['__loginform_only'];
1692
+ }
1693
+
1694
+ preg_match_all( "/\[cf ([^\]]+)\]/", $template, $cfmatches );
1695
+ preg_match_all( "/\[thumb_([0-9]+)x([0-9]+)\]/", $template, $matches );
1696
+ preg_match_all( "/\[thumb_url_([0-9]+)x([0-9]+)\]/", $template, $umatches );
1697
+ preg_match_all( "/\[thumb_gallery_([0-9]+)x([0-9]+)\]/", $template, $gmatches );
1698
+ preg_match_all( "/\[excerpt_([0-9]+)\]/", $template, $xmatches );
1699
+ preg_match_all( "/\[pdf_thumb_([0-9]+)x([0-9]+)\]/", $template, $pmatches );
1700
+ preg_match_all( "/\[txt=([^\]]+)\]/", $template, $txtmatches );
1701
+ preg_match_all( "/\[hide_empty:([^\]]+)\]/", $template, $hematches );
1702
+ preg_match_all( "/\[video_player_([0-9]+)\]/", $template, $vdmatches );
1703
+ preg_match_all( "/\[product_preview_([0-9]+)x([0-9]+)\]/", $template, $ppmatches );
1704
+ preg_match_all( "/\[file_list_extended_([0-9]+)x([0-9]+)x([0-9]+)\]/", $template, $flematches );
1705
+ preg_match_all( "/\[image_gallery_([0-9]+)x([0-9]+)x([0-9]+)\]/", $template, $igematches );
1706
+
1707
+
1708
+ //$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($vars['ID']), 'full');
1709
+ //$vars['preview'] = $thumb['0'];
1710
+ //$vars['featured_image'] = ($vars['preview'] != '')?"<img src='{$vars['preview']}' alt='{$vars['title']}' />":"";
1711
+
1712
+ $tfiles = $vars['files'];
1713
+ $pdf = is_array( $tfiles ) ? array_shift( $tfiles ) : "";
1714
+ $ext = FileSystem::fileExt( $pdf );
1715
+
1716
+ // [video_player_...]
1717
+ foreach ( $vdmatches[0] as $nd => $scode ) {
1718
+ $scode = str_replace( array( '[', ']' ), '', $scode );
1719
+ $vars[ $scode ] = self::videoPlayer( $vars['ID'], $vars['files'], $vdmatches[1][ $nd ] );
1720
+ }
1721
+
1722
+ //Replace all file list extended tags
1723
+ foreach ( $flematches[0] as $nd => $scode ) {
1724
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1725
+ $vars[ $scode ] = FileList::Box( $vars, $flematches[1][ $nd ], $flematches[2][ $nd ], $flematches[3][ $nd ] );
1726
+ }
1727
+
1728
+ //Replace all image gallery tags
1729
+ foreach ( $igematches[0] as $nd => $scode ) {
1730
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1731
+ $vars[ $scode ] = FileList::imageGallery( $vars, $igematches[1][ $nd ], $igematches[2][ $nd ], $igematches[3][ $nd ] );
1732
+ }
1733
+
1734
+
1735
+ //Replace all txt variables
1736
+ foreach ( $txtmatches[0] as $nd => $scode ) {
1737
+ $scode = str_replace( array( '[', ']' ), '', $scode );
1738
+ $vars[ $scode ] = __( $txtmatches[1][ $nd ], "download-manager" );
1739
+ }
1740
+
1741
+ // Parse [pdf_thumb] tag in link/page template
1742
+ if ( strpos( $template, 'pdf_thumb' ) ) {
1743
+ if ( $ext == 'pdf' ) {
1744
+ $pdf_preview = FileSystem::pdfThumbnail( $pdf, $vars['ID'] );
1745
+ $vars['pdf_thumb'] = "<img alt='{$vars['title']}' src='" . $pdf_preview . "' />";
1746
+ $vars['pdf_thumb_url'] = $pdf_preview;
1747
+ $vars['pdf_name'] = str_replace( [ "pdf", "PDF" ], "", wp_basename( $pdf ) );
1748
+ } else {
1749
+ $vars['pdf_thumb'] = $vars['preview'] != '' ? "<img alt='{$vars['title']}' src='{$vars['preview']}' />" : "";
1750
+ }
1751
+ }
1752
+
1753
+ // Parse [pdf_thumb_WxH] tag in link/page template
1754
+ foreach ( $pmatches[0] as $nd => $scode ) {
1755
+ $imsrc = wpdm_dynamic_thumb( FileSystem::pdfThumbnail( $pdf, $vars['ID'] ), array(
1756
+ $pmatches[1][ $nd ],
1757
+ $pmatches[2][ $nd ]
1758
+ ) );
1759
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1760
+ $vars[ $scode ] = $imsrc != '' ? "<img src='" . $imsrc . "' alt='{$vars['title']}' />" : '';
1761
+ }
1762
+
1763
+ // Parse [file_type] tag in link/page template
1764
+ if ( strpos( $template, 'file_type' ) ) {
1765
+ $vars['file_types'] = self::fileTypes( $vars['ID'], false );
1766
+ if ( is_array( $vars['file_types'] ) ) {
1767
+ $vars['file_types'] = implode( ", ", $vars['file_types'] );
1768
+ }
1769
+ $vars['file_type_icons'] = self::fileTypes( $vars['ID'] );
1770
+ }
1771
+
1772
+ $crop = get_option( '__wpdm_crop_thumbs', true );
1773
+
1774
+ // [thumb_WxH]
1775
+ foreach ( $matches[0] as $nd => $scode ) {
1776
+ $imsrc = wpdm_dynamic_thumb( $vars['preview'], array(
1777
+ $matches[1][ $nd ],
1778
+ $matches[2][ $nd ]
1779
+ ), $crop );
1780
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1781
+ $vars[ $scode ] = $vars['preview'] != '' ? "<img class='wpdm-thumb wpdm-thumb-{$matches[1][$nd]}x{$matches[2][$nd]} wpdm-thumb-{$vars['ID']}' src='" . $imsrc . "' alt='{$vars['title']}' />" : '';
1782
+ }
1783
+
1784
+ // [thumb_url...]
1785
+ foreach ( $umatches[0] as $nd => $scode ) {
1786
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1787
+ $vars[ $scode ] = $vars['preview'] != '' ? wpdm_dynamic_thumb( $vars['preview'], array(
1788
+ $umatches[1][ $nd ],
1789
+ $umatches[2][ $nd ]
1790
+ ), $crop ) : '';
1791
+ }
1792
+
1793
+ // [thumb_gallery...]
1794
+ foreach ( $gmatches[0] as $nd => $scode ) {
1795
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1796
+ $vars[ $scode ] = $this->additionalPreviewImages( $vars, $gmatches[1][ $nd ], $gmatches[2][ $nd ] );
1797
+ }
1798
+
1799
+ // [product_preview...]
1800
+ foreach ( $ppmatches[0] as $nd => $scode ) {
1801
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1802
+ $vars[ $scode ] = self::productPreview( $vars['ID'], $vars['files'], $ppmatches[1][ $nd ], $ppmatches[2][ $nd ] );
1803
+ }
1804
+
1805
+ // [excerpt_...]
1806
+ foreach ( $xmatches[0] as $nd => $scode ) {
1807
+ $ss = substr( strip_tags( $vars['description'] ), 0, intval( $xmatches[1][ $nd ] ) );
1808
+ $tmp = explode( " ", substr( strip_tags( $vars['description'] ), intval( $xmatches[1][ $nd ] ) ) );
1809
+ $bw = array_shift( $tmp );
1810
+ $ss .= $bw;
1811
+ $scode = str_replace( array( "[", "]" ), "", $scode );
1812
+ $vars[ $scode ] = $ss . '...';
1813
+ }
1814
+
1815
+ if ( $type == 'page' && ( strpos( $template, '[similar_downloads]' ) || strpos( $vars['description'], '[similar_downloads]' ) ) ) {
1816
+ $vars['similar_downloads'] = $this->similarPackages( $vars, 6 );
1817
+ }
1818
+
1819
+ if ( strpos( $template, 'doc_preview' ) ) {
1820
+ $vars['doc_preview'] = self::docPreview( $vars );
1821
+ }
1822
+
1823
+ if ( substr_count( $template, 'video_preview_modal' ) ) {
1824
+ $vars['video_preview_modal'] = self::videoPreviewModal( $vars, $type );
1825
+ }
1826
+
1827
+
1828
+ $vars['fav_button'] = self::favBtn( $vars['ID'] );
1829
+ $vars['fav_button_sm'] = self::favBtn( $vars['ID'], array(
1830
+ 'size' => 'btn-sm',
1831
+ 'a2f_label' => "<i class='fa fa-heart'></i> &nbsp; " . __( "Add to favourite", "download-manager" ),
1832
+ 'rff_label' => "<i class='fa fa-heart'></i> &nbsp; " . __( "Remove from favourite", "download-manager" )
1833
+ ) );
1834
+ $vars['fav_button_ico_sm'] = self::favBtn( $vars['ID'], array(
1835
+ 'size' => 'btn-sm',
1836
+ 'a2f_label' => "<i class='far fa-heart'></i>",
1837
+ 'rff_label' => "<i class='fas fa-heart'></i>"
1838
+ ) );
1839
+ $vars['fav_button_ico'] = self::favBtn( $vars['ID'], array(
1840
+ 'size' => '',
1841
+ 'a2f_label' => "<i class='fa fa-heart'></i>",
1842
+ 'rff_label' => "<i class='fa fa-heart'></i>"
1843
+ ) );
1844
+
1845
+
1846
+ // If need to re-process any data before fetch template
1847
+ $vars['__template_type'] = $type;
1848
+ $vars = apply_filters( "wdm_before_fetch_template", $vars, $template, $type );
1849
+
1850
+ foreach ( $hematches[0] as $index => $hide_empty ) {
1851
+ $hide_empty = str_replace( array( '[', ']' ), '', $hide_empty );
1852
+ if ( ! isset( $vars[ $hematches[1][ $index ] ] ) || ( $vars[ $hematches[1][ $index ] ] == '' || $vars[ $hematches[1][ $index ] ] == '0' ) ) {
1853
+ $vars[ $hide_empty ] = 'wpdm_hide wpdm_remove_empty';
1854
+ } else {
1855
+ $value[ $hide_empty ] = '';
1856
+ }
1857
+ }
1858
+
1859
+
1860
+ $keys = array();
1861
+ $values = array();
1862
+
1863
+ foreach ( $vars as $key => $value ) {
1864
+ if ( ! is_array( $value ) && ! is_object( $value ) ) {
1865
+ $keys[] = "[$key]";
1866
+ $values[] = $value;
1867
+ }
1868
+ }
1869
+
1870
+
1871
+ $loginform = wpdm_login_form( array( 'redirect' => get_permalink( $vars['ID'] ) ) );
1872
+ $hide_all_message = get_option( '__wpdm_login_form', 0 ) == 1 ? $loginform : stripcslashes( str_replace( array(
1873
+ "[loginform]",
1874
+ "[this_url]",
1875
+ "[package_url]"
1876
+ ), array( $loginform, $_SERVER['REQUEST_URI'], get_permalink( $vars['ID'] ) ), $loginmsg ) );
1877
+
1878
+ if ( $vars['download_link'] == 'blocked' && $type == 'link' ) {
1879
+ return "";
1880
+ }
1881
+ if ( $vars['download_link'] == 'blocked' && $type == 'page' ) {
1882
+ return get_option( '__wpdm_permission_denied_msg' );
1883
+ }
1884
+ if ( $vars['download_link'] == 'loginform' && $type == 'link' ) {
1885
+ return "";
1886
+ }
1887
+ if ( $vars['download_link'] == 'loginform' && $type == 'page' ) {
1888
+ return $hide_all_message;
1889
+ }
1890
+
1891
+
1892
+ $template = str_replace( $keys, $values, @stripcslashes( $template ) );
1893
+
1894
+ $template = apply_filters( "wpdm_after_fetch_template", $template, $vars );
1895
+
1896
+ //wp_reset_query();
1897
+ //wp_reset_postdata();
1898
+ return $template;
1899
+ }
1900
+
1901
+ /*public static function parseTemplate($template, $post, $type = 'link')
1902
  {
1903
 
1904
  if (!strpos(strip_tags($template), "]")) {
1943
  return $template;
1944
  }*/
1945
 
1946
+ /**
1947
+ * @usage Find attached files types with a package
1948
+ *
1949
+ * @param $ID
1950
+ * @param bool|true $img
1951
+ *
1952
+ * @return array|string
1953
+ */
1954
+ public static function fileTypes( $ID, $img = true, $size = 16 ) {
1955
+ $files = maybe_unserialize( get_post_meta( $ID, '__wpdm_files', true ) );
1956
+ $ext = array();
1957
+ if ( is_array( $files ) ) {
1958
+ foreach ( $files as $f ) {
1959
+ $f = trim( $f );
1960
+ $ext[] = FileSystem::fileExt( $f );
1961
+ }
1962
+ }
1963
+
1964
+ $ext = array_unique( $ext );
1965
+ $exico = '';
1966
+ foreach ( $ext as $exi ) {
1967
+ $exico .= "<img alt='{$exi}' title='{$exi}' class='ttip' style='width:{$size}px;height:{$size}px;' src='" . FileSystem::fileTypeIcon( $exi ) . "' /> ";
1968
+ }
1969
+ if ( $img ) {
1970
+ return $exico;
1971
+ }
1972
+
1973
+ return $ext;
1974
+ }
1975
+
1976
+
1977
+ /**
1978
+ * @param $package
1979
+ *
1980
+ * @return string
1981
+ * @usage Generate Google Doc Preview
1982
+ */
1983
+ public function docPreview( $package ) {
1984
+
1985
+ //$files = $package['files'];
1986
+ $files = $this->getFiles( $package['ID'] );
1987
+ if ( ! is_array( $files ) ) {
1988
+ return "";
1989
+ }
1990
+ $ind = - 1;
1991
+ $fext = '';
1992
+ foreach ( $files as $i => $sfile ) {
1993
+ $ifile = $sfile;
1994
+ $sfile = explode( ".", $sfile );
1995
+ $fext = end( $sfile );
1996
+ if ( in_array( end( $sfile ), array( 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx' ) ) ) {
1997
+ $ind = $i;
1998
+ break;
1999
+ }
2000
+ }
2001
+
2002
+ if ( $ind == - 1 ) {
2003
+ return "";
2004
+ }
2005
+ $ext = count( $files ) > 1 ? 'ind=' . $ind : '';
2006
+ $params = array( 'ind' => $ind, 'filename' => $files[ $ind ] );
2007
+ $url = $this->getDownloadURL( $package['ID'], $params );
2008
+ $url .= "&open=1";
2009
+ if ( strpos( $ifile, "://" ) ) {
2010
+ $url = $ifile;
2011
+ }
2012
+ $doc_preview_html = FileSystem::docViewer( $url, $package['ID'], $fext );
2013
+ $doc_preview_html = apply_filters( 'wpdm_doc_preview', $doc_preview_html, $package, $url, $fext );
2014
+
2015
+ return $doc_preview_html;
2016
+ }
2017
+
2018
+ /**
2019
+ * Get additional preview images
2020
+ *
2021
+ * @param $file
2022
+ * @param $w
2023
+ * @param $h
2024
+ *
2025
+ * @return string
2026
+ */
2027
+ function additionalPreviewImages( $file, $w, $h ) {
2028
+
2029
+ $file['additional_previews'] = maybe_unserialize( get_post_meta( $file['ID'], '__wpdm_additional_previews', true ) );
2030
+ $k = 0;
2031
+ $img = '';
2032
+ $id = uniqid();
2033
+ if ( $file['additional_previews'] ) {
2034
+ foreach ( $file['additional_previews'] as $p ) {
2035
+ ++ $k;
2036
+ if ( is_numeric( $p ) ) {
2037
+ $img .= "<a href='" . wp_get_attachment_url( $p ) . "' id='more_previews_a_{$k}' class='more_previews_a imgpreview wpdm-lightbox' data-lightbox-gallery='gallery_{$id}' rel='previews'><img id='more_previews_{$k}' class='more_previews img-rounded' src='" . wpdm_dynamic_thumb( get_attached_file( $p ), array(
2038
+ $w,
2039
+ $h
2040
+ ) ) . "'/></a>";
2041
+ } else {
2042
+ $img .= "<a href='{$p}' id='more_previews_a_{$k}' class='more_previews_a imgpreview wpdm-lightbox' data-lightbox-gallery='gallery_{$id}' rel='previews' ><img id='more_previews_{$k}' class='more_previews img-rounded' src='" . wpdm_dynamic_thumb( $p, array(
2043
+ $w,
2044
+ $h
2045
+ ) ) . "'/></a>";
2046
+ }
2047
+ }
2048
+ }
2049
+ $js = ""; // "<script>jQuery(function($){ $('a.more_previews_a').nivoLightbox(); });</script>";
2050
+
2051
+ return $img . $js;
2052
+ }
2053
+
2054
+ /**
2055
+ * @usage Generates view preview modal link of the given package
2056
+ *
2057
+ * @param $package
2058
+ * @param int $embed
2059
+ * @param array $extras
2060
+ *
2061
+ * @return string
2062
+ */
2063
+ public function videoPreviewModal( $package, $template_type = 'link' ) {
2064
+ if ( is_int( $package ) ) {
2065
+ $package = get_post( $package, ARRAY_A );
2066
+ }
2067
+ $link_label = wpdm_valueof( $package, 'link_label' );
2068
+ $link_label = $link_label ? $link_label : $this->linkLabel( $package['ID'] );
2069
+ $style = wpdm_download_button_style( $template_type === 'page', $package['ID'] );
2070
+ $files = isset( $package['files'] ) ? $package['files'] : $this->getFiles( $package['ID'] );
2071
+ $video = "";
2072
+ foreach ( $files as $file ) {
2073
+ if ( substr_count( $file, 'youtu.be' ) || substr_count( $file, 'youtube.com' ) || substr_count( $file, 'vimeo.com' ) ) {
2074
+ $video = $file;
2075
+ }
2076
+ }
2077
+ $link = "<a class='wpdm-lightbox {$style}' href='{$video}'>{$link_label}</a>";
2078
+
2079
+ return $link;
2080
+
2081
+ }
2082
+
2083
+ /**
2084
+ * Get package link label
2085
+ *
2086
+ * @param $ID
2087
+ *
2088
+ * @return mixed|string|void
2089
+ */
2090
+ public function linkLabel( $ID ) {
2091
+ $link_label = get_post_meta( $ID, '__wpdm_link_label', true );
2092
+ $link_label = esc_attr( $link_label );
2093
+ $link_label = $link_label ? $link_label : __( "Download", "download-manager" );
2094
+
2095
+ return $link_label;
2096
+ }
2097
+
2098
+ /**
2099
+ * @usage Create New Package
2100
+ *
2101
+ * @param $data
2102
+ *
2103
+ * @return mixed
2104
+ */
2105
+ public static function create( $package_data ) {
2106
+
2107
+ if ( isset( $package_data['post_type'] ) ) {
2108
+ unset( $package_data['post_type'] );
2109
+ }
2110
+
2111
+ $package_data_core = array(
2112
+ 'post_title' => '',
2113
+ 'post_content' => '',
2114
+ 'post_excerpt' => '',
2115
+ 'post_status' => 'publish',
2116
+ 'post_type' => 'wpdmpro',
2117
+ 'post_author' => get_current_user_id(),
2118
+ 'ping_status' => get_option( 'default_ping_status' ),
2119
+ 'post_parent' => 0,
2120
+ 'menu_order' => 0,
2121
+ 'to_ping' => '',
2122
+ 'pinged' => '',
2123
+ 'post_password' => '',
2124
+ 'guid' => '',
2125
+ 'post_content_filtered' => '',
2126
+ 'import_id' => 0
2127
+ );
2128
+
2129
+ $package_data_meta = array(
2130
+ 'files' => array(),
2131
+ 'fileinfo' => array(),
2132
+ 'package_dir' => '',
2133
+ 'link_label' => __( "Download", "download-manager" ),
2134
+ 'download_count' => 0,
2135
+ 'view_count' => 0,
2136
+ 'version' => '1.0.0',
2137
+ 'stock' => 0,
2138
+ 'package_size' => 0,
2139
+ 'package_size_b' => 0,
2140
+ 'access' => '',
2141
+ 'individual_file_download' => - 1,
2142
+ 'cache_zip' => - 1,
2143
+ 'template' => 'link-template-panel.php',
2144
+ 'page_template' => 'page-template-1col-flat.php',
2145
+ 'password_lock' => '0',
2146
+ 'facebook_lock' => '0',
2147
+ 'gplusone_lock' => '0',
2148
+ 'linkedin_lock' => '0',
2149
+ 'tweet_lock' => '0',
2150
+ 'email_lock' => '0',
2151
+ 'icon' => '',
2152
+ 'import_id' => 0
2153
+ );
2154
+
2155
+ foreach ( $package_data_core as $key => &$value ) {
2156
+ $value = isset( $package_data[ $key ] ) ? $package_data[ $key ] : $package_data_core[ $key ];
2157
+ }
2158
+
2159
+ if ( ! isset( $package_data['ID'] ) ) {
2160
+ $post_id = wp_insert_post( $package_data_core );
2161
+ } else {
2162
+ $post_id = $package_data['ID'];
2163
+ $package_data_core['ID'] = $post_id;
2164
+ wp_update_post( $package_data_core );
2165
+ }
2166
+
2167
+ foreach ( $package_data_meta as $key => $value ) {
2168
+ $value = isset( $package_data[ $key ] ) ? $package_data[ $key ] : $package_data_meta[ $key ];
2169
+ update_post_meta( $post_id, '__wpdm_' . $key, $value );
2170
+ }
2171
+
2172
+ if ( isset( $package_data['cats'] ) ) {
2173
+ wp_set_post_terms( $post_id, $package_data['cats'], 'wpdmcategory' );
2174
+ }
2175
+
2176
+ if ( isset( $package_data['featured_image'] ) ) {
2177
+
2178
+ $wp_filetype = wp_check_filetype( wp_basename( $package_data['featured_image'] ), null );
2179
+
2180
+ if ( __::is_url( $package_data['featured_image'] ) ) {
2181
+ $upload_dir = wp_upload_dir();
2182
+ $file_path = $upload_dir['path'] . '/' . sanitize_file_name( $package_data['post_title'] ) . '.' . $wp_filetype['ext'];
2183
+ //$data = remote_get($package_data['featured_image']);
2184
+ //$ret = copy($package_data['featured_image'], $file_path);
2185
+ //if(!$ret) wpdmdd($package_data['featured_image']);
2186
+ file_put_contents( $file_path, wpdm_remote_get( $package_data['featured_image'] ) );
2187
+ $package_data['featured_image'] = $file_path;
2188
+ }
2189
+
2190
+ $mime_type = '';
2191
+
2192
+ if ( isset( $wp_filetype['type'] ) && $wp_filetype['type'] ) {
2193
+ $mime_type = $wp_filetype['type'];
2194
+ }
2195
+ unset( $wp_filetype );
2196
+ $attachment = array(
2197
+ 'post_mime_type' => $mime_type,
2198
+ 'post_parent' => $post_id,
2199
+ 'post_title' => wp_basename( $package_data['featured_image'] ),
2200
+ 'post_status' => 'inherit'
2201
+ );
2202
+ $attachment_id = wp_insert_attachment( $attachment, $package_data['featured_image'], $post_id );
2203
+ unset( $attachment );
2204
+
2205
+ if ( ! is_wp_error( $attachment_id ) ) {
2206
+ $attachment_data = wp_generate_attachment_metadata( $attachment_id, $package_data['featured_image'] );
2207
+ wp_update_attachment_metadata( $attachment_id, $attachment_data );
2208
+ unset( $attachment_data );
2209
+ set_post_thumbnail( $post_id, $attachment_id );
2210
+ }
2211
+ }
2212
+
2213
+ return $post_id;
2214
+ }
2215
+
2216
+ /**
2217
+ * @param $id
2218
+ * @param array $atfb
2219
+ *
2220
+ * @return string
2221
+ */
2222
+ public static function favBtn( $id, $atfb = array(), $count = true ) {
2223
+ if ( empty( $atfb ) ) {
2224
+ $atfb = array(
2225
+ 'size' => '',
2226
+ 'a2f_label' => "<i class='fa fa-heart'></i> &nbsp;" . __( "Add to favourite", "download-manager" ),
2227
+ 'rff_label' => "<i class='fa fa-heart'></i> &nbsp; " . __( "Remove from favourite", "download-manager" )
2228
+ );
2229
+ }
2230
+ $atfb = apply_filters( "wpdm_fav_btn", $atfb, $id );
2231
+ $myfavs = maybe_unserialize( get_user_meta( get_current_user_id(), '__wpdm_favs', true ) );
2232
+ $ufavs = maybe_unserialize( get_post_meta( $id, '__wpdm_favs', true ) );
2233
+ $pfc = is_array( $ufavs ) ? count( $ufavs ) : 0;
2234
+ $btnclass = is_array( $myfavs ) && in_array( $id, $myfavs ) ? 'btn-danger' : 'btn-secondary';
2235
+ $label = is_array( $myfavs ) && in_array( $id, $myfavs ) ? $atfb['rff_label'] : $atfb['a2f_label'];
2236
+ extract( $atfb );
2237
+ if ( $count ) {
2238
+ return "<div class='btn-group'><button type='button' data-alabel=\"{$atfb['a2f_label']}\" data-rlabel=\"{$atfb['rff_label']}\" data-package='{$id}' class='btn btn-wpdm-a2f {$btnclass} {$size} btn-simple'>{$label}</button><button class='btn btn-secondary btn-simple {$size}' disabled='disabled'>{$pfc}</button></div>";
2239
+ } else {
2240
+ return "<button type='button' data-alabel=\"{$atfb['a2f_label']}\" data-rlabel=\"{$atfb['rff_label']}\" data-package='{$id}' class='btn btn-wpdm-a2f {$btnclass} {$size} btn-simple'>{$label}</button>";
2241
+ }
2242
+
2243
+ }
2244
+
2245
+ /**
2246
+ * Shows favourite count
2247
+ *
2248
+ * @param $id
2249
+ *
2250
+ * @return int
2251
+ */
2252
+ public static function favCount( $id ) {
2253
+ $ufavs = maybe_unserialize( get_post_meta( $id, '__wpdm_favs', true ) );
2254
+ $pfc = is_array( $ufavs ) ? count( $ufavs ) : 0;
2255
+
2256
+ return $pfc;
2257
+ }
2258
+
2259
+ /**
2260
+ * @param $ID
2261
+ * @param $emails
2262
+ * @param string $names
2263
+ * @param int $usageLimit
2264
+ * @param int $expireTime
2265
+ *
2266
+ * @usage mail package link to specified email address
2267
+ * @since 4.7.4
2268
+ */
2269
+ static function emailDownloadLink( $ID, $emails, $names = '', $usageLimit = 3, $expireTime = 604800 ) {
2270
+ if ( ! is_array( $emails ) ) {
2271
+ $emails = explode( ",", $emails );
2272
+ }
2273
+ if ( ! is_array( $names ) ) {
2274
+ $names = explode( ",", $names );
2275
+ }
2276
+ $title = get_the_title( $ID );
2277
+ $banner = get_the_post_thumbnail_url( $ID, array( 600, 400 ) );
2278
+ $logo = get_site_icon_url();
2279
+ foreach ( $emails as $index => $email ) {
2280
+ $download_link = WPDM()->package->expirableDownloadLink( $ID, $usageLimit, $expireTime );
2281
+ $download_page_link = WPDM()->package->expirableDownloadPage( $ID, $usageLimit, $expireTime );
2282
+ $params = array(
2283
+ 'to_email' => $email,
2284
+ 'name' => isset( $names[ $index ] ) ? $names[ $index ] : '',
2285
+ 'package_name' => $title,
2286
+ 'download_url' => $download_link,
2287
+ 'download_page_url' => $download_page_link,
2288
+ 'img_logo' => $logo,
2289
+ 'banner' => $banner
2290
+ );
2291
+ \WPDM\__\Email::send( "email-lock", $params );
2292
+ }
2293
+ }
2294
+
2295
+ /**
2296
+ * Check if specified link or page template have the tag
2297
+ *
2298
+ * @param null $template
2299
+ * @param $tag
2300
+ *
2301
+ * @return bool|string
2302
+ */
2303
+ static function templateHasTag( $template = null, $tag = '' ) {
2304
+ if ( ! $template ) {
2305
+ return true;
2306
+ } else if ( is_string( $tag ) ) {
2307
+ return substr_count( $template, "[{$tag}]" );
2308
+ } else if ( is_array( $tag ) ) {
2309
+ foreach ( $tag as $t ) {
2310
+ if ( substr_count( $template, "[{$t}]" ) ) {
2311
+ return true;
2312
+ }
2313
+ }
2314
+ }
2315
+
2316
+ return false;
2317
+
2318
+ }
2319
+
2320
+ /**
2321
+ * Returns package icon
2322
+ *
2323
+ * @param $ID
2324
+ *
2325
+ * @return string
2326
+ */
2327
+ static function icon( $ID, $html = false, $class = '' ) {
2328
+ $icon = get_post_meta( $ID, '__wpdm_icon', true );
2329
+ if ( $icon == '' ) {
2330
+ $file_types = WPDM()->package->fileTypes( $ID, false );
2331
+ if ( count( $file_types ) ) {
2332
+ if ( count( $file_types ) == 1 ) {
2333
+ $tmpavar = $file_types;
2334
+ $ext = $tmpvar = array_shift( $tmpavar );
2335
+ } else {
2336
+ $ext = 'zip';
2337
+ }
2338
+ } else {
2339
+ $ext = "unknown";
2340
+ }
2341
+ if ( $ext === '' ) {
2342
+ $ext = 'wpdm';
2343
+ }
2344
+ $icon = FileSystem::fileTypeIcon( $ext );
2345
+ }
2346
+ if ( $html ) {
2347
+ $icon = "<img src='{$icon}' alt='Icon' class='$class' />";
2348
+ }
2349
+
2350
+ return apply_filters( "wpdm_package_icon", $icon, $ID );
2351
+ }
2352
+
2353
+ /**
2354
+ * Create a copy of a given package
2355
+ *
2356
+ * @param $ID
2357
+ *
2358
+ * @return int|\WP_Error
2359
+ */
2360
+ static function copy( $ID, $author = null, $new_meta = array() ) {
2361
+ $old_pack = (array) get_post( $ID );
2362
+ $package = array(
2363
+ 'post_title' => $old_pack['post_title'],
2364
+ 'post_content' => $old_pack['post_content'],
2365
+ 'post_status' => $old_pack['post_status'],
2366
+ 'comment_status' => $old_pack['comment_status'],
2367
+ 'ping_status' => $old_pack['ping_status'],
2368
+ 'post_type' => 'wpdmpro'
2369
+ );
2370
+
2371
+ if ( $author ) {
2372
+ $package['post_author'] = $author;
2373
+ }
2374
+ $new_ID = wp_insert_post( $package );
2375
+
2376
+ $meta = get_post_meta( $ID );
2377
+
2378
+ foreach ( $meta as $key => $value ) {
2379
+ foreach ( $value as $v ) {
2380
+ update_post_meta( $new_ID, $key, maybe_unserialize( $v ) );
2381
+ }
2382
+ }
2383
+ if ( is_array( $new_meta ) ) {
2384
+ foreach ( $new_meta as $key => $value ) {
2385
+ update_post_meta( $new_ID, $key, maybe_unserialize( $value ) );
2386
+ }
2387
+ }
2388
+
2389
+ return $new_ID;
2390
+ }
2391
+
2392
+ static function dummy() {
2393
+ $package = array(
2394
+ 'post_title' => __( 'Sample Package', 'download-manager' ),
2395
+ 'post_content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. ',
2396
+ 'excerpt' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s',
2397
+ 'post_status' => 'publish',
2398
+ 'download_link' => '<a href="#">Download</a>',
2399
+ 'download_link_extended' => '<a href="#">Download</a>',
2400
+ );
2401
+
2402
+ return $package;
2403
+ }
2404
+
2405
+ /**
2406
+ * @param null $ID
2407
+ *
2408
+ * @return array Additional preview image urls
2409
+ */
2410
+ function additionalPreviews( $ID = null ) {
2411
+ $ID = $ID ? $ID : $this->ID;
2412
+ if ( ! $ID && is_singular( 'wpdmpro' ) ) {
2413
+ $ID = get_the_ID();
2414
+ }
2415
+ if ( ! $ID ) {
2416
+ return array();
2417
+ }
2418
+ $additional_previews = get_post_meta( $ID, '__wpdm_additional_previews', true );
2419
+ $previews = array();
2420
+ foreach ( $additional_previews as $media_id ) {
2421
+ $previews[] = wp_get_attachment_url( $media_id );
2422
+ }
2423
+
2424
+ return $previews;
2425
+ }
2426
+
2427
+ function getThumbnail( $ID, $FILEID, $size ) {
2428
+ if ( ! $this->files ) {
2429
+ $this->files = self::getFiles( $ID, true );
2430
+ }
2431
+ $file = wpdm_valueof( $this->files, $FILEID );
2432
+
2433
+ $imgext = array( 'png', 'jpg', 'jpeg', 'gif' );
2434
+ $ext = FileSystem::fileExt( $file );
2435
+ $thumb = '';
2436
+ $abspath = WPDM()->package->locateFile( $file );
2437
+
2438
+ if ( in_array( $ext, $imgext ) && $abspath ) {
2439
+ $thumb = FileSystem::imageThumbnail( $abspath, $size[0], $size[1], WPDM_USE_GLOBAL, true );
2440
+ } else if ( $ext === 'svg' ) {
2441
+ $thumb = str_replace( ABSPATH, home_url( '/' ), $file );
2442
+ } else if ( strtolower( $ext ) === 'pdf' && class_exists( 'Imagick' ) ) {
2443
+ $thumb = FileSystem::pdfThumbnail( $file, md5( $file ) );
2444
+ } else {
2445
+ $thumb = FileSystem::fileTypeIcon( $ext );
2446
+ }
2447
+
2448
+ return apply_filters( "wpdm_file_thumbnail", $thumb, [
2449
+ 'file' => $file,
2450
+ 'FILEID' => $FILEID,
2451
+ 'ID' => $ID,
2452
+ 'size' => $size
2453
+ ] );
2454
+ }
2455
+
2456
+ function locateFile( $file ) {
2457
+ if ( file_exists( $file ) ) {
2458
+ return $file;
2459
+ }
2460
+ if ( file_exists( UPLOAD_DIR . $file ) ) {
2461
+ return UPLOAD_DIR . $file;
2462
+ }
2463
+
2464
+ return false;
2465
+ }
2466
+
2467
+ function addViewCount() {
2468
+ if ( isset( $_REQUEST['__wpdm_view_count'] ) && wp_verify_nonce( $_REQUEST['__wpdm_view_count'], NONCE_KEY ) ) {
2469
+
2470
+ $id = (int) ( $_REQUEST['id'] );
2471
+ $views = (int) get_post_meta( $id, '__wpdm_view_count', true );
2472
+ update_post_meta( $id, '__wpdm_view_count', $views + 1 );
2473
+ wp_send_json( [ 'views' => $views + 1 ] );
2474
+ }
2475
+ }
2476
+
2477
+
2478
+ /**
2479
+ * @usage Find similar packages
2480
+ *
2481
+ * @param null $package_id
2482
+ * @param int $count
2483
+ * @param bool|true $html
2484
+ *
2485
+ * @return array|bool|string
2486
+ */
2487
+ function similarPackages( $package_id = null, $count = 5, $html = true ) {
2488
+ $id = $package_id ? $package_id : get_the_ID();
2489
+ if ( is_array( $package_id ) ) {
2490
+ $id = $package_id['ID'];
2491
+ }
2492
+ $tags = wp_get_post_terms( $id, 'wpdmtag' );
2493
+ $cats = wp_get_post_terms( $id, 'wpdmcategory' );
2494
+ $posts = array();
2495
+ if ( $tags ) {
2496
+ $tag_ids = array();
2497
+ foreach ( $tags as $individual_tag ) {
2498
+ $tag_ids[] = $individual_tag->term_id;
2499
+ }
2500
+ foreach ( $cats as $individual_cat ) {
2501
+ $cat_ids[] = $individual_cat->term_id;
2502
+ }
2503
+ $args = array(
2504
+ 'post_type' => 'wpdmpro',
2505
+ 'tax_query' => [
2506
+ [
2507
+ 'taxonomy' => 'wpdmtag',
2508
+ 'field' => 'id',
2509
+ 'terms' => $tag_ids,
2510
+ 'operator' => 'IN'
2511
+ ],
2512
+ [
2513
+ 'taxonomy' => 'wpdmcategory',
2514
+ 'field' => 'id',
2515
+ 'terms' => $cat_ids,
2516
+ 'operator' => 'IN'
2517
+ ],
2518
+ 'relation' => 'OR'
2519
+ ],
2520
+ 'post__not_in' => array( $id ),
2521
+ 'posts_per_page' => $count
2522
+ );
2523
+
2524
+ $posts = get_posts( $args );
2525
+
2526
+ if ( ! $html ) {
2527
+ return $posts;
2528
+ }
2529
+
2530
+ $html = "";
2531
+ //Filter hook to change related packages/downloads template
2532
+ $template = apply_filters( "wpdm_replated_package_template", "link-template-panel.php", $package_id );
2533
+ $cols = apply_filters( "wpdm_replated_package_columns", 6, $package_id );
2534
+ foreach ( $posts as $p ) {
2535
+
2536
+ $package['ID'] = $p->ID;
2537
+ $package['post_title'] = $p->post_title;
2538
+ $package['post_content'] = $p->post_content;
2539
+ $package['post_excerpt'] = $p->post_excerpt;
2540
+ $html .= "<div class='col-md-{$cols}'>" . wpdm_fetch_template( $template, $package, 'link' ) . "</div>";
2541
+
2542
+ }
2543
+ }
2544
+ if ( count( $posts ) == 0 ) {
2545
+ $html = "<div class='col-md-12'><div class='alert alert-info'>" . __( "No related download found!", "download-manager" ) . "</div> </div>";
2546
+ }
2547
+ $html = "<div class='w3eden'><div class='row'>" . $html . "</div></div>";
2548
+ wp_reset_query();
2549
+
2550
+ return $html;
2551
+ }
2552
+
2553
+ function search( $keyword = '' ) {
2554
+ $keyword = wpdm_query_var( 'search' ) ?: $keyword;
2555
+ if ( $keyword ) {
2556
+ $query = new Query();
2557
+ $query->search( $keyword );
2558
+ if ( wpdm_query_var( 'premium', 'int' ) > 0 ) {
2559
+ $query->meta( '__wpdm_base_price', 0, '>' );
2560
+ $query->meta_relation( 'AND' );
2561
+ }
2562
+ $query->process();
2563
+ $packages = $query->packages();
2564
+ foreach ( $packages as &$package ) {
2565
+ $package = (object) [
2566
+ 'ID' => $package->ID,
2567
+ 'post_title' => $package->post_title,
2568
+ 'post_content' => $package->post_content
2569
+ ];
2570
+ }
2571
+ if ( wpdm_query_var( 'premium', 'int' ) > 0 && function_exists( 'wpdmpp_product_license_options' ) ) {
2572
+ foreach ( $packages as &$package ) {
2573
+ $licenses = wpdmpp_product_license_options( $package->ID );
2574
+ $package->licenses = count( $licenses ) > 0 ? $licenses : null;
2575
+ }
2576
+ }
2577
+ wp_send_json( [ 'total' => $query->count, 'packages' => $packages, 'q' => $query->params ] );
2578
+ }
2579
+ }
2580
 
2581
 
2582
  }
src/__/FileSystem.php CHANGED
@@ -91,7 +91,7 @@ class FileSystem
91
  $fsize = filesize($filepath);
92
  else
93
  $fsize = 0;
94
- $org_size = $fsize;
95
 
96
  nocache_headers();
97
  header("X-Robots-Tag: noindex, nofollow", true);
@@ -116,7 +116,7 @@ class FileSystem
116
  }
117
 
118
  if ((isset($extras['play']) && strpos($_SERVER['HTTP_USER_AGENT'], "Safari")) || get_option('__wpdm_download_resume', 1) == 2) {
119
- header("Content-Length: " . $fsize);
120
  header("Content-disposition: attachment;filename=\"{$filename}\"");
121
  TempStorage::set("download." . wpdm_get_client_ip(), 1, 60);
122
  readfile($filepath);
@@ -136,17 +136,17 @@ class FileSystem
136
  $new_length = $fsize - $set_pointer;
137
 
138
  header("Accept-Ranges: bytes");
139
- header("Accept-Ranges: 0-$fsize");
140
  $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
141
  header("{$proto} 206 Partial Content");
142
 
143
  header("Content-Length: $new_length");
144
- header("Content-Range: bytes $http_range-$fsize/$org_size");
145
 
146
  fseek($file, $set_pointer);
147
 
148
  } else {
149
- header("Content-Length: " . $fsize);
150
  }
151
  $packet = 1;
152
 
@@ -181,6 +181,7 @@ class FileSystem
181
 
182
  }
183
 
 
184
  /**
185
  * @usage Download any content as a file
186
  * @param $filename
91
  $fsize = filesize($filepath);
92
  else
93
  $fsize = 0;
94
+ $orig_size = $fsize;
95
 
96
  nocache_headers();
97
  header("X-Robots-Tag: noindex, nofollow", true);
116
  }
117
 
118
  if ((isset($extras['play']) && strpos($_SERVER['HTTP_USER_AGENT'], "Safari")) || get_option('__wpdm_download_resume', 1) == 2) {
119
+ header("Content-Length: " . $orig_size);
120
  header("Content-disposition: attachment;filename=\"{$filename}\"");
121
  TempStorage::set("download." . wpdm_get_client_ip(), 1, 60);
122
  readfile($filepath);
136
  $new_length = $fsize - $set_pointer;
137
 
138
  header("Accept-Ranges: bytes");
139
+ header("Accept-Ranges: 0-".($orig_size - 1));
140
  $proto = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
141
  header("{$proto} 206 Partial Content");
142
 
143
  header("Content-Length: $new_length");
144
+ header("Content-Range: bytes $set_pointer-".($fsize-1)."/$orig_size");
145
 
146
  fseek($file, $set_pointer);
147
 
148
  } else {
149
+ header("Content-Length: " . $orig_size);
150
  }
151
  $packet = 1;
152
 
181
 
182
  }
183
 
184
+
185
  /**
186
  * @usage Download any content as a file
187
  * @param $filename
src/wpdm-functions.php CHANGED
@@ -130,11 +130,8 @@ function wpdm_package_size($id)
130
  */
131
  function wpdm_file_size($file)
132
  {
133
- if (file_exists($file))
134
- $size = filesize($file);
135
- else if (file_exists(UPLOAD_DIR . $file))
136
- $size = filesize(UPLOAD_DIR . $file);
137
- else $size = 0;
138
  $size = $size / 1024;
139
  if ($size > 1024) $size = number_format($size / 1024, 2) . ' MB';
140
  else $size = number_format($size, 2) . ' KB';
130
  */
131
  function wpdm_file_size($file)
132
  {
133
+ $file = WPDM()->fileSystem->absPath($file);
134
+ $size = filesize($file);
 
 
 
135
  $size = $size / 1024;
136
  if ($size > 1024) $size = number_format($size / 1024, 2) . ' MB';
137
  else $size = number_format($size, 2) . ' KB';
src/wpdm-start-download.php CHANGED
@@ -110,6 +110,7 @@ else {
110
  }*/
111
 
112
  $filepath = WPDM()->fileSystem->absPath($indfile, $package['ID']);
 
113
  if(!$filepath)
114
  Messages::fullPage('Error!', "<div class='card bg-danger text-white p-4'>" . __("Sorry! File not found!", "download-manager") . "</div>", 'error');
115
  //$plock = get_wpdm_meta($file['id'],'password_lock',true);
110
  }*/
111
 
112
  $filepath = WPDM()->fileSystem->absPath($indfile, $package['ID']);
113
+
114
  if(!$filepath)
115
  Messages::fullPage('Error!', "<div class='card bg-danger text-white p-4'>" . __("Sorry! File not found!", "download-manager") . "</div>", 'error');
116
  //$plock = get_wpdm_meta($file['id'],'password_lock',true);