public
Created

Update role after which the user has read all the posts in a category

  • Download Gist
update_role
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
/*
* Function: Update role after the user has read all the posts in a category
*/
 
class WPSE78727
{
// set this to a proper name for a metakey
const METAKEY = 'user_read_posts';
 
public $user_ID = 0;
public $userdata = null;
public $current_role = '';
public $updated = false;
 
// edit this to your needs
// this is the sequence of category names the user
// has to read to get updated to the next level
public $role_sequence = array(
'foo',
'bar',
'baz'
);
 
public function __construct() {
 
if( is_single() && is_user_logged_in() && !current_user_can('manage_options') ) {
 
$this->get_user_data();
$updated = $this->save_post_id_in_user_meta();
 
$this->updated = ( is_string( $updated ) ) ? $updated : false;
 
}
 
}
 
public function get_user_data() {
 
$this->userdata = wp_get_current_user();
$this->user_ID = $this->userdata->ID;
//check the actual user role
foreach($this->role_sequence as $role){
if($this->userdata->has_cap($role)) $this->current_role = $role;
}
//if it haven't any of the roles I assign the lowest
if(!in_array($this->current_role, $this->role_sequence)){
$this->current_role = $this->role_sequence[0];
$this->userdata->add_role($this->current_role);
}
 
return true;
}
 
public function save_post_id_in_user_meta() {
 
global $post;
//if the post isn't part of the corresponding category to the current role do nothing
$categorie = array();
foreach(get_the_category( $post->ID ) as $category){
$categorie[] = $category->name;
}
if (!in_array($this->current_role, $categorie )) return;
 
// make the code more readable
$role = &$this->current_role;
 
// $read_post should be an array
$posts_read = get_user_meta( $this->user_ID, self::METAKEY, true );
 
// be sure $posts_read is an array
if( ! is_array( $posts_read ) )
$posts_read = array();
 
// make an array for the post IDs which belongs to the current role
if( ! isset( $posts_read[$role] ) || ! is_array( $posts_read[$role] ) )
$posts_read[$role] = array();
 
// if the current post ID is not in the array, push it into the array
if( ! in_array( $post->ID, $posts_read[$role] ) )
array_push( $posts_read[$role], $post->ID );
 
// update the metadata
update_user_meta( $this->user_ID, self::METAKEY, $posts_read );
 
$new_role = $this->maybe_all_posts_read( $posts_read );
 
return ( ! is_array( $new_role ) ) ? $new_role : $posts_read;
 
}
 
public function maybe_all_posts_read( $posts_read = array() ) {
 
$current_cat_id = get_cat_ID( $this->current_role );
 
// get all post IDs from this category
$post_ids = get_posts(
array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $current_cat_id,
),
),
'fields' => 'ids', // Only get post IDs
));
 
// check if the user has read all posts. Simply check the differences between
// $post_ids (all post IDs in the current category (e.g. foo) and the read posts
// stored in user meta
$left_posts = array_diff( $post_ids, $posts_read[$this->current_role] );
 
// if no post is left, update the user
if( empty( $left_posts ) )
$new_role = $this->update_user();
 
return ( empty( $left_posts ) ) ? $new_role : $left_posts;
 
}
 
public function update_user() {
 
// get the next role out of the sequence
$key = array_search( $this->current_role, $this->role_sequence );
 
$new_role = $this->current_role;
if(isset($this->role_sequence[$key+1]))
$new_role = $this->role_sequence[$key+1];
 
$this->userdata->set_role($new_role);
return $new_role;
 
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.