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


Generated: Sun Dec 15 01:01:38 2019 Cross-referenced by PHPXref 0.7.1