[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-group.php (source)

   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       * @since 2.4.0 Introduced `$member_type` argument.
 241       * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
 242       *
 243       * @global object $wpdb WordPress DB access object.
 244       *
 245       * @param array $args {
 246       *  Array of optional arguments:
 247       *      @type int          $profile_group_id   Limit results to a single profile group.
 248       *      @type int          $user_id            Required if you want to load a specific user's data.
 249       *                                             Default: displayed user's ID.
 250       *      @type array|string $member_type        Limit fields by those restricted to a given member type, or array of
 251       *                                             member types. If `$user_id` is provided, the value of `$member_type`
 252       *                                             will be overridden by the member types of the provided user. The
 253       *                                             special value of 'any' will return only those fields that are
 254       *                                             unrestricted by member type - i.e., those applicable to any type.
 255       *      @type bool         $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
 256       *      @type bool         $hide_empty_fields  True to hide fields where the user has not provided data.
 257       *                                             Default: false.
 258       *      @type bool         $fetch_fields       Whether to fetch each group's fields. Default: false.
 259       *      @type bool         $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
 260       *                                             Default: false.
 261       *      @type int[]|bool   $exclude_groups     Comma-separated list or array of group IDs to exclude.
 262       *      @type int[]|bool   $exclude_fields     Comma-separated list or array of field IDs to exclude.
 263       *      @type string[]     $hide_field_types   List of field types to hide form loop. Default: empty array.
 264       *      @type bool         $signup_fields_only Whether to only return signup fields. Default: false.
 265       *      @type bool         $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
 266       *                                             and data. Default: true.
 267       * }
 268       * @return array $groups
 269       */
 270  	public static function get( $args = array() ) {
 271          global $wpdb;
 272  
 273          // Parse arguments.
 274          $r = wp_parse_args( $args, array(
 275              'profile_group_id'       => false,
 276              'user_id'                => bp_displayed_user_id(),
 277              'member_type'            => false,
 278              'hide_empty_groups'      => false,
 279              'hide_empty_fields'      => false,
 280              'fetch_fields'           => false,
 281              'fetch_field_data'       => false,
 282              'fetch_visibility_level' => false,
 283              'exclude_groups'         => false,
 284              'exclude_fields'         => false,
 285              'hide_field_types'       => array(),
 286              'update_meta_cache'      => true,
 287              'signup_fields_only'     => false,
 288          ) );
 289  
 290          // Keep track of object IDs for cache-priming.
 291          $object_ids = array(
 292              'group' => array(),
 293              'field' => array(),
 294              'data'  => array(),
 295          );
 296  
 297          $bp = buddypress();
 298  
 299          $group_ids = self::get_group_ids( $r );
 300  
 301          // Get all group data.
 302          $groups = self::get_group_data( $group_ids );
 303  
 304          // Bail if not also getting fields.
 305          if ( empty( $r['fetch_fields'] ) ) {
 306              return $groups;
 307          }
 308  
 309          // Get the group ids from the groups we found.
 310          $group_ids = wp_list_pluck( $groups, 'id' );
 311  
 312          // Store for meta cache priming.
 313          $object_ids['group'] = $group_ids;
 314  
 315          // Bail if no groups found.
 316          if ( empty( $group_ids ) ) {
 317              return $groups;
 318          }
 319  
 320          $field_ids = self::get_group_field_ids( $group_ids, $r );
 321  
 322          foreach( $groups as $group ) {
 323              $group->fields = array();
 324          }
 325  
 326          // Bail if no fields.
 327          if ( empty( $field_ids ) ) {
 328              return $groups;
 329          }
 330  
 331          $field_ids = array_map( 'intval', $field_ids );
 332  
 333          // Prime the field cache.
 334          $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
 335          if ( ! empty( $uncached_field_ids ) ) {
 336              $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
 337              $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
 338              foreach ( $uncached_fields as $uncached_field ) {
 339                  $fid = intval( $uncached_field->id );
 340                  wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
 341              }
 342          }
 343  
 344          // Pull field objects from the cache.
 345          $fields = array();
 346          foreach ( $field_ids as $field_id ) {
 347              if ( true === $r['signup_fields_only'] && ! in_array( $field_id, bp_xprofile_get_signup_field_ids(), true ) ) {
 348                  continue;
 349              }
 350  
 351              $_field = xprofile_get_field( $field_id, null, false );
 352  
 353              if ( in_array( $_field->type, $r['hide_field_types'], true ) ) {
 354                  continue;
 355              }
 356  
 357              $fields[] = $_field;
 358          }
 359  
 360          // Store field IDs for meta cache priming.
 361          $object_ids['field'] = $field_ids;
 362  
 363          // Maybe fetch field data.
 364          if ( ! empty( $r['fetch_field_data'] ) ) {
 365              $field_type_objects = wp_list_pluck( $fields, 'type_obj', 'id' );
 366  
 367              // Get field data for user ID.
 368              if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) {
 369                  $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids, $field_type_objects );
 370              }
 371  
 372              // Remove data-less fields, if necessary.
 373              if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {
 374  
 375                  // Loop through the results and find the fields that have data.
 376                  foreach( (array) $field_data as $data ) {
 377  
 378                      // Empty fields may contain a serialized empty array.
 379                      $maybe_value = maybe_unserialize( $data->value );
 380  
 381                      // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
 382                      if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {
 383  
 384                          // Fields that have data get removed from the list.
 385                          unset( $field_ids[ $key ] );
 386                      }
 387                  }
 388  
 389                  // The remaining members of $field_ids are empty. Remove them.
 390                  foreach( $fields as $field_key => $field ) {
 391                      if ( in_array( $field->id, $field_ids ) ) {
 392                          unset( $fields[ $field_key ] );
 393                      }
 394                  }
 395  
 396                  // Reset indexes.
 397                  $fields = array_values( $fields );
 398              }
 399  
 400              // Field data was found.
 401              if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) {
 402  
 403                  // Loop through fields.
 404                  foreach( (array) $fields as $field_key => $field ) {
 405  
 406                      // Loop through the data in each field.
 407                      foreach( (array) $field_data as $data ) {
 408  
 409                          // Assign correct data value to the field.
 410                          if ( $field->id == $data->field_id ) {
 411                              $fields[ $field_key ]->data        = new stdClass;
 412                              $fields[ $field_key ]->data->value = $data->value;
 413                              $fields[ $field_key ]->data->id    = $data->id;
 414                          }
 415  
 416                          // Store for meta cache priming.
 417                          $object_ids['data'][] = $data->id;
 418                      }
 419                  }
 420              }
 421          }
 422  
 423          // Prime the meta cache, if necessary.
 424          if ( ! empty( $r['update_meta_cache'] ) ) {
 425              bp_xprofile_update_meta_cache( $object_ids );
 426          }
 427  
 428          // Maybe fetch visibility levels.
 429          if ( ! empty( $r['fetch_visibility_level'] ) ) {
 430              $fields = self::fetch_visibility_level( $r['user_id'], $fields );
 431          }
 432  
 433          // Merge the field array back in with the group array.
 434          foreach( (array) $groups as $group ) {
 435              // Indexes may have been shifted after previous deletions, so we get a
 436              // fresh one each time through the loop.
 437              $index = array_search( $group, $groups );
 438  
 439              foreach( (array) $fields as $field ) {
 440                  if ( $group->id === $field->group_id ) {
 441                      $groups[ $index ]->fields[] = $field;
 442                  }
 443              }
 444  
 445              // When we unset fields above, we may have created empty groups.
 446              // Remove them, if necessary.
 447              if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) {
 448                  unset( $groups[ $index ] );
 449              }
 450  
 451              // Reset indexes.
 452              $groups = array_values( $groups );
 453          }
 454  
 455          return $groups;
 456      }
 457  
 458      /**
 459       * Gets group IDs, based on passed parameters.
 460       *
 461       * @since 5.0.0
 462       *
 463       * @param array $args {
 464       *    Array of optional arguments:
 465       *    @type int   $profile_group_id  Limit results to a single profile group. Default false.
 466       *    @type array $exclude_groups    Comma-separated list or array of group IDs to exclude. Default false.
 467       *    @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
 468       * }
 469       * @return array
 470       */
 471  	public static function get_group_ids( $args = array() ) {
 472          global $wpdb;
 473  
 474          $r = array_merge(
 475              array(
 476                  'profile_group_id'  => false,
 477                  'exclude_groups'    => false,
 478                  'hide_empty_groups' => false,
 479              ),
 480              $args
 481          );
 482  
 483          $bp = buddypress();
 484  
 485          if ( ! empty( $r['profile_group_id'] ) ) {
 486              $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
 487          } elseif ( $r['exclude_groups'] ) {
 488              $exclude   = join( ',', wp_parse_id_list( $r['exclude_groups'] ) );
 489              $where_sql = "WHERE g.id NOT IN ({$exclude})";
 490          } else {
 491              $where_sql = '';
 492          }
 493  
 494          // Include or exclude empty groups.
 495          if ( ! empty( $r['hide_empty_groups'] ) ) {
 496              $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";
 497          } else {
 498              $group_ids_sql = "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC";
 499          }
 500  
 501          $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' );
 502          if ( false === $cached ) {
 503              $group_ids = $wpdb->get_col( $group_ids_sql );
 504              bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids );
 505          } else {
 506              $group_ids = $cached;
 507          }
 508  
 509          return array_map( 'intval', $group_ids );
 510      }
 511  
 512      /**
 513       * Gets group field IDs, based on passed parameters.
 514       *
 515       * @since 5.0.0
 516       *
 517       * @param array $group_ids Array of group IDs.
 518       * @param array $args {
 519       *    Array of optional arguments:
 520       *      @type array        $exclude_fields    Comma-separated list or array of field IDs to exclude.
 521       *                                            Default empty.
 522       *      @type int          $user_id           Limit results to fields associated with a given user's
 523       *                                            member type. Default empty.
 524       *      @type array|string $member_type       Limit fields by those restricted to a given member type, or array of
 525       *                                            member types. If `$user_id` is provided, the value of `$member_type`
 526       *                                            is honored.
 527       * }
 528       * @return array
 529       */
 530  	public static function get_group_field_ids( $group_ids, $args = array() ) {
 531          global $wpdb;
 532  
 533          $r = array_merge(
 534              array(
 535                  'exclude_fields' => false,
 536                  'user_id' => false,
 537                  'member_type' => false,
 538              ),
 539              $args
 540          );
 541  
 542          $bp = buddypress();
 543  
 544          // Setup IN query from group IDs.
 545          if ( empty( $group_ids ) ) {
 546              $group_ids = array( 0 );
 547          }
 548          $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) );
 549  
 550          // Support arrays and comma-separated strings.
 551          $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] );
 552  
 553          // Visibility - Handled here so as not to be overridden by sloppy use of the
 554          // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user().
 555          $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] );
 556          $exclude_fields_cs  = array_merge( $exclude_fields_cs, $hidden_user_fields );
 557          $exclude_fields_cs  = implode( ',', $exclude_fields_cs );
 558  
 559          // Set up NOT IN query for excluded field IDs.
 560          if ( ! empty( $exclude_fields_cs ) ) {
 561              $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})";
 562          } else {
 563              $exclude_fields_sql = '';
 564          }
 565  
 566          // Set up IN query for included field IDs.
 567          $include_field_ids = array();
 568  
 569          // Member-type restrictions.
 570          if ( bp_get_member_types() ) {
 571              if ( $r['user_id'] || false !== $r['member_type'] ) {
 572                  $member_types = $r['member_type'];
 573                  if ( $r['user_id'] ) {
 574                      $member_types = bp_get_member_type( $r['user_id'], false );
 575                      if ( empty( $member_types ) ) {
 576                          $member_types = array( 'null' );
 577                      }
 578                  }
 579  
 580                  $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types );
 581                  $include_field_ids += array_keys( $member_types_fields );
 582              }
 583          }
 584  
 585          $in_sql = '';
 586          if ( ! empty( $include_field_ids ) ) {
 587              $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) );
 588              $in_sql = " AND id IN ({$include_field_ids_cs}) ";
 589          }
 590  
 591          $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";
 592  
 593          $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' );
 594          if ( false === $cached ) {
 595              $field_ids = $wpdb->get_col( $field_ids_sql );
 596              bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids );
 597          } else {
 598              $field_ids = $cached;
 599          }
 600  
 601          return array_map( 'intval', $field_ids );
 602      }
 603  
 604      /**
 605       * Get data about a set of groups, based on IDs.
 606       *
 607       * @since 2.0.0
 608       *
 609       * @param array $group_ids Array of IDs.
 610       * @return array
 611       */
 612  	protected static function get_group_data( $group_ids ) {
 613          global $wpdb;
 614  
 615          // Bail if no group IDs are passed.
 616          if ( empty( $group_ids ) ) {
 617              return array();
 618          }
 619  
 620          // Setup empty arrays.
 621          $groups        = array();
 622          $uncached_gids = array();
 623  
 624          // Loop through groups and look for cached & uncached data.
 625          foreach ( $group_ids as $group_id ) {
 626  
 627              // If cached data is found, use it.
 628              $group_data = wp_cache_get( $group_id, 'bp_xprofile_groups' );
 629              if ( false !== $group_data ) {
 630                  $groups[ $group_id ] = $group_data;
 631  
 632              // Otherwise leave a placeholder so we don't lose the order.
 633              } else {
 634                  $groups[ $group_id ] = '';
 635  
 636                  // Add to the list of items to be queried.
 637                  $uncached_gids[] = $group_id;
 638              }
 639          }
 640  
 641          // Fetch uncached data from the DB if necessary.
 642          if ( ! empty( $uncached_gids ) ) {
 643  
 644              // Setup IN query for uncached group data.
 645              $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) );
 646  
 647              // Get table name to query.
 648              $table_name = buddypress()->profile->table_name_groups;
 649  
 650              // Fetch data, preserving order.
 651              $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )");
 652  
 653              // Make sure query returned valid data.
 654              if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) {
 655  
 656                  // Put queried data into the placeholders created earlier,
 657                  // and add it to the cache.
 658                  foreach ( (array) $queried_gdata as $gdata ) {
 659  
 660                      // Add group to groups array.
 661                      $groups[ $gdata->id ] = $gdata;
 662  
 663                      // Cache previously uncached group data.
 664                      wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' );
 665                  }
 666              }
 667          }
 668  
 669          // Integer casting.
 670          foreach ( (array) $groups as $key => $data ) {
 671              $groups[ $key ]->id          = (int) $groups[ $key ]->id;
 672              $groups[ $key ]->group_order = (int) $groups[ $key ]->group_order;
 673              $groups[ $key ]->can_delete  = (int) $groups[ $key ]->can_delete;
 674          }
 675  
 676          // Reset indexes & return.
 677          return array_values( $groups );
 678      }
 679  
 680      /**
 681       * Validate field group when form submitted.
 682       *
 683       * @since 1.0.0
 684       *
 685       * @global string $message
 686       *
 687       * @return boolean
 688       */
 689  	public static function admin_validate() {
 690          global $message;
 691  
 692          // Validate Form.
 693          if ( empty( $_POST['group_name'] ) ) {
 694              $message = __( 'Please make sure you give the group a name.', 'buddypress' );
 695              return false;
 696          } else {
 697              return true;
 698          }
 699      }
 700  
 701      /**
 702       * Update field group position.
 703       *
 704       * @since 1.5.0
 705       *
 706       * @global $wpdb $wpdb
 707       *
 708       * @param  int $field_group_id ID of the group the field belongs to.
 709       * @param  int $position       Field group position.
 710       * @return boolean
 711       */
 712  	public static function update_position( $field_group_id, $position ) {
 713          global $wpdb;
 714  
 715          if ( ! is_numeric( $position ) ) {
 716              return false;
 717          }
 718  
 719          // Purge profile field group and group query caches.
 720          wp_cache_delete( 'all', 'bp_xprofile_groups' );
 721          bp_core_reset_incrementor( 'bp_xprofile_groups' );
 722  
 723          $bp = buddypress();
 724  
 725          return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET group_order = %d WHERE id = %d", $position, $field_group_id ) );
 726      }
 727  
 728      /**
 729       * Fetch the field visibility level for the fields returned by the query.
 730       *
 731       * @since 1.6.0
 732       *
 733       * @param int   $user_id The profile owner's user_id.
 734       * @param array $fields  The database results returned by the get() query.
 735       * @return array $fields The database results, with field_visibility added
 736       */
 737  	public static function fetch_visibility_level( $user_id = 0, $fields = array() ) {
 738  
 739          // Get the user's visibility level preferences.
 740          $visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true );
 741  
 742          foreach( (array) $fields as $key => $field ) {
 743  
 744              // Does the admin allow this field to be customized?
 745              $visibility   = bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' );
 746              $allow_custom = (bool) ( 'disabled' !== $visibility );
 747  
 748              // Look to see if the user has set the visibility for this field.
 749              if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) {
 750                  $field_visibility = $visibility_levels[ $field->id ];
 751  
 752              // If no admin-set default is saved, fall back on a global default.
 753              } else {
 754                  $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' );
 755  
 756                  /**
 757                   * Filters the XProfile default visibility level for a field.
 758                   *
 759                   * @since 1.6.0
 760                   *
 761                   * @param string $value Default visibility value.
 762                   */
 763                  $field_visibility = ! empty( $fallback_visibility )
 764                      ? $fallback_visibility
 765                      : apply_filters( 'bp_xprofile_default_visibility_level', 'public' );
 766              }
 767  
 768              $fields[ $key ]->visibility_level = $field_visibility;
 769          }
 770  
 771          return $fields;
 772      }
 773  
 774      /**
 775       * Fetch the admin-set preferences for all fields.
 776       *
 777       * @since 1.6.0
 778       *
 779       * @return array $default_visibility_levels An array, keyed by field_id, of default
 780       *                                          visibility level + allow_custom
 781       *                                          (whether the admin allows this
 782       *                                          field to be set by user)
 783       */
 784  	public static function fetch_default_visibility_levels() {
 785          global $wpdb;
 786  
 787          $default_visibility_levels = wp_cache_get( 'default_visibility_levels', 'bp_xprofile' );
 788  
 789          if ( false === $default_visibility_levels ) {
 790              $bp = buddypress();
 791  
 792              $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' )" );
 793  
 794              // Arrange so that the field id is the key and the visibility level the value.
 795              $default_visibility_levels = array();
 796              foreach ( $levels as $level ) {
 797                  switch ( $level->meta_key ) {
 798                      case 'default_visibility' :
 799                          $default_visibility_levels[ $level->object_id ]['default']      = $level->meta_value;
 800                          break;
 801                      case 'allow_custom_visibility' :
 802                          $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value;
 803                          break;
 804                  }
 805              }
 806  
 807              wp_cache_set( 'default_visibility_levels', $default_visibility_levels, 'bp_xprofile' );
 808          }
 809  
 810          return $default_visibility_levels;
 811      }
 812  
 813      /** Admin Output **********************************************************/
 814  
 815      /**
 816       * Output the admin area field group form.
 817       *
 818       * @since 1.0.0
 819       *
 820       * @global string $message
 821       */
 822  	public function render_admin_form() {
 823          global $message;
 824  
 825          // Users Admin URL.
 826          $users_url = bp_get_admin_url( 'users.php' );
 827  
 828          // URL to cancel to.
 829          $cancel_url = add_query_arg( array(
 830              'page' => 'bp-profile-setup'
 831          ), $users_url );
 832  
 833          // New field group.
 834          if ( empty( $this->id ) ) {
 835              $title    = __( 'Add New Field Group', 'buddypress' );
 836              $button    = __( 'Save',                'buddypress' );
 837              $action    = add_query_arg( array(
 838                  'page' => 'bp-profile-setup',
 839                  'mode' => 'add_group'
 840              ), $users_url );
 841  
 842          // Existing field group.
 843          } else {
 844              $title  = __( 'Edit Field Group', 'buddypress' );
 845              $button    = __( 'Update',           'buddypress' );
 846              $action    = add_query_arg( array(
 847                  'page'     => 'bp-profile-setup',
 848                  'mode'     => 'edit_group',
 849                  'group_id' => (int) $this->id
 850              ), $users_url );
 851  
 852              if ( $this->can_delete ) {
 853                  // Delete Group URL.
 854                  $delete_url = wp_nonce_url( add_query_arg( array(
 855                      'page'     => 'bp-profile-setup',
 856                      'mode'     => 'delete_group',
 857                      'group_id' => (int) $this->id
 858                  ), $users_url ), 'bp_xprofile_delete_group' );
 859              }
 860          } ?>
 861  
 862          <div class="wrap">
 863  
 864              <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
 865              <hr class="wp-header-end">
 866  
 867              <?php if ( ! empty( $message ) ) : ?>
 868  
 869                  <div id="message" class="error fade notice is-dismissible">
 870                      <p><?php echo esc_html( $message ); ?></p>
 871                  </div>
 872  
 873              <?php endif; ?>
 874  
 875              <form id="bp-xprofile-add-field-group" action="<?php echo esc_url( $action ); ?>" method="post">
 876                  <div id="poststuff">
 877                      <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>">
 878                          <div id="post-body-content">
 879                              <div id="titlediv">
 880                                  <div class="titlewrap">
 881                                      <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress') ?></label>
 882                                      <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
 883                                  </div>
 884                              </div>
 885                              <div class="postbox">
 886                                  <h2><?php esc_html_e( 'Field Group Description', 'buddypress' ); ?></h2>
 887                                  <div class="inside">
 888                                      <label for="group_description" class="screen-reader-text"><?php
 889                                          /* translators: accessibility text */
 890                                          esc_html_e( 'Add description', 'buddypress' );
 891                                      ?></label>
 892                                      <textarea name="group_description" id="group_description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea>
 893                                  </div>
 894                              </div>
 895  
 896                              <?php
 897  
 898                              /**
 899                               * Fires after the XProfile group description field is rendered in wp-admin.
 900                               *
 901                               * @since 2.6.0
 902                               *
 903                               * @param BP_XProfile_Group $this Current XProfile group.
 904                               */
 905                              do_action( 'xprofile_group_admin_after_description', $this ); ?>
 906  
 907                          </div><!-- #post-body-content -->
 908  
 909                          <div id="postbox-container-1" class="postbox-container">
 910  
 911                              <?php
 912  
 913                              /**
 914                               * Fires before XProfile Group submit metabox.
 915                               *
 916                               * @since 2.1.0
 917                               *
 918                               * @param BP_XProfile_Group $this Current XProfile group.
 919                               */
 920                              do_action( 'xprofile_group_before_submitbox', $this ); ?>
 921  
 922                              <div id="submitdiv" class="postbox">
 923                                  <h2><?php esc_html_e( 'Submit', 'buddypress' ); ?></h2>
 924                                  <div class="inside">
 925                                      <div id="submitcomment" class="submitbox">
 926                                          <div id="major-publishing-actions">
 927  
 928                                              <?php
 929  
 930                                              // Nonce fields.
 931                                              wp_nonce_field( 'bp_xprofile_admin_group', 'bp_xprofile_admin_group' );
 932  
 933                                              /**
 934                                               * Fires at the beginning of the XProfile Group publishing actions section.
 935                                               *
 936                                               * @since 2.1.0
 937                                               *
 938                                               * @param BP_XProfile_Group $this Current XProfile group.
 939                                               */
 940                                              do_action( 'xprofile_group_submitbox_start', $this ); ?>
 941  
 942                                              <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" />
 943                                              <div id="publishing-action">
 944                                                  <input type="submit" name="save_group" value="<?php echo esc_attr( $button ); ?>" class="button-primary"/>
 945                                              </div>
 946                                              <div id="delete-action">
 947                                                  <?php if ( ! empty( $this->id ) && isset( $delete_url ) ) : ?>
 948                                                      <a href="<?php echo esc_url( $delete_url ); ?>" class="submitdelete deletion"><?php esc_html_e( 'Delete Group', 'buddypress' ); ?></a>
 949                                                  <?php endif; ?>
 950  
 951                                                  <div><a href="<?php echo esc_url( $cancel_url ); ?>" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a></div>
 952                                              </div>
 953                                              <div class="clear"></div>
 954                                          </div>
 955                                      </div>
 956                                  </div>
 957                              </div>
 958  
 959                              <?php
 960  
 961                              /**
 962                               * Fires after XProfile Group submit metabox.
 963                               *
 964                               * @since 2.1.0
 965                               *
 966                               * @param BP_XProfile_Group $this Current XProfile group.
 967                               */
 968                              do_action( 'xprofile_group_after_submitbox', $this ); ?>
 969  
 970                          </div>
 971                      </div>
 972                  </div>
 973              </form>
 974          </div>
 975  
 976      <?php
 977      }
 978  }


Generated: Mon Apr 19 01:01:38 2021 Cross-referenced by PHPXref 0.7.1