BP_XProfile_Data_Template

The main profile template loop class.

Defined (1)

The class is defined in the following location(s).

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