/wp-content/plugins/achievements/includes/users/options.php

https://github.com/livinglab/openlab · PHP · 359 lines · 127 code · 61 blank · 171 comment · 41 complexity · 30f63feaa4a0f9ad602ce026d4938b7f MD5 · raw file

  1. <?php
  2. /**
  3. * Achievements user options
  4. *
  5. * @package Achievements
  6. * @subpackage UserOptions
  7. */
  8. // Exit if accessed directly
  9. if ( ! defined( 'ABSPATH' ) ) exit;
  10. /**
  11. * Get the default user options and their values
  12. *
  13. * @return array
  14. * @since Achievements (3.0)
  15. */
  16. function dpa_get_default_user_options() {
  17. return apply_filters( 'dpa_get_default_user_options', array(
  18. '_dpa_last_unlocked' => 0, // ID of the last achievement this user unlocked (per site or network)
  19. '_dpa_unlocked_count' => 0, // How many achievements this user has unlocked (per site or network)
  20. '_dpa_points' => 0, // How many points this user has (per site or network)
  21. '_dpa_notifications' => array(), // User notifications (per site or network)
  22. ) );
  23. }
  24. /**
  25. * Add default user options
  26. *
  27. * This is destructive, so existing Achievements user options will be overridden.
  28. *
  29. * @param int $user_id Optional; defaults to current user
  30. * @since Achievements (3.0)
  31. */
  32. function dpa_add_user_options( $user_id = 0 ) {
  33. // Default to current user
  34. if ( empty( $user_id ) && is_user_logged_in() )
  35. $user_id = get_current_user_id();
  36. // No user, bail out
  37. if ( empty( $user_id ) )
  38. return;
  39. // As Achievements can run independently (as well as sitewide) on a multisite, decide where to store the user option
  40. $store_global = is_multisite() && dpa_is_running_networkwide();
  41. // Add default options
  42. foreach ( array_keys( dpa_get_default_user_options() ) as $key => $value )
  43. update_user_option( $user_id, $key, $value, $store_global );
  44. // Allow previously activated plugins to append their own user options.
  45. do_action( 'dpa_add_user_options', $user_id );
  46. }
  47. /**
  48. * Delete default user options
  49. *
  50. * Hooked to dpa_uninstall, it is only called once when Achievements is uninstalled.
  51. * This is destructive, so existing Achievements user options will be destroyed.
  52. *
  53. * @param int $user_id Optional; defaults to current user
  54. * @since Achievements (3.0)
  55. */
  56. function dpa_delete_user_options( $user_id = 0 ) {
  57. // Default to current user
  58. if ( empty( $user_id ) && is_user_logged_in() )
  59. $user_id = get_current_user_id();
  60. // No user, bail out
  61. if ( empty( $user_id ) )
  62. return;
  63. // Delete default options (both per site and per network)
  64. foreach ( array_keys( dpa_get_default_user_options() ) as $key => $value ) {
  65. delete_user_option( $user_id, $key, false );
  66. delete_user_option( $user_id, $key, true );
  67. }
  68. // Allow previously activated plugins to append their own options.
  69. do_action( 'dpa_delete_user_options', $user_id );
  70. }
  71. /**
  72. * Add filters to each Achievement option and allow them to be overloaded from
  73. * inside the achievements()->options array.
  74. *
  75. * @since Achievements (3.0)
  76. */
  77. function dpa_setup_user_option_filters() {
  78. // Add filters to each Achievements option
  79. foreach ( array_keys( dpa_get_default_user_options() ) as $key => $value )
  80. add_filter( 'get_user_option_' . $key, 'dpa_filter_get_user_option', 10, 3 );
  81. // Allow previously activated plugins to append their own options.
  82. do_action( 'dpa_setup_user_option_filters' );
  83. }
  84. /**
  85. * Filter default options and allow them to be overloaded from inside the
  86. * achievements()->user_options array.
  87. *
  88. * @since Achievements (3.0)
  89. * @param bool $value Optional. Fallback value if none found (default is false).
  90. * @param string $option Optional. Option name
  91. * @param WP_User $user Optional. User to get option for
  92. * @return mixed false if not overloaded, mixed if set
  93. */
  94. function dpa_filter_get_user_option( $value = false, $option = '', $user = null ) {
  95. // Check the options global for preset value
  96. if ( isset( $user->ID ) && isset( achievements()->user_options[$user->ID] ) && ! empty( achievements()->user_options[$user->ID][$option] ) )
  97. $value = achievements()->user_options[$user->ID][$option];
  98. return $value;
  99. }
  100. /**
  101. * _dpa_unlocked_count option - user's unlocked achievements count
  102. */
  103. /**
  104. * Update a user's unlocked achievement count
  105. *
  106. * @param int $user_id Optional. User ID to update. Optional, defaults to current logged in user.
  107. * @param int $new_value Optional. The new value.
  108. * @return bool False if no user or failure, true if successful
  109. * @since Achievements (3.0)
  110. */
  111. function dpa_update_user_unlocked_count( $user_id = 0, $new_value = 0 ) {
  112. // Default to current user
  113. if ( empty( $user_id ) && is_user_logged_in() )
  114. $user_id = get_current_user_id();
  115. // No user, bail out
  116. if ( empty( $user_id ) )
  117. return false;
  118. // As Achievements can run independently (as well as sitewide) on a multisite, decide where to store the user option
  119. $store_global = is_multisite() && dpa_is_running_networkwide();
  120. $new_value = apply_filters( 'dpa_update_user_unlocked_count', $new_value, $user_id );
  121. return update_user_option( $user_id, '_dpa_unlocked_count', absint( $new_value ), $store_global );
  122. }
  123. /**
  124. * Output the total of how many achievements that this user has unlocked
  125. *
  126. * @param int $user_id Optional. User ID to retrieve value for
  127. * @since Achievements (3.0)
  128. */
  129. function dpa_user_unlocked_count( $user_id = 0 ) {
  130. echo number_format_i18n( dpa_get_user_unlocked_count( $user_id ) );
  131. }
  132. /**
  133. * Return the total of how many achievements that this user has unlocked
  134. *
  135. * @param int $user_id Optional. User ID to retrieve value for
  136. * @return mixed False if no user, option value otherwise.
  137. * @since Achievements (3.0)
  138. */
  139. function dpa_get_user_unlocked_count( $user_id = 0 ) {
  140. // Default to current user
  141. if ( empty( $user_id ) && is_user_logged_in() )
  142. $user_id = get_current_user_id();
  143. // No user, bail out
  144. if ( empty( $user_id ) )
  145. return false;
  146. $value = get_user_option( '_dpa_unlocked_count', $user_id );
  147. return absint( apply_filters( 'dpa_get_user_unlocked_count', $value, $user_id ) );
  148. }
  149. /**
  150. * _dpa_points option - this user's points total
  151. */
  152. /**
  153. * Update a user's points total
  154. *
  155. * @param int $new_value Optional. The new value
  156. * @param int $user_id User ID to update. Optional, defaults to current logged in user.
  157. * @return bool False if no user or failure, true if successful
  158. * @since Achievements (3.0)
  159. */
  160. function dpa_update_user_points( $new_value = 0, $user_id = 0 ) {
  161. // Default to current user
  162. if ( empty( $user_id ) && is_user_logged_in() )
  163. $user_id = get_current_user_id();
  164. // No user, bail out
  165. if ( empty( $user_id ) )
  166. return false;
  167. // As Achievements can run independently (as well as sitewide) on a multisite, decide where to store the user option
  168. $store_global = is_multisite() && dpa_is_running_networkwide();
  169. $new_value = apply_filters( 'dpa_update_user_points', $new_value, $user_id );
  170. $retval = update_user_option( $user_id, '_dpa_points', (int) $new_value, $store_global );
  171. // Effectively clears the cache for leaderboard results
  172. wp_cache_set( 'last_changed', microtime(), 'achievements_leaderboard' );
  173. return $retval;
  174. }
  175. /**
  176. * Output the user's points total
  177. *
  178. * @param int $user_id Optional. User ID to retrieve value for
  179. * @since Achievements (3.0)
  180. */
  181. function dpa_user_points( $user_id = 0 ) {
  182. echo number_format_i18n( dpa_get_user_points( $user_id ) );
  183. }
  184. /**
  185. * Return the user's points total
  186. *
  187. * @param int $user_id Optional. User ID to retrieve value for
  188. * @return mixed False if no user, option value otherwise (int).
  189. * @since Achievements (3.0)
  190. */
  191. function dpa_get_user_points( $user_id = 0 ) {
  192. // Default to current user
  193. if ( empty( $user_id ) && is_user_logged_in() )
  194. $user_id = get_current_user_id();
  195. // No user, bail out
  196. if ( empty( $user_id ) )
  197. return false;
  198. $value = get_user_option( '_dpa_points', $user_id );
  199. return (int) apply_filters( 'dpa_get_user_points', $value, $user_id );
  200. }
  201. /**
  202. * _dpa_notifications option - this user's notifications
  203. */
  204. /**
  205. * Update a user's notifications
  206. *
  207. * @param array $notifications Optional. The new value
  208. * @param int $user_id User ID to update. Optional, defaults to current logged in user.
  209. * @return bool False if no user or failure, true if successful
  210. * @since Achievements (3.0)
  211. */
  212. function dpa_update_user_notifications( $notifications = array(), $user_id = 0 ) {
  213. // Default to current user
  214. if ( empty( $user_id ) && is_user_logged_in() )
  215. $user_id = get_current_user_id();
  216. // No user, bail out
  217. if ( empty( $user_id ) )
  218. return false;
  219. // As Achievements can run independently (as well as sitewide) on a multisite, decide where to store the user option
  220. $store_global = is_multisite() && dpa_is_running_networkwide();
  221. $notifications = (array) apply_filters( 'dpa_update_user_notifications', $notifications, $user_id );
  222. $new_values = array();
  223. // Prevent people filtering in array keys that aren't unsigned integers
  224. foreach ( $notifications as $ID => $value )
  225. $new_values[absint( $ID )] = $value;
  226. if ( isset( $new_values[0] ) )
  227. unset( $new_values[0] );
  228. return update_user_option( $user_id, '_dpa_notifications', $new_values, $store_global );
  229. }
  230. /**
  231. * Return the user's notifications
  232. *
  233. * @param int $user_id Optional. User ID to retrieve value for
  234. * @return array
  235. * @since Achievements (3.0)
  236. */
  237. function dpa_get_user_notifications( $user_id = 0 ) {
  238. // Default to current user
  239. if ( empty( $user_id ) && is_user_logged_in() )
  240. $user_id = get_current_user_id();
  241. // No user, bail out
  242. if ( empty( $user_id ) )
  243. return array();
  244. // Get notifications for this user
  245. $value = get_user_option( '_dpa_notifications', $user_id );
  246. if ( empty( $value ) )
  247. return array();
  248. return (array) apply_filters( 'dpa_get_user_notifications', $value, $user_id );
  249. }
  250. /**
  251. * _dpa_last_unlocked option - ID of user's last unlocked achievement
  252. */
  253. /**
  254. * Update the ID of the last achievement this user unlocked
  255. *
  256. * @param int $user_id Optional. User ID to update. Optional, defaults to current logged in user.
  257. * @param int $new_value Optional. The new value.
  258. * @return bool False if no user or failure, true if successful
  259. * @since Achievements (3.0)
  260. */
  261. function dpa_update_user_last_unlocked( $user_id = 0, $new_value = 0 ) {
  262. // Default to current user
  263. if ( empty( $user_id ) && is_user_logged_in() )
  264. $user_id = get_current_user_id();
  265. // No user, bail out
  266. if ( empty( $user_id ) )
  267. return false;
  268. // As Achievements can run independently (as well as sitewide) on a multisite, decide where to store the user option
  269. $store_global = is_multisite() && dpa_is_running_networkwide();
  270. $new_value = apply_filters( 'dpa_update_user_last_unlocked', $new_value, $user_id );
  271. return update_user_option( $user_id, '_dpa_last_unlocked', (int) $new_value, $store_global );
  272. }
  273. /**
  274. * Output the ID of the last achievement this user unlocked
  275. *
  276. * @param int $user_id Optional. User ID to retrieve value for
  277. * @since Achievements (3.0)
  278. */
  279. function dpa_user_last_unlocked( $user_id = 0 ) {
  280. echo number_format_i18n( dpa_get_user_last_unlocked( $user_id ) );
  281. }
  282. /**
  283. * Return the ID of the last achievement this user unlocked
  284. *
  285. * @param int $user_id Optional. User ID to retrieve value for
  286. * @return mixed False if no user, option value otherwise (int).
  287. * @since Achievements (3.0)
  288. */
  289. function dpa_get_user_last_unlocked( $user_id = 0 ) {
  290. // Default to current user
  291. if ( empty( $user_id ) && is_user_logged_in() )
  292. $user_id = get_current_user_id();
  293. // No user, bail out
  294. if ( empty( $user_id ) )
  295. return false;
  296. $value = get_user_option( '_dpa_last_unlocked', $user_id );
  297. return (int) apply_filters( 'dpa_get_user_last_unlocked', $value, $user_id );
  298. }