Advertisement
vapvarun

Only list active MemberPress members in the members directory

Jul 25th, 2021
1,763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. /**
  2.  * Only list active MemberPress members in the members directory.
  3.  * sources:
  4.  * https://buddydev.com/hiding-users-on-buddypress-based-site
  5.  * https://buddypress.org/support/topic/member-loop-only-memberpress-members/
  6.  * @param array $args args.
  7.  *
  8.  * @return array
  9.  */
  10. function tmp_only_show_members_in_directory( $args ) {
  11.     // do not exclude in admin.
  12.     if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
  13.         return $args;
  14.     }
  15.     $args['exclude'] = tmp_get_users_to_exclude_from_directory();
  16.  
  17.     return $args;
  18. }
  19. add_filter( 'bp_after_has_members_parse_args', 'tmp_only_show_members_in_directory' );
  20.  
  21. /**
  22.  * Returns an array of IDs to exclude, runs the queries if empty
  23.  * @return false|mixed|void
  24.  */
  25. function tmp_get_users_to_exclude_from_directory() {
  26.     $ids_to_exclude = get_option('tmp_update_members_to_exclude_from_directory');
  27.     if (empty($ids_to_exclude) || !is_array($ids_to_exclude) || count($ids_to_exclude) < 1) {
  28.         tmp_update_members_to_exclude_from_directory();
  29.     }
  30.     return get_option('tmp_update_members_to_exclude_from_directory');
  31. }
  32.  
  33. // Scheduled Action Hook
  34. function tmp_update_members_to_exclude_from_directory() {
  35.     global $wpdb;
  36.     $member_ids = $wpdb->get_col("SELECT DISTINCT user_id FROM ".$wpdb->prefix."mepr_transactions WHERE status IN('confirmed','complete') AND (expires_at >= NOW() OR expires_at = '0000-00-00 00:00:00')");
  37.     $user_ids = get_users(['fields'=>'ID']);
  38.     $user_ids_to_exclude = array_diff($user_ids, $member_ids);
  39.     return update_option('tmp_update_members_to_exclude_from_directory',$user_ids_to_exclude);
  40. }
  41. add_action( 'hook_name', 'tmp_update_members_to_exclude_from_directory' );
  42.  
  43. // Schedule Cron Job Event
  44. function tmp_update_members_to_exclude_from_directory_job() {
  45.     if ( ! wp_next_scheduled( 'tmp_update_members_to_exclude_from_directory' ) ) {
  46.         wp_schedule_event( current_time( 'timestamp' ), 'twicedaily', 'tmp_update_members_to_exclude_from_directory' );
  47.     }
  48. }
  49. add_action( 'wp', 'tmp_update_members_to_exclude_from_directory_job' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement