eta( $id, '__wpdm_favs', true ) );
$pfc = is_array( $ufavs ) ? count( $ufavs ) : 0;
return $pfc;
}
/**
* @param $ID
* @param $emails
* @param string $names
* @param int $usageLimit
* @param int $expireTime
*
* @usage mail package link to specified email address
* @since 4.7.4
*/
static function emailDownloadLink( $ID, $emails, $names = '', $usageLimit = 3, $expireTime = 604800 ) {
if ( ! is_array( $emails ) ) {
$emails = explode( ",", $emails );
}
if ( ! is_array( $names ) ) {
$names = explode( ",", $names );
}
$title = get_the_title( $ID );
$banner = get_the_post_thumbnail_url( $ID, array( 600, 400 ) );
$logo = get_site_icon_url();
foreach ( $emails as $index => $email ) {
$download_link = WPDM()->package->expirableDownloadLink( $ID, $usageLimit, $expireTime );
$download_page_link = WPDM()->package->expirableDownloadPage( $ID, $usageLimit, $expireTime );
$params = array(
'to_email' => $email,
'name' => isset( $names[ $index ] ) ? $names[ $index ] : '',
'package_name' => $title,
'download_url' => $download_link,
'download_page_url' => $download_page_link,
'img_logo' => $logo,
'banner' => $banner
);
\WPDM\__\Email::send( "email-lock", $params );
}
}
/**
* Check if specified link or page template have the tag
*
* @param null $template
* @param $tag
*
* @return bool|string
*/
static function templateHasTag( $template = null, $tag = '' ) {
if ( ! $template ) {
return true;
} else if ( is_string( $tag ) ) {
return substr_count( $template, "[{$tag}]" );
} else if ( is_array( $tag ) ) {
foreach ( $tag as $t ) {
if ( substr_count( $template, "[{$t}]" ) ) {
return true;
}
}
}
return false;
}
/**
* Returns package icon
*
* @param $ID
*
* @return string
*/
static function icon( $ID, $html = false, $class = '' ) {
$icon = get_post_meta( $ID, '__wpdm_icon', true );
if ( $icon == '' ) {
$file_types = WPDM()->package->fileTypes( $ID, false );
if ( count( $file_types ) ) {
if ( count( $file_types ) == 1 ) {
$tmpavar = $file_types;
$ext = $tmpvar = array_shift( $tmpavar );
} else {
$ext = 'zip';
}
} else {
$ext = "unknown";
}
if ( $ext === '' ) {
$ext = 'wpdm';
}
$icon = FileSystem::fileTypeIcon( $ext );
}
if ( $html ) {
$icon = "";
}
return apply_filters( "wpdm_package_icon", $icon, $ID );
}
/**
* Create a copy of a given package
*
* @param $ID
*
* @return int|\WP_Error
*/
static function copy( $ID, $author = null, $new_meta = array() ) {
$old_pack = (array) get_post( $ID );
$package = array(
'post_title' => $old_pack['post_title'],
'post_content' => $old_pack['post_content'],
'post_status' => $old_pack['post_status'],
'comment_status' => $old_pack['comment_status'],
'ping_status' => $old_pack['ping_status'],
'post_type' => 'wpdmpro'
);
if ( $author ) {
$package['post_author'] = $author;
}
$new_ID = wp_insert_post( $package );
$meta = get_post_meta( $ID );
foreach ( $meta as $key => $value ) {
foreach ( $value as $v ) {
update_post_meta( $new_ID, $key, maybe_unserialize( $v ) );
}
}
if ( is_array( $new_meta ) ) {
foreach ( $new_meta as $key => $value ) {
update_post_meta( $new_ID, $key, maybe_unserialize( $value ) );
}
}
$terms = get_the_terms( $ID, 'wpdmcategory' );
foreach ( $terms as $term ) {
wp_set_post_terms( $new_ID, (int)$term->term_id, 'wpdmcategory', true );
}
$terms = get_the_terms( $ID, WPDM_TAG );
foreach ( $terms as $term ) {
wp_set_post_terms( $new_ID, $term->name, WPDM_TAG, true );
}
return $new_ID;
}
static function dummy() {
$package = array(
'post_title' => __( 'Sample Package', 'download-manager' ),
'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. ',
'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',
'post_status' => 'publish',
'download_link' => 'Download',
'download_link_extended' => 'Download',
);
return $package;
}
/**
* @param null $ID
*
* @return array Additional preview image urls
*/
function additionalPreviews( $ID = null ) {
$ID = $ID ? $ID : $this->ID;
if ( ! $ID && is_singular( 'wpdmpro' ) ) {
$ID = get_the_ID();
}
if ( ! $ID ) {
return array();
}
$additional_previews = get_post_meta( $ID, '__wpdm_additional_previews', true );
$previews = array();
foreach ( $additional_previews as $media_id ) {
$previews[] = wp_get_attachment_url( $media_id );
}
return $previews;
}
function getThumbnail( $ID, $FILEID, $size ) {
if ( ! $this->files ) {
$this->files = self::getFiles( $ID, true );
}
$file = wpdm_valueof( $this->files, $FILEID );
$imgext = array( 'png', 'jpg', 'jpeg', 'gif' );
$ext = FileSystem::fileExt( $file );
$thumb = '';
$abspath = WPDM()->package->locateFile( $file );
if ( in_array( $ext, $imgext ) && $abspath ) {
$thumb = FileSystem::imageThumbnail( $abspath, $size[0], $size[1], WPDM_USE_GLOBAL, true );
} else if ( $ext === 'svg' ) {
$thumb = str_replace( ABSPATH, home_url( '/' ), $file );
} else if ( strtolower( $ext ) === 'pdf' && class_exists( 'Imagick' ) ) {
$thumb = FileSystem::pdfThumbnail( $file, md5( $file ) );
} else {
$thumb = FileSystem::fileTypeIcon( $ext );
}
return apply_filters( "wpdm_file_thumbnail", $thumb, [
'file' => $file,
'FILEID' => $FILEID,
'ID' => $ID,
'size' => $size
] );
}
function locateFile( $file ) {
if ( file_exists( $file ) ) {
return $file;
}
if ( file_exists( UPLOAD_DIR . $file ) ) {
return UPLOAD_DIR . $file;
}
return false;
}
function addViewCount() {
//__::isAuthentic( '__wpdm_view_count', NONCE_KEY, 'read', false );
$id = (int) ( $_REQUEST['id'] );
$views = (int) get_post_meta( $id, '__wpdm_view_count', true );
update_post_meta( $id, '__wpdm_view_count', $views + 1 );
wp_send_json( [ 'views' => $views + 1 ] );
}
/**
* @usage Find similar packages
*
* @param null $package_id
* @param int $count
* @param bool|true $html
*
* @return array|bool|string
*/
function similarPackages( $package_id = null, $count = 5, $html = true ) {
$id = $package_id ? $package_id : get_the_ID();
if ( is_array( $package_id ) ) {
$id = $package_id['ID'];
}
$tags = wp_get_post_terms( $id, WPDM_TAG );
$cats = wp_get_post_terms( $id, 'wpdmcategory' );
$posts = array();
if ( $tags ) {
$tag_ids = array();
foreach ( $tags as $individual_tag ) {
$tag_ids[] = $individual_tag->term_id;
}
foreach ( $cats as $individual_cat ) {
$cat_ids[] = $individual_cat->term_id;
}
$args = array(
'post_type' => 'wpdmpro',
'tax_query' => [
[
'taxonomy' => WPDM_TAG,
'field' => 'id',
'terms' => $tag_ids,
'operator' => 'IN'
],
[
'taxonomy' => 'wpdmcategory',
'field' => 'id',
'terms' => $cat_ids,
'operator' => 'IN'
],
'relation' => 'OR'
],
'post__not_in' => array( $id ),
'posts_per_page' => $count
);
$posts = get_posts( $args );
if ( ! $html ) {
return $posts;
}
$html = "";
//Filter hook to change related packages/downloads template
$template = apply_filters( "wpdm_replated_package_template", "link-template-panel.php", $package_id );
$cols = apply_filters( "wpdm_replated_package_columns", 6, $package_id );
foreach ( $posts as $p ) {
$package['ID'] = $p->ID;
$package['post_title'] = $p->post_title;
$package['post_content'] = $p->post_content;
$package['post_excerpt'] = $p->post_excerpt;
$html .= "