Skip to content

Instantly share code, notes, and snippets.

@raviousprime
Last active April 7, 2020 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raviousprime/295490658f2fb81637e601c389637e11 to your computer and use it in GitHub Desktop.
Save raviousprime/295490658f2fb81637e601c389637e11 to your computer and use it in GitHub Desktop.
List members who add displayed user in his circle
<?php
/**
* Class BuddyCircles_Affiliate_Users
*/
class BuddyCircles_Affiliate_Users {
/**
* Affiliate user list.
*
* @var array
*/
private $affiliate_users = array();
/**
* Boot class
*/
public static function boot() {
$self = new self();
$self->setup();
}
/**
* Add setup
*/
private function setup() {
add_action( 'bp_setup_nav', array( $this, 'add_nav' ) );
}
/**
* Add new nav that show affiliate members
*/
public function add_nav() {
if ( ! function_exists( 'buddycircles' ) ) {
return;
}
$count = count( $this->get_affiliate_users() );
$class = ( 0 === $count ) ? 'no-count' : 'count';
$main_nav_name = sprintf(
__( 'Affiliate Users %s' ),
sprintf(
'<span class="%s">%s</span>',
esc_attr( $class ),
bp_core_number_format( $count )
)
);
bp_core_new_nav_item(
array(
'name' => $main_nav_name,
'slug' => 'affiliate-users',
'show_for_displayed_user' => true,
'screen_function' => array( $this, 'render' ),
),
'members'
);
}
/**
* Render list of affiliate users who added displayed user in their circles
*/
public function render() {
add_action( 'bp_template_content', array( $this, 'list_affiliate_users' ) );
bp_core_load_template( 'members\single\plugins' );
}
/**
* Modify members args
*
* @param array $r Members args.
*
* @return array
*/
public function modify_loop_args( $r ) {
$r['include'] = $this->affiliate_users;
return $r;
}
/**
* Get affiliate users
*
* @return array
*/
private function get_affiliate_users() {
global $wpdb;
$circles_table = \PressThemes\BuddyCircles\Schema\Schema::table( 'circles' );
$members_table = \PressThemes\BuddyCircles\Schema\Schema::table( 'members' );
$users = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$circles_table} WHERE id IN ( SELECT circle_id FROM {$members_table} WHERE user_id = %d )", bp_displayed_user_id() ) );
$this->affiliate_users = empty( $users ) ? array() : array_unique( $users );
return $this->affiliate_users;
}
/**
* Render affiliate users
*/
public function list_affiliate_users() {
$this->affiliate_users = empty( $this->affiliate_users ) ? array( 0 ) : $this->affiliate_users;
add_filter( 'bp_after_has_members_parse_args', array( $this, 'modify_loop_args' ) );
bp_get_template_part( 'members/members-loop' );
add_filter( 'bp_after_has_members_parse_args', array( $this, 'modify_loop_args' ) );
}
}
BuddyCircles_Affiliate_Users::boot();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment