[ 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 = wp_parse_args( $args, array(
 152              'profile_group_id'       => false,
 153              'user_id'                => false,
 154              'member_type'            => 'any',
 155              'hide_empty_groups'      => false,
 156              'hide_empty_fields'      => false,
 157              'fetch_fields'           => false,
 158              'fetch_field_data'       => false,
 159              'fetch_visibility_level' => false,
 160              'exclude_groups'         => false,
 161              'exclude_fields'         => false,
 162              'hide_field_types'       => array(),
 163              'signup_fields_only'     => false,
 164              'update_meta_cache'      => true
 165          ) );
 166  
 167          $groups = bp_xprofile_get_groups( $r );
 168  
 169          if ( true === $r['signup_fields_only'] && bp_get_signup_allowed() ) {
 170              $signup_fields_order       = bp_xprofile_get_signup_field_ids();
 171              $signup_group              = new BP_XProfile_Group();
 172              $signup_group->id          = 0;
 173              $signup_group->name        = __( 'Signup Fields', 'buddypress' );
 174              $signup_group->description = '';
 175              $signup_group->can_delete  = 0;
 176              $signup_group->group_order = 0;
 177              $fields                    = array();
 178              $signup_group->fields      = array();
 179  
 180              // Get all group fields.
 181              foreach ( $groups as $group ) {
 182                  if ( ! $group->fields ) {
 183                      continue;
 184                  }
 185  
 186                  // Populate fields using the field ID as key.
 187                  foreach ( $group->fields as $signup_field ) {
 188                      $fields[ $signup_field->id ] = $signup_field;
 189                  }
 190              }
 191  
 192              if ( $fields ) {
 193                  // Reorder signup fields.
 194                  foreach ( $signup_fields_order as $ordered_signup_field_id ) {
 195                      if ( ! isset( $fields[ $ordered_signup_field_id ] ) ) {
 196                          continue;
 197                      }
 198  
 199                      $signup_group->fields[] = $fields[ $ordered_signup_field_id ];
 200                  }
 201              }
 202  
 203              // Override groups with the signup one.
 204              $groups = array( $signup_group );
 205          }
 206  
 207          $this->groups      = $groups;
 208          $this->group_count = count( $this->groups );
 209          $this->user_id     = $r['user_id'];
 210      }
 211  
 212      /**
 213       * Whether or not the loop has field groups.
 214       *
 215       * @since 1.0.0
 216       *
 217       * @return bool
 218       */
 219  	public function has_groups() {
 220          if ( ! empty( $this->group_count ) ) {
 221              return true;
 222          }
 223  
 224          return false;
 225      }
 226  
 227      /**
 228       * Increments to the next group of fields.
 229       *
 230       * @since 1.0.0
 231       *
 232       * @return object
 233       */
 234  	public function next_group() {
 235          $this->current_group++;
 236  
 237          $this->group       = $this->groups[ $this->current_group ];
 238          $this->field_count = 0;
 239  
 240          if ( ! empty( $this->group->fields ) ) {
 241  
 242              /**
 243               * Filters the group fields for the next_group method.
 244               *
 245               * @since 1.1.0
 246               *
 247               * @param array $fields Array of fields for the group.
 248               * @param int   $id     ID of the field group.
 249               */
 250              $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id );
 251              $this->field_count   = count( $this->group->fields );
 252          }
 253  
 254          return $this->group;
 255      }
 256  
 257      /**
 258       * Rewinds to the start of the groups list.
 259       *
 260       * @since 1.0.0
 261       */
 262  	public function rewind_groups() {
 263          $this->current_group = -1;
 264          if ( $this->group_count > 0 ) {
 265              $this->group = $this->groups[0];
 266          }
 267      }
 268  
 269      /**
 270       * Kicks off the profile groups.
 271       *
 272       * @since 1.0.0
 273       *
 274       * @return bool
 275       */
 276  	public function profile_groups() {
 277          if ( $this->current_group + 1 < $this->group_count ) {
 278              return true;
 279          } elseif ( $this->current_group + 1 == $this->group_count ) {
 280  
 281              /**
 282               * Fires right before the rewinding of profile groups.
 283               *
 284               * @since 1.1.0
 285               */
 286              do_action( 'xprofile_template_loop_end' );
 287  
 288              // Do some cleaning up after the loop.
 289              $this->rewind_groups();
 290          }
 291  
 292          $this->in_the_loop = false;
 293          return false;
 294      }
 295  
 296      /**
 297       * Sets up the profile group.
 298       *
 299       * @since 1.0.0
 300       */
 301  	public function the_profile_group() {
 302          global $group;
 303  
 304          $this->in_the_loop = true;
 305          $group = $this->next_group();
 306  
 307          // Loop has just started.
 308          if ( 0 === $this->current_group ) {
 309  
 310              /**
 311               * Fires if the current group is the first in the loop.
 312               *
 313               * @since 1.1.0
 314               */
 315              do_action( 'xprofile_template_loop_start' );
 316          }
 317      }
 318  
 319      /** Fields ****************************************************************/
 320  
 321      /**
 322       * Increments to the next field.
 323       *
 324       * @since 1.0.0
 325       *
 326       * @return int
 327       */
 328  	public function next_field() {
 329          $this->current_field++;
 330  
 331          $this->field = $this->group->fields[ $this->current_field ];
 332  
 333          return $this->field;
 334      }
 335  
 336      /**
 337       * Rewinds to the start of the fields.
 338       *
 339       * @since 1.0.0
 340       */
 341  	public function rewind_fields() {
 342          $this->current_field = -1;
 343          if ( $this->field_count > 0 ) {
 344              $this->field = $this->group->fields[0];
 345          }
 346      }
 347  
 348      /**
 349       * Whether or not the loop has fields.
 350       *
 351       * @since 1.0.0
 352       *
 353       * @return bool
 354       */
 355  	public function has_fields() {
 356          $has_data = false;
 357  
 358          for ( $i = 0, $count = count( $this->group->fields ); $i < $count; ++$i ) {
 359              $field = &$this->group->fields[ $i ];
 360  
 361              if ( ! empty( $field->data ) && ( $field->data->value != null ) ) {
 362                  $has_data = true;
 363              }
 364          }
 365  
 366          return $has_data;
 367      }
 368  
 369      /**
 370       * Kick off the profile fields.
 371       *
 372       * @since 1.0.0
 373       *
 374       * @return bool
 375       */
 376  	public function profile_fields() {
 377          if ( $this->current_field + 1 < $this->field_count ) {
 378              return true;
 379          } elseif ( $this->current_field + 1 == $this->field_count ) {
 380              // Do some cleaning up after the loop.
 381              $this->rewind_fields();
 382          }
 383  
 384          return false;
 385      }
 386  
 387      /**
 388       * Set up the profile fields.
 389       *
 390       * @since 1.0.0
 391       */
 392  	public function the_profile_field() {
 393          global $field;
 394  
 395          $field = $this->next_field();
 396  
 397          // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
 398          if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || ( '0' === $field->data->value ) ) ) {
 399              $value = maybe_unserialize( $field->data->value );
 400          } else {
 401              $value = false;
 402          }
 403  
 404          if ( ! empty( $value ) || ( '0' === $value ) ) {
 405              $this->field_has_data = true;
 406          } else {
 407              $this->field_has_data = false;
 408          }
 409      }
 410  }


Generated: Wed Apr 21 01:01:43 2021 Cross-referenced by PHPXref 0.7.1