Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/24/2021 04:16:17 AM (3 years ago)
Author:
espellcaste
Message:

Making PHPDoc Improvements to the BP Friends (component) files.

Also, adding several minor PHP changes.

See #8553

File:
1 edited

Legend:

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

    r13086 r13092  
    44 *
    55 * @package BuddyPress
    6  * @subpackage FriendsClasses
     6 * @subpackage FriendsFriendship
    77 * @since 1.0.0
    88 */
     
    132132     *
    133133     * @since 1.0.0
     134     *
     135     * @global BuddyPress $bp The one true BuddyPress instance.
     136     * @global wpdb $wpdb WordPress database object.
    134137     */
    135138    public function populate() {
     
    161164
    162165        if ( ! empty( $this->populate_friend_details ) ) {
    163             if ( $this->friend_user_id == bp_displayed_user_id() ) {
     166            if ( bp_displayed_user_id() === $this->friend_user_id ) {
    164167                $this->friend = new BP_Core_User( $this->initiator_user_id );
    165168            } else {
     
    174177     * @since 1.0.0
    175178     *
     179     * @global BuddyPress $bp The one true BuddyPress instance.
     180     * @global wpdb $wpdb WordPress database object.
     181     *
    176182     * @return bool True on success, false on failure.
    177183     */
     
    182188
    183189        $this->initiator_user_id = apply_filters( 'friends_friendship_initiator_user_id_before_save', $this->initiator_user_id, $this->id );
    184         $this->friend_user_id    = apply_filters( 'friends_friendship_friend_user_id_before_save',    $this->friend_user_id,    $this->id );
    185         $this->is_confirmed      = apply_filters( 'friends_friendship_is_confirmed_before_save',      $this->is_confirmed,      $this->id );
    186         $this->is_limited        = apply_filters( 'friends_friendship_is_limited_before_save',        $this->is_limited,        $this->id );
    187         $this->date_created      = apply_filters( 'friends_friendship_date_created_before_save',      $this->date_created,      $this->id );
     190        $this->friend_user_id    = apply_filters( 'friends_friendship_friend_user_id_before_save', $this->friend_user_id, $this->id );
     191        $this->is_confirmed      = apply_filters( 'friends_friendship_is_confirmed_before_save', $this->is_confirmed, $this->id );
     192        $this->is_limited        = apply_filters( 'friends_friendship_is_limited_before_save', $this->is_limited, $this->id );
     193        $this->date_created      = apply_filters( 'friends_friendship_date_created_before_save', $this->date_created, $this->id );
    188194
    189195        /**
     
    192198         * @since 1.0.0
    193199         *
    194          * @param BP_Friends_Friendship $value Current friendship request object.
     200         * @param BP_Friends_Friendship $value Current friendship object. Passed by reference.
    195201         */
    196202        do_action_ref_array( 'friends_friendship_before_save', array( &$this ) );
    197203
    198204        // Update.
    199         if (!empty( $this->id ) ) {
     205        if ( ! empty( $this->id ) ) {
    200206            $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 ) );
    201207
     
    211217         * @since 1.0.0
    212218         *
    213          * @param BP_Friends_Friendship $value Current friendship request object.
     219         * @param BP_Friends_Friendship $value Current friendship object. Passed by reference.
    214220         */
    215221        do_action_ref_array( 'friends_friendship_after_save', array( &$this ) );
     
    223229     * @since 1.0.0
    224230     *
     231     * @global BuddyPress $bp The one true BuddyPress instance.
     232     * @global wpdb $wpdb WordPress database object.
     233     *
    225234     * @return bool|int
    226235     */
     
    240249     * @since 2.6.0
    241250     *
    242      * @param int   $user_id              ID of the user whose friends are being retrieved.
    243      * @param array $args {
     251     * @param int    $user_id  ID of the user whose friends are being retrieved.
     252     * @param array  $args    {
    244253     *        Optional. Filter parameters.
    245254     *        @type int    $id                ID of specific friendship to retrieve.
     
    251260     *        @type string $sort_order        ASC or DESC. Default DESC.
    252261     * }
    253      * @param string $operator            Optional. Operator to use in `wp_list_filter()`.
     262     * @param string $operator Optional. Operator to use in `wp_list_filter()`.
    254263     *
    255264     * @return array $friendships Array of friendship objects.
     
    376385     * @since 2.7.0
    377386     *
     387     * @global BuddyPress $bp The one true BuddyPress instance.
     388     * @global wpdb $wpdb WordPress database object.
     389     *
    378390     * @param int $user_id ID of the user.
    379391     * @return array
     
    403415     */
    404416    public static function get_friend_user_ids( $user_id, $friend_requests_only = false, $assoc_arr = false ) {
    405         global $wpdb;
    406417
    407418        if ( ! empty( $friend_requests_only ) ) {
    408419            $args = array(
    409                 'is_confirmed' => 0,
    410                 'friend_user_id' => $user_id
     420                'is_confirmed'   => 0,
     421                'friend_user_id' => $user_id,
    411422            );
    412423        } else {
     
    443454
    444455        // Can't friend yourself.
    445         if ( $user_id == $friend_id ) {
     456        if ( $user_id === $friend_id ) {
    446457            return $friendship_id;
    447458        }
     
    453464        $args = array(
    454465            'initiator_user_id' => $friend_id,
    455             'friend_user_id'    => $friend_id
     466            'friend_user_id'    => $friend_id,
    456467        );
     468
    457469        $result = self::get_friendships( $user_id, $args, 'OR' );
    458470        if ( $result ) {
     
    469481     * @param int $user_id The ID of the user who has received the
    470482     *                     friendship requests.
    471      * @return array|bool An array of user IDs, or false if none are found.
     483     * @return array|bool An array of user IDs or false if none are found.
    472484     */
    473485    public static function get_friendship_request_user_ids( $user_id ) {
     
    499511     */
    500512    public static function total_friend_count( $user_id = 0 ) {
    501         global $wpdb;
    502513
    503514        if ( empty( $user_id ) ) {
     
    510521         */
    511522
    512         $args = array(
    513             'is_confirmed' => 1,
    514         );
     523        $args        = array( 'is_confirmed' => 1 );
    515524        $friendships = self::get_friendships( $user_id, $args );
    516525        $count       = count( $friendships );
     
    529538     * Search the friends of a user by a search string.
    530539     *
    531      * @since 1.0.0
     540     * @todo Optimize this function.
     541     *
     542     * @since 1.0.0
     543     *
     544     * @global BuddyPress $bp The one true BuddyPress instance.
     545     * @global wpdb $wpdb WordPress database object.
    532546     *
    533547     * @param string   $filter  The search string, matched against xprofile
     
    546560        global $wpdb;
    547561
    548         /*
    549          * TODO: Optimize this function.
    550          */
    551 
    552         if ( empty( $user_id ) )
     562        $bp = buddypress();
     563
     564        if ( empty( $user_id ) ) {
    553565            $user_id = bp_loggedin_user_id();
     566        }
    554567
    555568        // Only search for matching strings at the beginning of the
     
    558571
    559572        $pag_sql = '';
    560         if ( !empty( $limit ) && !empty( $page ) )
     573        if ( ! empty( $limit ) && ! empty( $page ) ) {
    561574            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    562 
    563         if ( !$friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ) )
     575        }
     576
     577        $friend_ids = self::get_friend_user_ids( $user_id );
     578        if ( ! $friend_ids ) {
    564579            return false;
     580        }
    565581
    566582        // Get all the user ids for the current user's friends.
    567583        $fids = implode( ',', wp_parse_id_list( $friend_ids ) );
    568584
    569         if ( empty( $fids ) )
     585        if ( empty( $fids ) ) {
    570586            return false;
    571 
    572         $bp = buddypress();
     587        }
    573588
    574589        // Filter the user_ids based on the search criteria.
     
    584599        $total_friend_ids    = $wpdb->get_var( $total_sql );
    585600
    586         if ( empty( $filtered_friend_ids ) )
     601        if ( empty( $filtered_friend_ids ) ) {
    587602            return false;
    588 
    589         return array( 'friends' => array_map( 'intval', $filtered_friend_ids ), 'total' => (int) $total_friend_ids );
     603        }
     604
     605        return array(
     606            'friends' => array_map( 'intval', $filtered_friend_ids ),
     607            'total'   => (int) $total_friend_ids,
     608        );
    590609    }
    591610
     
    608627     */
    609628    public static function check_is_friend( $initiator_userid, $possible_friend_userid ) {
    610         global $wpdb;
    611629
    612630        if ( empty( $initiator_userid ) || empty( $possible_friend_userid ) ) {
     
    615633
    616634        // Can't friend yourself.
    617         if ( $initiator_userid == $possible_friend_userid ) {
     635        if ( $initiator_userid === $possible_friend_userid ) {
    618636            return 'not_friends';
    619637        }
    620638
    621         BP_Friends_Friendship::update_bp_friends_cache( $initiator_userid, $possible_friend_userid );
     639        self::update_bp_friends_cache( $initiator_userid, $possible_friend_userid );
    622640
    623641        return bp_core_get_incremented_cache( $initiator_userid . ':' . $possible_friend_userid, 'bp_friends' );
    624642    }
    625643
    626 
    627644    /**
    628645     * Find uncached friendships between a user and one or more other users and cache them.
     
    630647     * @since 3.0.0
    631648     *
    632      * @param int $user_id                          The ID of the primary user for whom we want
     649     * @global BuddyPress $bp The one true BuddyPress instance.
     650     * @global wpdb $wpdb WordPress database object.
     651     *
     652     * @param int              $user_id             The ID of the primary user for whom we want
    633653     *                                              to check friendships statuses.
    634654     * @param int|array|string $possible_friend_ids The IDs of the one or more users
    635655     *                                              to check friendship status with primary user.
    636      * @return null
    637656     */
    638657    public static function update_bp_friends_cache( $user_id, $possible_friend_ids ) {
    639658        global $wpdb;
    640         $bp = buddypress();
     659
     660        $bp                  = buddypress();
    641661        $possible_friend_ids = wp_parse_id_list( $possible_friend_ids );
    642662
     
    649669            }
    650670        }
     671
    651672        if ( empty( $fetch ) ) {
    652673            return;
     
    695716     */
    696717    public static function get_bulk_last_active( $user_ids ) {
    697         global $wpdb;
    698 
    699718        $last_activities = BP_Core_User::get_last_activity( $user_ids );
    700719
    701720        // Sort and structure as expected in legacy function.
    702721        usort( $last_activities, function( $a, $b ) {
    703             if ( $a['date_recorded'] == $b['date_recorded'] ) {
     722            if ( $a['date_recorded'] === $b['date_recorded'] ) {
    704723                return 0;
    705724            }
     
    710729        $retval = array();
    711730        foreach ( $last_activities as $last_activity ) {
    712             $u = new stdClass;
     731            $u                = new stdClass();
    713732            $u->last_activity = $last_activity['date_recorded'];
    714733            $u->user_id       = $last_activity['user_id'];
     
    725744     * @since 1.0.0
    726745     *
     746     * @global BuddyPress $bp The one true BuddyPress instance.
     747     * @global wpdb $wpdb WordPress database object.
     748     *
    727749     * @param int $friendship_id ID of the friendship to be accepted.
    728750     * @return int Number of database rows updated.
    729751     */
    730     public static function accept($friendship_id) {
     752    public static function accept( $friendship_id ) {
    731753        global $wpdb;
    732754
     
    740762     *
    741763     * @since 1.6.0
     764     *
     765     * @global BuddyPress $bp The one true BuddyPress instance.
     766     * @global wpdb $wpdb WordPress database object.
    742767     *
    743768     * @param int $friendship_id ID of the friendship to be withdrawn.
    744769     * @return int Number of database rows deleted.
    745770     */
    746     public static function withdraw($friendship_id) {
     771    public static function withdraw( $friendship_id ) {
    747772        global $wpdb;
    748773
     
    756781     *
    757782     * @since 1.0.0
     783     *
     784     * @global BuddyPress $bp The one true BuddyPress instance.
     785     * @global wpdb $wpdb WordPress database object.
    758786     *
    759787     * @param int $friendship_id ID of the friendship to be rejected.
    760788     * @return int Number of database rows deleted.
    761789     */
    762     public static function reject($friendship_id) {
     790    public static function reject( $friendship_id ) {
    763791        global $wpdb;
    764792
     
    774802     *
    775803     * @since 1.0.0
     804     *
     805     * @global BuddyPress $bp The one true BuddyPress instance.
     806     * @global wpdb $wpdb WordPress database object.
    776807     *
    777808     * @param string   $filter  String to search by.
     
    793824
    794825        $pag_sql = '';
    795         if ( !empty( $limit ) && !empty( $page ) )
     826        if ( ! empty( $limit ) && ! empty( $page ) ) {
    796827            $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) );
     828        }
    797829
    798830        $bp = buddypress();
     
    805837        }
    806838
    807         $filtered_fids = $wpdb->get_col($sql);
    808 
    809         if ( empty( $filtered_fids ) )
     839        $filtered_fids = $wpdb->get_col( $sql );
     840
     841        if ( empty( $filtered_fids ) ) {
    810842            return false;
     843        }
    811844
    812845        return $filtered_fids;
     
    819852     *
    820853     * @since 1.0.0
     854     *
     855     * @global BuddyPress $bp The one true BuddyPress instance.
     856     * @global wpdb $wpdb WordPress database object.
    821857     *
    822858     * @param string $filter Search term.
     
    842878        }
    843879
    844         $user_count = $wpdb->get_col($sql);
    845 
    846         if ( empty( $user_count ) )
     880        $user_count = $wpdb->get_col( $sql );
     881
     882        if ( empty( $user_count ) ) {
    847883            return false;
     884        }
    848885
    849886        return $user_count[0];
     
    857894     * @since 1.0.0
    858895     *
     896     * @global BuddyPress $bp The one true BuddyPress instance.
     897     * @global wpdb $wpdb WordPress database object.
     898     *
    859899     * @param array $user_ids Array of user IDs.
    860      * @return array User IDs, sorted by the associated display names.
     900     * @return array|bool User IDs, sorted by the associated display names.
     901     *                    False if XProfile component is not active.
    861902     */
    862903    public static function sort_by_name( $user_ids ) {
    863904        global $wpdb;
    864905
    865         if ( !bp_is_active( 'xprofile' ) )
     906        if ( ! bp_is_active( 'xprofile' ) ) {
    866907            return false;
     908        }
    867909
    868910        $bp = buddypress();
     
    877919     *
    878920     * @since 1.0.0
     921     *
     922     * @global BuddyPress $bp The one true BuddyPress instance.
     923     * @global wpdb $wpdb WordPress database object.
    879924     *
    880925     * @param int $user_id       ID of the user whose friends are being retrieved.
     
    893938
    894939        for ( $i = 0, $count = count( $results ); $i < $count; ++$i ) {
    895             $fids[] = ( $results[$i]->friend_user_id == $user_id ) ? $results[$i]->initiator_user_id : $results[$i]->friend_user_id;
     940            $fids[] = ( $results[ $i ]->friend_user_id === $user_id ) ? $results[ $i ]->initiator_user_id : $results[ $i ]->friend_user_id;
    896941        }
    897942
    898943        // Remove duplicates.
    899         if ( count( $fids ) > 0 )
     944        if ( count( $fids ) > 0 ) {
    900945            return array_flip( array_flip( $fids ) );
    901         else
    902             return false;
     946        }
     947
     948        return false;
    903949    }
    904950
     
    912958     * - users who have been banned from the group
    913959     *
    914      * @since 1.0.0
    915960     * @todo Need to do a group component check before using group functions.
     961     *
     962     * @since 1.0.0
    916963     *
    917964     * @param int $user_id  ID of the user whose friends are being counted.
    918965     * @param int $group_id ID of the group friends are being invited to.
    919      * @return int $invitable_count Eligible friend count.
     966     * @return bool|int False if group component is not active, and friend count.
    920967     */
    921968    public static function get_invitable_friend_count( $user_id, $group_id ) {
     969
     970        if ( ! bp_is_active( 'group' ) ) {
     971            return false;
     972        }
    922973
    923974        // Setup some data we'll use below.
    924975        $is_group_admin  = groups_is_user_admin( $user_id, $group_id );
    925         $friend_ids      = BP_Friends_Friendship::get_friend_user_ids( $user_id );
     976        $friend_ids      = self::get_friend_user_ids( $user_id );
    926977        $invitable_count = 0;
    927978
     
    929980
    930981            // If already a member, they cannot be invited again.
    931             if ( groups_is_user_member( (int) $friend_ids[$i], $group_id ) ) {
     982            if ( groups_is_user_member( (int) $friend_ids[ $i ], $group_id ) ) {
    932983                continue;
    933984            }
    934985
    935986            // If user already has invite, they cannot be added.
    936             if ( groups_check_user_has_invite( (int) $friend_ids[$i], $group_id ) ) {
     987            if ( groups_check_user_has_invite( (int) $friend_ids[ $i ], $group_id ) ) {
    937988                continue;
    938989            }
    939990
    940991            // If user is not group admin and friend is banned, they cannot be invited.
    941             if ( ( false === $is_group_admin ) && groups_is_user_banned( (int) $friend_ids[$i], $group_id ) ) {
     992            if ( ( false === $is_group_admin ) && groups_is_user_banned( (int) $friend_ids[ $i ], $group_id ) ) {
    942993                continue;
    943994            }
     
    9531004     *
    9541005     * @since 2.7.0
     1006     *
     1007     * @global BuddyPress $bp The one true BuddyPress instance.
     1008     * @global wpdb $wpdb WordPress database object.
    9551009     *
    9561010     * @param int|string|array $friendship_ids Single friendship ID or comma-separated/array list of friendship IDs.
     
    9721026     *
    9731027     * @param int $friendship_id ID of the friendship.
    974      * @return null|stdClass friend_user_id and initiator_user_id.
     1028     * @return null|stdClass
    9751029     */
    9761030    public static function get_user_ids_for_friendship( $friendship_id ) {
    977 
    9781031        $friendship = new BP_Friends_Friendship( $friendship_id, false, false );
    9791032
     
    9821035        }
    9831036
    984         $retval = new StdClass;
    985         $retval->friend_user_id = $friendship->friend_user_id;
     1037        $retval                    = new StdClass();
     1038        $retval->friend_user_id    = $friendship->friend_user_id;
    9861039        $retval->initiator_user_id = $friendship->initiator_user_id;
    9871040
     
    9931046     *
    9941047     * @since 1.0.0
     1048     *
     1049     * @global BuddyPress $bp The one true BuddyPress instance.
     1050     * @global wpdb $wpdb WordPress database object.
    9951051     *
    9961052     * @param int $user_id ID of the user being expunged.
     
    10331089            // Delete cached friendships.
    10341090            wp_cache_delete( $friend_id, 'bp_friends_friendships_for_user' );
    1035             BP_Friends_Friendship::total_friend_count( $friend_id );
     1091
     1092            self::total_friend_count( $friend_id );
    10361093        }
    10371094
Note: See TracChangeset for help on using the changeset viewer.