Changeset 10525 for trunk/src/bp-xprofile/bp-xprofile-template.php
- Timestamp:
- 02/05/2016 05:37:16 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/bp-xprofile-template.php
r10460 r10525 11 11 defined( 'ABSPATH' ) || exit; 12 12 13 /** 14 * The main profile template loop class. 15 * 16 * This is responsible for loading profile field, group, and data and displaying it. 17 * 18 * @since 1.0.0 19 */ 20 class BP_XProfile_Data_Template { 21 22 /** 23 * The loop iterator. 24 * 25 * @since 1.5.0 26 * @var int 27 */ 28 public $current_group = -1; 29 30 /** 31 * The number of groups returned by the paged query. 32 * 33 * @since 1.5.0 34 * @var int 35 */ 36 public $group_count; 37 38 /** 39 * Array of groups located by the query. 40 * 41 * @since 1.5.0 42 * @var array 43 */ 44 public $groups; 45 46 /** 47 * The group object currently being iterated on. 48 * 49 * @since 1.5.0 50 * @var object 51 */ 52 public $group; 53 54 /** 55 * The current field. 56 * 57 * @since 1.5.0 58 * @var int 59 */ 60 public $current_field = -1; 61 62 /** 63 * The field count. 64 * 65 * @since 1.5.0 66 * @var int 67 */ 68 public $field_count; 69 70 /** 71 * Field has data. 72 * 73 * @since 1.5.0 74 * @var bool 75 */ 76 public $field_has_data; 77 78 /** 79 * The field. 80 * 81 * @since 1.5.0 82 * @var int 83 */ 84 public $field; 85 86 /** 87 * A flag for whether the loop is currently being iterated. 88 * 89 * @since 1.5.0 90 * @var bool 91 */ 92 public $in_the_loop; 93 94 /** 95 * The user ID. 96 * 97 * @since 1.5.0 98 * @var int 99 */ 100 public $user_id; 101 102 /** 103 * Get activity items, as specified by parameters. 104 * 105 * @see BP_XProfile_Group::get() for more details about parameters. 106 * 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 $func_args = func_get_args(); 145 $args = bp_core_parse_args_array( $old_args_keys, $func_args ); 146 } 147 148 $r = wp_parse_args( $args, array( 149 'profile_group_id' => false, 150 'user_id' => false, 151 'member_type' => 'any', 152 'hide_empty_groups' => false, 153 'hide_empty_fields' => false, 154 'fetch_fields' => false, 155 'fetch_field_data' => false, 156 'fetch_visibility_level' => false, 157 'exclude_groups' => false, 158 'exclude_fields' => false, 159 'update_meta_cache' => true 160 ) ); 161 162 $this->groups = bp_xprofile_get_groups( $r ); 163 $this->group_count = count( $this->groups ); 164 $this->user_id = $r['user_id']; 165 } 166 167 /** 168 * Whether or not the loop has field groups. 169 * 170 * @return bool 171 */ 172 public function has_groups() { 173 if ( ! empty( $this->group_count ) ) { 174 return true; 175 } 176 177 return false; 178 } 179 180 /** 181 * Increments to the next group of fields. 182 * 183 * @return object 184 */ 185 public function next_group() { 186 $this->current_group++; 187 188 $this->group = $this->groups[ $this->current_group ]; 189 $this->field_count = 0; 190 191 if ( ! empty( $this->group->fields ) ) { 192 193 /** 194 * Filters the group fields for the next_group method. 195 * 196 * @since 1.1.0 197 * 198 * @param array $fields Array of fields for the group. 199 * @param int $id ID of the field group. 200 */ 201 $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id ); 202 $this->field_count = count( $this->group->fields ); 203 } 204 205 return $this->group; 206 } 207 208 /** 209 * Rewinds to the start of the groups list. 210 */ 211 public function rewind_groups() { 212 $this->current_group = -1; 213 if ( $this->group_count > 0 ) { 214 $this->group = $this->groups[0]; 215 } 216 } 217 218 /** 219 * Kicks off the profile groups. 220 * 221 * @return bool 222 */ 223 public function profile_groups() { 224 if ( $this->current_group + 1 < $this->group_count ) { 225 return true; 226 } elseif ( $this->current_group + 1 == $this->group_count ) { 227 228 /** 229 * Fires right before the rewinding of profile groups. 230 * 231 * @since 1.1.0 232 */ 233 do_action( 'xprofile_template_loop_end' ); 234 235 // Do some cleaning up after the loop. 236 $this->rewind_groups(); 237 } 238 239 $this->in_the_loop = false; 240 return false; 241 } 242 243 /** 244 * Sets up the profile group. 245 */ 246 public function the_profile_group() { 247 global $group; 248 249 $this->in_the_loop = true; 250 $group = $this->next_group(); 251 252 // Loop has just started. 253 if ( 0 === $this->current_group ) { 254 255 /** 256 * Fires if the current group is the first in the loop. 257 * 258 * @since 1.1.0 259 */ 260 do_action( 'xprofile_template_loop_start' ); 261 } 262 } 263 264 /** Fields ****************************************************************/ 265 266 /** 267 * Increments to the next field. 268 * 269 * @return int 270 */ 271 public function next_field() { 272 $this->current_field++; 273 274 $this->field = $this->group->fields[ $this->current_field ]; 275 276 return $this->field; 277 } 278 279 /** 280 * Rewinds to the start of the fields. 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 289 /** 290 * Whether or not the loop has fields. 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 } 304 305 return $has_data; 306 } 307 308 /** 309 * Kick off the profile fields. 310 * 311 * @return bool 312 */ 313 public function profile_fields() { 314 if ( $this->current_field + 1 < $this->field_count ) { 315 return true; 316 } elseif ( $this->current_field + 1 == $this->field_count ) { 317 // Do some cleaning up after the loop. 318 $this->rewind_fields(); 319 } 320 321 return false; 322 } 323 324 /** 325 * Set up the profile fields. 326 */ 327 public function the_profile_field() { 328 global $field; 329 330 $field = $this->next_field(); 331 332 // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. 333 if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || ( '0' === $field->data->value ) ) ) { 334 $value = maybe_unserialize( $field->data->value ); 335 } else { 336 $value = false; 337 } 338 339 if ( ! empty( $value ) || ( '0' === $value ) ) { 340 $this->field_has_data = true; 341 } else { 342 $this->field_has_data = false; 343 } 344 } 345 } 13 require dirname( __FILE__ ) . '/classes/class-bp-xprofile-data-template.php'; 346 14 347 15 /**
Note: See TracChangeset
for help on using the changeset viewer.