Page 2 of 2

Re: counting downloads

PostPosted: December 2nd, 2011, 5:05 pm
by Jason Caldwell
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: Select all
<?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: Select all
<?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: Select all
<?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
(523 Bytes) Downloaded 17 times