Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/01/2015 02:23:20 PM (9 years ago)
Author:
johnjamesjacoby
Message:

XProfile: Improvemets to BP_XProfile_Group:

  • PHPDoc all variables and methods
  • Remove extract() usage
  • Clean up code formatting
  • No functional changes at this time
  • Also small clean up to BP_XProfile_Field docs

See #6318.

File:
1 edited

Legend:

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

    r9635 r9671  
    1111
    1212class BP_XProfile_Group {
     13   
     14    /**
     15     * @since BuddyPress (1.1.0)
     16     *
     17     * @var int ID of field group
     18     */
    1319    public $id = null;
     20   
     21    /**
     22     * @since BuddyPress (1.1.0)
     23     *
     24     * @var string Name of field group
     25     */
    1426    public $name;
     27   
     28    /**
     29     * @since BuddyPress (1.1.0)
     30     *
     31     * @var string Description of field group
     32     */
    1533    public $description;
     34   
     35    /**
     36     * @since BuddyPress (1.1.0)
     37     *
     38     * @var bool Can this group be deleted?
     39     */
    1640    public $can_delete;
     41   
     42    /**
     43     * @since BuddyPress (1.1.0)
     44     *
     45     * @var int Group order relative to other groups
     46     */
    1747    public $group_order;
     48   
     49    /**
     50     * @since BuddyPress (1.1.0)
     51     *
     52     * @var array Fields of group
     53     */
    1854    public $fields;
    1955
     56    /**
     57     * Initialize and/or populate profile field group
     58     *
     59     * @since BuddyPress (1.1.0)
     60     *
     61     * @param int  $id
     62     * @param int  $user_id
     63     * @param bool $get_data
     64     */
    2065    public function __construct( $id = null ) {
    21         if ( !empty( $id ) )
     66        if ( ! empty( $id ) ) {
    2267            $this->populate( $id );
    23     }
    24 
     68        }
     69    }
     70
     71    /**
     72     * Populate a profile field group
     73     *
     74     * @since BuddyPress (1.0.0)
     75     *
     76     * @global $wpdb $wpdb
     77     * @param  int   $id
     78     *
     79     * @return boolean
     80     */
    2581    public function populate( $id ) {
    2682        global $wpdb;
     
    44100    }
    45101
     102    /**
     103     * Save a profile field group
     104     *
     105     * @since BuddyPress (1.1.0)
     106     *
     107     * @global object $wpdb
     108     *
     109     * @return boolean
     110     */
    46111    public function save() {
    47112        global $wpdb;
     
    63128        $bp = buddypress();
    64129
    65         if ( $this->id )
     130        if ( ! empty( $this->id ) ) {
    66131            $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id );
    67         else
     132        } else {
    68133            $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_groups} (name, description, can_delete) VALUES (%s, %s, 1)", $this->name, $this->description );
    69 
    70         if ( is_wp_error( $wpdb->query( $sql ) ) )
     134        }
     135
     136        // Bail if query fails
     137        if ( is_wp_error( $wpdb->query( $sql ) ) ) {
    71138            return false;
     139        }
    72140
    73141        // If not set, update the ID in the group object
    74         if ( ! $this->id )
     142        if ( empty( $this->id ) ) {
    75143            $this->id = $wpdb->insert_id;
     144        }
    76145
    77146        /**
     
    87156    }
    88157
     158    /**
     159     * Delete a profile field group
     160     *
     161     * @since BuddyPress (1.1.0)
     162     *
     163     * @global object  $wpdb
     164     * @return boolean
     165     */
    89166    public function delete() {
    90167        global $wpdb;
    91168
    92         if ( empty( $this->can_delete ) )
     169        // Bail if field group cannot be deleted
     170        if ( empty( $this->can_delete ) ) {
    93171            return false;
     172        }
    94173
    95174        /**
     
    102181        do_action_ref_array( 'xprofile_group_before_delete', array( &$this ) );
    103182
    104         $bp = buddypress();
     183        $bp      = buddypress();
     184        $sql     = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id );
     185        $deleted = $wpdb->query( $sql );
    105186
    106187        // Delete field group
    107         if ( !$wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id ) ) ) {
     188        if ( empty( $deleted ) || is_wp_error( $deleted ) ) {
    108189            return false;
    109         } else {
    110 
    111             // Remove the group's fields.
    112             if ( BP_XProfile_Field::delete_for_group( $this->id ) ) {
    113 
    114                 // Remove profile data for the groups fields
    115                 for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) {
    116                     BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id );
    117                 }
    118             }
    119 
    120             /**
    121              * Fires after the current group instance gets deleted.
    122              *
    123              * @since BuddyPress (2.0.0)
    124              *
    125              * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference.
    126              */
    127             do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) );
    128 
    129             return true;
    130         }
     190        }
     191
     192        // Remove the group's fields.
     193        if ( BP_XProfile_Field::delete_for_group( $this->id ) ) {
     194
     195            // Remove profile data for the groups fields
     196            for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) {
     197                BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id );
     198            }
     199        }
     200
     201        /**
     202         * Fires after the current group instance gets deleted.
     203         *
     204         * @since BuddyPress (2.0.0)
     205         *
     206         * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference.
     207         */
     208        do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) );
     209
     210        return true;
    131211    }
    132212
     
    134214
    135215    /**
    136      * get()
    137      *
    138      * Populates the BP_XProfile_Group object with profile field groups, fields, and field data
     216     * Populates the BP_XProfile_Group object with profile field groups, fields,
     217     * and field data
    139218     *
    140219     * @package BuddyPress XProfile
    141220     *
    142221     * @global $wpdb WordPress DB access object.
    143      * @global BuddyPress $bp The one true BuddyPress instance
    144222     *
    145223     * @param array $args {
     
    169247        global $wpdb;
    170248
    171         $defaults = array(
     249        // Parse arguments
     250        $r = wp_parse_args( $args, array(
    172251            'profile_group_id'       => false,
    173252            'user_id'                => bp_displayed_user_id(),
     
    180259            'exclude_fields'         => false,
    181260            'update_meta_cache'      => true,
    182         );
    183 
    184         $r = wp_parse_args( $args, $defaults );
    185         extract( $r, EXTR_SKIP );
     261        ) );
    186262
    187263        // Keep track of object IDs for cache-priming
     
    192268        );
    193269
    194         $where_sql = '';
    195 
    196         if ( ! empty( $profile_group_id ) ) {
    197             $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $profile_group_id );
    198         } elseif ( $exclude_groups ) {
    199             $exclude_groups = join( ',', wp_parse_id_list( $exclude_groups ) );
    200             $where_sql = "WHERE g.id NOT IN ({$exclude_groups})";
     270        // WHERE
     271        if ( ! empty( $r['profile_group_id'] ) ) {
     272            $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
     273        } elseif ( $r['exclude_groups'] ) {
     274            $exclude   = join( ',', wp_parse_id_list( $r['exclude_groups'] ) );
     275            $where_sql = "WHERE g.id NOT IN ({$exclude})";
     276        } else {
     277            $where_sql = '';
    201278        }
    202279
    203280        $bp = buddypress();
    204281
    205         if ( ! empty( $hide_empty_groups ) ) {
     282        // Include or exclude empty groups
     283        if ( ! empty( $r['hide_empty_groups'] ) ) {
    206284            $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" );
    207285        } else {
     
    209287        }
    210288
     289        // Get all group data
    211290        $groups = self::get_group_data( $group_ids );
    212291
    213         if ( empty( $fetch_fields ) )
     292        // Bail if not also getting fields
     293        if ( empty( $r['fetch_fields'] ) ) {
    214294            return $groups;
    215 
    216         // Get the group ids
    217         $group_ids = array();
    218         foreach( (array) $groups as $group ) {
    219             $group_ids[] = $group->id;
    220         }
     295        }
     296
     297        // Get the group ids from the groups we found
     298        $group_ids = wp_list_pluck( $groups, 'id' );
    221299
    222300        // Store for meta cache priming
    223301        $object_ids['group'] = $group_ids;
    224302
    225         $group_ids = implode( ',', (array) $group_ids );
    226 
    227         if ( empty( $group_ids ) )
     303        // Bail if no groups foundS
     304        if ( empty( $group_ids ) ) {
    228305            return $groups;
     306        }
     307
     308        // Setup IN query from group IDs
     309        $group_ids_in = implode( ',', (array) $group_ids );
    229310
    230311        // Support arrays and comma-separated strings
    231         $exclude_fields_cs = wp_parse_id_list( $exclude_fields );
     312        $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] );
    232313
    233314        // Visibility - Handled here so as not to be overridden by sloppy use of the
    234315        // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user()
    235         $exclude_fields_cs = array_merge( $exclude_fields_cs, bp_xprofile_get_hidden_fields_for_user( $user_id ) );
    236         $exclude_fields_cs = implode( ',', $exclude_fields_cs );
    237 
    238         if ( !empty( $exclude_fields_cs ) ) {
     316        $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] );
     317        $exclude_fields_cs  = array_merge( $exclude_fields_cs, $hidden_user_fields );
     318        $exclude_fields_cs  = implode( ',', $exclude_fields_cs );
     319
     320        // Setup IN query for field IDs
     321        if ( ! empty( $exclude_fields_cs ) ) {
    239322            $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})";
    240323        } else {
     
    243326
    244327        // Fetch the fields
    245         $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} ) AND parent_id = 0 {$exclude_fields_sql} ORDER BY field_order" );
     328        $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} ORDER BY field_order" );
     329        $field_ids = wp_list_pluck( $fields, 'id' );
    246330
    247331        // Store field IDs for meta cache priming
    248         $object_ids['field'] = wp_list_pluck( $fields, 'id' );
    249 
    250         if ( empty( $fields ) )
     332        $object_ids['field'] = $field_ids;
     333
     334        // Bail if no fields
     335        if ( empty( $fields ) ) {
    251336            return $groups;
     337        }
    252338
    253339        // Maybe fetch field data
    254         if ( ! empty( $fetch_field_data ) ) {
    255 
    256             // Fetch the field data for the user.
    257             foreach( (array) $fields as $field ) {
    258                 $field_ids[] = $field->id;
    259             }
    260 
    261             $field_ids_sql = implode( ',', (array) $field_ids );
    262 
    263             if ( ! empty( $field_ids ) && ! empty( $user_id ) ) {
    264                 $field_data = BP_XProfile_ProfileData::get_data_for_user( $user_id, $field_ids );
     340        if ( ! empty( $r['fetch_field_data'] ) ) {
     341
     342            // Get field data for user ID
     343            if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) {
     344                $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids );
    265345            }
    266346
    267347            // Remove data-less fields, if necessary
    268             if ( !empty( $hide_empty_fields ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {
     348            if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) {
    269349
    270350                // Loop through the results and find the fields that have data.
     
    278358
    279359                        // Fields that have data get removed from the list
    280                         unset( $field_ids[$key] );
     360                        unset( $field_ids[ $key ] );
    281361                    }
    282362                }
     
    285365                foreach( $fields as $field_key => $field ) {
    286366                    if ( in_array( $field->id, $field_ids ) ) {
    287                         unset( $fields[$field_key] );
     367                        unset( $fields[ $field_key ] );
    288368                    }
    289369                }
     
    294374
    295375            // Field data was found
    296             if ( ! empty( $fields ) && !empty( $field_data ) && !is_wp_error( $field_data ) ) {
     376            if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) {
    297377
    298378                // Loop through fields
     
    304384                        // Assign correct data value to the field
    305385                        if ( $field->id == $data->field_id ) {
    306                             $fields[$field_key]->data        = new stdClass;
    307                             $fields[$field_key]->data->value = $data->value;
    308                             $fields[$field_key]->data->id    = $data->id;
     386                            $fields[ $field_key ]->data        = new stdClass;
     387                            $fields[ $field_key ]->data->value = $data->value;
     388                            $fields[ $field_key ]->data->id    = $data->id;
    309389                        }
    310390
     
    317397
    318398        // Prime the meta cache, if necessary
    319         if ( $update_meta_cache ) {
     399        if ( ! empty( $r['update_meta_cache'] ) ) {
    320400            bp_xprofile_update_meta_cache( $object_ids );
    321401        }
    322402
    323403        // Maybe fetch visibility levels
    324         if ( !empty( $fetch_visibility_level ) ) {
    325             $fields = self::fetch_visibility_level( $user_id, $fields );
     404        if ( ! empty( $r['fetch_visibility_level'] ) ) {
     405            $fields = self::fetch_visibility_level( $r['user_id'], $fields );
    326406        }
    327407
     
    334414
    335415            foreach( (array) $fields as $field ) {
    336                 if ( $group->id == $field->group_id ) {
    337                     $groups[$index]->fields[] = $field;
     416                if ( $group->id === $field->group_id ) {
     417                    $groups[ $index ]->fields[] = $field;
    338418                }
    339419            }
     
    341421            // When we unset fields above, we may have created empty groups.
    342422            // Remove them, if necessary.
    343             if ( empty( $group->fields ) && $hide_empty_groups ) {
    344                 unset( $groups[$index] );
     423            if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) {
     424                unset( $groups[ $index ] );
    345425            }
    346426
     
    368448        }
    369449
     450        // Setup empty arrays
    370451        $groups        = array();
    371452        $uncached_gids = array();
    372453
     454        // Loop through groups and look for cached & uncached data
    373455        foreach ( $group_ids as $group_id ) {
    374456
    375457            // If cached data is found, use it
    376             if ( $group_data = wp_cache_get( 'xprofile_group_' . $group_id, 'bp' ) ) {
     458            $cache_key  = 'xprofile_group_' . $group_id;
     459            $group_data = wp_cache_get( $cache_key, 'bp' );
     460            if ( false !== $group_data ) {
    377461                $groups[ $group_id ] = $group_data;
    378462
     
    388472        // Fetch uncached data from the DB if necessary
    389473        if ( ! empty( $uncached_gids ) ) {
     474
     475            // Setup IN query for uncached group data
    390476            $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) );
    391477
    392             $bp = buddypress();
     478            // Get table name to query
     479            $table_name = buddypress()->profile->table_name_groups;
    393480
    394481            // Fetch data, preserving order
    395             $queried_gdata = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_groups} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )");
    396 
    397             // Put queried data into the placeholders created earlier,
    398             // and add it to the cache
    399             foreach ( (array) $queried_gdata as $gdata ) {
    400                 $groups[ $gdata->id ] = $gdata;
    401                 wp_cache_set( 'xprofile_group_' . $gdata->id, $gdata, 'bp' );
    402             }
    403         }
    404 
    405         // Reset indexes
    406         $groups = array_values( $groups );
    407 
    408         return $groups;
    409     }
    410 
     482            $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )");
     483
     484            // Make sure query returned valid data
     485            if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) {
     486
     487                // Put queried data into the placeholders created earlier,
     488                // and add it to the cache
     489                foreach ( (array) $queried_gdata as $gdata ) {
     490
     491                    // Add group to groups array
     492                    $groups[ $gdata->id ] = $gdata;
     493
     494                    // Cache previously uncached group data
     495                    $cache_key = 'xprofile_group_' . $gdata->id;
     496                    wp_cache_set( $cache_key, $gdata, 'bp' );
     497                }
     498            }
     499        }
     500
     501        // Reset indexes & return
     502        return array_values( $groups );
     503    }
     504
     505    /**
     506     * Validate field group when form submitted
     507     *
     508     * @since BuddyPress (1.0.0)
     509     *
     510     * @global string $message
     511     *
     512     * @return boolean
     513     */
    411514    public static function admin_validate() {
    412515        global $message;
    413516
    414         /* Validate Form */
     517        // Validate Form
    415518        if ( empty( $_POST['group_name'] ) ) {
    416519            $message = __( 'Please make sure you give the group a name.', 'buddypress' );
     
    421524    }
    422525
     526    /**
     527     * Update field group position
     528     *
     529     * @since BuddyPress (1.5.0)
     530     *
     531     * @global $wpdb $wpdb
     532     * @param  int $field_group_id
     533     * @param  int $position
     534     *
     535     * @return boolean
     536     */
    423537    public static function update_position( $field_group_id, $position ) {
    424538        global $wpdb;
    425539
    426         if ( !is_numeric( $position ) ) {
     540        if ( ! is_numeric( $position ) ) {
    427541            return false;
    428542        }
    429543
    430         // purge profile field group cache
     544        // Purge profile field group cache
    431545        wp_cache_delete( 'xprofile_groups_inc_empty', 'bp' );
    432546
     
    453567
    454568            // Does the admin allow this field to be customized?
    455             $allow_custom = 'disabled' !== bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' );
     569            $visibility   = bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' );
     570            $allow_custom = (bool) ( 'disabled' !== $visibility );
    456571
    457572            // Look to see if the user has set the visibility for this field
    458             if ( $allow_custom && isset( $visibility_levels[$field->id] ) ) {
    459                 $field_visibility = $visibility_levels[$field->id];
     573            if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) {
     574                $field_visibility = $visibility_levels[ $field->id ];
    460575
    461576            // If no admin-set default is saved, fall back on a global default
     
    470585                 * @param string $value Default visibility value.
    471586                 */
    472                 $field_visibility = ! empty( $fallback_visibility ) ? $fallback_visibility : apply_filters( 'bp_xprofile_default_visibility_level', 'public' );
    473             }
    474 
    475             $fields[$key]->visibility_level = $field_visibility;
     587                $field_visibility = ! empty( $fallback_visibility )
     588                    ? $fallback_visibility
     589                    : apply_filters( 'bp_xprofile_default_visibility_level', 'public' );
     590            }
     591
     592            $fields[ $key ]->visibility_level = $field_visibility;
    476593        }
    477594
     
    501618            $default_visibility_levels = array();
    502619            foreach ( $levels as $level ) {
    503                 if ( 'default_visibility' == $level->meta_key ) {
    504                     $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value;
    505                 } elseif ( 'allow_custom_visibility' == $level->meta_key ) {
    506                     $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value;
     620                switch ( $level->meta_key ) {
     621                    case 'default_visibility' :
     622                        $default_visibility_levels[ $level->object_id ]['default']      = $level->meta_value;
     623                        break;
     624                    case 'allow_custom_visibility' :
     625                        $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value;
     626                        break;
    507627                }
    508628            }
     
    514634    }
    515635
     636    /** Admin Output **********************************************************/
     637
     638    /**
     639     * Output the admin area field group form
     640     *
     641     * @since BuddyPress (1.0.0)
     642     *
     643     * @global string $message
     644     */
    516645    public function render_admin_form() {
    517646        global $message;
    518647
     648        // New field group
    519649        if ( empty( $this->id ) ) {
    520650            $title  = __( 'Add New Field Group', 'buddypress' );
    521             $action = "users.php?page=bp-profile-setup&amp;mode=add_group";
     651            $action = add_query_arg( array( 'page' => 'bp-profile-setup', 'mode' => 'add_group' ), 'users.php' );
    522652            $button = __( 'Save', 'buddypress' );
     653
     654        // Existing field group
    523655        } else {
    524656            $title  = __( 'Edit Field Group', 'buddypress' );
    525             $action = "users.php?page=bp-profile-setup&amp;mode=edit_group&amp;group_id=" . $this->id;
     657            $action = add_query_arg( array( 'page' => 'bp-profile-setup', 'mode' => 'edit_group', 'group_id' => $this->id ), 'users.php' );
    526658            $button = __( 'Update', 'buddypress' );
    527659        } ?>
     
    533665            <h2><?php echo esc_html( $title ); ?></h2>
    534666
    535             <?php if ( !empty( $message ) ) : ?>
     667            <?php if ( ! empty( $message ) ) : ?>
    536668
    537669                <div id="message" class="error fade">
     
    570702                             * @param BP_XProfile_Group $this Current XProfile group.
    571703                             */
    572                             do_action( 'xprofile_group_before_submitbox', $this );
    573                             ?>
     704                            do_action( 'xprofile_group_before_submitbox', $this ); ?>
    574705
    575706                            <div id="submitdiv" class="postbox">
     
    588719                                             * @param BP_XProfile_Group $this Current XProfile group.
    589720                                             */
    590                                             do_action( 'xprofile_group_submitbox_start', $this );
    591                                             ?>
     721                                            do_action( 'xprofile_group_submitbox_start', $this ); ?>
    592722
    593723                                            <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" />
     
    613743                             * @param BP_XProfile_Group $this Current XProfile group.
    614744                             */
    615                             do_action( 'xprofile_group_after_submitbox', $this );
    616                             ?>
     745                            do_action( 'xprofile_group_after_submitbox', $this ); ?>
     746
    617747                        </div>
    618748                    </div>
     
    621751        </div>
    622752
    623 <?php
     753    <?php
    624754    }
    625755}
Note: See TracChangeset for help on using the changeset viewer.