Skip to content

Instantly share code, notes, and snippets.

@jeffward3283
Created January 20, 2015 11:37
Show Gist options
  • Save jeffward3283/74e02aaf1edf8f178257 to your computer and use it in GitHub Desktop.
Save jeffward3283/74e02aaf1edf8f178257 to your computer and use it in GitHub Desktop.
<?php
/////////////////////////////////////////////////////////////////
# Create a Simple JSON Based Download Counter with WordPress
/////////////////////////////////////////////////////////////////
// Extending from:
// http://www.kavoir.com/2010/05/simplest-php-hit-counter-or-download-counter-count-the-number-of-times-of-access-visits-or-downloads.html
function download_count_actions($id = null, $step = 'get', $file = null){
# Note for newbies: Because we know what we're doing, we will prepend the file functions with the at sign (@) to supress known PHP errors
# Set the parameters
$id = (!empty($id)) ? $id : 0;
$file = (!empty($file)) ? $file : TEMPLATEPATH . '/download-counter.json';
# Parse the data
if(file_exists( $file )){
$file_contents = @file_get_contents($file);
if(!empty($file_contents)){
$json_data = (object) json_decode($file_contents);
} else {
$json_data = (object) array();
}
$hit_count = $json_data->$id;
}
$hit_count = (!empty($hit_count)) ? $hit_count : 0;
# Loop through the actions
switch($step){
# Grab All Counts
case 'get_all':
return $json_data;
break;
# Add to the count
case 'add':
$hit_count++;
break;
# Subtract from the count
case 'subtract':
$hit_count--;
$hit_count = ($hit_count < 0) ? 0 : $hit_count;
break;
# Reset the count
case 'reset':
$hit_count = 0;
break;
# Delete All Counts
case 'delete':
@unlink( $file );
return false;
break;
}
# Save the file
if(in_array($step, array('add', 'subtract', 'reset'))){
$json_data->$id = $hit_count;
$file_contents = json_encode($json_data);
@file_put_contents($file, $file_contents);
}
# Return the count
return $hit_count;
}
//////////////////////////
# Get Download Counts
//////////////////////////
function get_download_counts($file = null){
return download_count_actions($id = null, $step = 'get_all', $file);
}
//////////////////////////
# Get Download Count
//////////////////////////
function get_download_count($id = null, $file = null){
return download_count_actions($id, $step = 'get', $file);
}
//////////////////////////
# Add Download Count
//////////////////////////
function add_download_count($id = null, $file = null){
return download_count_actions($id, $step = 'add', $file);
}
////////////////////////////
# Subtract Download Count
////////////////////////////
function subtract_download_count($id = null, $file = null){
return download_count_actions($id, $step = 'subtract', $file);
}
////////////////////////////
# Reset Download Count
////////////////////////////
function reset_download_count($id = null, $file = null){
return download_count_actions($id, $step = 'reset', $file);
}
//////////////////////////
# Delete All Counts
//////////////////////////
function delete_download_counts($file = null){
return download_count_actions($id = null, $step = 'delete', $file);
}
?>
<?php
//////////////////////////
# Download Example
//////////////////////////
add_action('init', 'test_download_count');
function test_download_count(){
////////////////////////////
# Grab all download counts
////////////////////////////
/*
$download_counts = get_download_counts();
echo '<pre>';
print_r( $download_counts );
echo '</pre>';
$download_id = 14;
$download_count = $download_counts->$download_id;
echo sprintf('<br />Download count for ID %s is %s', $download_id, $download_count);
exit();
*/
////////////////////////////
# Grab the download count
////////////////////////////
/*
$download_id = 14;
$download_count = get_download_count( $download_id );
echo sprintf('<br />Download count for ID %s is %s', $download_id, $download_count);
exit();
*/
////////////////////////////////
# Add multiple counts for testing
////////////////////////////////
/*
$array = array(14,21,313,14,2,11,14,4521);
foreach($array as $download_id){
add_download_count( $download_id );
}
exit();
*/
////////////////////////////////
# Delete the download count file
////////////////////////////////
/*
delete_download_counts();
exit();
*/
////////////////////////////
# Add the download count
////////////////////////////
/*
$download_id = 1234;
add_download_count( $download_id );
# Redirect to the real file to be downloaded
header('Location: http://www.example.com/download.zip');
exit();
*/
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment