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


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1