Skip to content

Instantly share code, notes, and snippets.

@flowtwo
Last active December 23, 2019 22:23
Show Gist options
  • Save flowtwo/89bf25828391c2a4bc4a to your computer and use it in GitHub Desktop.
Save flowtwo/89bf25828391c2a4bc4a to your computer and use it in GitHub Desktop.
Count downloads of files from the WordPress Media Library
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $download_base_dir;
$download_base_dir = dirname(__FILE__);
global $download_prefix;
$download_prefix = 'download_';
function gr_download_init() {
add_meta_box( 'gr_download_shortcode_box', __( 'Insert a download link', 'green' ), 'gr_download_meta_box_shortcode', 'attachment', 'side', 'high' );
}
function gr_download_meta_box_shortcode() {
global $post;
echo '<p>'. __('To automatically count downloads, please use this shortcode:', 'green') .'</p>';
echo '<p><span class="showcase-code">[download id="'. $post->ID .'"]'. __('Click here to download', 'green') .'[/download]</span>';
echo '</p>';
}
add_action('admin_init', 'gr_download_init');
function gr_download_create_field($form_fields, $post) {
global $post;
if ( is_object( $post ) ) {
$form_fields['download-download-count'] = array(
'label' => __('Downloads', 'green'),
'input' => 'text',
'value' => get_post_meta( $post->ID ,'_download-download-count', TRUE ),
);
}
return $form_fields;
}
add_filter("attachment_fields_to_edit", "gr_download_create_field", null, 2);
function gr_download_save_field($post, $attachment) {
if( isset($attachment['download-download-count']) ){
$attach = sanitize_text_field( $attachment['download-download-count'] );
update_post_meta( $post['ID'], '_download-download-count', $attach );
}
return $post;
}
add_filter('attachment_fields_to_save', 'gr_download_save_field', null , 2);
function gr_download_shortcode($atts, $content = null) {
extract(shortcode_atts(array(
"id" => ''
), $atts));
return '<a href="'.site_url().'?download='.$id.'">'.$content.'</a>';
}
add_shortcode("download", "gr_download_shortcode");
function gr_download_shortcode_show($atts) {
extract(shortcode_atts(array(
"id" => ''
), $atts));
return get_post_meta( $id, '_download-download-count', TRUE );
}
add_shortcode("download_show", "gr_download_shortcode_show");
if( !is_admin() ) {
if( isset($_GET['download']) AND is_numeric($_GET['download']) ) {
$count = get_post_meta($_GET['download'], '_download-download-count', TRUE);
$count = sanitize_text_field( $count );
update_post_meta($_GET['download'], '_download-download-count', $count+1);
$file = wp_get_attachment_url( $_GET['download'] );
$file_name = basename($file);
if ( isset($file) ) {
$wp_upload_dir = wp_upload_dir();
$current_upload_dir = $wp_upload_dir['path'];
$filepath = $current_upload_dir.'/'.$file_name;
switch(strtolower(substr(strrchr($file_name,'.'),1))) {
case 'pdf': $mime = 'application/pdf'; break;
case 'zip': $mime = 'application/zip'; break;
case 'jpeg': $mime = 'image/jpg'; break;
case 'jpg': $mime = 'image/jpg'; break;
case 'png': $mime = 'image/jpg'; break;
default: $mime = 'application/force-download';
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',FALSE);
header('Content-Type: '. $mime);
header('Content-Disposition: attachment; filename="'. basename($file_name) .'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
$access_type = NULL;
if ( function_exists('get_filesystem_method') ) {
$access_type = get_filesystem_method();
}
if ( $access_type === 'direct' ) {
$creds = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, array() );
if ( !WP_Filesystem( $creds ) ) {
return false;
}
global $wp_filesystem;
echo $wp_filesystem->get_contents( $file );
} else {
readfile( $file );
}
exit();
}
}
}
@ajtruckle
Copy link

Hi

I am interested in this code that you have kindly provided and I have some questions:

  1. Will it work with the latest WordPress 5.3.2?
  2. How do I use this code? It doesn't look like it is a plugin. Do I add it to my child theme functions.php?
  3. Where do you see the download counts? In the media library back-end?

Thanks for your clarification.

Andrew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment