BP_XProfile_Group::get( array $args = array() )
Populates the BP_XProfile_Group object with profile field groups, fields, and field data.
Description
Parameters
- $args
-
(Optional) Array of optional arguments:
- 'profile_group_id'
(int) Limit results to a single profile group. - 'user_id'
(int) Required if you want to load a specific user's data. Default: displayed user's ID. - 'member_type'
(array|string) Limit fields by those restricted to a given profile type, or array of profile types. If$user_id
is provided, the value of$member_type
will be overridden by the profile types of the provided user. The special value of 'any' will return only those fields that are unrestricted by profile type - i.e., those applicable to any type. - 'hide_empty_groups'
(bool) True to hide groups that don't have any fields. Default: false. - 'hide_empty_fields'
(bool) True to hide fields where the user has not provided data. Default: false. - 'fetch_fields'
(bool) Whether to fetch each group's fields. Default: false. - 'fetch_field_data'
(bool) Whether to fetch data for each field. Requires a $user_id. Default: false. - 'exclude_groups'
(array) Comma-separated list or array of group IDs to exclude. - 'exclude_fields'
(array) Comma-separated list or array of field IDs to exclude. - 'includ_fields'
(array) Comma-separated list or array of field IDs to include. - 'update_meta_cache'
(bool) Whether to pre-fetch xprofilemeta for all retrieved groups, fields, and data. Default: true.
Default value: array()
- 'profile_group_id'
Return
(array) $groups
Source
File: bp-xprofile/classes/class-bp-xprofile-group.php
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 | public static function get( $args = array () ) { global $wpdb ; // Parse arguments. $r = wp_parse_args( $args , array ( 'profile_group_id' => false, 'user_id' => bp_displayed_user_id(), 'member_type' => false, 'hide_empty_groups' => false, 'hide_empty_fields' => false, 'fetch_fields' => false, 'fetch_field_data' => false, 'fetch_visibility_level' => false, 'exclude_groups' => false, 'exclude_fields' => false, 'update_meta_cache' => true, 'repeater_show_main_fields_only' => false, ) ); // Keep track of object IDs for cache-priming. $object_ids = array ( 'group' => array (), 'field' => array (), 'data' => array (), ); // WHERE. if ( ! empty ( $r [ 'profile_group_id' ] ) ) { $where_sql = $wpdb ->prepare( 'WHERE g.id = %d' , $r [ 'profile_group_id' ] ); } elseif ( $r [ 'exclude_groups' ] ) { $exclude = join( ',' , wp_parse_id_list( $r [ 'exclude_groups' ] ) ); $where_sql = "WHERE g.id NOT IN ({$exclude})" ; } else { $where_sql = '' ; } $bp = buddypress(); // Include or exclude empty groups. if ( ! empty ( $r [ 'hide_empty_groups' ] ) ) { $group_ids = $wpdb ->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" ); } else { $group_ids = $wpdb ->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" ); } // Get all group data. $groups = self::get_group_data( $group_ids ); // Bail if not also getting fields. if ( empty ( $r [ 'fetch_fields' ] ) ) { return $groups ; } // Get the group ids from the groups we found. $group_ids = wp_list_pluck( $groups , 'id' ); // Store for meta cache priming. $object_ids [ 'group' ] = $group_ids ; // Bail if no groups found. if ( empty ( $group_ids ) ) { return $groups ; } // Setup IN query from group IDs. $group_ids_in = implode( ',' , ( array ) $group_ids ); // Support arrays and comma-separated strings. $exclude_fields_cs = wp_parse_id_list( $r [ 'exclude_fields' ] ); /** * For each group, check if it is a repeater set. * If so, * - get the number of sets, user has created. * - create as many set of clones for each such field in the group, if not created already * - include those field ids in loop */ foreach ( $group_ids as $group_id ) { $is_repeater_enabled = 'on' == BP_XProfile_Group::get_group_meta( $group_id , 'is_repeater_enabled' ) ? true : false; if ( $is_repeater_enabled ) { $clone_field_ids_all = bp_get_repeater_clone_field_ids_all( $group_id ); if ( $r [ 'repeater_show_main_fields_only' ] ) { //exclude clones $exclude_fields_cs = array_merge ( $exclude_fields_cs , $clone_field_ids_all ); } else { //exclude template fields $template_field_ids = bp_get_repeater_template_field_ids( $group_id ); $exclude_fields_cs = array_merge ( $exclude_fields_cs , $template_field_ids ); //include only the subset of clones the current user has data in $user_field_set_count = bp_get_profile_field_set_count( $group_id , $r [ 'user_id' ] ); $clone_field_ids_has_data = bp_get_repeater_clone_field_ids_subset( $group_id , $user_field_set_count ); $clones_to_exclude = array_diff ( $clone_field_ids_all , $clone_field_ids_has_data ); $exclude_fields_cs = array_merge ( $exclude_fields_cs , $clones_to_exclude ); } } } // Visibility - Handled here so as not to be overridden by sloppy use of the // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user(). $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r [ 'user_id' ] ); $exclude_fields_cs = array_merge ( $exclude_fields_cs , $hidden_user_fields ); $exclude_fields_cs = implode( ',' , $exclude_fields_cs ); // Set up NOT IN query for excluded field IDs. if ( ! empty ( $exclude_fields_cs ) ) { $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})" ; } else { $exclude_fields_sql = '' ; } // Set up IN query for included field IDs. $include_field_ids = array (); // Member-type restrictions. if ( bp_get_member_types() ) { if ( $r [ 'user_id' ] || false !== $r [ 'member_type' ] ) { $member_types = $r [ 'member_type' ]; if ( $r [ 'user_id' ] ) { $member_types = bp_get_member_type( $r [ 'user_id' ], false ); if ( empty ( $member_types ) ) { $member_types = array ( 'null' ); } } $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types ); $include_field_ids += array_keys ( $member_types_fields ); } } $in_sql = '' ; if ( ! empty ( $include_field_ids ) ) { $include_field_ids_cs = implode( ',' , array_map ( 'intval' , $include_field_ids ) ); $in_sql = " AND id IN ({$include_field_ids_cs}) " ; } // Fetch the fields. $field_ids = $wpdb ->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" ); foreach ( $groups as $group ) { $group ->fields = array (); } // Bail if no fields. if ( empty ( $field_ids ) ) { return $groups ; } $field_ids = array_map ( 'intval' , $field_ids ); // Start Remove the social network fields from dashboard page. $social_networks_key = 0; if ( bp_is_user_profile() && ! bp_is_user_profile_edit() && ! bp_is_register_page() ) { foreach ( $field_ids as $profile_fields ) { $field_obj = xprofile_get_field( $profile_fields , null, false ); if ( 'socialnetworks' === $field_obj ->type ) { $social_networks_key = (int) $profile_fields ; } } } // If social networks field found then remove from the $field_ids array. if ( $social_networks_key > 0 ) { if (( $key = array_search ( $social_networks_key , $field_ids )) !== false) { unset( $field_ids [ $key ]); } } // End Remove the social network fields from dashboard page. // Prime the field cache. $uncached_field_ids = bp_get_non_cached_ids( $field_ids , 'bp_xprofile_fields' ); if ( ! empty ( $uncached_field_ids ) ) { $_uncached_field_ids = implode( ',' , array_map ( 'intval' , $uncached_field_ids ) ); $uncached_fields = $wpdb ->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" ); foreach ( $uncached_fields as $uncached_field ) { $fid = intval ( $uncached_field ->id ); wp_cache_set( $fid , $uncached_field , 'bp_xprofile_fields' ); } } // Pull field objects from the cache. $fields = array (); foreach ( $field_ids as $field_id ) { $fields [] = xprofile_get_field( $field_id , null, false ); } // Store field IDs for meta cache priming. $object_ids [ 'field' ] = $field_ids ; // Maybe fetch field data. if ( ! empty ( $r [ 'fetch_field_data' ] ) ) { // Get field data for user ID. if ( ! empty ( $field_ids ) && ! empty ( $r [ 'user_id' ] ) ) { $field_data = BP_XProfile_ProfileData::get_data_for_user( $r [ 'user_id' ], $field_ids ); } // Remove data-less fields, if necessary. if ( ! empty ( $r [ 'hide_empty_fields' ] ) && ! empty ( $field_ids ) && ! empty ( $field_data ) ) { // Loop through the results and find the fields that have data. foreach ( ( array ) $field_data as $data ) { // Empty fields may contain a serialized empty array. $maybe_value = maybe_unserialize( $data ->value ); // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. if ( ( ! empty ( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search ( $data ->field_id, $field_ids ) ) { // Fields that have data get removed from the list. unset( $field_ids [ $key ] ); } } // The remaining members of $field_ids are empty. Remove them. foreach ( $fields as $field_key => $field ) { if ( in_array( $field ->id, $field_ids ) ) { unset( $fields [ $field_key ] ); } } // Reset indexes. $fields = array_values ( $fields ); } // Field data was found. if ( ! empty ( $fields ) && ! empty ( $field_data ) && ! is_wp_error( $field_data ) ) { // Loop through fields. foreach ( ( array ) $fields as $field_key => $field ) { // Loop through the data in each field. foreach ( ( array ) $field_data as $data ) { // Assign correct data value to the field. if ( $field ->id == $data ->field_id ) { $fields [ $field_key ]->data = new stdClass; $fields [ $field_key ]->data->value = $data ->value; $fields [ $field_key ]->data->id = $data ->id; } // Store for meta cache priming. $object_ids [ 'data' ][] = $data ->id; } } } } // Prime the meta cache, if necessary. if ( ! empty ( $r [ 'update_meta_cache' ] ) ) { bp_xprofile_update_meta_cache( $object_ids ); } // Maybe fetch visibility levels. if ( ! empty ( $r [ 'fetch_visibility_level' ] ) ) { $fields = self::fetch_visibility_level( $r [ 'user_id' ], $fields ); } // Merge the field array back in with the group array. foreach ( ( array ) $groups as $group ) { // Indexes may have been shifted after previous deletions, so we get a // fresh one each time through the loop. $index = array_search ( $group , $groups ); foreach ( ( array ) $fields as $field ) { if ( $group ->id === $field ->group_id ) { $groups [ $index ]->fields[] = $field ; } } // When we unset fields above, we may have created empty groups. // Remove them, if necessary. if ( empty ( $group ->fields ) && ! empty ( $r [ 'hide_empty_groups' ] ) ) { unset( $groups [ $index ] ); } // Reset indexes. $groups = array_values ( $groups ); } return $groups ; } |
Changelog
Version | Description |
---|---|
BuddyPress 1.2.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.