Save date of ccaps purchase

Hello guys!

My question is this: is there a method to save the date of a purchasing of a custom capabilities?

Only to know what are the users that in a specific period had bought specific ccaps.
Thank you!

1 Like

Hi Antony.

There is a log of access, so the time the user got that ccap is saved, but there isn’t an interface for it yet. See: s2member/src/includes/classes/access-cap-times.inc.php

I have this in my to-do, and have added your vote for this feature.

:slight_smile:

So, If I’ve understood well, I’ve to find in that log the date of the purchase of that user.
Right?

You would look for the user’s times for that access you’re interested in.

/**
 * Gets access capability times.
 *
 * @package s2Member\CCAPS
 * @since 140514
 *
 * @param integer $user_id WP User ID.
 * @param array   $access_caps Optional. If not passed, this returns all times for all caps.
 *    If passed, please pass an array of specific access capabilities to get the times for.
 *    If removal times are desired, you should add a `-` prefix.
 *    e.g., `array('ccap_music','level2','-ccap_video')`
 *
 * @return array An array of all access capability times.
 *    Keys are UTC timestamps (w/ microtime precision), values are the capabilities (including `-` prefixed removals).
 *    e.g., `array('1234567890.0001' => 'ccap_music', '1234567890.0002' => 'level2', '1234567890.0003' => '-ccap_video')`
 */
public static function get_access_cap_times($user_id, $access_caps = array())
{
	$ac_times = array();
	if(($user_id = (integer)$user_id))
	{
		$ac_times = get_user_option('s2member_access_cap_times', $user_id);
		if(!is_array($ac_times)) $ac_times = array();

		/* ------- Begin back compat. with `s2member_paid_registration_times`. */

		// $update_ac_times = empty($ac_times) ? FALSE : TRUE;
		$ac_times_min = !empty($ac_times) ? min(array_keys($ac_times)) : 0;
		if(($r_time = c_ws_plugin__s2member_registration_times::registration_time($user_id)) && (empty($ac_times_min) || $r_time < $ac_times_min))
			$ac_times[number_format(($r_time += .0001), 4, '.', '')] = 'level0';

		if(is_array($pr_times = get_user_option('s2member_paid_registration_times', $user_id)))
		{
			$role_objects = $GLOBALS['wp_roles']->role_objects;
			foreach($pr_times as $_level => $_time)
				if(isset($role_objects['s2member_'.$_level]) && (empty($ac_times_min) || $_time < $ac_times_min))
					foreach(array_keys($role_objects['s2member_'.$_level]->capabilities) as $_cap)
						if(strpos($_cap, 'access_s2member_') === 0)
							$ac_times[number_format(($_time += .0001), 4, '.', '')] = substr($_cap, 16);
			unset($_level, $_time, $_cap);
		}
		/* ------- End back compat. with `s2member_paid_registration_times`. */

		if($access_caps)
			$ac_times = array_intersect($ac_times, (array)$access_caps);

		ksort($ac_times, SORT_NUMERIC);

		//if($update_ac_times)
		//	update_user_option($user_id, 's2member_access_cap_times', $ac_times);
	}
	return apply_filters('ws_plugin__s2member_get_access_cap_times', $ac_times, get_defined_vars());
}

You’ll see there that the cap times are stored separately for each user. So to get users with a certain access in a time period, you’d need to get a full list of the users, query the cap times for each, and then filter them by time range.