Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/03/2015 04:22:14 PM (11 years ago)
Author:
johnjamesjacoby
Message:

XProfile: Refactor bp_has_profile() stack to support passing arguments as associative arrays.

  • Maintains backward compatibility by parsing old-style parameters using bp_core_parse_args_array(), converting to new-style array, and throwing _deprecated_argument() error when old-style is used
  • Changes calls to BP_XProfile_Data_Template::__construct() to use the new format
  • Hat-tip boonebgorges for similar work to other components previously.

See #6347.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/bp-xprofile-template.php

    r9685 r9686  
    1212
    1313class BP_XProfile_Data_Template {
     14
    1415    /**
    1516     * The loop iterator.
     
    102103        public $user_id;
    103104
    104         function __construct( $user_id, $profile_group_id, $hide_empty_groups = false, $fetch_fields = false, $fetch_field_data = false, $exclude_groups = false, $exclude_fields = false, $hide_empty_fields = false, $fetch_visibility_level = false, $update_meta_cache = true ) {
    105                 $this->groups = bp_xprofile_get_groups( array(
    106                         'profile_group_id'    => $profile_group_id,
    107                         'user_id'             => $user_id,
    108                         'hide_empty_groups'   => $hide_empty_groups,
    109                         'hide_empty_fields'   => $hide_empty_fields,
    110                         'fetch_fields'        => $fetch_fields,
    111                         'fetch_field_data'    => $fetch_field_data,
    112                         'fetch_visibility_level' => $fetch_visibility_level,
    113                         'exclude_groups'      => $exclude_groups,
    114                         'exclude_fields'      => $exclude_fields,
    115                         'update_meta_cache'   => $update_meta_cache,
     105        /**
     106         * Get activity items, as specified by parameters
     107         *
     108         * @param array $args {
     109         *     An array of arguments. All items are optional.
     110         *
     111         *     @type int       $user_id                 Fetch field data for this user ID
     112         *     @type int       $profile_group_id        Field group to fetch fields & data for
     113         *     @type int|bool  $hide_empty_groups       Should empty field groups be skipped
     114         *     @type int|bool  $fetch_fields            Fetch fields for field group
     115         *     @type int|bool  $fetch_field_data        Fetch field data for fields in group
     116         *     @type array     $exclude_groups          Exclude these field groups
     117         *     @type array     $exclude_fields          Exclude these fields
     118         *     @type int|bool  $hide_empty_fields       Should empty fields be skipped
     119         *     @type int|bool  $fetch_visibility_level  Fetch visibility levels
     120         *     @type int|bool  $update_meta_cache       Should metadata cache be updated
     121         * }
     122         * @return array The array returned has two keys:
     123         *     - 'total' is the count of located activities
     124         *     - 'activities' is an array of the located activities
     125         */
     126        public function __construct( $args = '' ) {
     127
     128                // Backward compatibility with old method of passing arguments
     129                if ( ! is_array( $args ) || func_num_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                        $func_args = func_get_args();
     146                        $args      = bp_core_parse_args_array( $old_args_keys, $func_args );
     147                }
     148
     149                $r = wp_parse_args( $args, array(
     150                        'profile_group_id'       => false,
     151                        'user_id'                => false,
     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
    116160                ) );
    117161
    118                 $this->group_count = count($this->groups);
    119                 $this->user_id = $user_id;
    120         }
    121 
    122         function has_groups() {
    123                 if ( $this->group_count )
     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        public function has_groups() {
     168                if ( ! empty( $this->group_count ) ) {
    124169                        return true;
     170                }
    125171
    126172                return false;
    127173        }
    128174
    129         function next_group() {
     175        public function next_group() {
    130176                $this->current_group++;
    131177
    132                 $this->group       = $this->groups[$this->current_group];
     178                $this->group       = $this->groups[ $this->current_group ];
    133179                $this->field_count = 0;
    134180
    135                 if( ! empty( $this->group->fields ) ) {
     181                if ( ! empty( $this->group->fields ) ) {
    136182
    137183                        /**
     
    150196        }
    151197
    152         function rewind_groups() {
     198        public function rewind_groups() {
    153199                $this->current_group = -1;
    154200                if ( $this->group_count > 0 ) {
     
    157203        }
    158204
    159         function profile_groups() {
     205        public function profile_groups() {
    160206                if ( $this->current_group + 1 < $this->group_count ) {
    161207                        return true;
     
    167213                         * @since BuddyPress (1.1.0)
    168214                         */
    169                         do_action('xprofile_template_loop_end');
     215                        do_action( 'xprofile_template_loop_end' );
     216
    170217                        // Do some cleaning up after the loop
    171218                        $this->rewind_groups();
     
    176223        }
    177224
    178         function the_profile_group() {
     225        public function the_profile_group() {
    179226                global $group;
    180227
     
    182229                $group = $this->next_group();
    183230
    184                 if ( 0 == $this->current_group ) // loop has just started
     231                // loop has just started
     232                if ( 0 === $this->current_group ) {
    185233
    186234                        /**
     
    189237                         * @since BuddyPress (1.1.0)
    190238                         */
    191                         do_action('xprofile_template_loop_start');
    192         }
    193 
    194         /**** FIELDS ****/
    195 
    196         function next_field() {
     239                        do_action( 'xprofile_template_loop_start' );
     240                }
     241        }
     242
     243        /** Fields ****************************************************************/
     244
     245        public function next_field() {
    197246                $this->current_field++;
    198247
    199                 $this->field = $this->group->fields[$this->current_field];
     248                $this->field = $this->group->fields[ $this->current_field ];
     249
    200250                return $this->field;
    201251        }
    202252
    203         function rewind_fields() {
     253        public function rewind_fields() {
    204254                $this->current_field = -1;
    205255                if ( $this->field_count > 0 ) {
     
    208258        }
    209259
    210         function has_fields() {
     260        public function has_fields() {
    211261                $has_data = false;
    212262
    213263                for ( $i = 0, $count = count( $this->group->fields ); $i < $count; ++$i ) {
    214                         $field = &$this->group->fields[$i];
    215 
    216                         if ( !empty( $field->data ) && $field->data->value != null ) {
     264                        $field = &$this->group->fields[ $i ];
     265
     266                        if ( ! empty( $field->data ) && ( $field->data->value != null ) ) {
    217267                                $has_data = true;
    218268                        }
    219269                }
    220270
    221                 if ( $has_data )
    222                         return true;
    223 
    224                 return false;
    225         }
    226 
    227         function profile_fields() {
     271                return $has_data;
     272        }
     273
     274        public function profile_fields() {
    228275                if ( $this->current_field + 1 < $this->field_count ) {
    229276                        return true;
     
    236283        }
    237284
    238         function the_profile_field() {
     285        public function the_profile_field() {
    239286                global $field;
    240287
     
    242289
    243290                // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731
    244                 if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || '0' == $field->data->value ) ) {
     291                if ( ! empty( $field->data ) && ( ! empty( $field->data->value ) || ( '0' === $field->data->value ) ) ) {
    245292                        $value = maybe_unserialize( $field->data->value );
    246293                } else {
     
    248295                }
    249296
    250                 if ( ! empty( $value ) || '0' == $value ) {
     297                if ( ! empty( $value ) || ( '0' === $value ) ) {
    251298                        $this->field_has_data = true;
    252299                } else {
     
    295342
    296343        // Populate the template loop global
    297         $profile_template = new BP_XProfile_Data_Template(
    298                 $r['user_id'],
    299                 $r['profile_group_id'],
    300                 $r['hide_empty_groups'],
    301                 $r['fetch_fields'],
    302                 $r['fetch_field_data'],
    303                 $r['exclude_groups'],
    304                 $r['exclude_fields'],
    305                 $r['hide_empty_fields'],
    306                 $r['fetch_visibility_level'],
    307                 $r['update_meta_cache']
    308         );
     344        $profile_template = new BP_XProfile_Data_Template( $r );
    309345
    310346        /**
Note: See TracChangeset for help on using the changeset viewer.