| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Access CAP Times. |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\CCAPS |
| 16 |
* @since 140514 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit ('Do not access this file directly.'); |
| 20 |
|
| 21 |
if(!class_exists('c_ws_plugin__s2member_access_cap_times')) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Access CAP Times. |
| 25 |
* |
| 26 |
* @package s2Member\CCAPS |
| 27 |
* @since 140514 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_access_cap_times |
| 30 |
{ |
| 31 |
/** |
| 32 |
* @var string Current log time increment. |
| 33 |
*/ |
| 34 |
protected static $log_time = NULL; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array Previous array of user CAPS. |
| 38 |
* For internal use only. |
| 39 |
*/ |
| 40 |
protected static $prev_caps_by_user = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Get user caps before udpate. |
| 44 |
* |
| 45 |
* @package s2Member\CCAPS |
| 46 |
* @since 140514 |
| 47 |
* |
| 48 |
* @attaches-to ``add_action('add_user_meta')`` (indirectly) |
| 49 |
* @attaches-to ``add_action('update_user_meta')`` |
| 50 |
* |
| 51 |
* @param integer $meta_id Meta row ID in database. |
| 52 |
* @param integer $object_id User ID. |
| 53 |
* @param string $meta_key Meta key. |
| 54 |
* @param mixed $meta_value Meta value. |
| 55 |
*/ |
| 56 |
public static function get_user_caps_before_update($meta_id, $object_id, $meta_key, $meta_value) |
| 57 |
{ |
| 58 |
$wpdb = $GLOBALS['wpdb']; |
| 59 |
/** @var $wpdb \wpdb For IDEs. */ |
| 60 |
|
| 61 |
if(strpos($meta_key, 'capabilities') === FALSE || $meta_key !== $wpdb->get_blog_prefix().'capabilities') |
| 62 |
return; // Not updating caps. |
| 63 |
|
| 64 |
$user_id = (int)$object_id; |
| 65 |
$user = new WP_User($user_id); |
| 66 |
if(!$user->ID || !$user->exists()) |
| 67 |
return; // Not a valid user. |
| 68 |
|
| 69 |
self::$prev_caps_by_user[$user_id] = $user->caps; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get user caps before udpate. |
| 74 |
* |
| 75 |
* @package s2Member\CCAPS |
| 76 |
* @since 140514 |
| 77 |
* |
| 78 |
* @attaches-to ``add_action('add_user_meta')`` |
| 79 |
* |
| 80 |
* @param integer $object_id User ID. |
| 81 |
* @param string $meta_key Meta key. |
| 82 |
* @param mixed $meta_value Meta value. |
| 83 |
*/ |
| 84 |
public static function get_user_caps_before_update_on_add($object_id, $meta_key, $meta_value) |
| 85 |
{ |
| 86 |
self::get_user_caps_before_update(0, $object_id, $meta_key, $meta_value); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Logs access capability times. |
| 91 |
* |
| 92 |
* @package s2Member\CCAPS |
| 93 |
* @since 140514 |
| 94 |
* |
| 95 |
* @attaches-to ``add_action('added_user_meta')`` |
| 96 |
* @attaches-to ``add_action('updated_user_meta')`` |
| 97 |
* @attaches-to ``add_action('deleted_user_meta')`` (indirectly) |
| 98 |
* |
| 99 |
* @param integer $meta_id Meta row ID in database. |
| 100 |
* @param integer $object_id User ID. |
| 101 |
* @param string $meta_key Meta key. |
| 102 |
* @param mixed $meta_value Meta value. |
| 103 |
*/ |
| 104 |
public static function log_access_cap_times($meta_id, $object_id, $meta_key, $meta_value) |
| 105 |
{ |
| 106 |
$wpdb = $GLOBALS['wpdb']; |
| 107 |
/** @var $wpdb \wpdb For IDEs. */ |
| 108 |
|
| 109 |
if(strpos($meta_key, 'capabilities') === FALSE || $meta_key !== $wpdb->get_blog_prefix().'capabilities') |
| 110 |
return; // Not updating caps. |
| 111 |
|
| 112 |
$user_id = (int)$object_id; |
| 113 |
$user = new WP_User($user_id); |
| 114 |
if(!$user->ID || !$user->exists()) |
| 115 |
return; // Not a valid user. |
| 116 |
|
| 117 |
$caps['prev'] = !empty(self::$prev_caps_by_user[$user_id]) ? self::$prev_caps_by_user[$user_id] : array(); |
| 118 |
self::$prev_caps_by_user = array(); // Reset this in case `get_user_caps_before_update()` doesn't run somehow. |
| 119 |
$caps['now'] = is_array($meta_value) ? $meta_value : array(); |
| 120 |
$role_objects = $GLOBALS['wp_roles']->role_objects; |
| 121 |
|
| 122 |
foreach($caps as &$_caps_prev_now) |
| 123 |
{ |
| 124 |
foreach(array_intersect(array_keys($_caps_prev_now), array_keys($role_objects)) as $_role) |
| 125 |
if($_caps_prev_now[$_role]) // If the cap (i.e., the role) is enabled; merge its caps. |
| 126 |
$_caps_prev_now = array_merge($_caps_prev_now, $role_objects[$_role]->capabilities); |
| 127 |
|
| 128 |
$_s2_caps_prev_now = array(); |
| 129 |
foreach($_caps_prev_now as $_cap => $_enabled) |
| 130 |
if(strpos($_cap, 'access_s2member_') === 0) |
| 131 |
$_s2_caps_prev_now[substr($_cap, 16)] = $_enabled; |
| 132 |
$_caps_prev_now = $_s2_caps_prev_now; |
| 133 |
} |
| 134 |
unset($_s2_caps_prev_now, $_caps_prev_now, $_role, $_cap, $_enabled); |
| 135 |
|
| 136 |
$ac_times = get_user_option('s2member_access_cap_times', $user_id); |
| 137 |
if(!is_array($ac_times)) $ac_times = array(); |
| 138 |
|
| 139 |
if(!isset(self::$log_time)) |
| 140 |
self::$log_time = (float)time(); |
| 141 |
|
| 142 |
foreach($caps['prev'] as $_cap => $_was_enabled) |
| 143 |
if($_was_enabled && empty($caps['now'][$_cap])) |
| 144 |
$ac_times[number_format((self::$log_time += .0001), 4, '.', '')] = '-'.$_cap; |
| 145 |
unset($_cap, $_was_enabled); |
| 146 |
|
| 147 |
foreach($caps['now'] as $_cap => $_now_enabled) |
| 148 |
if($_now_enabled && empty($caps['prev'][$_cap])) |
| 149 |
$ac_times[number_format((self::$log_time += .0001), 4, '.', '')] = $_cap; |
| 150 |
unset($_cap, $_now_enabled); |
| 151 |
|
| 152 |
update_user_option($user_id, 's2member_access_cap_times', $ac_times); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Logs access capability times. |
| 157 |
* |
| 158 |
* @package s2Member\CCAPS |
| 159 |
* @since 140514 |
| 160 |
* |
| 161 |
* @attaches-to ``add_action('deleted_user_meta')`` |
| 162 |
* |
| 163 |
* @param array $meta_ids Meta row ID in database. |
| 164 |
* @param integer $object_id User ID. |
| 165 |
* @param string $meta_key Meta key. |
| 166 |
*/ |
| 167 |
public static function log_access_cap_times_on_delete($meta_ids, $object_id, $meta_key) |
| 168 |
{ |
| 169 |
$wpdb = $GLOBALS['wpdb']; |
| 170 |
/** @var $wpdb \wpdb For IDEs. */ |
| 171 |
|
| 172 |
if(strpos($meta_key, 'capabilities') === FALSE || $meta_key !== $wpdb->get_blog_prefix().'capabilities') |
| 173 |
return; // Not updating caps. |
| 174 |
|
| 175 |
if(!is_array($meta_ids) || !$meta_ids) |
| 176 |
return; // Nothing to do. |
| 177 |
|
| 178 |
if(count($meta_ids) > 50) |
| 179 |
if(function_exists('set_time_limit')) |
| 180 |
@set_time_limit(900); |
| 181 |
|
| 182 |
$user_ids = $wpdb->get_col("SELECT DISTINCT `user_id` FROM `".$wpdb->usermeta."` WHERE `umeta_id` IN('".implode("','", $meta_ids)."')"); |
| 183 |
|
| 184 |
if(count($user_ids) > 50) |
| 185 |
if(function_exists('set_time_limit')) |
| 186 |
@set_time_limit(900); |
| 187 |
|
| 188 |
foreach($user_ids as $_user_id) |
| 189 |
self::log_access_cap_times(0, $_user_id, $meta_key, array()); |
| 190 |
unset($_user_id); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Gets access capability times. |
| 195 |
* |
| 196 |
* @package s2Member\CCAPS |
| 197 |
* @since 140514 |
| 198 |
* |
| 199 |
* @param integer $user_id WP User ID. |
| 200 |
* @param array $access_caps Optional. If not passed, this returns all times for all caps. |
| 201 |
* If passed, please pass an array of specific access capabilities to get the times for. |
| 202 |
* If removal times are desired, you should add a `-` prefix. |
| 203 |
* e.g., `array('ccap_music','level2','-ccap_video')` |
| 204 |
* |
| 205 |
* @return array An array of all access capability times. |
| 206 |
* Keys are UTC timestamps (w/ microtime precision), values are the capabilities (including `-` prefixed removals). |
| 207 |
* e.g., `array('1234567890.0001' => 'ccap_music', '1234567890.0002' => 'level2', '1234567890.0003' => '-ccap_video')` |
| 208 |
*/ |
| 209 |
public static function get_access_cap_times($user_id, $access_caps = array()) |
| 210 |
{ |
| 211 |
$ac_times = array(); |
| 212 |
if(($user_id = (int)$user_id)) |
| 213 |
{ |
| 214 |
$ac_times = get_user_option('s2member_access_cap_times', $user_id); |
| 215 |
if(!is_array($ac_times)) $ac_times = array(); |
| 216 |
|
| 217 |
/* ------- Begin back compat. with `s2member_paid_registration_times`. */ |
| 218 |
|
| 219 |
// $update_ac_times = empty($ac_times) ? FALSE : TRUE; |
| 220 |
$ac_times_min = !empty($ac_times) ? min(array_keys($ac_times)) : 0; |
| 221 |
if(($r_time = c_ws_plugin__s2member_registration_times::registration_time($user_id)) && (empty($ac_times_min) || $r_time < $ac_times_min)) |
| 222 |
$ac_times[number_format(($r_time += .0001), 4, '.', '')] = 'level0'; |
| 223 |
|
| 224 |
if(is_array($pr_times = get_user_option('s2member_paid_registration_times', $user_id))) |
| 225 |
{ |
| 226 |
$role_objects = $GLOBALS['wp_roles']->role_objects; |
| 227 |
foreach($pr_times as $_level => $_time) |
| 228 |
if(isset($role_objects['s2member_'.$_level]) && (empty($ac_times_min) || $_time < $ac_times_min)) |
| 229 |
foreach(array_keys($role_objects['s2member_'.$_level]->capabilities) as $_cap) |
| 230 |
if(strpos($_cap, 'access_s2member_') === 0) |
| 231 |
$ac_times[number_format(($_time += .0001), 4, '.', '')] = substr($_cap, 16); |
| 232 |
unset($_level, $_time, $_cap); |
| 233 |
} |
| 234 |
/* ------- End back compat. with `s2member_paid_registration_times`. */ |
| 235 |
|
| 236 |
if($access_caps) |
| 237 |
$ac_times = array_intersect($ac_times, (array)$access_caps); |
| 238 |
|
| 239 |
ksort($ac_times, SORT_NUMERIC); |
| 240 |
|
| 241 |
//if($update_ac_times) |
| 242 |
// update_user_option($user_id, 's2member_access_cap_times', $ac_times); |
| 243 |
} |
| 244 |
return apply_filters('ws_plugin__s2member_get_access_cap_times', $ac_times, get_defined_vars()); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|