BP_Core_Follow_Follower_Widget

Follow_Follower widget for the logged-in user

Description

Source

File: bp-core/classes/class-bp-core-follow-follower-widget.php

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class BP_Core_Follow_Follower_Widget extends WP_Widget {
    /**
     * Constructor.
     */
    function __construct() {
        // Set up optional widget args
        $widget_ops = array(
            'classname'   => 'widget_bp_follow_follower_widget widget buddypress',
            'description' => __( 'A list of member avatars that are following the logged-in user.', 'buddyboss' ),
        );
 
        // Set up the widget
        parent::__construct(
            false,
            __( '(BB) Members Following Me', 'buddyboss' ),
            $widget_ops
        );
    }
 
    /**
     * Displays the widget.
     */
    function widget( $args, $instance ) {
 
        // do not do anything if user isn't logged in
        if ( ! is_user_logged_in() ) {
            return;
        }
 
        $id = bp_displayed_user_id();
        $filter = $show_more = false;
 
        if ( ! $id ) {
            // If member widget is putted on other pages then will not get the bp_displayed_user_id so set the bp_loggedin_user_id to bp_displayed_user_id.
            add_filter( 'bp_displayed_user_id', array( $this, 'set_display_user' ), 9999, 1 );
            $id     = bp_displayed_user_id();
            $filter = true;
 
            // If $id still blank then return.
            if ( ! $id ) {
                return;
            }
        }
 
        if ( empty( $instance['max_users'] ) ) {
            $instance['max_users'] = 15;
        }
 
        // logged-in user isn't follower anyone, so stop!
        if ( ! $follower = bp_get_follower_ids( array( 'user_id' => $id ) ) ) {
            return false;
        }
 
        $follower_ids          = bp_get_follower_ids( array( 'user_id' => $id ) );
        $follower_array        = explode( ',', $follower_ids );
        $follower_count        = '<span class="widget-num-count">' . count( $follower_array ) . '</span>';
        $follower_count_number = count( $follower_array );
 
        $instance['title'] = (
            bp_loggedin_user_id() === bp_displayed_user_id()
            ? __( "My Followers", 'buddyboss' )
            : sprintf( __( "%s's Followers", 'buddyboss' ), $this->get_user_display_name( $id ) )
        );
 
        if ( bp_loggedin_user_id() === bp_displayed_user_id() ) {
            $show_more = true;
        }
 
        // Remove the filter.
        if ( $filter ) {
            remove_filter( 'bp_displayed_user_id', array( $this, 'set_display_user' ), 9999, 1 );
        }
 
        /**
         * Filters the Connections widget title.
         *
         * @since BuddyBoss 1.2.5 Added 'instance' and 'id_base' to arguments passed to filter.
         *
         * @param string $title    The widget title.
         * @param array  $instance The settings for the particular instance of the widget.
         * @param string $id_base  Root ID for all widgets of this type.
         */
        $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
 
        // show the users the logged-in user is follower
        if ( bp_has_members(
            array(
                'include'         => $follower,
                'max'             => $instance['max_users'],
                'populate_extras' => false,
            )
        ) ) {
            do_action( 'bp_before_follower_widget' );
 
            echo $args['before_widget'];
            echo $args['before_title']
               . $title
               . $follower_count
               . $args['after_title'];
            ?>
 
            <div class="avatar-block">
                <?php
                while ( bp_members() ) :
                    bp_the_member();
                    ?>
                    <div class="item-avatar">
                        <a href="<?php bp_member_permalink() ?>" class="bp-tooltip" data-bp-tooltip-pos="up" data-bp-tooltip="<?php echo bp_core_get_user_displayname( bp_get_member_user_id() ); ?>"><?php bp_member_avatar() ?></a>
                    </div>
                <?php endwhile; ?>
            </div>
            <?php if ( $follower_count_number > $instance['max_users'] && $show_more ) { ?>
                <div class="more-block"><a href="<?php bp_members_directory_permalink(); ?>" class="count-more"><?php _e( 'More', 'buddyboss' ); ?><i class="bb-icon-angle-right"></i></a></div>
            <?php } ?>
 
            <?php echo $args['after_widget']; ?>
 
            <?php do_action( 'bp_after_follower_widget' ); ?>
 
            <?php
        }
    }
 
    /**
     * Callback to save widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance              = $old_instance;
        $instance['max_users'] = (int) $new_instance['max_users'];
 
        return $instance;
    }
 
    /**
     * Widget settings form.
     */
    function form( $instance ) {
        $instance = wp_parse_args(
            (array) $instance,
            array(
                'max_users' => 16,
            )
        );
        ?>
 
        <p><label for="bp-follow-widget-users-max"><?php _e( 'Max members to show:', 'buddyboss' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_users' ); ?>" name="<?php echo $this->get_field_name( 'max_users' ); ?>" type="text" value="<?php echo esc_attr( (int) $instance['max_users'] ); ?>" style="width: 30%" /></label></p>
        <p><small><?php _e( 'Note: This widget is only displayed if a member is followed by other members.', 'buddyboss' ); ?></small></p>
 
        <?php
    }
 
    /**
     * Set Display user_id to loggedin_user_id if someone added the widget on outside bp pages.
     *
     * @since BuddyBoss 1.2.5
     */
    public function set_display_user( $id ) {
        if ( ! $id ) {
            $id = bp_loggedin_user_id();
        }
        return $id;
    }
 
    /**
     * Display user name to 'First Name' when they have selected 'First Name & Last Name' in display format.
     *
     * @since BuddyBoss 1.2.5
     */
    public function get_user_display_name( $user_id ) {
 
        if ( ! $user_id ) {
            return;
        }
 
        $format = bp_get_option( 'bp-display-name-format' );
 
        if (
            'first_name' === $format
            || 'first_last_name' === $format
        ) {
            $first_name_id = (int) bp_get_option( 'bp-xprofile-firstname-field-id' );
            $display_name = xprofile_get_field_data( $first_name_id, $user_id );
        } else {
            $display_name = bp_core_get_user_displayname( $user_id );
        }
 
        return apply_filters( 'bp_core_widget_user_display_name', $display_name, $user_id );
    }
}

Methods

  • __construct — Constructor.
  • form — Widget settings form.
  • get_user_display_name — Display user name to 'First Name' when they have selected 'First Name & Last Name' in display format.
  • set_display_user — Set Display user_id to loggedin_user_id if someone added the widget on outside bp pages.
  • update — Callback to save widget settings.
  • widget — Displays the widget.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.