[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * BuddyPress XProfile Template Loop Class.
   4   *
   5   * @package BuddyPress
   6   * @since 1.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * The main profile template loop class.
  14   *
  15   * This is responsible for loading profile field, group, and data and displaying it.
  16   *
  17   * @since 1.0.0
  18   */
  19  class BP_XProfile_Data_Template {
  20  
  21      /**
  22       * The loop iterator.
  23       *
  24       * @since 1.5.0
  25       * @var int
  26       */
  27      public $current_group = -1;
  28  
  29      /**
  30       * The number of groups returned by the paged query.
  31       *
  32       * @since 1.5.0
  33       * @var int
  34       */
  35      public $group_count;
  36  
  37      /**
  38       * Array of groups located by the query.
  39       *
  40       * @since 1.5.0
  41       * @var array
  42       */
  43      public $groups;
  44  
  45      /**
  46       * The group object currently being iterated on.
  47       *
  48       * @since 1.5.0
  49       * @var object
  50       */
  51      public $group;
  52  
  53      /**
  54       * The current field.
  55       *
  56       * @since 1.5.0
  57       * @var int
  58       */
  59      public $current_field = -1;
  60  
  61      /**
  62       * The field count.
  63       *
  64       * @since 1.5.0
  65       * @var int
  66       */
  67      public $field_count;
  68  
  69      /**
  70       * Field has data.
  71       *
  72       * @since 1.5.0
  73       * @var bool
  74       */
  75      public $field_has_data;
  76  
  77      /**
  78       * The field.
  79       *
  80       * @since 1.5.0
  81       * @var int
  82       */
  83      public $field;
  84  
  85      /**
  86       * A flag for whether the loop is currently being iterated.
  87       *
  88       * @since 1.5.0
  89       * @var bool
  90       */
  91      public $in_the_loop;
  92  
  93      /**
  94       * The user ID.
  95       *
  96       * @since 1.5.0
  97       * @var int
  98       */
  99      public $user_id;
 100  
 101      /**
 102       * Get activity items, as specified by parameters.
 103       *
 104       * @see BP_XProfile_Group::get() for more details about parameters.
 105       *
 106       * @since 1.5.0
 107       * @since 2.4.0 Introduced `$member_type` argument.
 108       * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
 109       *
 110       * @param array|string $args {
 111       *     An array of arguments. All items are optional.
 112       *
 113       *     @type int          $user_id                 Fetch field data for this user ID.
 114       *     @type string|array $member_type             Limit results to those matching member type(s).
 115       *     @type int          $profile_group_id        Field group to fetch fields & data for.
 116       *     @type int|bool     $hide_empty_groups       Should empty field groups be skipped.
 117       *     @type int|bool     $fetch_fields            Fetch fields for field group.
 118       *     @type int|bool     $fetch_field_data        Fetch field data for fields in group.
 119       *     @type array        $exclude_groups          Exclude these field groups.
 120       *     @type array        $exclude_fields          Exclude these fields.
 121       *     @type int|bool     $hide_empty_fields       Should empty fields be skipped.
 122       *     @type int|bool     $fetch_visibility_level  Fetch visibility levels.
 123       *     @type string[]     $hide_field_types        List of field types to hide form loop. Default: empty array.
 124       *     @type bool         $signup_fields_only      Whether to only return signup fields. Default: false.
 125       *     @type int|bool     $update_meta_cache       Should metadata cache be updated.
 126       * }
 127       */
 128  	public function __construct( $args = '' ) {
 129          $function_args = func_get_args();
 130  
 131          // Backward compatibility with old method of passing arguments.
 132          if ( ! is_array( $args ) || count( $function_args ) > 1 ) {
 133              _deprecated_argument( __METHOD__, '2.3.0', sprintf( __( 'Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress' ), __METHOD__, __FILE__ ) );
 134  
 135              $old_args_keys = array(
 136                  0 => 'user_id',
 137                  1 => 'profile_group_id',
 138                  2 => 'hide_empty_groups',
 139                  3 => 'fetch_fields',
 140                  4 => 'fetch_field_data',
 141                  5 => 'exclude_groups',
 142                  6 => 'exclude_fields',
 143                  7 => 'hide_empty_fields',
 144                  8 => 'fetch_visibility_level',
 145                  9 => 'update_meta_cache'
 146              );
 147  
 148              $args = bp_core_parse_args_array( $old_args_keys, $function_args );
 149          }
 150  
 151          $r = bp_parse_args(
 152              $args,
 153              array(
 154                  'profile_group_id'       => false,
 155                  'user_id'                => false,
 156                  'member_type'            => 'any',
 157                  'hide_empty_groups'      => false,
 158                  'hide_empty_fields'      => false,
 159                  'fetch_fields'           => false,
 160                  'fetch_field_data'       => false,
 161                  'fetch_visibility_level' => false,
 162                  'exclude_groups'         => false,
 163                  'exclude_fields'         => false,
 164                  'hide_field_types'       => array(),
 165                  'signup_fields_only'     => false,
 166                  'update_meta_cache'      => true,
 167              )
 168          );
 169  
 170          $groups = bp_xprofile_get_groups( $r );
 171  
 172          if ( true === $r['signup_fields_only'] ) {
 173              $signup_fields_order       = bp_xprofile_get_signup_field_ids();
 174              $signup_group              = new BP_XProfile_Group();
 175              $signup_group->id          = 0;
 176              $signup_group->name        = __( 'Signup Fields', 'buddypress' );
 177              $signup_group->description = '';
 178              $signup_group->can_delete  = 0;
 179              $signup_group->group_order = 0;
 180              $fields                    = array();
 181              $signup_group->fields      = array();
 182  
 183              // Get all group fields.
 184              foreach ( $groups as $group ) {
 185                  if ( ! $group->fields ) {
 186                      continue;
 187                  }
 188  
 189                  // Populate fields using the field ID as key.
 190                  foreach ( $group->fields as $signup_field ) {
 191                      $fields[ $signup_field->id ] = $signup_field;
 192                  }
 193              }
 194  
 195              if ( $fields ) {
 196                  // Reorder signup fields.
 197                  foreach ( $signup_fields_order as $ordered_signup_field_id ) {
 198                      if ( ! isset( $fields[ $ordered_signup_field_id ] ) ) {
 199                          continue;
 200                      }
 201  
 202                      $signup_group->fields[] = $fields[ $ordered_signup_field_id ];
 203                  }
 204              }
 205  
 206              // Override groups with the signup one.
 207              $groups = array( $signup_group );
 208          }
 209  
 210          $this->groups      = $groups;
 211          $this->group_count = count( $this->groups );
 212          $this->user_id     = $r['user_id'];
 213      }
 214  
 215      /**
 216       * Whether or not the loop has field groups.
 217       *
 218       * @since 1.0.0
 219       *
 220       * @return bool
 221       */
 222  	public function has_groups() {
 223          if ( ! empty( $this->group_count ) ) {
 224              return true;
 225          }
 226  
 227          return false;
 228      }
 229  
 230      /**
 231       * Increments to the next group of fields.
 232       *
 233       * @since 1.0.0
 234       *
 235       * @return object
 236       */
 237  	public function next_group() {
 238          $this->current_group++;
 239  
 240          $this->group       = $this->groups[ $this->current_group ];
 241          $this->field_count = 0;
 242  
 243          if ( ! empty( $this->group->fields ) ) {
 244  
 245              /**
 246               * Filters the group fields for the next_group method.
 247               *
 248               * @since 1.1.0
 249               *
 250               * @param array $fields Array of fields for the group.
 251               * @param int   $id     ID of the field group.
 252               */
 253              $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id );
 254              $this->field_count   = count( $this->group->fields );
 255          }
 256  
 257          return $this->group;
 258      }
 259  
 260      /**
 261       * Rewinds to the start of the groups list.
 262       *
 263       * @since 1.0.0
 264       */
 265  	public function rewind_groups() {
 266          $this->current_group = -1;
 267          if ( $this->group_count > 0 ) {
 268              $this->group = $this->groups[0];
 269          }
 270      }
 271  
 272      /**
 273       * Kicks off the profile groups.
 274       *
 275       * @since 1.0.0
 276       *
 277       * @return bool
 278       */
 279  	public function profile_groups() {
 280          if ( $this->current_group + 1 < $this->group_count ) {
 281              return true;
 282          } elseif ( $this->current_group + 1 == $this->group_count ) {
 283  
 284              /**
 285               * Fires right before the rewinding of profile groups.
 286               *
 287               * @since 1.1.0
 288               */
 289              do_action( 'xprofile_template_loop_end' );
 290  
 291              // Do some cleaning up after the loop.
 292              $this->rewind_groups();
 293          }
 294  
 295          $this->in_the_loop = false;
 296          return false;
 297      }
 298  
 299      /**
 300       * Sets up the profile group.
 301       *
 302       * @since 1.0.0
 303       */
 304  	public function the_profile_group() {
 305          global $group;
 306  
 307          $this->in_the_loop = true;
 308          $group = $this->next_group();
 309  
 310          // Loop has just started.
 311          if ( 0 === $this->current_group ) {
 312  
 313              /**
 314               * Fires if the current group is the first in the loop.
 315               *
 316               * @since 1.1.0
 317               */
 318              do_action( 'xprofile_template_loop_start' );
 319          }
 320      }
 321  
 322      /** Fields ****************************************************************/
 323  
 324      /**
 325       * Increments to the next field.
 326       *
 327       * @since 1.0.0
 328       *
 329       * @return int
 330       */
 331  	public function next_field() {
 332          $this->current_field++;
 333  
 334          $this->field = $this->group->fields[ $this->current_field ];
 335  
 336          return $this->field;
 337      }
 338  
 339      /**
 340       * Rewinds to the start of the fields.
 341       *
 342       * @since 1.0.0
 343       */
 344  	public function rewind_fields() {
 345          $this->current_field = -1;
 346          if ( $this->field_count > 0 ) {
 347              $this->field = $this->group->fields[0];
 348          }
 349      }
 350  
 351      /**
 352       * Whether or not the loop has fields.
 353       *
 354       * @since 1.0.0
 355       *
 356       * @return bool
 357       */
 358  	public function has_fields() {
 359          $has_data = false;
 360  
 361          for ( $i = 0, $count = count( $this->group->fields ); $i < $count; ++$i ) {
 362              $field = &$this->group->fields[ $i ];
 363  
 364              if ( ! empty( $field->data ) && ( $field->data->value != null ) ) {
 365                  $has_data = true;
 366              }
 367          }
 368  
 369          return $has_data;
 370      }
 371  
 372      /**
 373       * Kick off the profile fields.
 374       *
 375       * @since 1.0.0
 376       *
 377       * @return bool
 378       */
 379  	public function profile_fields() {
 380          if ( $this->current_field + 1 < $this->field_count ) {
 381              return true;
 382          } elseif ( $this->current_field + 1 == $this->field_count ) {
 383              // Do some cleaning up after the loop.
 384              $this->rewind_fields();
 385          }
 386  
 387          return false;
 388      }
 389  
 390      /**
 391       * Set up the profile fields.
 392       *
 393       * @since 1.0.0
 394       */
 395  	public function the_profile_field() {
 396          global $field;
 397  
 398          $field = $this->next_field();
 399  
 400          // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
 401          if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || ( '0' === $field->data->value ) ) ) {
 402              $value = maybe_unserialize( $field->data->value );
 403          } else {
 404              $value = false;
 405          }
 406  
 407          if ( ! empty( $value ) || ( '0' === $value ) ) {
 408              $this->field_has_data = true;
 409          } else {
 410              $this->field_has_data = false;
 411          }
 412      }
 413  }


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1