[ 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       *
 109       * @param array|string $args {
 110       *     An array of arguments. All items are optional.
 111       *
 112       *     @type int          $user_id                 Fetch field data for this user ID.
 113       *     @type string|array $member_type             Limit results to those matching member type(s).
 114       *     @type int          $profile_group_id        Field group to fetch fields & data for.
 115       *     @type int|bool     $hide_empty_groups       Should empty field groups be skipped.
 116       *     @type int|bool     $fetch_fields            Fetch fields for field group.
 117       *     @type int|bool     $fetch_field_data        Fetch field data for fields in group.
 118       *     @type array        $exclude_groups          Exclude these field groups.
 119       *     @type array        $exclude_fields          Exclude these fields.
 120       *     @type int|bool     $hide_empty_fields       Should empty fields be skipped.
 121       *     @type int|bool     $fetch_visibility_level  Fetch visibility levels.
 122       *     @type int|bool     $update_meta_cache       Should metadata cache be updated.
 123       * }
 124       */
 125  	public function __construct( $args = '' ) {
 126  
 127          // Backward compatibility with old method of passing arguments.
 128          if ( ! is_array( $args ) || func_num_args() > 1 ) {
 129              _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__ ) );
 130  
 131              $old_args_keys = array(
 132                  0 => 'user_id',
 133                  1 => 'profile_group_id',
 134                  2 => 'hide_empty_groups',
 135                  3 => 'fetch_fields',
 136                  4 => 'fetch_field_data',
 137                  5 => 'exclude_groups',
 138                  6 => 'exclude_fields',
 139                  7 => 'hide_empty_fields',
 140                  8 => 'fetch_visibility_level',
 141                  9 => 'update_meta_cache'
 142              );
 143  
 144              $args = bp_core_parse_args_array( $old_args_keys, func_get_args() );
 145          }
 146  
 147          $r = wp_parse_args( $args, array(
 148              'profile_group_id'       => false,
 149              'user_id'                => false,
 150              'member_type'            => 'any',
 151              'hide_empty_groups'      => false,
 152              'hide_empty_fields'      => false,
 153              'fetch_fields'           => false,
 154              'fetch_field_data'       => false,
 155              'fetch_visibility_level' => false,
 156              'exclude_groups'         => false,
 157              'exclude_fields'         => false,
 158              'update_meta_cache'      => true
 159          ) );
 160  
 161          $this->groups      = bp_xprofile_get_groups( $r );
 162          $this->group_count = count( $this->groups );
 163          $this->user_id     = $r['user_id'];
 164      }
 165  
 166      /**
 167       * Whether or not the loop has field groups.
 168       *
 169       * @since 1.0.0
 170       *
 171       * @return bool
 172       */
 173  	public function has_groups() {
 174          if ( ! empty( $this->group_count ) ) {
 175              return true;
 176          }
 177  
 178          return false;
 179      }
 180  
 181      /**
 182       * Increments to the next group of fields.
 183       *
 184       * @since 1.0.0
 185       *
 186       * @return object
 187       */
 188  	public function next_group() {
 189          $this->current_group++;
 190  
 191          $this->group       = $this->groups[ $this->current_group ];
 192          $this->field_count = 0;
 193  
 194          if ( ! empty( $this->group->fields ) ) {
 195  
 196              /**
 197               * Filters the group fields for the next_group method.
 198               *
 199               * @since 1.1.0
 200               *
 201               * @param array $fields Array of fields for the group.
 202               * @param int   $id     ID of the field group.
 203               */
 204              $this->group->fields = apply_filters( 'xprofile_group_fields', $this->group->fields, $this->group->id );
 205              $this->field_count   = count( $this->group->fields );
 206          }
 207  
 208          return $this->group;
 209      }
 210  
 211      /**
 212       * Rewinds to the start of the groups list.
 213       *
 214       * @since 1.0.0
 215       */
 216  	public function rewind_groups() {
 217          $this->current_group = -1;
 218          if ( $this->group_count > 0 ) {
 219              $this->group = $this->groups[0];
 220          }
 221      }
 222  
 223      /**
 224       * Kicks off the profile groups.
 225       *
 226       * @since 1.0.0
 227       *
 228       * @return bool
 229       */
 230  	public function profile_groups() {
 231          if ( $this->current_group + 1 < $this->group_count ) {
 232              return true;
 233          } elseif ( $this->current_group + 1 == $this->group_count ) {
 234  
 235              /**
 236               * Fires right before the rewinding of profile groups.
 237               *
 238               * @since 1.1.0
 239               */
 240              do_action( 'xprofile_template_loop_end' );
 241  
 242              // Do some cleaning up after the loop.
 243              $this->rewind_groups();
 244          }
 245  
 246          $this->in_the_loop = false;
 247          return false;
 248      }
 249  
 250      /**
 251       * Sets up the profile group.
 252       *
 253       * @since 1.0.0
 254       */
 255  	public function the_profile_group() {
 256          global $group;
 257  
 258          $this->in_the_loop = true;
 259          $group = $this->next_group();
 260  
 261          // Loop has just started.
 262          if ( 0 === $this->current_group ) {
 263  
 264              /**
 265               * Fires if the current group is the first in the loop.
 266               *
 267               * @since 1.1.0
 268               */
 269              do_action( 'xprofile_template_loop_start' );
 270          }
 271      }
 272  
 273      /** Fields ****************************************************************/
 274  
 275      /**
 276       * Increments to the next field.
 277       *
 278       * @since 1.0.0
 279       *
 280       * @return int
 281       */
 282  	public function next_field() {
 283          $this->current_field++;
 284  
 285          $this->field = $this->group->fields[ $this->current_field ];
 286  
 287          return $this->field;
 288      }
 289  
 290      /**
 291       * Rewinds to the start of the fields.
 292       *
 293       * @since 1.0.0
 294       */
 295  	public function rewind_fields() {
 296          $this->current_field = -1;
 297          if ( $this->field_count > 0 ) {
 298              $this->field = $this->group->fields[0];
 299          }
 300      }
 301  
 302      /**
 303       * Whether or not the loop has fields.
 304       *
 305       * @since 1.0.0
 306       *
 307       * @return bool
 308       */
 309  	public function has_fields() {
 310          $has_data = false;
 311  
 312          for ( $i = 0, $count = count( $this->group->fields ); $i < $count; ++$i ) {
 313              $field = &$this->group->fields[ $i ];
 314  
 315              if ( ! empty( $field->data ) && ( $field->data->value != null ) ) {
 316                  $has_data = true;
 317              }
 318          }
 319  
 320          return $has_data;
 321      }
 322  
 323      /**
 324       * Kick off the profile fields.
 325       *
 326       * @since 1.0.0
 327       *
 328       * @return bool
 329       */
 330  	public function profile_fields() {
 331          if ( $this->current_field + 1 < $this->field_count ) {
 332              return true;
 333          } elseif ( $this->current_field + 1 == $this->field_count ) {
 334              // Do some cleaning up after the loop.
 335              $this->rewind_fields();
 336          }
 337  
 338          return false;
 339      }
 340  
 341      /**
 342       * Set up the profile fields.
 343       *
 344       * @since 1.0.0
 345       */
 346  	public function the_profile_field() {
 347          global $field;
 348  
 349          $field = $this->next_field();
 350  
 351          // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
 352          if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || ( '0' === $field->data->value ) ) ) {
 353              $value = maybe_unserialize( $field->data->value );
 354          } else {
 355              $value = false;
 356          }
 357  
 358          if ( ! empty( $value ) || ( '0' === $value ) ) {
 359              $this->field_has_data = true;
 360          } else {
 361              $this->field_has_data = false;
 362          }
 363      }
 364  }


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1