Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/01/2015 04:18:13 AM (11 years ago)
Author:
tw2113
Message:

First pass of documentation cleanup for the XProfile Component.

See #6406.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r10142 r10163  
    11<?php
    22/**
    3  * BuddyPress XProfile Classes
     3 * BuddyPress XProfile Classes.
    44 *
    55 * @package BuddyPress
    … …  
    1313
    1414        /**
     15         * Field group ID.
     16         *
    1517         * @since 1.1.0
    1618         *
    … …  
    2022
    2123        /**
     24         * Field group name.
     25         *
    2226         * @since 1.1.0
    2327         *
    … …  
    2731
    2832        /**
     33         * Field group Description.
     34         *
    2935         * @since 1.1.0
    3036         *
    … …  
    3440
    3541        /**
     42         * Group deletion boolean.
     43         *
    3644         * @since 1.1.0
    3745         *
    … …  
    4149
    4250        /**
     51         * Group order.
     52         *
    4353         * @since 1.1.0
    4454         *
    … …  
    4858
    4959        /**
     60         * Group fields.
     61         *
    5062         * @since 1.1.0
    5163         *
    … …  
    5567
    5668        /**
    57          * Initialize and/or populate profile field group
     69         * Initialize and/or populate profile field group.
    5870         *
    5971         * @since 1.1.0
    6072         *
    61          * @param int  $id
    62          * @param int  $user_id
    63          * @param bool $get_data
     73         * @param int|null $id Field group ID.
    6474         */
    6575        public function __construct( $id = null ) {
    … …  
    7080
    7181        /**
    72          * Populate a profile field group
     82         * Populate a profile field group.
    7383         *
    7484         * @since 1.0.0
    7585         *
    7686         * @global $wpdb $wpdb
    77          * @param  int   $id
     87         *
     88         * @param int $id Field group ID.
    7889         *
    7990         * @return boolean
    … …  
    8192        public function populate( $id ) {
    8293
    83                 // Get this group
     94                // Get this group.
    8495                $group = self::get( array(
    8596                        'profile_group_id' => $id
    8697                ) );
    8798
    88                 // Bail if group not found
     99                // Bail if group not found.
    89100                if ( empty( $group ) ) {
    90101                        return false;
    91102                }
    92103
    93                 // Get the first array element
     104                // Get the first array element.
    94105                $group = reset( $group );
    95106
    96                 // Set object properties
     107                // Set object properties.
    97108                $this->id          = $group->id;
    98109                $this->name        = $group->name;
    … …  
    103114
    104115        /**
    105          * Save a profile field group
     116         * Save a profile field group.
    106117         *
    107118         * @since 1.1.0
    … …  
    114125                global $wpdb;
    115126
    116                 // Filter the field group attributes
     127                // Filter the field group attributes.
    117128                $this->name        = apply_filters( 'xprofile_group_name_before_save',        $this->name,        $this->id );
    118129                $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id );
    … …  
    125136                 * @since 1.0.0
    126137                 *
    127                  * @param BP_XProfile_Group Current instance of the group being saved. Passed by reference.
     138                 * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference.
    128139                 */
    129140                do_action_ref_array( 'xprofile_group_before_save', array( &$this ) );
    … …  
    131142                $bp = buddypress();
    132143
    133                 // Update or insert
     144                // Update or insert.
    134145                if ( ! empty( $this->id ) ) {
    135146                        $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id );
    … …  
    138149                }
    139150
    140                 // Attempt to insert or update
     151                // Attempt to insert or update.
    141152                $query = $wpdb->query( $sql );
    142153
    … …  
    146157                }
    147158
    148                 // If not set, update the ID in the group object
     159                // If not set, update the ID in the group object.
    149160                if ( empty( $this->id ) ) {
    150161                        $this->id = $wpdb->insert_id;
    … …  
    156167                 * @since 1.0.0
    157168                 *
    158                  * @param BP_XProfile_Group Current instance of the group being saved. Passed by reference.
     169                 * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference.
    159170                 */
    160171                do_action_ref_array( 'xprofile_group_after_save', array( &$this ) );
    … …  
    174185                global $wpdb;
    175186
    176                 // Bail if field group cannot be deleted
     187                // Bail if field group cannot be deleted.
    177188                if ( empty( $this->can_delete ) ) {
    178189                        return false;
    … …  
    184195                 * @since 2.0.0
    185196                 *
    186                  * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference.
     197                 * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference.
    187198                 */
    188199                do_action_ref_array( 'xprofile_group_before_delete', array( &$this ) );
    … …  
    192203                $deleted = $wpdb->query( $sql );
    193204
    194                 // Delete field group
     205                // Delete field group.
    195206                if ( empty( $deleted ) || is_wp_error( $deleted ) ) {
    196207                        return false;
    … …  
    200211                if ( BP_XProfile_Field::delete_for_group( $this->id ) ) {
    201212
    202                         // Remove profile data for the groups fields
     213                        // Remove profile data for the groups fields.
    203214                        for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) {
    204215                                BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id );
    … …  
    211222                 * @since 2.0.0
    212223                 *
    213                  * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference.
     224                 * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference.
    214225                 */
    215226                do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) );
    … …  
    226237         * @package BuddyPress XProfile
    227238         *
    228          * @global $wpdb WordPress DB access object.
     239         * @global object $wpdb WordPress DB access object.
    229240         *
    230241         * @param array $args {
    231          *      Array of optional arguments:
    232          *      @type int   $profile_group_id   Limit results to a single profile group.
    233          *      @type int   $user_id            Required if you want to load a specific user's data.
    234          *                                      Default: displayed user's ID.
    235          *      @type array|string $member_type Limit fields by those restricted to a given member type, or array of
    236          *                                      member types. If `$user_id` is provided, the value of `$member_type`
    237          *                                      will be overridden by the member types of the provided user. The
    238          *                                      special value of 'any' will return only those fields that are
    239          *                                      unrestricted by member type - i.e., those applicable to any type.
    240          *      @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
    241          *      @type bool  $hide_empty_fields True to hide fields where the user has not provided data.
    242          *                                      Default: false.
    243          *      @type bool  $fetch_fields       Whether to fetch each group's fields. Default: false.
    244          *      @type bool  $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
    245          *                                      Default: false.
    246          *      @type array $exclude_groups     Comma-separated list or array of group IDs to exclude.
    247          *      @type array $exclude_fields     Comma-separated list or array of field IDs to exclude.
    248          *      @type bool  $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
    249          *                                      and data. Default: true.
     242         *  Array of optional arguments:
     243         *      @type int          $profile_group_id  Limit results to a single profile group.
     244         *      @type int          $user_id           Required if you want to load a specific user's data.
     245         *                                            Default: displayed user's ID.
     246         *      @type array|string $member_type       Limit fields by those restricted to a given member type, or array of
     247         *                                            member types. If `$user_id` is provided, the value of `$member_type`
     248         *                                            will be overridden by the member types of the provided user. The
     249         *                                            special value of 'any' will return only those fields that are
     250         *                                            unrestricted by member type - i.e., those applicable to any type.
     251         *      @type bool         $hide_empty_groups True to hide groups that don't have any fields. Default: false.
     252         *      @type bool         $hide_empty_fields True to hide fields where the user has not provided data.
     253         *                                            Default: false.
     254         *      @type bool         $fetch_fields      Whether to fetch each group's fields. Default: false.
     255         *      @type bool         $fetch_field_data  Whether to fetch data for each field. Requires a $user_id.
     256         *                                            Default: false.
     257         *      @type array        $exclude_groups    Comma-separated list or array of group IDs to exclude.
     258         *      @type array        $exclude_fields    Comma-separated list or array of field IDs to exclude.
     259         *      @type bool         $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
     260         *                                            and data. Default: true.
    250261         * }
    251262         * @return array $groups
    … …  
    254265                global $wpdb;
    255266
    256                 // Parse arguments
     267                // Parse arguments.
    257268                $r = wp_parse_args( $args, array(
    258269                        'profile_group_id'       => false,
    … …  
    269280                ) );
    270281
    271                 // Keep track of object IDs for cache-priming
     282                // Keep track of object IDs for cache-priming.
    272283                $object_ids = array(
    273284                        'group' => array(),
    … …  
    276287                );
    277288
    278                 // WHERE
     289                // WHERE.
    279290                if ( ! empty( $r['profile_group_id'] ) ) {
    280291                        $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
    … …  
    288299                $bp = buddypress();
    289300
    290                 // Include or exclude empty groups
     301                // Include or exclude empty groups.
    291302                if ( ! empty( $r['hide_empty_groups'] ) ) {
    292303                        $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" );
    … …  
    295306                }
    296307
    297                 // Get all group data
     308                // Get all group data.
    298309                $groups = self::get_group_data( $group_ids );
    299310
    300                 // Bail if not also getting fields
     311                // Bail if not also getting fields.
    301312                if ( empty( $r['fetch_fields'] ) ) {
    302313                        return $groups;
    303314                }
    304315
    305                 // Get the group ids from the groups we found
     316                // Get the group ids from the groups we found.
    306317                $group_ids = wp_list_pluck( $groups, 'id' );
    307318
    308                 // Store for meta cache priming
     319                // Store for meta cache priming.
    309320                $object_ids['group'] = $group_ids;
    310321
    311                 // Bail if no groups foundS
     322                // Bail if no groups found.
    312323                if ( empty( $group_ids ) ) {
    313324                        return $groups;
    314325                }
    315326
    316                 // Setup IN query from group IDs
     327                // Setup IN query from group IDs.
    317328                $group_ids_in = implode( ',', (array) $group_ids );
    318329
    319                 // Support arrays and comma-separated strings
     330                // Support arrays and comma-separated strings.
    320331                $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] );
    321332
    322333                // Visibility - Handled here so as not to be overridden by sloppy use of the
    323                 // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user()
     334                // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user().
    324335                $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] );
    325336                $exclude_fields_cs  = array_merge( $exclude_fields_cs, $hidden_user_fields );
    … …  
    358369                }
    359370
    360                 // Fetch the fields
     371                // Fetch the fields.
    361372                $fields = $wpdb->get_results( "SELECT id, name, description, type, group_id, is_required FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
    362373
    363374                $field_ids = wp_list_pluck( $fields, 'id' );
    364375
    365                 // Store field IDs for meta cache priming
     376                // Store field IDs for meta cache priming.
    366377                $object_ids['field'] = $field_ids;
    367378
    368                 // Bail if no fields
     379                // Bail if no fields.
    369380                if ( empty( $fields ) ) {
    370381                        return $groups;
    371382                }
    372383
    373                 // Maybe fetch field data
     384                // Maybe fetch field data.
    374385                if ( ! empty( $r['fetch_field_data'] ) ) {
    375386
    376                         // Get field data for user ID
     387                        // Get field data for user ID.
    377388                        if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) {
    378389                                $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids );
    379390                        }
    380391
    381                         // Remove data-less fields, if necessary
     392                        // Remove data-less fields, if necessary.
    382393                        if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {
    383394
    … …  
    385396                                foreach( (array) $field_data as $data ) {
    386397
    387                                         // Empty fields may contain a serialized empty array
     398                                        // Empty fields may contain a serialized empty array.
    388399                                        $maybe_value = maybe_unserialize( $data->value );
    389400
    390                                         // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731
     401                                        // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
    391402                                        if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {
    392403
    393                                                 // Fields that have data get removed from the list
     404                                                // Fields that have data get removed from the list.
    394405                                                unset( $field_ids[ $key ] );
    395406                                        }
    … …  
    403414                                }
    404415
    405                                 // Reset indexes
     416                                // Reset indexes.
    406417                                $fields = array_values( $fields );
    407418                        }
    408419
    409                         // Field data was found
     420                        // Field data was found.
    410421                        if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) {
    411422
    412                                 // Loop through fields
     423                                // Loop through fields.
    413424                                foreach( (array) $fields as $field_key => $field ) {
    414425
    415                                         // Loop through the data in each field
     426                                        // Loop through the data in each field.
    416427                                        foreach( (array) $field_data as $data ) {
    417428
    418                                                 // Assign correct data value to the field
     429                                                // Assign correct data value to the field.
    419430                                                if ( $field->id == $data->field_id ) {
    420431                                                        $fields[ $field_key ]->data        = new stdClass;
    … …  
    423434                                                }
    424435
    425                                                 // Store for meta cache priming
     436                                                // Store for meta cache priming.
    426437                                                $object_ids['data'][] = $data->id;
    427438                                        }
    … …  
    430441                }
    431442
    432                 // Prime the meta cache, if necessary
     443                // Prime the meta cache, if necessary.
    433444                if ( ! empty( $r['update_meta_cache'] ) ) {
    434445                        bp_xprofile_update_meta_cache( $object_ids );
    435446                }
    436447
    437                 // Maybe fetch visibility levels
     448                // Maybe fetch visibility levels.
    438449                if ( ! empty( $r['fetch_visibility_level'] ) ) {
    439450                        $fields = self::fetch_visibility_level( $r['user_id'], $fields );
    440451                }
    441452
    442                 // Merge the field array back in with the group array
     453                // Merge the field array back in with the group array.
    443454                foreach( (array) $groups as $group ) {
    444455
    445456                        // Indexes may have been shifted after previous deletions, so we get a
    446                         // fresh one each time through the loop
     457                        // fresh one each time through the loop.
    447458                        $index = array_search( $group, $groups );
    448459
    … …  
    459470                        }
    460471
    461                         // Reset indexes
     472                        // Reset indexes.
    462473                        $groups = array_values( $groups );
    463474                }
    … …  
    472483         *
    473484         * @param array $group_ids Array of IDs.
     485         *
    474486         * @return array
    475487         */
    … …  
    477489                global $wpdb;
    478490
    479                 // Bail if no group IDs are passed
     491                // Bail if no group IDs are passed.
    480492                if ( empty( $group_ids ) ) {
    481493                        return array();
    482494                }
    483495
    484                 // Setup empty arrays
     496                // Setup empty arrays.
    485497                $groups        = array();
    486498                $uncached_gids = array();
    487499
    488                 // Loop through groups and look for cached & uncached data
     500                // Loop through groups and look for cached & uncached data.
    489501                foreach ( $group_ids as $group_id ) {
    490502
    491                         // If cached data is found, use it
     503                        // If cached data is found, use it.
    492504                        $group_data = wp_cache_get( $group_id, 'bp_xprofile_groups' );
    493505                        if ( false !== $group_data ) {
    494506                                $groups[ $group_id ] = $group_data;
    495507
    496                         // Otherwise leave a placeholder so we don't lose the order
     508                        // Otherwise leave a placeholder so we don't lose the order.
    497509                        } else {
    498510                                $groups[ $group_id ] = '';
    499511
    500                                 // Add to the list of items to be queried
     512                                // Add to the list of items to be queried.
    501513                                $uncached_gids[] = $group_id;
    502514                        }
    503515                }
    504516
    505                 // Fetch uncached data from the DB if necessary
     517                // Fetch uncached data from the DB if necessary.
    506518                if ( ! empty( $uncached_gids ) ) {
    507519
    508                         // Setup IN query for uncached group data
     520                        // Setup IN query for uncached group data.
    509521                        $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) );
    510522
    511                         // Get table name to query
     523                        // Get table name to query.
    512524                        $table_name = buddypress()->profile->table_name_groups;
    513525
    514                         // Fetch data, preserving order
     526                        // Fetch data, preserving order.
    515527                        $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )");
    516528
    517                         // Make sure query returned valid data
     529                        // Make sure query returned valid data.
    518530                        if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) {
    519531
    520532                                // Put queried data into the placeholders created earlier,
    521                                 // and add it to the cache
     533                                // and add it to the cache.
    522534                                foreach ( (array) $queried_gdata as $gdata ) {
    523535
    524                                         // Add group to groups array
     536                                        // Add group to groups array.
    525537                                        $groups[ $gdata->id ] = $gdata;
    526538
    527                                         // Cache previously uncached group data
     539                                        // Cache previously uncached group data.
    528540                                        wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' );
    529541                                }
    … …  
    531543                }
    532544
    533                 // Reset indexes & return
     545                // Reset indexes & return.
    534546                return array_values( $groups );
    535547        }
    536548
    537549        /**
    538          * Validate field group when form submitted
     550         * Validate field group when form submitted.
    539551         *
    540552         * @since 1.0.0
    … …  
    547559                global $message;
    548560
    549                 // Validate Form
     561                // Validate Form.
    550562                if ( empty( $_POST['group_name'] ) ) {
    551563                        $message = __( 'Please make sure you give the group a name.', 'buddypress' );
    … …  
    557569
    558570        /**
    559          * Update field group position
     571         * Update field group position.
    560572         *
    561573         * @since 1.5.0
    562574         *
    563575         * @global $wpdb $wpdb
    564          * @param  int $field_group_id
    565          * @param  int $position
     576         * @param  int $field_group_id ID of the group the field belongs to.
     577         * @param  int $position       Field group position.
    566578         *
    567579         * @return boolean
    … …  
    574586                }
    575587
    576                 // Purge profile field group cache
     588                // Purge profile field group cache.
    577589                wp_cache_delete( 'all', 'bp_xprofile_groups' );
    578590
    … …  
    583595
    584596        /**
    585          * Fetch the field visibility level for the fields returned by the query
     597         * Fetch the field visibility level for the fields returned by the query.
    586598         *
    587599         * @since 1.6.0
    588600         *
    589          * @param int $user_id The profile owner's user_id
    590          * @param array $fields The database results returned by the get() query
     601         * @param int   $user_id The profile owner's user_id.
     602         * @param array $fields  The database results returned by the get() query.
     603         *
    591604         * @return array $fields The database results, with field_visibility added
    592605         */
    593606        public static function fetch_visibility_level( $user_id = 0, $fields = array() ) {
    594607
    595                 // Get the user's visibility level preferences
     608                // Get the user's visibility level preferences.
    596609                $visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true );
    597610
    … …  
    602615                        $allow_custom = (bool) ( 'disabled' !== $visibility );
    603616
    604                         // Look to see if the user has set the visibility for this field
     617                        // Look to see if the user has set the visibility for this field.
    605618                        if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) {
    606619                                $field_visibility = $visibility_levels[ $field->id ];
    607620
    608                         // If no admin-set default is saved, fall back on a global default
     621                        // If no admin-set default is saved, fall back on a global default.
    609622                        } else {
    610623                                $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' );
    … …  
    633646         * @since 1.6.0
    634647         *
    635          * @return array $default_visibility_levels An array, keyed by
    636          *         field_id, of default visibility level + allow_custom
    637          *         (whether the admin allows this field to be set by user)
     648         * @return array $default_visibility_levels An array, keyed by field_id, of default
     649         *                                          visibility level + allow_custom
     650         *                                          (whether the admin allows this
     651         *                                          field to be set by user)
    638652         */
    639653        public static function fetch_default_visibility_levels() {
    … …  
    647661                        $levels = $wpdb->get_results( "SELECT object_id, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND ( meta_key = 'default_visibility' OR meta_key = 'allow_custom_visibility' )" );
    648662
    649                         // Arrange so that the field id is the key and the visibility level the value
     663                        // Arrange so that the field id is the key and the visibility level the value.
    650664                        $default_visibility_levels = array();
    651665                        foreach ( $levels as $level ) {
    … …  
    669683
    670684        /**
    671          * Output the admin area field group form
     685         * Output the admin area field group form.
    672686         *
    673687         * @since 1.0.0
    … …  
    678692                global $message;
    679693
    680                 // New field group
     694                // New field group.
    681695                if ( empty( $this->id ) ) {
    682696                        $title  = __( 'Add New Field Group', 'buddypress' );
    … …  
    684698                        $button = __( 'Save', 'buddypress' );
    685699
    686                 // Existing field group
     700                // Existing field group.
    687701                } else {
    688702                        $title  = __( 'Edit Field Group', 'buddypress' );
Note: See TracChangeset for help on using the changeset viewer.