BP_Core_Follow_Follower_Widget::widget( $args,  $instance )

Displays the widget.

Description

Source

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

	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
		}
	}

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.