php 从BuddyPress组中删除用户,其中用户是特定m2p成员资格的成员或没有成员资格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 从BuddyPress组中删除用户,其中用户是特定m2p成员资格的成员或没有成员资格相关的知识,希望对你有一定的参考价值。

<?php

/**
* Plugin Name: Remove m2p Specific Members From BuddyPress Groups
* Plugin URI: https://premium.wpmudev.org/
* Description: Remove Users from BuddyPress Groups where user is member of specific m2p membership or with no membership
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/


if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


if ( ! class_exists( 'M2PMembersRemoveFromBuddyPressGroups' ) ) {

	class M2PMembersRemoveFromBuddyPressGroups {

		private static $_instance 	= null;

		private $escape_admin_user 	= true; // For remove admin user

		private $wp_user_ids 		= []; // All WP User IDs

		private $m2p_user_ids 		= []; // User IDs who have m2p membership

		private $no_m2p_user_ids 	= []; // User IDs who have no m2p membership

		private $user_ids 			= []; // For remove from bp groups

		private $m2p_membership_ids = []; // For get users ids of specific memberships

		
		public static function get_instance() {

			if ( is_null( self::$_instance ) ) {
				self::$_instance = new M2PMembersRemoveFromBuddyPressGroups();
			}

			return self::$_instance;

		}

		private function __construct() {

			if ( !class_exists('BuddyPress') || !class_exists('MS_Factory') ) return;

			// Fix buddyPress undefined table names
			$this->fix_bp_missing_table_names();

			// Set all wp user ids
			$this->set_wp_user_ids();

			// Set m2p and no mp2 user ids
			$this->set_m2p_and_no_m2p_user_ids();

			// Set m2p membership ids manually for get these specific membership users ids
			$this->set_m2p_membership_ids();

			// Set specific users ids which is under specific m2p memberships
			// Disable it if you not like to remove these users from buddyPress
			$this->set_specific_m2p_membership_user_ids();

			// Set specific users ids which is not under any m2p membership
			// Disable it if you not like to remove these users from buddyPress
			$this->set_no_m2p_membership_user_ids();

			// Remove specific users from buddyPress all groups
			// Disable it if you not like to remove specific users from buddyPress
			$this->remove_users_from_buddyPress_groups();
			
		}

		public function fix_bp_missing_table_names() {

			global $wpdb;

			$bp = BuddyPress::instance();

			if ( empty($bp->groups->table_name) ) {
				$bp->groups->table_name           = $wpdb->prefix . 'bp_groups';
				$bp->groups->table_name_members   = $wpdb->prefix . 'bp_groups_members';
				$bp->groups->table_name_groupmeta = $wpdb->prefix . 'bp_groups_groupmeta';
			}

		}

		public function set_m2p_membership_ids() {

			$membership_ids = array(); // provide m2p specific membersihp ids like: array(2,5,8)
			$this->m2p_membership_ids = array_merge( $this->m2p_membership_ids, $membership_ids );

		}

		public function set_wp_user_ids() {

			global $wpdb;

			if ( $wp_user_ids = $wpdb->get_results( "SELECT `id` FROM {$wpdb->prefix}users", ARRAY_A ) ) {
				$this->wp_user_ids = array_merge( $this->wp_user_ids, wp_list_pluck( $wp_user_ids, 'id') );
			}

		}

		public function set_m2p_and_no_m2p_user_ids() {

			$members = array_map(function($user_id) {
				return MS_Factory::load( 'MS_Model_Member', $user_id );
			}, $this->wp_user_ids);

			foreach ($members as $member) {
				if ( $this->escape_admin_user && $member->is_admin_user($member->id) ) continue;
				$member->has_membership() ? $this->m2p_user_ids[] = $member->id : $this->no_m2p_user_ids[] = $member->id;
			}

		}
		
		public function set_specific_m2p_membership_user_ids() {

			$members = array_map(function($user_id) {
				return MS_Factory::load( 'MS_Model_Member', $user_id );
			}, $this->m2p_user_ids);

			$user_ids = array();

			foreach ($members as $member) {
				foreach ( $member->get_membership_ids() as $ms_id ) {
					if ( !in_array($ms_id, $this->m2p_membership_ids) ) continue;
					$user_ids[] = $member->wp_user->ID;
				}
			}

			$this->user_ids = array_merge( $this->user_ids, $user_ids );

		}

		public function set_no_m2p_membership_user_ids() {

			$this->user_ids = array_merge( $this->user_ids, $this->no_m2p_user_ids );

		}

		public function remove_users_from_buddyPress_groups() {

			foreach ($this->user_ids as $user_id) {

				if ( $bp_groups = BP_Groups_Member::get_group_ids( $user_id ) ) {
					foreach ( $bp_groups['groups'] as $group_id) {
						BP_Groups_Member::delete($user_id, $group_id);
					}
				}
			}

		}

	}


	function render_M2P_members_remove_from_buddyPress_groups(){

		$GLOBALS['M2PMembersRemoveFromBuddyPressGroups'] = M2PMembersRemoveFromBuddyPressGroups::get_instance();

	}

}

add_action( 'wp_loaded', 'render_M2P_members_remove_from_buddyPress_groups' );

以上是关于php 从BuddyPress组中删除用户,其中用户是特定m2p成员资格的成员或没有成员资格的主要内容,如果未能解决你的问题,请参考以下文章

php [约会] - 从BuddyPress添加提供商

PHP 在Wordpress MU和Buddypress上的地图上显示用户

使用昵称或用户 ID 在 Buddypress 中更改用户配置文件 URL

删除Buddypress管理栏

CentOS7 从管理员组中 新增/删除用户

php 添加TOS / PP复选框到BuddyPress注册