Skip to:
Content

BuddyPress.org

Changeset 5709


Ignore:
Timestamp:
02/11/2012 04:25:41 AM (13 years ago)
Author:
johnjamesjacoby
Message:

PHP5'ize bp-friends-classes.php - Set methods as public, private, static, and kill off PHP4 constructors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-friends/bp-friends-classes.php

    r5676 r5709  
    11<?php
     2
    23// Exit if accessed directly
    34if ( !defined( 'ABSPATH' ) ) exit;
     
    1617    var $friend;
    1718
    18     function bp_friends_friendship( $id = null, $is_request = false, $populate_friend_details = true ) {
    19         $this->__construct( $id, $is_request, $populate_friend_details );
    20     }
    21 
    22     function __construct( $id = null, $is_request = false, $populate_friend_details = true ) {
     19    public function __construct( $id = null, $is_request = false, $populate_friend_details = true ) {
    2320        $this->is_request = $is_request;
    2421
    25         if ( $id ) {
    26             $this->id = $id;
     22        if ( !empty( $id ) ) {
     23            $this->id                      = $id;
    2724            $this->populate_friend_details = $populate_friend_details;
    2825            $this->populate( $this->id );
     
    3027    }
    3128
    32     function populate() {
    33         global $wpdb, $bp, $creds;
     29    private function populate() {
     30        global $wpdb, $bp;
    3431
    3532        if ( $friendship = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->friends->table_name} WHERE id = %d", $this->id ) ) ) {
     
    4138        }
    4239
    43         // if running from ajax.
    44         if ( !bp_displayed_user_id() )
    45             $bp->displayed_user->id = $creds['current_userid'];
    46 
    47         if ( $this->populate_friend_details ) {
     40        if ( !empty( $this->populate_friend_details ) ) {
    4841            if ( $this->friend_user_id == bp_displayed_user_id() ) {
    4942                $this->friend = new BP_Core_User( $this->initiator_user_id );
     
    5447    }
    5548
    56     function save() {
     49    public function save() {
    5750        global $wpdb, $bp;
    5851
     
    6558        do_action_ref_array( 'friends_friendship_before_save', array( &$this ) );
    6659
    67         if ( $this->id ) {
    68             // Update
     60        // Update
     61        if (!empty( $this->id ) ) {
    6962            $result = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->friends->table_name} SET initiator_user_id = %d, friend_user_id = %d, is_confirmed = %d, is_limited = %d, date_created = %s ) WHERE id = %d", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created, $this->id ) );
    70         } else {
    71             // Save
     63
     64        // Save
     65        } else {
    7266            $result = $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->friends->table_name} ( initiator_user_id, friend_user_id, is_confirmed, is_limited, date_created ) VALUES ( %d, %d, %d, %d, %s )", $this->initiator_user_id, $this->friend_user_id, $this->is_confirmed, $this->is_limited, $this->date_created ) );
    7367            $this->id = $wpdb->insert_id;
     
    7973    }
    8074
    81     function delete() {
     75    public function delete() {
    8276        global $wpdb, $bp;
    8377
     
    8579    }
    8680
    87     /* Static Functions */
    88 
    89     function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {
    90         global $wpdb, $bp;
    91 
    92         if ( $friend_requests_only ) {
     81    /** Static Methods ********************************************************/
     82
     83    public static function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {
     84        global $wpdb, $bp;
     85
     86        if ( !empty( $friend_requests_only ) ) {
    9387            $oc_sql = $wpdb->prepare( "AND is_confirmed = 0" );
    9488            $friend_sql = $wpdb->prepare ( " WHERE friend_user_id = %d", $user_id );
     
    10296
    10397        for ( $i = 0, $count = count( $friends ); $i < $count; ++$i ) {
    104             if ( $assoc_arr )
     98            if ( !empty( $assoc_arr ) ) {
    10599                $fids[] = array( 'user_id' => ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id );
    106             else
     100            } else {
    107101                $fids[] = ( $friends[$i]->friend_user_id == $user_id ) ? $friends[$i]->initiator_user_id : $friends[$i]->friend_user_id;
     102            }
    108103        }
    109104
     
    111106    }
    112107
    113     function get_friendship_id( $user_id, $friend_id ) {
     108    public static function get_friendship_id( $user_id, $friend_id ) {
    114109        global $wpdb, $bp;
    115110
     
    117112    }
    118113
    119     function get_friendship_request_user_ids( $user_id ) {
     114    public static function get_friendship_request_user_ids( $user_id ) {
    120115        global $wpdb, $bp;
    121116
     
    123118    }
    124119
    125     function total_friend_count( $user_id = 0 ) {
    126         global $wpdb, $bp;
    127 
    128         if ( !$user_id )
     120    public static function total_friend_count( $user_id = 0 ) {
     121        global $wpdb, $bp;
     122
     123        if ( empty( $user_id ) )
    129124            $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
    130125
     
    135130
    136131        // Do not update meta if user has never had friends
    137         if ( !$count && !bp_get_user_meta( $user_id, 'total_friend_count', true ) )
     132        if ( empty( $count ) && !bp_get_user_meta( $user_id, 'total_friend_count', true ) )
    138133            return 0;
    139134
    140135        bp_update_user_meta( $user_id, 'total_friend_count', (int)$count );
    141         return (int)$count;
    142     }
    143 
    144     function search_friends( $filter, $user_id, $limit = null, $page = null ) {
     136        return (int) $count;
     137    }
     138
     139    public static function search_friends( $filter, $user_id, $limit = null, $page = null ) {
    145140        global $wpdb, $bp;
    146141
    147142        // TODO: Optimize this function.
    148143
    149         if ( !$user_id )
     144        if ( empty( $user_id ) )
    150145            $user_id = bp_loggedin_user_id();
    151146
    152147        $filter = like_escape( $wpdb->escape( $filter ) );
    153148
    154         if ( $limit && $page )
     149        if ( !empty( $limit ) && !empty( $page ) )
    155150            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    156151
     
    161156        $fids = implode( ',', $friend_ids );
    162157
    163         if ( empty($fids) )
     158        if ( empty( $fids ) )
    164159            return false;
    165160
     
    173168        }
    174169
    175         $filtered_friend_ids = $wpdb->get_col($sql);
    176         $total_friend_ids = $wpdb->get_var($total_sql);
    177 
    178         if ( !$filtered_friend_ids )
     170        $filtered_friend_ids = $wpdb->get_col( $sql );
     171        $total_friend_ids    = $wpdb->get_var( $total_sql );
     172
     173        if ( empty( $filtered_friend_ids ) )
    179174            return false;
    180175
     
    182177    }
    183178
    184     function check_is_friend( $loggedin_userid, $possible_friend_userid ) {
    185         global $wpdb, $bp;
    186 
    187         if ( !$loggedin_userid || !$possible_friend_userid )
     179    public static function check_is_friend( $loggedin_userid, $possible_friend_userid ) {
     180        global $wpdb, $bp;
     181
     182        if ( empty( $loggedin_userid ) || empty( $possible_friend_userid ) )
    188183            return false;
    189184
    190185        $result = $wpdb->get_results( $wpdb->prepare( "SELECT id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id = %d) OR (initiator_user_id = %d AND friend_user_id = %d)", $loggedin_userid, $possible_friend_userid, $possible_friend_userid, $loggedin_userid ) );
    191186
    192         if ( $result ) {
    193             if ( 0 == (int)$result[0]->is_confirmed ) {
     187        if ( !empty( $result ) ) {
     188            if ( 0 == (int) $result[0]->is_confirmed ) {
    194189                return 'pending';
    195190            } else {
     
    201196    }
    202197
    203     function get_bulk_last_active( $user_ids ) {
    204         global $wpdb, $bp;
     198    public static function get_bulk_last_active( $user_ids ) {
     199        global $wpdb;
    205200
    206201        return $wpdb->get_results( $wpdb->prepare( "SELECT meta_value as last_activity, user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key( 'last_activity' ) ) );
    207202    }
    208203
    209     function accept($friendship_id) {
     204    public static function accept($friendship_id) {
    210205        global $wpdb, $bp;
    211206
     
    213208    }
    214209
    215     function reject($friendship_id) {
     210    public static function reject($friendship_id) {
    216211        global $wpdb, $bp;
    217212
     
    219214    }
    220215
    221     function search_users( $filter, $user_id, $limit = null, $page = null ) {
    222         global $wpdb, $bp;
     216    public static function search_users( $filter, $user_id, $limit = null, $page = null ) {
     217        global $wpdb;
    223218
    224219        $filter = like_escape( $wpdb->escape( $filter ) );
    225220
    226221        $usermeta_table = $wpdb->base_prefix . 'usermeta';
    227         $users_table = $wpdb->base_prefix . 'users';
    228 
    229         if ( $limit && $page )
     222        $users_table    = $wpdb->base_prefix . 'users';
     223
     224        if ( !empty( $limit ) && !empty( $page ) )
    230225            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) );
    231226
     
    239234        $filtered_fids = $wpdb->get_col($sql);
    240235
    241         if ( !$filtered_fids )
     236        if ( empty( $filtered_fids ) )
    242237            return false;
    243238
     
    245240    }
    246241
    247     function search_users_count( $filter ) {
     242    public static function search_users_count( $filter ) {
    248243        global $wpdb, $bp;
    249244
     
    251246
    252247        $usermeta_table = $wpdb->prefix . 'usermeta';
    253         $users_table = $wpdb->base_prefix . 'users';
     248        $users_table    = $wpdb->base_prefix . 'users';
    254249
    255250        // filter the user_ids based on the search criteria.
     
    262257        $user_count = $wpdb->get_col($sql);
    263258
    264         if ( !$user_count )
     259        if ( empty( $user_count ) )
    265260            return false;
    266261
     
    268263    }
    269264
    270     function sort_by_name( $user_ids ) {
     265    public static function sort_by_name( $user_ids ) {
    271266        global $wpdb, $bp;
    272267
     
    277272    }
    278273
    279     function get_random_friends( $user_id, $total_friends = 5 ) {
    280         global $wpdb, $bp;
    281 
    282         $sql = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends );
    283         $results = $wpdb->get_results($sql);
     274    public static function get_random_friends( $user_id, $total_friends = 5 ) {
     275        global $wpdb, $bp;
     276
     277        $fids    = array();
     278        $sql     = $wpdb->prepare( "SELECT friend_user_id, initiator_user_id FROM {$bp->friends->table_name} WHERE (friend_user_id = %d || initiator_user_id = %d) && is_confirmed = 1 ORDER BY rand() LIMIT %d", $user_id, $user_id, $total_friends );
     279        $results = $wpdb->get_results( $sql );
    284280
    285281        for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) {
     
    289285        // remove duplicates
    290286        if ( count( $fids ) > 0 )
    291             return array_flip(array_flip($fids));
     287            return array_flip( array_flip( $fids ) );
    292288        else
    293289            return false;
    294290    }
    295291
    296     function get_invitable_friend_count( $user_id, $group_id ) {
     292    public static function get_invitable_friend_count( $user_id, $group_id ) {
    297293
    298294        // Setup some data we'll use below
     
    321317    }
    322318
    323     function get_user_ids_for_friendship( $friendship_id ) {
     319    public static function get_user_ids_for_friendship( $friendship_id ) {
    324320        global $wpdb, $bp;
    325321
     
    327323    }
    328324
    329     function delete_all_for_user( $user_id ) {
     325    public static function delete_all_for_user( $user_id ) {
    330326        global $wpdb, $bp;
    331327
     
    345341    }
    346342}
     343
    347344?>
Note: See TracChangeset for help on using the changeset viewer.