Skip to content

Instantly share code, notes, and snippets.

@fritids
Forked from technosailor/ab-bp-retroactiveusers.php
Created February 13, 2014 18:26
Show Gist options
  • Save fritids/8980855 to your computer and use it in GitHub Desktop.
Save fritids/8980855 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Retroactive BP User Acticity
Plugin URI: https://gist.github.com/3953927
Description: Makes all BuddyPress users visible immediately on user creation and retroactively adjust users to allow for their display before logging in.
Author: Aaron Brazell
Version: 1.0
Author URI: http://technosailor.com
License: MIT
License URI: http://opensource.org/licenses/MIT
*/
/**
* A BuddyPress class for user creation simplicity
*
* @author Aaron Brazell <[email protected]>
* @package ab-bp-prepopulate-users
*/
class BP_Prepoulate_Users {
/**
* PHP constructor method. Requires PHP 5.2+
*
* @author Aaron Brazell <[email protected]>
* @package ab-bp-prepopulate-users
*/
public function __construct()
{
$this->hooks();
}
/**
* Method to tie methods in this class into the WordPress hook system
*
* @author Aaron Brazell <[email protected]>
* @package ab-bp-prepopulate-users
* @return void
*/
public function hooks()
{
// Fix existing users. Will only ever be run once
add_action( 'init', array( $this, 'retroactive_user_fix' ) );
// New users get the last_active meta
add_action( 'user_register', array( $this, 'user_active_on_create' ) );
}
/**
* A method for retroactively loading existing users with the last_active meta_key. BuddyPress will not show
* users who have never logged in so this is a quick fix to get around that problem. Method stores the abbp_retroactive
* option in the WordPress options table after it is run once on page load. After that, the method short circuits
* if the option exists, preventing unnecessary database queries in the future.
*
* @author Aaron Brazell <[email protected]>
* @package ab-bp-prepopulate-users
* @return void
*/
public function retroactive_user_fix()
{
if( get_option( 'abbp_retroactive' ) )
return false;
$users = get_users();
foreach( $users as $user )
{
// Check to see if the user is active
if( get_user_meta( $user->ID, 'last_activity', true ) )
continue;
// Inactive so lets activate
$timestamp = date( 'Y-m-d G:i:s' );
add_user_meta( $user->ID, 'last_activity', $timestamp );
}
// Only do this once so not to tax the database unnecessarily
add_option( 'abbp_retroactive', true );
}
/**
* A method for pre-populating the last_active meta_key in the usermeta table. BuddyPress only displays users with
* this key so we force the user to have that key to get around the limitation. Hooks on the user_register hook.
*
* @author Aaron Brazell <[email protected]>
* @package ab-bp-prepopulate-users
* @return void
*/
public function user_active_on_create( $user_id )
{
$timestamp = date( 'Y-m-d G:i:s' );
add_user_meta( $user_id, 'last_activity', $timestamp );
}
}
$abbppu = new BP_Prepoulate_Users;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment