Skip to content

Instantly share code, notes, and snippets.

@sbrajesh sbrajesh/hv2.php
Created Nov 5, 2018

Embed
What would you like to do?
Featured Member Expiration based on role
/**
* Class BP_Featured_Member_Expirable_Membership
*
* @author Ravi Sharma
*/
class BP_Featured_Member_Expirable_Membership{
/**
* Singleton Instance
*
* @var BP_Featured_Member_Expirable_Membership
*/
private static $instance = null;
/**
* BP_Featured_Member_Addon constructor.
*/
private function __construct() {
$this->setup();
}
/**
* Get class instance
*
* @return BP_Featured_Member_Expirable_Membership
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Setup callback on necessaries hooks
*/
private function setup() {
register_activation_hook( __FILE__, array( $this, 'on_activation' ) );
register_deactivation_hook( __FILE__, array( $this, 'on_deactivation' ) );
add_action( 'bp_featured_members_user_added', array( $this, 'save_date' ) );
add_action( 'bp_fma_remove_featured', array( $this, 'remove_featured' ) );
}
/**
* Setup hourly cronjob to remove featured members
*/
public function on_activation() {
if ( ! wp_next_scheduled( 'bp_fma_remove_featured' ) ) {
wp_schedule_event( time(), 'hourly', 'bp_fma_remove_featured' );
}
}
/**
* Remove cronjob
*/
public function on_deactivation() {
wp_clear_scheduled_hook( 'bp_fma_remove_featured' );
}
/**
* Save datetime when a user marked as featured
*
* @param int $user_id User id.
*/
public function save_date( $user_id ) {
$current = time();
// We calculate future expiry date here.
$upto = $current + $this->get_role_based_interval( $user_id );
update_user_meta( $user_id, '_bpfm_featured_at_time', $current );
update_user_meta( $user_id, '_bpfm_featured_expiry_time', $upto );
}
/**
* Get role based allowed time interval in seconds.
*
* We add an expiration time stamp using it when a user is marked as featured.
*
* @param int $user_id user id.
*
* @return int
*/
private function get_role_based_interval( $user_id ) {
$default = 10; // 10 days by default?
$user = get_user_by( 'id', $user_id );
if ( empty( $user ) ) {
return $default * DAY_IN_SECONDS;
}
$user_roles = $user->roles;
if ( empty( $user_roles ) ) {
return $default * DAY_IN_SECONDS;
}
// Map role to days.
$map = array(
'administrator' => 60, // 60 days
'editor' => 40,
'author' => 30,
'contributor' => 20,
'subscriber' => 5,
);
$allowed_days = 0; // 0 days.
// calculate the max allowed days.
foreach ( $user_roles as $role ) {
if ( isset( $map[ $role ] ) && $map[ $role ] > $allowed_days ) {
$allowed_days = $map[ $role ];
}
}
// if no user role has mapped value.
if ( ! $allowed_days ) {
$allowed_days = $default;
}
return $allowed_days * DAY_IN_SECONDS;
}
/**
* Removed featured members and extra meta keys.
*
* @return mixed
*/
public function remove_featured() {
global $wpdb;
if ( ! function_exists( 'bp_featured_members' ) ) {
return;
}
// we remove all users whose time has expired.
// Find all expired users.
$user_query = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND CAST( meta_value AS UNSIGNED ) < %d", '_bpfm_featured_expiry_time', time() );
$user_ids = $wpdb->get_col( $user_query );
if ( empty( $user_ids ) ) {
return;
}
$where_sql = $wpdb->prepare( "( meta_key=%s OR meta_key=%s OR meta_key=%s )", '_is_featured', '_bpfm_featured_expiry_time', '_bpfm_featured_at_time' );
$list = join( ',', $user_ids );
$query = "DELETE FROM {$wpdb->usermeta} WHERE {$where_sql} AND user_id IN ( $list )";
$wpdb->query( $query );
}
}
BP_Featured_Member_Expirable_Membership::get_instance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.