downloads = $wpdb->prefix.'downloads'; ### Function: Downloads Administration Menu add_action('admin_menu', 'downloads_menu'); function downloads_menu() { add_menu_page(__('Downloads', 'wp-downloadmanager'), __('Downloads', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-manager.php', '', 'dashicons-download'); add_submenu_page('wp-downloadmanager/download-manager.php', __('Manage Downloads', 'wp-downloadmanager'), __('Manage Downloads', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-manager.php'); add_submenu_page('wp-downloadmanager/download-manager.php', __('Add File', 'wp-downloadmanager'), __('Add File', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-add.php'); add_submenu_page('wp-downloadmanager/download-manager.php', __('Download Options', 'wp-downloadmanager'), __('Download Options', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-options.php'); add_submenu_page('wp-downloadmanager/download-manager.php', __('Download Templates', 'wp-downloadmanager'), __('Download Templates', 'wp-downloadmanager'), 'manage_downloads', 'wp-downloadmanager/download-templates.php'); } ### Function: Enqueue Downloads Stylesheets add_action('wp_enqueue_scripts', 'downloads_stylesheets'); function downloads_stylesheets() { if(@file_exists(TEMPLATEPATH.'/download-css.css')) { wp_enqueue_style('wp-downloadmanager', get_stylesheet_directory_uri().'/download-css.css', false, WP_DOWNLOADMANAGER_VERSION, 'all'); } else { wp_enqueue_style('wp-downloadmanager', plugins_url('wp-downloadmanager/download-css.css'), false, WP_DOWNLOADMANAGER_VERSION, 'all'); } } ### Function: Enqueue Downloads Stylesheets In WP-Admin add_action('admin_enqueue_scripts', 'downloads_stylesheets_admin'); function downloads_stylesheets_admin($hook_suffix) { $downloads_admin_pages = array('wp-downloadmanager/download-manager.php', 'wp-downloadmanager/download-add.php', 'wp-downloadmanager/download-options.php', 'wp-downloadmanager/download-templates.php', 'wp-downloadmanager/download-uninstall.php'); if(in_array($hook_suffix, $downloads_admin_pages)) { wp_enqueue_style('wp-downloadmanager-admin', plugins_url('wp-downloadmanager/download-admin-css.css'), false, WP_DOWNLOADMANAGER_VERSION, 'all'); } } ### Function: Displays Download Manager Footer In WP-Admin add_action('admin_footer-post-new.php', 'downloads_footer_admin'); add_action('admin_footer-post.php', 'downloads_footer_admin'); add_action('admin_footer-page-new.php', 'downloads_footer_admin'); add_action('admin_footer-page.php', 'downloads_footer_admin'); function downloads_footer_admin() { ?> = WordPress 2.5 add_action('init', 'download_tinymce_addbuttons'); function download_tinymce_addbuttons() { if(!current_user_can('edit_posts') && ! current_user_can('edit_pages')) { return; } if(get_user_option('rich_editing') == 'true') { add_filter('mce_external_plugins', 'download_tinymce_addplugin'); add_filter('mce_buttons', 'download_tinymce_registerbutton'); add_filter('wp_mce_translation', 'download_tinymce_translation'); } } function download_tinymce_registerbutton($buttons) { array_push($buttons, 'separator', 'downloadmanager'); return $buttons; } function download_tinymce_addplugin( $plugin_array ) { if( WP_DEBUG ) { $plugin_array['downloadmanager'] = plugins_url( 'wp-downloadmanager/tinymce/plugins/downloadmanager/plugin.js?v=' . WP_DOWNLOADMANAGER_VERSION); } else { $plugin_array['downloadmanager'] = plugins_url( 'wp-downloadmanager/tinymce/plugins/downloadmanager/plugin.min.js?v= ' . WP_DOWNLOADMANAGER_VERSION); } return $plugin_array; } function download_tinymce_translation($mce_translation) { $mce_translation['Enter File ID (Separate Multiple IDs By A Comma)'] = esc_js(__('Enter File ID (Separate Multiple IDs By A Comma)', 'wp-downloadmanager')); $mce_translation['Insert File Download'] = esc_js(__('Insert File Download', 'wp-downloadmanager')); return $mce_translation; } ### Function: Add Download Query Vars add_filter('query_vars', 'download_query_vars'); function download_query_vars($public_query_vars) { $public_query_vars[] = "dl_id"; $public_query_vars[] = "dl_name"; return $public_query_vars; } ### Function: Download htaccess ReWrite Rules add_filter('generate_rewrite_rules', 'download_rewrite'); function download_rewrite($wp_rewrite) { $wp_rewrite->rules = array_merge(array('download/([0-9]{1,})/?$' => 'index.php?dl_id=$matches[1]', 'download/(.*)$' => 'index.php?dl_name=$matches[1]'), $wp_rewrite->rules); } ### Function: Add Download RSS Link To Download Page add_action('wp_head', 'download_rss_link'); function download_rss_link() { if(is_page() && strpos(get_option('download_page_url'), $_SERVER['REQUEST_URI'])) { $download_nice_permalink = (int) get_option('download_nice_permalink'); if($download_nice_permalink == 1) { $download_rss_link = get_option('home').'/download/rss/'; } else { $download_rss_link = get_option('home').'/?dl_name=rss'; } echo ''."\n"; } } ### Function: Download File add_action('template_redirect', 'download_file', 5); function download_file() { global $wpdb, $user_ID; $dl_id = (int) get_query_var('dl_id'); $dl_name = addslashes(get_query_var('dl_name')); $download_options = get_option('download_options'); if($dl_name === 'rss') { load_template(WP_PLUGIN_DIR.'/wp-downloadmanager/download-rss.php'); exit; } if($dl_id > 0 || !empty($dl_name)) { if($dl_id > 0 && $download_options['use_filename'] === 0) { $file = $wpdb->get_row("SELECT file_id, file, file_permission FROM $wpdb->downloads WHERE file_id = $dl_id AND file_permission != -2"); } elseif(!empty($dl_name) && $download_options['use_filename'] == 1) { if(!is_remote_file($dl_name)) { $dl_name = '/'.$dl_name; } $file = $wpdb->get_row("SELECT file_id, file, file_permission FROM $wpdb->downloads WHERE file = \"$dl_name\" AND file_permission != -2"); } if( empty( $file ) ) { header('HTTP/1.0 404 Not Found'); die(__('Invalid File ID or File Name.', 'wp-downloadmanager')); } $file_path = stripslashes(get_option('download_path')); $file_url = stripslashes(get_option('download_path_url')); $download_method = (int) get_option('download_method'); $file_id = (int) $file->file_id; $file_name = stripslashes($file->file); $file_permission = (int) $file->file_permission; $current_user = wp_get_current_user(); if( $file_permission === -1 || ( $file_permission === 0 && (int) $user_ID > 0 ) || ($file_permission > 0 && get_wp_user_level() >= $file_permission && (int) $user_ID > 0 ) ) { $update_hits = $wpdb->query("UPDATE $wpdb->downloads SET file_hits = (file_hits + 1), file_last_downloaded_date = '".current_time('timestamp')."' WHERE file_id = $file_id AND file_permission != -2"); if(!is_remote_file($file_name)) { if(!is_file($file_path.$file_name)) { header('HTTP/1.0 404 Not Found'); die(__('File does not exist.', 'wp-downloadmanager')); } if($download_method === 0) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=".basename($file_name).";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file_path.$file_name)); @readfile($file_path.$file_name); } else { header('Location: '.$file_url.$file_name); } exit(); } else { if(ini_get('allow_url_fopen') && $download_method == 0) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=".basename($file_name).";"); header("Content-Transfer-Encoding: binary"); $file_size = remote_filesize($file_name); if($file_size !== __('unknown', 'wp-downloadmanager')) { header("Content-Length: ".$file_size); } @readfile($file_name); } else { header('Location: '.$file_name); } exit(); } } else { _e('You do not have permission to download this file.', 'wp-downloadmanager'); exit(); } } } ### Function: Print Out File Extension Image function file_extension_image( $file_name, $file_ext_images ) { $file_ext = file_extension( $file_name ) . '.gif'; $file_extension_image = 'unknown.gif'; if( in_array( $file_ext, $file_ext_images, true ) ) { $file_extension_image = $file_ext; } return apply_filters( 'wp_downloadmanager_file_extension_image', $file_extension_image, $file_ext, $file_name ); } ### Function: Get File Extension Images function file_extension_images() { $file_ext_images = array(); $dir = apply_filters( 'wp_downloadmanager_file_extension_images_path', WP_PLUGIN_DIR . '/wp-downloadmanager/images/ext' ); if ( is_dir( $dir ) ) { if ( $dh = opendir( $dir ) ) { while ( ( $file = readdir( $dh ) ) !== false ) { if( $file != '.' && $file != '..' ) { $file_ext_images[] = $file; } } closedir( $dh ); } } return $file_ext_images; } ### Function: Get File Extension if ( ! function_exists( 'file_extension' ) ) { function file_extension( $filename ) { $file_ext = explode( '.', $filename ); $file_ext = $file_ext[ sizeof( $file_ext ) - 1 ]; $file_ext = strtolower( $file_ext ); return $file_ext; } } ### Function: Get Remote File Size if(!function_exists('remote_filesize')) { function remote_filesize($uri) { $header_array = @get_headers($uri, 1); $file_size = $header_array['Content-Length']; if(!empty($file_size)) { return $file_size; } else { return __('unknown', 'wp-downloadmanager'); } } } ### Function: Format Bytes Into TiB/GiB/MiB/KiB/Bytes if ( ! function_exists( 'format_filesize' ) ) { function format_filesize($rawSize) { $rawSize = (int) $rawSize; if ( $rawSize / 1099511627776 > 1 ) { return number_format_i18n( $rawSize/1099511627776, 1 ) . ' ' . __( 'TiB', 'wp-downloadmanager' ); } elseif ( $rawSize / 1073741824 > 1 ) { return number_format_i18n( $rawSize/1073741824, 1 ) . ' ' . __( 'GiB', 'wp-downloadmanager' ); } elseif ( $rawSize / 1048576 > 1 ) { return number_format_i18n( $rawSize/1048576, 1 ) . ' ' . __( 'MiB', 'wp-downloadmanager' ); } elseif ( $rawSize / 1024 > 1 ) { return number_format_i18n( $rawSize/1024, 1 ) . ' ' . __( 'KiB', 'wp-downloadmanager' ); } elseif ( $rawSize > 1 ) { return number_format_i18n( $rawSize ) . ' ' . __( 'bytes', 'wp-downloadmanager' ); } else { return __( 'unknown', 'wp-downloadmanager' ); } } } ### Function: Format Bytes Into TB/GB/MB/KB/Bytes if ( ! function_exists( 'format_filesize_dec' ) ) { function format_filesize_dec( $rawSize ) { $rawSize = (int) $rawSize; if( $rawSize / 1000000000000 > 1 ) { return number_format_i18n( $rawSize/1000000000000, 1 ) . ' ' . __( 'TB', 'wp-downloadmanager' ); } elseif ( $rawSize / 1000000000 > 1 ) { return number_format_i18n( $rawSize/1000000000, 1 ) .' ' . __( 'GB', 'wp-downloadmanager' ); } elseif ( $rawSize / 1000000 > 1 ) { return number_format_i18n( $rawSize/1000000, 1 ) . ' ' . __( 'MB', 'wp-downloadmanager' ); } elseif ( $rawSize / 1000 > 1 ) { return number_format_i18n( $rawSize/1000, 1 ) . ' ' . __( 'KB', 'wp-downloadmanager' ); } elseif ( $rawSize > 1 ) { return number_format_i18n( $rawSize ) . ' ' . __( 'bytes', 'wp-downloadmanager' ); } else { return __( 'unknown', 'wp-downloadmanager' ); } } } ### Function: Get Max File Size That Can Be Uploaded function get_max_upload_size() { $maxsize = ini_get('upload_max_filesize'); if (!is_numeric($maxsize)) { if (strpos($maxsize, 'M') !== false) { $maxsize = (int) $maxsize * 1024 * 1024; } elseif (strpos($maxsize, 'K') !== false) { $maxsize = (int) $maxsize * 1024; } elseif (strpos($maxsize, 'G') !== false) { $maxsize = (int) $maxsize * 1024 * 1024 * 1024; } } return $maxsize; } ### Function: Is Remote File function is_remote_file($file_name) { if(strpos($file_name, 'http://') === false && strpos($file_name, 'https://') === false && strpos($file_name, 'ftp://') === false) { return false; } return true; } ### Function: Snippet Text if(!function_exists('snippet_text')) { function snippet_text($text, $length = 0) { if (defined('MB_OVERLOAD_STRING')) { $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); if (mb_strlen($text) > $length) { return htmlentities(mb_substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; } else { return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); } } else { $text = @html_entity_decode($text, ENT_QUOTES, get_option('blog_charset')); if (strlen($text) > $length) { return htmlentities(substr($text,0,$length), ENT_COMPAT, get_option('blog_charset')).'...'; } else { return htmlentities($text, ENT_COMPAT, get_option('blog_charset')); } } } } ### Function: Download URL function download_file_url($file_id, $file_name) { $file_id = (int) $file_id; $file_name = stripslashes($file_name); if(!is_remote_file($file_name)) { $file_name = substr($file_name, 1); } $download_options = get_option('download_options'); $download_use_filename = (int) $download_options['use_filename']; $download_nice_permalink = (int) get_option('download_nice_permalink'); if( $download_nice_permalink === 1 ) { if( $download_use_filename === 1 ) { $download_file_url = get_option('home').'/download/'.$file_name; } else { $download_file_url = get_option('home').'/download/'.$file_id.'/'; } } else { if( $download_use_filename === 1 ) { $download_file_url = get_option('home').'/?dl_name='.$file_name; } else { $download_file_url = get_option('home').'/?dl_id='.$file_id; } } return $download_file_url; } ### Function: Download Category URL function download_category_url( $cat_id ) { return get_option( 'download_page_url' ) . '?' . http_build_query( array_merge( $_GET, array( 'dl_cat' => $cat_id ) ) ); } ### Function: Download Page Link function download_page_link( $page ) { return parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) . '?' . http_build_query( array_merge( $_GET, array( 'dl_page' => $page ) ) ); } ### Function Highlight Download Search function download_search_highlight( $search_word, $search_text ) { if( ! empty( $search_word ) ) { $search_words_array = explode( ' ', $search_word ); foreach( $search_words_array as $search_word_array ) { $search_text = preg_replace( "/\w*?$search_word_array\w*/i", '$0', $search_text ); } } return $search_text; } ### Function: Short Code For Inserting Downloads Page Into Page add_shortcode( 'page_download', 'download_page_shortcode' ); add_shortcode( 'page_downloads', 'download_page_shortcode' ); function download_page_shortcode( $atts ) { $attributes = shortcode_atts( array( 'category' => 0 ), $atts ); return downloads_page( $attributes['category'] ); } ### Function: Short Code For Inserting Files Download Into Posts add_shortcode( 'download', 'download_shortcode' ); function download_shortcode( $atts ) { $attributes = shortcode_atts( array( 'id' => 0, 'category' => 0, 'display' => 'both', 'sort_by' => 'file_id', 'sort_order' => 'asc', 'stream_limit' => 0 ), $atts ); if(!is_feed()) { $conditions = array(); $id = $attributes['id']; $category = $attributes['category']; // To maintain backward compatibility with [download=1]. if( ! $id && ! empty( $atts[0] ) ) { $id = trim( $atts[0], '="\'' ); } if( $id !== 0 ) { if( strpos($id, ',') !== false ) { $ids = array_map( 'intval', explode( ',', $id ) ); $conditions[] = 'file_id IN (' . implode( ',', $ids ) . ')'; } else { $conditions[] = 'file_id = ' . (int) $id; } } if( $category !== 0 ) { if( strpos( $category, ',' ) !== false ) { $categories = array_map( 'intval', explode( ',', $category ) ); $conditions[] = 'file_category IN (' . implode( ',', $categories ) . ')'; } else { $conditions[] = 'file_category = ' . (int) $category; } } if( $conditions ) { return download_embedded( implode( ' AND ', $conditions ), $attributes['display'], $attributes['sort_by'], $attributes['sort_order'], $attributes['stream_limit'] ); } return ''; } return __( 'Note: There is a file embedded within this post, please visit this post to download the file.', 'wp-downloadmanager' ); } ### Function: Downloads Page function downloads_page($category_id = 0) { global $wpdb, $user_ID; // Variables $category_id = (int) $category_id; $category = ! empty( $_GET['dl_cat'] ) ? (int) $_GET['dl_cat'] : 0; $page = ! empty( $_GET['dl_page'] ) ? (int) $_GET['dl_page'] : 0; $search_word = ! empty( $_GET['dl_search'] ) ? strip_tags( addslashes( trim( $_GET['dl_search'] ) ) ) : ''; $search_words_array = array(); $search = stripslashes($search_word); $download_categories = get_option('download_categories'); $download_categories[0] = __('total', 'wp-downloadmanager'); $category_stats = array(); $total_stats = array('files' => 0, 'size' => 0, 'hits' => 0); $file_sort = get_option('download_sort'); if ( $file_sort['by'] === 'file_date' ) { $file_sort['by'] = 'FROM_UNIXTIME(file_date)'; } $file_extensions_images = file_extension_images(); $current_user = wp_get_current_user(); // If There Is Category Set $category_sql = ''; if($category === 0 && $category_id > 0) { $category = $category_id; } if($category > 0) { $category_sql = "AND file_category = $category"; } // If There Is A Search Term $search_sql = ''; if(!empty($search)) { $search_words_array = explode(' ', $search_word); foreach($search_words_array as $search_word_array) { $search_sql .= " AND ((file_name LIKE('%$search_word_array%') OR file_des LIKE ('%$search_word_array%') OR file LIKE ('%$search_word_array%')))"; } } // Calculate Categories And Total Stats $categories = $wpdb->get_results("SELECT file_category, COUNT(file_id) as category_files, SUM(file_size) category_size, SUM(file_hits) as category_hits FROM $wpdb->downloads WHERE 1=1 $category_sql $search_sql AND file_permission != -2 GROUP BY file_category"); if($categories) { foreach($categories as $cat) { $cat_id = (int) $cat->file_category; $category_stats[$cat_id]['files'] = $cat->category_files; $category_stats[$cat_id]['hits'] = $cat->category_hits; $category_stats[$cat_id]['size'] = $cat->category_size; $total_stats['files'] +=$cat->category_files; $total_stats['hits'] += $cat->category_hits; $total_stats['size'] += $cat->category_size; } } // Calculate Paging $numposts = $total_stats['files']; $perpage = $file_sort['perpage']; $max_page = ceil($numposts/$perpage); if(empty($page) || $page === 0) { $page = 1; } $offset = ($page-1) * $perpage; $pages_to_show = 10; $pages_to_show_minus_1 = $pages_to_show-1; $half_page_start = floor($pages_to_show_minus_1/2); $half_page_end = ceil($pages_to_show_minus_1/2); $start_page = $page - $half_page_start; if($start_page <= 0) { $start_page = 1; } $end_page = $page + $half_page_end; if(($end_page - $start_page) !== $pages_to_show_minus_1) { $end_page = $start_page + $pages_to_show_minus_1; } if($end_page > $max_page) { $start_page = $max_page - $pages_to_show_minus_1; $end_page = $max_page; } if($start_page <= 0) { $start_page = 1; } if(($offset + $perpage) > $numposts) { $max_on_page = $numposts; } else { $max_on_page = ($offset + $perpage); } if (($offset + 1) > ($numposts)) { $display_on_page = $numposts; } else { $display_on_page = ($offset + 1); } // Get Sorting Group $group_sql = ''; if($file_sort['group'] === 1) { $group_sql = 'file_category ASC,'; } // Get Files $output = ''; $files = $wpdb->get_results("SELECT * FROM $wpdb->downloads WHERE 1=1 $category_sql $search_sql AND file_permission != -2 ORDER BY $group_sql {$file_sort['by']} {$file_sort['order']} LIMIT $offset, {$file_sort['perpage']}"); if($files) { // Get Download Page Header $template_download_header = stripslashes(get_option('download_template_header')); if( (int) get_option('download_nice_permalink') === 0 && preg_match('/[\?\&]page_id=(\d+)/i', get_option('download_page_url'), $matches)) { $template_download_header = preg_replace('/(