Community Support Forums — WordPress® ( Users Helping Users ) — 2011-12-02T17:05:27-05:00 http://www.primothemes.com/forums/feed.php?f=4&t=14084 2011-12-02T17:05:27-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54446#p54446 <![CDATA[Re: counting downloads]]> Thanks for the follow-up.

Right, Members at Level 4 would need access to Custom Capabilities in order to download files that are inside Custom Capability sub-directories. However, s2Member also supports Membership Level sub-directories. So files placed here would be available to Level 4 Members, and only to Level 4 Members.
/s2member-files/access-s2member-level4/file.mov


If all else fails, you might consider implementing your own counter using a Hook into s2Member's existing routines. For instance, you might do something like this.

Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
( these are MUST USE plugins, see: http://codex.wordpress.org/Must_Use_Plugins )
Code:
<?php
add_action 
("ws_plugin__s2member_during_file_download_access", "my_s2_file_download_counts");
function my_s2_file_download_counts ($vars = array ())
    {
        if ($vars["serving"] && $vars["valid_file_download_key"] && ($file = $vars["req"]["file_download"]))
            if (is_user_logged_in () && is_object ($user = wp_get_current_user ()) && !empty ($user->ID) && ($user_id = $user->ID))
                {
                    $current_download_counts = (array)get_user_option ("my_s2_file_download_counts", $user_id);
                    update_user_option ($user_id, "my_s2_file_download_counts", (int)@$current_download_counts[$file] + 1);
                }
    }
function my_s2_file_download_count_for ($file = FALSE, $user_id = FALSE)
    {
        if ($file && ($user_id || (is_object ($user = wp_get_current_user ()) && !empty ($user->ID) && ($user_id = $user->ID))))
            {
                $current_download_counts = (array)get_user_option ("my_s2_file_download_counts", $user_id);
                return (int)@$current_download_counts[$file];
            }
        return /* Default return value. */ 0;
    }
?>

With this file in place, you now have your own API Function for download counts.
Code:
<?php echo my_s2_file_download_count_for("access-s2member-ccap-post345/file.mov"); # Returns the count for this file, for the current User. ?>
Or, to find the count of a specific file, for a specific User's ID.
Code:
<?php echo my_s2_file_download_count_for("access-s2member-ccap-post345/file.mov", ($user_id = 123)); # Returns the count for this file, for a specific User. ?>
s2-hacks.zip

Statistics: Posted by Jason Caldwell — December 2nd, 2011, 5:05 pm


]]>
2011-12-02T07:53:18-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54390#p54390 <![CDATA[Re: counting downloads]]>
If I use ccap folders as you've explained, will my Level 4 members be able to access those files? Or would they need to have that folder's ccap, too?

The ccaps are dynamically inserted into a auto-generated paypal button available on each post, so in theory there are infinite numbers of ccaps because there could be an infinite number of posts created. In practice, there will probably be 20-50 or so. Creating all of those folders might be manageable in order to solve the download counting dilemma, but only if Level 4 members can access those files, too, without needing the ccaps in their account. If they can't, then that kills it.

Statistics: Posted by kennymcnett — December 2nd, 2011, 7:53 am


]]>
2011-12-02T06:46:23-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54388#p54388 <![CDATA[Re: counting downloads]]> Thank you for clarifying.

OK. So you really need the ability to use s2Member's API Functions for counting downloads, but since you're using a Download Key, that's not possible, given the current limitations in this regard. As I understand it, the only reason you're using a File Download Key right now, is to prevent link sharing among members that may not have a specific Custom Capability related to a specific Post/Page.

Just to clarify this ...
s2member-files/videos/file.mov
This file WOULD be available to anyone that meets your Basic Download Restrictions. Although it's in a sub-directory, it's NOT inside a special Custom Capability sub-directory. If you wanted to ensure that this file could ONLY be downloaded by a Member with a specific Custom Capability, you would upload it here:
s2member-files/access-s2member-ccap-videos/file.mov

Likewise, if you wanted to make a file ONLY available to a Member with Custom Capability post345, you would place the file here: s2member-files/access-s2member-ccap-post345/file.mov.

For further details on this, please check your Dashboard, under:
s2Member -> API Scripting -> Custom Capability and Member Level Files.

So, in your case, I would recommend creating sub-directories for each of the Custom Capabilities that you plan to sell, and then place files inside these Custom Capability sub-directories, so that link sharing is ONLY possible between one or more Members that both have this specific Custom Capability ( e.g. there is no security issue here, because the file is always going to require that a Member is logged in with a specific Custom Capability ). And, this will allow you to use s2Member's standard linking methods, as opposed to using a Download Key, which is prohibiting you from enjoying the benefits of counting downloads with s2Member's API Functions.

For example, upload your file here:
s2member-files/access-s2member-ccap-post345/file.mov
A link might look like this:
/?s2member_file_download=access-s2member-ccap-post345/file.mov

I suppose the next question is... how many different Custom Capabilities are you going to sell? Is it going to be practical to create Custom Capability sub-directories like this for each of them? If you're only selling 5-10 different Posts/Pages, I'd say it's well worth it, even if you have to duplicate certain files into some of these sub-directories to accomplish everything you need to.

On the other hand, if you're selling hundreds of different Custom Capabilities, you might get a headache with this ( i.e. too much work to create all of those sub-directories for each Custom Capability ). In that case, you might try to consolidate some of your most important Custom Capabilities a bit. For instance, you might sell Custom Capabilities "downloads_a", "downloads_b", "downloads_c". In other words, try to group them together a bit further to prevent the need to create a brand new Custom Capability sub-directory for each specific purchase. You could still sell Custom Capability post345, but it might also include access to "downloads_a". Anyway, just an idea.

Statistics: Posted by Jason Caldwell — December 2nd, 2011, 6:46 am


]]>
2011-12-02T06:16:30-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54386#p54386 <![CDATA[Re: counting downloads]]>
When a Level 0 user buys access to a post, they get a ccap equivalent to the post ID (e.g., post345). With that ccap, or if they are a Level 4 member, they can see the links in the post that were previously hidden (I do this by conditionally toggling the display of the content below the "More..." tag of the post).

Those links are to files in several different subfolders of s2member-files. For example
Link #1: s2member-files/videos/file.mov
Link #2: s2memberfiles/posters/file.jpg

The user does NOT have the ccaps of either 'videos' or 'posters'.

In testing, I was putting all of my downloadable files in the root of s2member-files, but it seemed that any logged-in user that had the direct URL to the file could access it. That's why I switched to using the Advanced Download Key--to obfuscate the direct URL in order to prevent link sharing among registered users who haven't paid for access to the post that houses those links.

Essentially, I aimed to lock down all of the files from any sort of user, and then use the Advanced Download Key to grant one-time access...because that was the only way I could figure out how to do it.

Happy to give more clarification if needed.

Thank you!

Statistics: Posted by kennymcnett — December 2nd, 2011, 6:16 am


]]>
2011-12-02T04:35:42-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54384#p54384 <![CDATA[Re: counting downloads]]> Thanks for the follow-up.

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.
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.

Statistics: Posted by Jason Caldwell — December 2nd, 2011, 4:35 am


]]>
2011-12-02T04:08:02-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54381#p54381 <![CDATA[Re: counting downloads]]>

Maybe I'm missing something in my setup that would allow me to avoid using an Advanced Download Key?

Here's what I'm doing:

Each post has one or more links to files located in several subfolders of /s2member-files/. (For example, s2member-files/videos)

Users purchase access to a post on a per-post basis via a ccap buy now button (which dynamically assigns the ccap based on the post ID), or users can purchase a 1-year sitewide access via a level 4 membership buy now button.

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.

So, to grant access to the files, I am using an Advanced Download Key because it allows the user to access the file regardless of the restrictions.

Not sure if that scenario is clear, but I hope so. Any ideas?

I can DM you a user login to the dev site if that helps.

Thanks a million!

Statistics: Posted by kennymcnett — December 2nd, 2011, 4:08 am


]]>
2011-12-02T03:46:59-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54378#p54378 <![CDATA[Re: counting downloads]]>
With a Download Key, not yet.

The latest release of s2Member v111105 did introduce some new API Functions for counting downloads though, so you might want to have a look at each these documents.

viewtopic.php?f=40&t=12453&src_doc_v=111105#src_doc_s2member_total_downloads_of%28%29
viewtopic.php?f=40&t=12453&src_doc_v=111105#src_doc_s2member_user_downloads%28%29
viewtopic.php?f=40&t=12453&src_doc_v=111105#src_doc_s2member_total_unique_downloads_of%28%29

Calculations returned by these functions do NOT include File Downloads that were accessed with an Advanced File Download Key.

Statistics: Posted by Jason Caldwell — December 2nd, 2011, 3:46 am


]]>
2011-12-01T12:32:11-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=54323#p54323 <![CDATA[Re: counting downloads]]>
If not, is there any way to do that? Inside my posts, I use the short code:
Code:
[s2File download="example.zip" download_key="true" /]


I need to display next to that link the number of times the current user has downloaded that file. Or, at the very least, whether the current user has downloaded the file previously (no count, just a boolean toggle).

Ideas?

Thank you!

Statistics: Posted by kennymcnett — December 1st, 2011, 12:32 pm


]]>
2011-10-27T12:29:02-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48983#p48983 <![CDATA[Re: counting downloads]]> Statistics: Posted by cassel — October 27th, 2011, 12:29 pm


]]>
2011-10-27T12:14:45-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48982#p48982 <![CDATA[Re: counting downloads]]>
Jason Caldwell wrote:
Something like this maybe.
Code:
<?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.
            }
    }
?>



Thanks Jason, that helped a lot, I now have a short-code that displays current and past files and the total downloads in the header.

Statistics: Posted by colinp386 — October 27th, 2011, 12:14 pm


]]>
2011-10-26T18:22:18-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48907#p48907 <![CDATA[Re: counting downloads]]> Statistics: Posted by kennymcnett — October 26th, 2011, 6:22 pm


]]>
2011-10-26T18:13:37-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48906#p48906 <![CDATA[Re: counting downloads]]>
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.
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.

Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php ( these are MUST USE plugins, that's what you want ).
Reference article: http://codex.wordpress.org/Must_Use_Plugins
Code:
<?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;
    }
?>
* Requires s2Member v111017+ for this to work as expected.

Now, with this file in place, you can do something like this:
Code:
<?php echo my_total_user_downloads_of("example-file.zip"); // For the current User. ?>
Or, for a specific User, instead of for the currently logged-in User:
Code:
<?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. */
?>

Statistics: Posted by Jason Caldwell — October 26th, 2011, 6:13 pm


]]>
2011-10-26T17:36:53-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48904#p48904 <![CDATA[Re: counting downloads]]>

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.

Statistics: Posted by kennymcnett — October 26th, 2011, 5:36 pm


]]>
2011-10-26T17:32:12-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48903#p48903 <![CDATA[Re: counting downloads]]>
lmllewellyn wrote:
As I asked earlier, is there a way to count the total number of downloads (but unique users) per file?
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.

That being said, you could do something like this, which would give you the total number of Users/Members that have downloaded a particular file. 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 ).

Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php ( these are MUST USE plugins, that's what you want ).
Reference article: http://codex.wordpress.org/Must_Use_Plugins
Code:
<?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;
    }
?>
Now, with this custom function in place, you could do something like this:
Code:
<?php echo s2_total_user_downloads_of("example-file.zip"); ?>

Statistics: Posted by Jason Caldwell — October 26th, 2011, 5:32 pm


]]>
2011-10-26T16:31:12-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48896#p48896 <![CDATA[Re: counting downloads]]> Statistics: Posted by lmllewellyn — October 26th, 2011, 4:31 pm


]]>
2011-10-26T16:17:39-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48894#p48894 <![CDATA[Re: counting downloads]]>
Code:
<?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.
            }
    }
?>

Statistics: Posted by Jason Caldwell — October 26th, 2011, 4:17 pm


]]>
2011-10-26T15:39:14-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48887#p48887 <![CDATA[Re: counting downloads]]>
Thanks again for the info on the function.
popped it into a shortcode that I placed into themes functions.php
Code:
/*
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);
}


And it works fine, however as you note, you get all whole array of data.
Any tips on parsing out just the Date and File?

Cheers

Colin

Statistics: Posted by colinp386 — October 26th, 2011, 3:39 pm


]]>
2011-10-21T11:25:46-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=48504#p48504 <![CDATA[Re: counting downloads]]>
It seems that s2 only tracks unique downloads, but I need to count total downloads of each file, per user.

Statistics: Posted by kennymcnett — October 21st, 2011, 11:25 am


]]>
2011-10-15T10:30:10-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=46903#p46903 <![CDATA[Re: counting downloads]]> Statistics: Posted by lmllewellyn — October 15th, 2011, 10:30 am


]]>
2011-10-07T19:20:05-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=45079#p45079 <![CDATA[Re: counting downloads]]> Statistics: Posted by Jason Caldwell — October 7th, 2011, 7:20 pm


]]>
2011-10-07T08:15:41-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=45044#p45044 <![CDATA[Re: counting downloads]]>
I know you are working very hard on improving the plugin. Some features are:
    mandatory
    obligatory
    optional
    de luxe ones

A nice stat feature would be probably in the optional category or in the "de luxe" one

Anyway, I am suggesting that a stat feature be added somewhere on the line. I know there is an API but let's say I am on a dummy side for all php "stuff".

Maybe in 2012!!! :geek:


Roger

Statistics: Posted by guardian — October 7th, 2011, 8:15 am


]]>
2011-10-03T23:17:12-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=44771#p44771 <![CDATA[Re: counting downloads]]>
Very helpful, I will go and play tomorrow!
I did find the API constants in the end, and that got me part the way there :-)

Kudos for the great plugin, once we start to make revenue we will be upgrading and sending you some $$ :-)

Statistics: Posted by colinp386 — October 3rd, 2011, 11:17 pm


]]>
2011-10-03T22:33:41-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=44762#p44762 <![CDATA[Re: counting downloads]]> Yes, s2Member keeps a log for each Member.

Code:
<?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. */
?>
The array contains one element for each unique file downloaded, and it includes the date the file was downloaded, and the name of the file. This record keeping has existed in s2Member since v1, but I've never gotten around to building a UI panel for this data.

This additional data is also available.
Code:
<?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. */
?>


All of that being said, if you just need basic information, s2Member provides that through API Constants, which are MUCH easier to work with. See: viewtopic.php?f=40&t=12449&src_doc_v=111003#src_doc_S2MEMBER_CURRENT_USER_DOWNLOADS_CURRENTLY

Statistics: Posted by Jason Caldwell — October 3rd, 2011, 10:33 pm


]]>
2011-09-29T20:03:30-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=41943#p41943 <![CDATA[Re: counting downloads]]> I am using S2member to authenticate and control subscription billing.
I have just started experimenting with the download levels to control the number of downloads a member can access a month, and yes it's very slick! Kudos! :D

However; I would like to be able to:
1) Show the total number of downloads a user has made in their user profile.
2) an ability to send our admin a warning message when a user has reached and/or exceeded their download quota.

This way we can run a report and either cut folks off manually, or push them to upgrade to a higher download subscription.

The most important is the ability to be able to count the number of download in a given period and/or overall.
You must be doing this anyway as you give the number in the popup when they select the download ;) I was a little surprised not to find it in the user display or did I miss it LoL

Many thanks!

colin

Statistics: Posted by colinp386 — September 29th, 2011, 8:03 pm


]]>
2011-07-11T22:04:06-05:00 http://www.primothemes.com/forums/viewtopic.php?t=14084&p=28165#p28165 <![CDATA[Re: counting downloads]]> Statistics: Posted by Cristián Lávaque — July 11th, 2011, 10:04 pm


]]>