/bp-xprofile/classes/class-bp-xprofile-data-template.php

  1. <?php 
  2. /** 
  3. * BuddyPress XProfile Template Loop Class. 
  4. * 
  5. * @package BuddyPress 
  6. * @since 1.0.0 
  7. */ 
  8.  
  9. // Exit if accessed directly. 
  10. defined( 'ABSPATH' ) || exit; 
  11.  
  12. /** 
  13. * The main profile template loop class. 
  14. * 
  15. * This is responsible for loading profile field, group, and data and displaying it. 
  16. * 
  17. * @since 1.0.0 
  18. */ 
  19. class BP_XProfile_Data_Template { 
  20.  
  21. /** 
  22. * The loop iterator. 
  23. * 
  24. * @since 1.5.0 
  25. * @var int 
  26. */ 
  27. public $current_group = -1; 
  28.  
  29. /** 
  30. * The number of groups returned by the paged query. 
  31. * 
  32. * @since 1.5.0 
  33. * @var int 
  34. */ 
  35. public $group_count; 
  36.  
  37. /** 
  38. * Array of groups located by the query. 
  39. * 
  40. * @since 1.5.0 
  41. * @var array 
  42. */ 
  43. public $groups; 
  44.  
  45. /** 
  46. * The group object currently being iterated on. 
  47. * 
  48. * @since 1.5.0 
  49. * @var object 
  50. */ 
  51. public $group; 
  52.  
  53. /** 
  54. * The current field. 
  55. * 
  56. * @since 1.5.0 
  57. * @var int 
  58. */ 
  59. public $current_field = -1; 
  60.  
  61. /** 
  62. * The field count. 
  63. * 
  64. * @since 1.5.0 
  65. * @var int 
  66. */ 
  67. public $field_count; 
  68.  
  69. /** 
  70. * Field has data. 
  71. * 
  72. * @since 1.5.0 
  73. * @var bool 
  74. */ 
  75. public $field_has_data; 
  76.  
  77. /** 
  78. * The field. 
  79. * 
  80. * @since 1.5.0 
  81. * @var int 
  82. */ 
  83. public $field; 
  84.  
  85. /** 
  86. * A flag for whether the loop is currently being iterated. 
  87. * 
  88. * @since 1.5.0 
  89. * @var bool 
  90. */ 
  91. public $in_the_loop; 
  92.  
  93. /** 
  94. * The user ID. 
  95. * 
  96. * @since 1.5.0 
  97. * @var int 
  98. */ 
  99. public $user_id; 
  100.  
  101. /** 
  102. * Get activity items, as specified by parameters. 
  103. * 
  104. * @see BP_XProfile_Group::get() for more details about parameters. 
  105. * 
  106. * @since 1.5.0 
  107. * @since 2.4.0 Introduced `$member_type` argument. 
  108. * 
  109. * @param array|string $args { 
  110. * An array of arguments. All items are optional. 
  111. * 
  112. * @type int $user_id Fetch field data for this user ID. 
  113. * @type string|array $member_type Limit results to those matching member type(s). 
  114. * @type int $profile_group_id Field group to fetch fields & data for. 
  115. * @type int|bool $hide_empty_groups Should empty field groups be skipped. 
  116. * @type int|bool $fetch_fields Fetch fields for field group. 
  117. * @type int|bool $fetch_field_data Fetch field data for fields in group. 
  118. * @type array $exclude_groups Exclude these field groups. 
  119. * @type array $exclude_fields Exclude these fields. 
  120. * @type int|bool $hide_empty_fields Should empty fields be skipped. 
  121. * @type int|bool $fetch_visibility_level Fetch visibility levels. 
  122. * @type int|bool $update_meta_cache Should metadata cache be updated. 
  123. * } 
  124. */ 
  125. public function __construct( $args = '' ) { 
  126.  
  127. // Backward compatibility with old method of passing arguments. 
  128. if ( ! is_array( $args ) || func_num_args() > 1 ) { 
  129. _deprecated_argument( __METHOD__, '2.3.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) ); 
  130.  
  131. $old_args_keys = array( 
  132. 0 => 'user_id',  
  133. 1 => 'profile_group_id',  
  134. 2 => 'hide_empty_groups',  
  135. 3 => 'fetch_fields',  
  136. 4 => 'fetch_field_data',  
  137. 5 => 'exclude_groups',  
  138. 6 => 'exclude_fields',  
  139. 7 => 'hide_empty_fields',  
  140. 8 => 'fetch_visibility_level',  
  141. 9 => 'update_meta_cache' 
  142. ); 
  143.  
  144. $args = bp_core_parse_args_array( $old_args_keys, func_get_args() ); 
  145.  
  146. $r = wp_parse_args( $args, array( 
  147. 'profile_group_id' => false,  
  148. 'user_id' => false,  
  149. 'member_type' => 'any',  
  150. 'hide_empty_groups' => false,  
  151. 'hide_empty_fields' => false,  
  152. 'fetch_fields' => false,  
  153. 'fetch_field_data' => false,  
  154. 'fetch_visibility_level' => false,  
  155. 'exclude_groups' => false,  
  156. 'exclude_fields' => false,  
  157. 'update_meta_cache' => true 
  158. ) ); 
  159.  
  160. $this->groups = bp_xprofile_get_groups( $r ); 
  161. $this->group_count = count( $this->groups ); 
  162. $this->user_id = $r['user_id']; 
  163.  
  164. /** 
  165. * Whether or not the loop has field groups. 
  166. * 
  167. * @since 1.0.0 
  168. * 
  169. * @return bool 
  170. */ 
  171. public function has_groups() { 
  172. if ( ! empty( $this->group_count ) ) { 
  173. return true; 
  174.  
  175. return false; 
  176.  
  177. /** 
  178. * Increments to the next group of fields. 
  179. * 
  180. * @since 1.0.0 
  181. * 
  182. * @return object 
  183. */ 
  184. public function next_group() { 
  185. $this->current_group++; 
  186.  
  187. $this->group = $this->groups[ $this->current_group ]; 
  188. $this->field_count = 0; 
  189.  
  190. if ( ! empty( $this->group->fields ) ) { 
  191.  
  192. /** 
  193. * Filters the group fields for the next_group method. 
  194. * 
  195. * @since 1.1.0 
  196. * 
  197. * @param array $fields Array of fields for the group. 
  198. * @param int $id ID of the field group. 
  199. */ 
  200. $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id ); 
  201. $this->field_count = count( $this->group->fields ); 
  202.  
  203. return $this->group; 
  204.  
  205. /** 
  206. * Rewinds to the start of the groups list. 
  207. * 
  208. * @since 1.0.0 
  209. */ 
  210. public function rewind_groups() { 
  211. $this->current_group = -1; 
  212. if ( $this->group_count > 0 ) { 
  213. $this->group = $this->groups[0]; 
  214.  
  215. /** 
  216. * Kicks off the profile groups. 
  217. * 
  218. * @since 1.0.0 
  219. * 
  220. * @return bool 
  221. */ 
  222. public function profile_groups() { 
  223. if ( $this->current_group + 1 < $this->group_count ) { 
  224. return true; 
  225. } elseif ( $this->current_group + 1 == $this->group_count ) { 
  226.  
  227. /** 
  228. * Fires right before the rewinding of profile groups. 
  229. * 
  230. * @since 1.1.0 
  231. */ 
  232. do_action( 'xprofile_template_loop_end' ); 
  233.  
  234. // Do some cleaning up after the loop. 
  235. $this->rewind_groups(); 
  236.  
  237. $this->in_the_loop = false; 
  238. return false; 
  239.  
  240. /** 
  241. * Sets up the profile group. 
  242. * 
  243. * @since 1.0.0 
  244. */ 
  245. public function the_profile_group() { 
  246. global $group; 
  247.  
  248. $this->in_the_loop = true; 
  249. $group = $this->next_group(); 
  250.  
  251. // Loop has just started. 
  252. if ( 0 === $this->current_group ) { 
  253.  
  254. /** 
  255. * Fires if the current group is the first in the loop. 
  256. * 
  257. * @since 1.1.0 
  258. */ 
  259. do_action( 'xprofile_template_loop_start' ); 
  260.  
  261. /** Fields ****************************************************************/ 
  262.  
  263. /** 
  264. * Increments to the next field. 
  265. * 
  266. * @since 1.0.0 
  267. * 
  268. * @return int 
  269. */ 
  270. public function next_field() { 
  271. $this->current_field++; 
  272.  
  273. $this->field = $this->group->fields[ $this->current_field ]; 
  274.  
  275. return $this->field; 
  276.  
  277. /** 
  278. * Rewinds to the start of the fields. 
  279. * 
  280. * @since 1.0.0 
  281. */ 
  282. public function rewind_fields() { 
  283. $this->current_field = -1; 
  284. if ( $this->field_count > 0 ) { 
  285. $this->field = $this->group->fields[0]; 
  286.  
  287. /** 
  288. * Whether or not the loop has fields. 
  289. * 
  290. * @since 1.0.0 
  291. * 
  292. * @return bool 
  293. */ 
  294. public function has_fields() { 
  295. $has_data = false; 
  296.  
  297. for ( $i = 0, $count = count( $this->group->fields ); $i < $count; ++$i ) { 
  298. $field = &$this->group->fields[ $i ]; 
  299.  
  300. if ( ! empty( $field->data ) && ( $field->data->value != null ) ) { 
  301. $has_data = true; 
  302.  
  303. return $has_data; 
  304.  
  305. /** 
  306. * Kick off the profile fields. 
  307. * 
  308. * @since 1.0.0 
  309. * 
  310. * @return bool 
  311. */ 
  312. public function profile_fields() { 
  313. if ( $this->current_field + 1 < $this->field_count ) { 
  314. return true; 
  315. } elseif ( $this->current_field + 1 == $this->field_count ) { 
  316. // Do some cleaning up after the loop. 
  317. $this->rewind_fields(); 
  318.  
  319. return false; 
  320.  
  321. /** 
  322. * Set up the profile fields. 
  323. * 
  324. * @since 1.0.0 
  325. */ 
  326. public function the_profile_field() { 
  327. global $field; 
  328.  
  329. $field = $this->next_field(); 
  330.  
  331. // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. 
  332. if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || ( '0' === $field->data->value ) ) ) { 
  333. $value = maybe_unserialize( $field->data->value ); 
  334. } else { 
  335. $value = false; 
  336.  
  337. if ( ! empty( $value ) || ( '0' === $value ) ) { 
  338. $this->field_has_data = true; 
  339. } else { 
  340. $this->field_has_data = false; 
.