[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress XProfile Classes. 4 * 5 * @package BuddyPress 6 * @subpackage XProfileClasses 7 * @since 1.0.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Class to help set up XProfile Groups. 15 * 16 * @since 1.0.0 17 */ 18 class BP_XProfile_Group { 19 20 /** 21 * Field group ID. 22 * 23 * @since 1.1.0 24 * @var int ID of field group. 25 */ 26 public $id = null; 27 28 /** 29 * Field group name. 30 * 31 * @since 1.1.0 32 * @var string Name of field group. 33 */ 34 public $name; 35 36 /** 37 * Field group Description. 38 * 39 * @since 1.1.0 40 * @var string Description of field group. 41 */ 42 public $description; 43 44 /** 45 * Group deletion boolean. 46 * 47 * @since 1.1.0 48 * @var bool Can this group be deleted? 49 */ 50 public $can_delete; 51 52 /** 53 * Group order. 54 * 55 * @since 1.1.0 56 * @var int Group order relative to other groups. 57 */ 58 public $group_order; 59 60 /** 61 * Group fields. 62 * 63 * @since 1.1.0 64 * @var array Fields of group. 65 */ 66 public $fields; 67 68 /** 69 * Initialize and/or populate profile field group. 70 * 71 * @since 1.1.0 72 * 73 * @param int|null $id Field group ID. 74 */ 75 public function __construct( $id = null ) { 76 if ( ! empty( $id ) ) { 77 $this->populate( $id ); 78 } 79 } 80 81 /** 82 * Populate a profile field group. 83 * 84 * @since 1.0.0 85 * 86 * @global $wpdb $wpdb 87 * 88 * @param int $id Field group ID. 89 * @return boolean 90 */ 91 public function populate( $id ) { 92 93 // Get this group. 94 $group = self::get( array( 95 'profile_group_id' => $id 96 ) ); 97 98 // Bail if group not found. 99 if ( empty( $group ) ) { 100 return false; 101 } 102 103 // Get the first array element. 104 $group = reset( $group ); 105 106 // Set object properties. 107 $this->id = $group->id; 108 $this->name = $group->name; 109 $this->description = $group->description; 110 $this->can_delete = $group->can_delete; 111 $this->group_order = $group->group_order; 112 } 113 114 /** 115 * Save a profile field group. 116 * 117 * @since 1.1.0 118 * 119 * @global object $wpdb 120 * 121 * @return boolean 122 */ 123 public function save() { 124 global $wpdb; 125 126 // Filter the field group attributes. 127 $this->name = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id ); 128 $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id ); 129 130 /** 131 * Fires before the current group instance gets saved. 132 * 133 * Please use this hook to filter the properties above. Each part will be passed in. 134 * 135 * @since 1.0.0 136 * 137 * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference. 138 */ 139 do_action_ref_array( 'xprofile_group_before_save', array( &$this ) ); 140 141 $bp = buddypress(); 142 143 // Update or insert. 144 if ( ! empty( $this->id ) ) { 145 $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id ); 146 } else { 147 $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_groups} (name, description, can_delete) VALUES (%s, %s, 1)", $this->name, $this->description ); 148 } 149 150 // Attempt to insert or update. 151 $query = $wpdb->query( $sql ); 152 153 // Bail if query fails. If `$query` is 0, it means the save was successful, but no fields were updated. 154 if ( false === $query || is_wp_error( $query ) ) { 155 return false; 156 } 157 158 // If not set, update the ID in the group object. 159 if ( empty( $this->id ) ) { 160 $this->id = $wpdb->insert_id; 161 } 162 163 /** 164 * Fires after the current group instance gets saved. 165 * 166 * @since 1.0.0 167 * 168 * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference. 169 */ 170 do_action_ref_array( 'xprofile_group_after_save', array( &$this ) ); 171 172 return $this->id; 173 } 174 175 /** 176 * Delete a profile field group 177 * 178 * @since 1.1.0 179 * 180 * @global object $wpdb 181 * 182 * @return boolean 183 */ 184 public function delete() { 185 global $wpdb; 186 187 // Bail if field group cannot be deleted. 188 if ( empty( $this->can_delete ) ) { 189 return false; 190 } 191 192 /** 193 * Fires before the current group instance gets deleted. 194 * 195 * @since 2.0.0 196 * 197 * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference. 198 */ 199 do_action_ref_array( 'xprofile_group_before_delete', array( &$this ) ); 200 201 $bp = buddypress(); 202 $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id ); 203 $deleted = $wpdb->query( $sql ); 204 205 // Delete field group. 206 if ( empty( $deleted ) || is_wp_error( $deleted ) ) { 207 return false; 208 } 209 210 // Remove the group's fields. 211 if ( BP_XProfile_Field::delete_for_group( $this->id ) ) { 212 213 // Remove profile data for the groups fields. 214 if ( ! empty( $this->fields ) ) { 215 for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) { 216 BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id ); 217 } 218 } 219 } 220 221 /** 222 * Fires after the current group instance gets deleted. 223 * 224 * @since 2.0.0 225 * 226 * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference. 227 */ 228 do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) ); 229 230 return true; 231 } 232 233 /** Static Methods ********************************************************/ 234 235 /** 236 * Populates the BP_XProfile_Group object with profile field groups, fields, 237 * and field data. 238 * 239 * @since 1.2.0 240 * 241 * @global object $wpdb WordPress DB access object. 242 * 243 * @param array $args { 244 * Array of optional arguments: 245 * @type int $profile_group_id Limit results to a single profile group. 246 * @type int $user_id Required if you want to load a specific user's data. 247 * Default: displayed user's ID. 248 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of 249 * member types. If `$user_id` is provided, the value of `$member_type` 250 * will be overridden by the member types of the provided user. The 251 * special value of 'any' will return only those fields that are 252 * unrestricted by member type - i.e., those applicable to any type. 253 * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. 254 * @type bool $hide_empty_fields True to hide fields where the user has not provided data. 255 * Default: false. 256 * @type bool $fetch_fields Whether to fetch each group's fields. Default: false. 257 * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id. 258 * Default: false. 259 * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. 260 * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. 261 * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields, 262 * and data. Default: true. 263 * } 264 * @return array $groups 265 */ 266 public static function get( $args = array() ) { 267 global $wpdb; 268 269 // Parse arguments. 270 $r = wp_parse_args( $args, array( 271 'profile_group_id' => false, 272 'user_id' => bp_displayed_user_id(), 273 'member_type' => false, 274 'hide_empty_groups' => false, 275 'hide_empty_fields' => false, 276 'fetch_fields' => false, 277 'fetch_field_data' => false, 278 'fetch_visibility_level' => false, 279 'exclude_groups' => false, 280 'exclude_fields' => false, 281 'update_meta_cache' => true, 282 ) ); 283 284 // Keep track of object IDs for cache-priming. 285 $object_ids = array( 286 'group' => array(), 287 'field' => array(), 288 'data' => array(), 289 ); 290 291 $bp = buddypress(); 292 293 $group_ids = self::get_group_ids( $r ); 294 295 // Get all group data. 296 $groups = self::get_group_data( $group_ids ); 297 298 // Bail if not also getting fields. 299 if ( empty( $r['fetch_fields'] ) ) { 300 return $groups; 301 } 302 303 // Get the group ids from the groups we found. 304 $group_ids = wp_list_pluck( $groups, 'id' ); 305 306 // Store for meta cache priming. 307 $object_ids['group'] = $group_ids; 308 309 // Bail if no groups found. 310 if ( empty( $group_ids ) ) { 311 return $groups; 312 } 313 314 $field_ids = self::get_group_field_ids( $group_ids, $r ); 315 316 foreach( $groups as $group ) { 317 $group->fields = array(); 318 } 319 320 // Bail if no fields. 321 if ( empty( $field_ids ) ) { 322 return $groups; 323 } 324 325 $field_ids = array_map( 'intval', $field_ids ); 326 327 // Prime the field cache. 328 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' ); 329 if ( ! empty( $uncached_field_ids ) ) { 330 $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) ); 331 $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" ); 332 foreach ( $uncached_fields as $uncached_field ) { 333 $fid = intval( $uncached_field->id ); 334 wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' ); 335 } 336 } 337 338 // Pull field objects from the cache. 339 $fields = array(); 340 foreach ( $field_ids as $field_id ) { 341 $fields[] = xprofile_get_field( $field_id, null, false ); 342 } 343 344 // Store field IDs for meta cache priming. 345 $object_ids['field'] = $field_ids; 346 347 // Maybe fetch field data. 348 if ( ! empty( $r['fetch_field_data'] ) ) { 349 350 // Get field data for user ID. 351 if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) { 352 $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids ); 353 } 354 355 // Remove data-less fields, if necessary. 356 if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) { 357 358 // Loop through the results and find the fields that have data. 359 foreach( (array) $field_data as $data ) { 360 361 // Empty fields may contain a serialized empty array. 362 $maybe_value = maybe_unserialize( $data->value ); 363 364 // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. 365 if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) { 366 367 // Fields that have data get removed from the list. 368 unset( $field_ids[ $key ] ); 369 } 370 } 371 372 // The remaining members of $field_ids are empty. Remove them. 373 foreach( $fields as $field_key => $field ) { 374 if ( in_array( $field->id, $field_ids ) ) { 375 unset( $fields[ $field_key ] ); 376 } 377 } 378 379 // Reset indexes. 380 $fields = array_values( $fields ); 381 } 382 383 // Field data was found. 384 if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) { 385 386 // Loop through fields. 387 foreach( (array) $fields as $field_key => $field ) { 388 389 // Loop through the data in each field. 390 foreach( (array) $field_data as $data ) { 391 392 // Assign correct data value to the field. 393 if ( $field->id == $data->field_id ) { 394 $fields[ $field_key ]->data = new stdClass; 395 $fields[ $field_key ]->data->value = $data->value; 396 $fields[ $field_key ]->data->id = $data->id; 397 } 398 399 // Store for meta cache priming. 400 $object_ids['data'][] = $data->id; 401 } 402 } 403 } 404 } 405 406 // Prime the meta cache, if necessary. 407 if ( ! empty( $r['update_meta_cache'] ) ) { 408 bp_xprofile_update_meta_cache( $object_ids ); 409 } 410 411 // Maybe fetch visibility levels. 412 if ( ! empty( $r['fetch_visibility_level'] ) ) { 413 $fields = self::fetch_visibility_level( $r['user_id'], $fields ); 414 } 415 416 // Merge the field array back in with the group array. 417 foreach( (array) $groups as $group ) { 418 // Indexes may have been shifted after previous deletions, so we get a 419 // fresh one each time through the loop. 420 $index = array_search( $group, $groups ); 421 422 foreach( (array) $fields as $field ) { 423 if ( $group->id === $field->group_id ) { 424 $groups[ $index ]->fields[] = $field; 425 } 426 } 427 428 // When we unset fields above, we may have created empty groups. 429 // Remove them, if necessary. 430 if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) { 431 unset( $groups[ $index ] ); 432 } 433 434 // Reset indexes. 435 $groups = array_values( $groups ); 436 } 437 438 return $groups; 439 } 440 441 /** 442 * Gets group IDs, based on passed parameters. 443 * 444 * @since 5.0.0 445 * 446 * @param array $args { 447 * Array of optional arguments: 448 * @type int $profile_group_id Limit results to a single profile group. Default false. 449 * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. Default false. 450 * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. 451 * } 452 * @return array 453 */ 454 public static function get_group_ids( $args = array() ) { 455 global $wpdb; 456 457 $r = array_merge( 458 array( 459 'profile_group_id' => false, 460 'exclude_groups' => false, 461 'hide_empty_groups' => false, 462 ), 463 $args 464 ); 465 466 $bp = buddypress(); 467 468 if ( ! empty( $r['profile_group_id'] ) ) { 469 $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] ); 470 } elseif ( $r['exclude_groups'] ) { 471 $exclude = join( ',', wp_parse_id_list( $r['exclude_groups'] ) ); 472 $where_sql = "WHERE g.id NOT IN ({$exclude})"; 473 } else { 474 $where_sql = ''; 475 } 476 477 // Include or exclude empty groups. 478 if ( ! empty( $r['hide_empty_groups'] ) ) { 479 $group_ids_sql = "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"; 480 } else { 481 $group_ids_sql = "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC"; 482 } 483 484 $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' ); 485 if ( false === $cached ) { 486 $group_ids = $wpdb->get_col( $group_ids_sql ); 487 bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids ); 488 } else { 489 $group_ids = $cached; 490 } 491 492 return array_map( 'intval', $group_ids ); 493 } 494 495 /** 496 * Gets group field IDs, based on passed parameters. 497 * 498 * @since 5.0.0 499 * 500 * @param array $group_ids Array of group IDs. 501 * @param array $args { 502 * Array of optional arguments: 503 * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. 504 * Default empty. 505 * @type int $user_id Limit results to fields associated with a given user's 506 * member type. Default empty. 507 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of 508 * member types. If `$user_id` is provided, the value of `$member_type` 509 * is honored. 510 * } 511 * @return array 512 */ 513 public static function get_group_field_ids( $group_ids, $args = array() ) { 514 global $wpdb; 515 516 $r = array_merge( 517 array( 518 'exclude_fields' => false, 519 'user_id' => false, 520 'member_type' => false, 521 ), 522 $args 523 ); 524 525 $bp = buddypress(); 526 527 // Setup IN query from group IDs. 528 if ( empty( $group_ids ) ) { 529 $group_ids = array( 0 ); 530 } 531 $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) ); 532 533 // Support arrays and comma-separated strings. 534 $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] ); 535 536 // Visibility - Handled here so as not to be overridden by sloppy use of the 537 // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user(). 538 $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] ); 539 $exclude_fields_cs = array_merge( $exclude_fields_cs, $hidden_user_fields ); 540 $exclude_fields_cs = implode( ',', $exclude_fields_cs ); 541 542 // Set up NOT IN query for excluded field IDs. 543 if ( ! empty( $exclude_fields_cs ) ) { 544 $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})"; 545 } else { 546 $exclude_fields_sql = ''; 547 } 548 549 // Set up IN query for included field IDs. 550 $include_field_ids = array(); 551 552 // Member-type restrictions. 553 if ( bp_get_member_types() ) { 554 if ( $r['user_id'] || false !== $r['member_type'] ) { 555 $member_types = $r['member_type']; 556 if ( $r['user_id'] ) { 557 $member_types = bp_get_member_type( $r['user_id'], false ); 558 if ( empty( $member_types ) ) { 559 $member_types = array( 'null' ); 560 } 561 } 562 563 $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types ); 564 $include_field_ids += array_keys( $member_types_fields ); 565 } 566 } 567 568 $in_sql = ''; 569 if ( ! empty( $include_field_ids ) ) { 570 $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) ); 571 $in_sql = " AND id IN ({$include_field_ids_cs}) "; 572 } 573 574 $field_ids_sql = "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"; 575 576 $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' ); 577 if ( false === $cached ) { 578 $field_ids = $wpdb->get_col( $field_ids_sql ); 579 bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids ); 580 } else { 581 $field_ids = $cached; 582 } 583 584 return array_map( 'intval', $field_ids ); 585 } 586 587 /** 588 * Get data about a set of groups, based on IDs. 589 * 590 * @since 2.0.0 591 * 592 * @param array $group_ids Array of IDs. 593 * @return array 594 */ 595 protected static function get_group_data( $group_ids ) { 596 global $wpdb; 597 598 // Bail if no group IDs are passed. 599 if ( empty( $group_ids ) ) { 600 return array(); 601 } 602 603 // Setup empty arrays. 604 $groups = array(); 605 $uncached_gids = array(); 606 607 // Loop through groups and look for cached & uncached data. 608 foreach ( $group_ids as $group_id ) { 609 610 // If cached data is found, use it. 611 $group_data = wp_cache_get( $group_id, 'bp_xprofile_groups' ); 612 if ( false !== $group_data ) { 613 $groups[ $group_id ] = $group_data; 614 615 // Otherwise leave a placeholder so we don't lose the order. 616 } else { 617 $groups[ $group_id ] = ''; 618 619 // Add to the list of items to be queried. 620 $uncached_gids[] = $group_id; 621 } 622 } 623 624 // Fetch uncached data from the DB if necessary. 625 if ( ! empty( $uncached_gids ) ) { 626 627 // Setup IN query for uncached group data. 628 $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) ); 629 630 // Get table name to query. 631 $table_name = buddypress()->profile->table_name_groups; 632 633 // Fetch data, preserving order. 634 $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )"); 635 636 // Make sure query returned valid data. 637 if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) { 638 639 // Put queried data into the placeholders created earlier, 640 // and add it to the cache. 641 foreach ( (array) $queried_gdata as $gdata ) { 642 643 // Add group to groups array. 644 $groups[ $gdata->id ] = $gdata; 645 646 // Cache previously uncached group data. 647 wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' ); 648 } 649 } 650 } 651 652 // Integer casting. 653 foreach ( (array) $groups as $key => $data ) { 654 $groups[ $key ]->id = (int) $groups[ $key ]->id; 655 $groups[ $key ]->group_order = (int) $groups[ $key ]->group_order; 656 $groups[ $key ]->can_delete = (int) $groups[ $key ]->can_delete; 657 } 658 659 // Reset indexes & return. 660 return array_values( $groups ); 661 } 662 663 /** 664 * Validate field group when form submitted. 665 * 666 * @since 1.0.0 667 * 668 * @global string $message 669 * 670 * @return boolean 671 */ 672 public static function admin_validate() { 673 global $message; 674 675 // Validate Form. 676 if ( empty( $_POST['group_name'] ) ) { 677 $message = __( 'Please make sure you give the group a name.', 'buddypress' ); 678 return false; 679 } else { 680 return true; 681 } 682 } 683 684 /** 685 * Update field group position. 686 * 687 * @since 1.5.0 688 * 689 * @global $wpdb $wpdb 690 * 691 * @param int $field_group_id ID of the group the field belongs to. 692 * @param int $position Field group position. 693 * @return boolean 694 */ 695 public static function update_position( $field_group_id, $position ) { 696 global $wpdb; 697 698 if ( ! is_numeric( $position ) ) { 699 return false; 700 } 701 702 // Purge profile field group and group query caches. 703 wp_cache_delete( 'all', 'bp_xprofile_groups' ); 704 bp_core_reset_incrementor( 'bp_xprofile_groups' ); 705 706 $bp = buddypress(); 707 708 return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET group_order = %d WHERE id = %d", $position, $field_group_id ) ); 709 } 710 711 /** 712 * Fetch the field visibility level for the fields returned by the query. 713 * 714 * @since 1.6.0 715 * 716 * @param int $user_id The profile owner's user_id. 717 * @param array $fields The database results returned by the get() query. 718 * @return array $fields The database results, with field_visibility added 719 */ 720 public static function fetch_visibility_level( $user_id = 0, $fields = array() ) { 721 722 // Get the user's visibility level preferences. 723 $visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true ); 724 725 foreach( (array) $fields as $key => $field ) { 726 727 // Does the admin allow this field to be customized? 728 $visibility = bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' ); 729 $allow_custom = (bool) ( 'disabled' !== $visibility ); 730 731 // Look to see if the user has set the visibility for this field. 732 if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) { 733 $field_visibility = $visibility_levels[ $field->id ]; 734 735 // If no admin-set default is saved, fall back on a global default. 736 } else { 737 $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' ); 738 739 /** 740 * Filters the XProfile default visibility level for a field. 741 * 742 * @since 1.6.0 743 * 744 * @param string $value Default visibility value. 745 */ 746 $field_visibility = ! empty( $fallback_visibility ) 747 ? $fallback_visibility 748 : apply_filters( 'bp_xprofile_default_visibility_level', 'public' ); 749 } 750 751 $fields[ $key ]->visibility_level = $field_visibility; 752 } 753 754 return $fields; 755 } 756 757 /** 758 * Fetch the admin-set preferences for all fields. 759 * 760 * @since 1.6.0 761 * 762 * @return array $default_visibility_levels An array, keyed by field_id, of default 763 * visibility level + allow_custom 764 * (whether the admin allows this 765 * field to be set by user) 766 */ 767 public static function fetch_default_visibility_levels() { 768 global $wpdb; 769 770 $default_visibility_levels = wp_cache_get( 'default_visibility_levels', 'bp_xprofile' ); 771 772 if ( false === $default_visibility_levels ) { 773 $bp = buddypress(); 774 775 $levels = $wpdb->get_results( "SELECT object_id, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND ( meta_key = 'default_visibility' OR meta_key = 'allow_custom_visibility' )" ); 776 777 // Arrange so that the field id is the key and the visibility level the value. 778 $default_visibility_levels = array(); 779 foreach ( $levels as $level ) { 780 switch ( $level->meta_key ) { 781 case 'default_visibility' : 782 $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value; 783 break; 784 case 'allow_custom_visibility' : 785 $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value; 786 break; 787 } 788 } 789 790 wp_cache_set( 'default_visibility_levels', $default_visibility_levels, 'bp_xprofile' ); 791 } 792 793 return $default_visibility_levels; 794 } 795 796 /** Admin Output **********************************************************/ 797 798 /** 799 * Output the admin area field group form. 800 * 801 * @since 1.0.0 802 * 803 * @global string $message 804 */ 805 public function render_admin_form() { 806 global $message; 807 808 // Users Admin URL. 809 $users_url = bp_get_admin_url( 'users.php' ); 810 811 // URL to cancel to. 812 $cancel_url = add_query_arg( array( 813 'page' => 'bp-profile-setup' 814 ), $users_url ); 815 816 // New field group. 817 if ( empty( $this->id ) ) { 818 $title = __( 'Add New Field Group', 'buddypress' ); 819 $button = __( 'Save', 'buddypress' ); 820 $action = add_query_arg( array( 821 'page' => 'bp-profile-setup', 822 'mode' => 'add_group' 823 ), $users_url ); 824 825 // Existing field group. 826 } else { 827 $title = __( 'Edit Field Group', 'buddypress' ); 828 $button = __( 'Update', 'buddypress' ); 829 $action = add_query_arg( array( 830 'page' => 'bp-profile-setup', 831 'mode' => 'edit_group', 832 'group_id' => (int) $this->id 833 ), $users_url ); 834 835 if ( $this->can_delete ) { 836 // Delete Group URL. 837 $delete_url = wp_nonce_url( add_query_arg( array( 838 'page' => 'bp-profile-setup', 839 'mode' => 'delete_group', 840 'group_id' => (int) $this->id 841 ), $users_url ), 'bp_xprofile_delete_group' ); 842 } 843 } ?> 844 845 <div class="wrap"> 846 847 <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1> 848 <hr class="wp-header-end"> 849 850 <?php if ( ! empty( $message ) ) : ?> 851 852 <div id="message" class="error fade notice is-dismissible"> 853 <p><?php echo esc_html( $message ); ?></p> 854 </div> 855 856 <?php endif; ?> 857 858 <form id="bp-xprofile-add-field-group" action="<?php echo esc_url( $action ); ?>" method="post"> 859 <div id="poststuff"> 860 <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>"> 861 <div id="post-body-content"> 862 <div id="titlediv"> 863 <div class="titlewrap"> 864 <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress') ?></label> 865 <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" /> 866 </div> 867 </div> 868 <div class="postbox"> 869 <h2><?php esc_html_e( 'Field Group Description', 'buddypress' ); ?></h2> 870 <div class="inside"> 871 <label for="group_description" class="screen-reader-text"><?php 872 /* translators: accessibility text */ 873 esc_html_e( 'Add description', 'buddypress' ); 874 ?></label> 875 <textarea name="group_description" id="group_description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea> 876 </div> 877 </div> 878 879 <?php 880 881 /** 882 * Fires after the XProfile group description field is rendered in wp-admin. 883 * 884 * @since 2.6.0 885 * 886 * @param BP_XProfile_Group $this Current XProfile group. 887 */ 888 do_action( 'xprofile_group_admin_after_description', $this ); ?> 889 890 </div><!-- #post-body-content --> 891 892 <div id="postbox-container-1" class="postbox-container"> 893 894 <?php 895 896 /** 897 * Fires before XProfile Group submit metabox. 898 * 899 * @since 2.1.0 900 * 901 * @param BP_XProfile_Group $this Current XProfile group. 902 */ 903 do_action( 'xprofile_group_before_submitbox', $this ); ?> 904 905 <div id="submitdiv" class="postbox"> 906 <h2><?php esc_html_e( 'Submit', 'buddypress' ); ?></h2> 907 <div class="inside"> 908 <div id="submitcomment" class="submitbox"> 909 <div id="major-publishing-actions"> 910 911 <?php 912 913 // Nonce fields. 914 wp_nonce_field( 'bp_xprofile_admin_group', 'bp_xprofile_admin_group' ); 915 916 /** 917 * Fires at the beginning of the XProfile Group publishing actions section. 918 * 919 * @since 2.1.0 920 * 921 * @param BP_XProfile_Group $this Current XProfile group. 922 */ 923 do_action( 'xprofile_group_submitbox_start', $this ); ?> 924 925 <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" /> 926 <div id="publishing-action"> 927 <input type="submit" name="save_group" value="<?php echo esc_attr( $button ); ?>" class="button-primary"/> 928 </div> 929 <div id="delete-action"> 930 <?php if ( ! empty( $this->id ) && isset( $delete_url ) ) : ?> 931 <a href="<?php echo esc_url( $delete_url ); ?>" class="submitdelete deletion"><?php esc_html_e( 'Delete Group', 'buddypress' ); ?></a> 932 <?php endif; ?> 933 934 <div><a href="<?php echo esc_url( $cancel_url ); ?>" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a></div> 935 </div> 936 <div class="clear"></div> 937 </div> 938 </div> 939 </div> 940 </div> 941 942 <?php 943 944 /** 945 * Fires after XProfile Group submit metabox. 946 * 947 * @since 2.1.0 948 * 949 * @param BP_XProfile_Group $this Current XProfile group. 950 */ 951 do_action( 'xprofile_group_after_submitbox', $this ); ?> 952 953 </div> 954 </div> 955 </div> 956 </form> 957 </div> 958 959 <?php 960 } 961 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Jan 19 01:01:32 2021 | Cross-referenced by PHPXref 0.7.1 |