/includes/users/options.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress User Options 
  5. * 
  6. * @package bbPress 
  7. * @subpackage UserOptions 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** 
  14. * Get the default user options and their values 
  15. * 
  16. * @since bbPress (r3910) 
  17. * @return array Filtered user option names and values 
  18. */ 
  19. function bbp_get_default_user_options() { 
  20.  
  21. // Default options 
  22. return apply_filters( 'bbp_get_default_user_options', array( 
  23. '_bbp_last_posted' => '0', // For checking flooding 
  24. '_bbp_topic_count' => '0', // Total topics per site 
  25. '_bbp_reply_count' => '0', // Total replies per site 
  26. '_bbp_favorites' => '', // Favorite topics per site 
  27. '_bbp_subscriptions' => '' // Subscribed topics per site 
  28. ) ); 
  29.  
  30. /** 
  31. * Add default user options 
  32. * 
  33. * This is destructive, so existing bbPress user options will be overridden. 
  34. * 
  35. * @since bbPress (r3910) 
  36. * @uses bbp_get_default_user_options() To get default options 
  37. * @uses update_user_option() Adds default options 
  38. * @uses do_action() Calls 'bbp_add_user_options' 
  39. */ 
  40. function bbp_add_user_options( $user_id = 0 ) { 
  41.  
  42. // Validate user id 
  43. $user_id = bbp_get_user_id( $user_id ); 
  44. if ( empty( $user_id ) ) 
  45. return; 
  46.  
  47. // Add default options 
  48. foreach ( bbp_get_default_user_options() as $key => $value ) 
  49. update_user_option( $user_id, $key, $value ); 
  50.  
  51. // Allow previously activated plugins to append their own user options. 
  52. do_action( 'bbp_add_user_options', $user_id ); 
  53.  
  54. /** 
  55. * Delete default user options 
  56. * 
  57. * Hooked to bbp_uninstall, it is only called once when bbPress is uninstalled. 
  58. * This is destructive, so existing bbPress user options will be destroyed. 
  59. * 
  60. * @since bbPress (r3910) 
  61. * @uses bbp_get_default_user_options() To get default options 
  62. * @uses delete_user_option() Removes default options 
  63. * @uses do_action() Calls 'bbp_delete_options' 
  64. */ 
  65. function bbp_delete_user_options( $user_id = 0 ) { 
  66.  
  67. // Validate user id 
  68. $user_id = bbp_get_user_id( $user_id ); 
  69. if ( empty( $user_id ) ) 
  70. return; 
  71.  
  72. // Add default options 
  73. foreach ( array_keys( bbp_get_default_user_options() ) as $key ) 
  74. delete_user_option( $user_id, $key ); 
  75.  
  76. // Allow previously activated plugins to append their own options. 
  77. do_action( 'bbp_delete_user_options', $user_id ); 
  78.  
  79. /** 
  80. * Add filters to each bbPress option and allow them to be overloaded from 
  81. * inside the $bbp->options array. 
  82. * 
  83. * @since bbPress (r3910) 
  84. * @uses bbp_get_default_user_options() To get default options 
  85. * @uses add_filter() To add filters to 'pre_option_{$key}' 
  86. * @uses do_action() Calls 'bbp_add_option_filters' 
  87. */ 
  88. function bbp_setup_user_option_filters() { 
  89.  
  90. // Add filters to each bbPress option 
  91. foreach ( array_keys( bbp_get_default_user_options() ) as $key ) 
  92. add_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option', 10, 3 ); 
  93.  
  94. // Allow previously activated plugins to append their own options. 
  95. do_action( 'bbp_setup_user_option_filters' ); 
  96.  
  97. /** 
  98. * Filter default options and allow them to be overloaded from inside the 
  99. * $bbp->user_options array. 
  100. * 
  101. * @since bbPress (r3910) 
  102. * @param bool $value Optional. Default value false 
  103. * @return mixed false if not overloaded, mixed if set 
  104. */ 
  105. function bbp_filter_get_user_option( $value = false, $option = '', $user = 0 ) { 
  106. $bbp = bbpress(); 
  107.  
  108. // Check the options global for preset value 
  109. if ( isset( $user->ID ) && isset( $bbp->user_options[$user->ID] ) && !empty( $bbp->user_options[$user->ID][$option] ) ) 
  110. $value = $bbp->user_options[$user->ID][$option]; 
  111.  
  112. // Always return a value, even if false 
  113. return $value; 
  114.  
  115. /** Post Counts ***************************************************************/ 
  116.  
  117. /** 
  118. * Output a users topic count 
  119. * 
  120. * @since bbPress (r3632) 
  121. * 
  122. * @param int $user_id 
  123. * @param boolean $integer Optional. Whether or not to format the result 
  124. * @uses bbp_get_user_topic_count() 
  125. * @return string 
  126. */ 
  127. function bbp_user_topic_count( $user_id = 0, $integer = false ) { 
  128. echo bbp_get_user_topic_count( $user_id, $integer ); 
  129. /** 
  130. * Return a users reply count 
  131. * 
  132. * @since bbPress (r3632) 
  133. * 
  134. * @param int $user_id 
  135. * @param boolean $integer Optional. Whether or not to format the result 
  136. * @uses bbp_get_user_id() 
  137. * @uses get_user_option() 
  138. * @uses apply_filters() 
  139. * @return string 
  140. */ 
  141. function bbp_get_user_topic_count( $user_id = 0, $integer = false ) { 
  142.  
  143. // Validate user id 
  144. $user_id = bbp_get_user_id( $user_id ); 
  145. if ( empty( $user_id ) ) 
  146. return false; 
  147.  
  148. $count = (int) get_user_option( '_bbp_topic_count', $user_id ); 
  149. $filter = ( false === $integer ) ? 'bbp_get_user_topic_count_int' : 'bbp_get_user_topic_count'; 
  150.  
  151. return apply_filters( $filter, $count, $user_id ); 
  152.  
  153. /** 
  154. * Output a users reply count 
  155. * 
  156. * @since bbPress (r3632) 
  157. * 
  158. * @param int $user_id 
  159. * @param boolean $integer Optional. Whether or not to format the result 
  160. * @uses bbp_get_user_reply_count() 
  161. * @return string 
  162. */ 
  163. function bbp_user_reply_count( $user_id = 0, $integer = false ) { 
  164. echo bbp_get_user_reply_count( $user_id, $integer ); 
  165. /** 
  166. * Return a users reply count 
  167. * 
  168. * @since bbPress (r3632) 
  169. * 
  170. * @param int $user_id 
  171. * @param boolean $integer Optional. Whether or not to format the result 
  172. * @uses bbp_get_user_id() 
  173. * @uses get_user_option() 
  174. * @uses apply_filters() 
  175. * @return string 
  176. */ 
  177. function bbp_get_user_reply_count( $user_id = 0, $integer = false ) { 
  178.  
  179. // Validate user id 
  180. $user_id = bbp_get_user_id( $user_id ); 
  181. if ( empty( $user_id ) ) 
  182. return false; 
  183.  
  184. $count = (int) get_user_option( '_bbp_reply_count', $user_id ); 
  185. $filter = ( true === $integer ) ? 'bbp_get_user_topic_count_int' : 'bbp_get_user_topic_count'; 
  186.  
  187. return apply_filters( $filter, $count, $user_id ); 
  188.  
  189. /** 
  190. * Output a users total post count 
  191. * 
  192. * @since bbPress (r3632) 
  193. * 
  194. * @param int $user_id 
  195. * @param boolean $integer Optional. Whether or not to format the result 
  196. * @uses bbp_get_user_post_count() 
  197. * @return string 
  198. */ 
  199. function bbp_user_post_count( $user_id = 0, $integer = false ) { 
  200. echo bbp_get_user_post_count( $user_id, $integer ); 
  201. /** 
  202. * Return a users total post count 
  203. * 
  204. * @since bbPress (r3632) 
  205. * 
  206. * @param int $user_id 
  207. * @param boolean $integer Optional. Whether or not to format the result 
  208. * @uses bbp_get_user_id() 
  209. * @uses get_user_option() 
  210. * @uses apply_filters() 
  211. * @return string 
  212. */ 
  213. function bbp_get_user_post_count( $user_id = 0, $integer = false ) { 
  214.  
  215. // Validate user id 
  216. $user_id = bbp_get_user_id( $user_id ); 
  217. if ( empty( $user_id ) ) 
  218. return false; 
  219.  
  220. $topics = bbp_get_user_topic_count( $user_id, true ); 
  221. $replies = bbp_get_user_reply_count( $user_id, true ); 
  222. $count = (int) $topics + $replies; 
  223. $filter = ( true === $integer ) ? 'bbp_get_user_post_count_int' : 'bbp_get_user_post_count'; 
  224.  
  225. return apply_filters( $filter, $count, $user_id ); 
  226.  
  227. /** Last Posted ***************************************************************/ 
  228.  
  229. /** 
  230. * Update a users last posted time, for use with post throttling 
  231. * 
  232. * @since bbPress (r3910) 
  233. * @param int $user_id User ID to update 
  234. * @param int $time Time in time() format 
  235. * @return bool False if no user or failure, true if successful 
  236. */ 
  237. function bbp_update_user_last_posted( $user_id = 0, $time = 0 ) { 
  238.  
  239. // Validate user id 
  240. $user_id = bbp_get_user_id( $user_id ); 
  241. if ( empty( $user_id ) ) 
  242. return false; 
  243.  
  244. // Set time to now if nothing is passed 
  245. if ( empty( $time ) ) 
  246. $time = time(); 
  247.  
  248. return update_user_option( $user_id, '_bbp_last_posted', $time ); 
  249.  
  250. /** 
  251. * Output the raw value of the last posted time. 
  252. * 
  253. * @since bbPress (r3910) 
  254. * @param int $user_id User ID to retrieve value for 
  255. * @uses bbp_get_user_last_posted() To output the last posted time 
  256. */ 
  257. function bbp_user_last_posted( $user_id = 0 ) { 
  258. echo bbp_get_user_last_posted( $user_id ); 
  259.  
  260. /** 
  261. * Return the raw value of teh last posted time. 
  262. * 
  263. * @since bbPress (r3910) 
  264. * @param int $user_id User ID to retrieve value for 
  265. * @return mixed False if no user, time() format if exists 
  266. */ 
  267. function bbp_get_user_last_posted( $user_id = 0 ) { 
  268.  
  269. // Validate user id 
  270. $user_id = bbp_get_user_id( $user_id ); 
  271. if ( empty( $user_id ) ) 
  272. return false; 
  273.  
  274. $time = get_user_option( '_bbp_last_posted', $user_id ); 
  275.  
  276. return apply_filters( 'bbp_get_user_last_posted', $time, $user_id ); 
.