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