PriMoThemes — now s2Member® (official notice)
This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™
<?php
$user_id = 1;
$downloads = get_user_option("s2member_file_download_access_log", $user_id);
print_r($downloads); /* Array of file downloads in the current period of time, as configured by the site owner under Basic Download Restrictions for s2Member. */
?>
<?php
$user_id = 1;
$archived_downloads = get_user_option("s2member_file_download_access_arc", $user_id);
print_r($archived_downloads); /* Array of file downloads for previous periods of time, as configured by the site owner under Basic Download Restrictions for s2Member. */
?>
/*
Shortcode Name: pkg-downloads
Description: if activated, will show the downloaded files for current user.
Author: Colin
Version: 1.0
*/
add_shortcode('pkg-downloads', 'pkg_downloads_shortcode');
function pkg_downloads_shortcode() {
global $user_id;
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
echo 'User ID: ' . $current_user->ID . '<br />';
$user_id = $current_user->ID;
$downloads = get_user_option("s2member_file_download_access_log", $user_id);
echo 'Current Downloads<br />';
print_r($downloads);
echo '<br />';
echo 'Archived Downloads<br />';
$archived_downloads = get_user_option("s2member_file_download_access_arc", $user_id);
print_r($archived_downloads);
}
<?php
$archived_downloads = get_user_option ("s2member_file_download_access_arc", $user_id);
if (is_array ($archived_downloads) && !empty ($archived_downloads))
{
foreach ($archived_downloads as $archived_download)
{
$date = $archived_download["date"];
$file = $archived_download["file"];
// Do something custom here if you like.
}
}
?>
s2Member currently does NOT store information in a site-wide manner for file downloads. That is, s2Member counts each unique file download exactly ONE time per User/Member, and that's how it's stored by s2Member ( i.e. on a per-User basis ), and not collectively.lmllewellyn wrote:As I asked earlier, is there a way to count the total number of downloads (but unique users) per file?
<?php
function s2_total_user_downloads_of ($file, $check_archives_too = TRUE)
{
global $wpdb; /* Global database object reference. */
/**/
$q1 = mysql_query ("SELECT SQL_CALC_FOUND_ROWS DISTINCT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_file_download_access_log'" . (($check_archives_too) ? " OR `meta_key` = '" . $wpdb->prefix . "s2member_file_download_access_arc'" : "") . ") AND `meta_value` REGEXP '.*\"file\";s:[0-9]+:\"" . esc_sql ($file) . "\".*' LIMIT 1", $wpdb->dbh);
$q2 = mysql_query ("SELECT FOUND_ROWS()", $wpdb->dbh);
/**/
$users = (int)mysql_result ($q2, 0);
/**/
mysql_free_result($q2);
mysql_free_result($q1);
/**/
return $users;
}
?>
<?php echo s2_total_user_downloads_of("example-file.zip"); ?>
Remember though, if a particular User/Member downloads the same exact file 10 times, s2Member only counts that as ONE file download ( this is the intended behavior ).
No, not really. But, you CAN handle this yourself using an Action Hook for s2Member's file download processing routine. In other words, you can keep your own count if you like, and your own count can tally the total number of times a file is downloaded, period. That is, this would NOT be limited to counting each file only ONE time. I've provided a code sample below to accomplish this.kennymcnett wrote:Remember though, if a particular User/Member downloads the same exact file 10 times, s2Member only counts that as ONE file download ( this is the intended behavior ).
Any way to skirt this? I need to show how many times a user has downloaded a file. I don't need any restrictions, just the number.
<?php
add_action ("ws_plugin__s2member_during_file_download_access", "my_s2_file_download_counts");
function my_s2_file_download_counts ($vars = array ())
{
if (($serving = $vars["serving"]) && ($file = $vars["req"]["file_download"]) && ($user_id = $vars["user_id"]))
{
$counts = (array)get_user_option ("my_s2_file_download_counts", $user_id);
$counts[$file] = (!empty ($counts[$file])) ? (int)$counts[$file] + 1 : (int)1;
update_user_option ($user_id, "my_s2_file_download_counts", $counts);
}
}
function my_total_user_downloads_of ($file, $user = FALSE)
{
if (($user = (is_object ($user)) ? $user : ((is_user_logged_in ()) ? wp_get_current_user () : false)) && !empty ($user->ID) && ($user_id = $user->ID))
{
$counts = (array)get_user_option ("my_s2_file_download_counts", $user_id);
$total = (!empty ($counts[$file])) ? (int)$counts[$file] : (int)0;
}
return (!empty ($total)) ? $total : 0;
}
?>
<?php echo my_total_user_downloads_of("example-file.zip"); // For the current User. ?>
<?php
$user = new WP_User("123"); /* For user ID #123 ( i.e. a specific User ). */
echo my_total_user_downloads_of("example-file.zip", $user); /* For a specific User. */
?>
Jason Caldwell wrote:Something like this maybe.
- Code: Select all
<?php
$archived_downloads = get_user_option ("s2member_file_download_access_arc", $user_id);
if (is_array ($archived_downloads) && !empty ($archived_downloads))
{
foreach ($archived_downloads as $archived_download)
{
$date = $archived_download["date"];
$file = $archived_download["file"];
// Do something custom here if you like.
}
}
?>
[s2File download="example.zip" download_key="true" /]
Calculations returned by these functions do NOT include File Downloads that were accessed with an Advanced File Download Key.
Sorry, just want to make sure I understand correctly. Can you elaborate just a bit further on this for me please? Did you say, "This IS good for me", or no? It sounds like you might not need the use of a Download Key, but just clarify a bit further so I can be sure please.The conditionals in my php only display the links to the s2member-files if they user has either the post's ccap or a premium membership.
As I understand the security for the s2member-files folder, because the files are in a subfolder, the only users that can access them via a direct url would be users who are logged in and possess a ccap equal to the subfolder name (e.g., 'videos'). This is good for me, because I don't want users sharing links with each other.
Users browsing this forum: Exabot [Bot], Google [Bot] and 2 guests