This entry is part 3 of 7 in the Creating a User Follow System Plugin Series
In the previous part of Creating a User Follow System, we looked at the basic shell functions that we need to write for our plugin to function. Now we are going to get into actually writing those functions. We will go one by one and write each one from scratch. Also note that we will be incorporating hooks and filters in order to make our plugin extensible.
We will start by writing the two functions that retrieve the list of users a user is following and the function that retrieves of list of users that are following the user.
The pwuf_get_following() function looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Retrieves all users that the specified user follows * * Gets all users that $user_id followers * * @access private * @since 1.0 * @param int $user_id - the ID of the user to retrieve following for * @return array */ function pwuf_get_following( $user_id = 0 ) { if ( empty( $user_id ) ) { $user_id = get_current_user_id(); } $following = get_user_meta( $user_id, '_pwuf_following', true ); return (array) apply_filters( 'pwuf_get_following', $following, $user_id ); } |
The first thing that happens is we check is the $user_id parameter passed to the function is empty. It will be empty if it is not passed explicitly, if it is null, if it is false, or if it is zero. If the $user_id is empty, we go ahead and set it to the currently logged in user. Doing this makes the parameter optional.
The list of users that are following our specified user is stored in the user meta table as an array, so we simply use get_user_meta() to retrieve the list. Once we have the $following variable set up, we return it through a function called pwuf_get_following. This filter allows other developers to filter the data before it is actually used. If you are not familiar with this technique, then please read my tutorial from WP Tuts+.
Our next function is pwuf_get_followers() This function is nearly identical to pwuf_get_following():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Retrieves users that follow a specified user * * Gets all users following $user_id * * @access private * @since 1.0 * @param int $user_id - the ID of the user to retrieve followers for * @return array */ function pwuf_get_followers( $user_id = 0 ) { if ( empty( $user_id ) ) { $user_id = get_current_user_id(); } $followers = get_user_meta( $user_id, '_pwuf_followers', true ); return (array) apply_filters( 'pwuf_get_followers', $followers, $user_id ); } |
Next we’re going to write the pwuf_follow_user() function. This is the one that actually performs the action of making one user follow another. The complete function looks like this:
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 | /** * Follow a user * * Makes a user follow another user * * @access private * @since 1.0 * @param int $user_id - the ID of the user that is doing the following * @param int $user_to_follow - the ID of the user that is being followed * @return bool */ function pwuf_follow_user( $user_id, $user_to_follow ) { $following = pwuf_get_following( $user_id ); if ( $following && is_array( $following ) ) { $following[] = $user_to_follow; } else { $following = array(); $following[] = $user_to_follow; } // retrieve the IDs of all users who are following $user_to_follow $followers = pwuf_get_followers( $user_to_follow ); if ( $followers && is_array( $followers ) ) { $followers[] = $user_id; } else { $followers = array(); $followers[] = $user_id; } do_action( 'pwuf_pre_follow_user', $user_id, $user_to_follow ); // update the IDs that this user is following $followed = update_user_meta( $user_id, '_pwuf_following', $following ); // update the IDs that follow $user_id $followers = update_user_meta( $user_to_follow, '_pwuf_followers', $followers ); // increase the followers count $followed_count = pwuf_increase_followed_by_count( $user_to_follow ) ; if ( $followed ) { do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow ); return true; } return false; } |
Let’s walk through what’s happening here.
1. A variable called $following is created and uses the pwuf_get_following() function to populate it with an array of the users that follow $user_id.
2. The user that is being followed, $user_to_follow, is appended to our $following array. We do that like this:
1 2 3 4 5 6 | if ( $following && is_array( $following ) ) { $following[] = $user_to_follow; } else { $following = array(); $following[] = $user_to_follow; } |
3. The followers of the user we are following are retrieved and stored in the $followers variable. This allows us to add $user_id to the list. We do this like so:
1 2 3 4 5 6 7 8 9 | // retrieve the IDs of all users who are following $user_to_follow $followers = pwuf_get_followers( $user_to_follow ); if ( $followers && is_array( $followers ) ) { $followers[] = $user_id; } else { $followers = array(); $followers[] = $user_id; } |
4. A hook is added so other developers can execute their own custom functions any time a user is followed but before any data is saved.
1 | do_action( 'pwuf_pre_follow_user', $user_id, $user_to_follow ); |
5. The list of users that $user_id is following is updated:
1 2 | // update the IDs that this user is following $followed = update_user_meta( $user_id, '_pwuf_following', $following ); |
6. The list of users that follow $user_to_follow:
1 2 | // update the IDs that follow $user_to_follow $followers = update_user_meta( $user_to_follow, '_pwuf_followers', $followers ); |
7. The count for how many users follow $user_to_follow is updated (we’ll write this function shortly):
1 2 | // increase the followers count $followed_count = pwuf_increase_followed_by_count( $user_to_follow ); |
8. A hook is added so other developer can execute their own custom functions any time a user is followed, just after the data is saved:
1 | do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow ); |
That’s it for this function.
Our next function is pwuf_unfollow_user(), which is very, very similar to pwuf_follow_user(). The complete function looks like this:
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 | /** * Unfollow a user * * Makes a user unfollow another user * * @access private * @since 1.0 * @param int $user_id - the ID of the user that is doing the unfollowing * @param int $unfollow_user - the ID of the user that is being unfollowed * @return bool */ function pwuf_unfollow_user( $user_id, $unfollow_user ) { do_action( 'pwuf_pre_unfollow_user', $user_id, $unfollow_user ); // get all IDs that $user_id follows $following = pwuf_get_following( $user_id ); if ( is_array( $following ) && in_array( $unfollow_user, $following ) ) { $modified = false; foreach ( $following as $key => $follow ) { if ( $follow == $unfollow_user ) { unset( $following[$key] ); $modified = true; } } if ( $modified ) { if ( update_user_meta( $user_id, '_pwuf_following', $following ) ) { pwuf_decrease_followed_by_count( $unfollow_user ); } } } // get all IDs that follow the user we have just unfollowed so that we can remove $user_id $followers = pwuf_get_followers( $unfollow_user ); if ( is_array( $followers ) && in_array( $user_id, $followers ) ) { $modified = false; foreach ( $followers as $key => $follower ) { if ( $follower == $user_id ) { unset( $followers[$key] ); $modified = true; } } if ( $modified ) { update_user_meta( $unfollow_user, '_pwuf_followers', $followers ); } } if ( $modified ) { do_action( 'pwuf_post_unfollow_user', $user_id, $unfollow_user ); return true; } return false; } |
Let’s walk through step by step and see what happens here.
1. A hook called “pwuf_pre_unfollow_user” is added so other developer scan execute custom functions before a user is unfollowed.
2. The list of users that $user_id follows is retrieved with the pwuf_get_following() function.
3. The $following var is checked to ensure that it is an array, and that the user we are unfollowing is included in the list:
1 2 3 | if ( is_array( $following ) && in_array( $unfollow_user, $following ) ) { .... } |
4. If the user we are unfollowing is found, the followed users is looped through until we find our user. Once found, the user is removed from the array with unset():
1 2 3 4 5 6 | foreach ( $following as $key => $follow ) { if ( $follow == $unfollow_user ) { unset( $following[$key] ); $modified = true; } } |
5. If the user was found and removed, we update the users $user_id follows and decrease the number of users that follow $unfollow_user:
1 2 3 4 5 | if ( $modified ) { if ( update_user_meta( $user_id, '_pwuf_following', $following ) ) { pwuf_decrease_followed_by_count( $unfollow_user ); } } |
6. The exact same steps done in 2-5 are repeated for the followers of the user we are unfollowing (we have to remove $user_id):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // get all IDs that follow the user we have just unfollowed so that we can remove $user_id $followers = pwuf_get_followers( $unfollow_user ); if ( is_array( $followers ) && in_array( $user_id, $followers ) ) { $modified = false; foreach ( $followers as $key => $follower ) { if ( $follower == $user_id ) { unset( $followers[$key] ); $modified = true; } } if ( $modified ) { update_user_meta( $unfollow_user, '_pwuf_followers', $followers ); } } |
7. If any data has been modified, we add a new hook called “pwuf_post_unfollow_user” and return true. If no data was modified, we return false:
1 2 3 4 5 6 | if ( $modified ) { do_action( 'pwuf_post_unfollow_user', $user_id, $unfollow_user ); return true; } return false; |
The next function we’re going to write is pwuf_get_following_count(), which is used for retrieving the number of users that a specified user follows. The complete function looks like this:
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 | /** * Retrieve following count * * Gets the total number of users that the specified user is following * * @access private * @since 1.0 * @param int $user_id - the ID of the user to retrieve a count for * @return int */ function pwuf_get_following_count( $user_id = 0 ) { if ( empty( $user_id ) ) { $user_id = get_current_user_id(); } $following = pwuf_get_following( $user_id ); $count = 0; if ( $following ) { $count = count( $following ); } return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id ); } |
This function is pretty simple. We first check to see if $user is empty and then set it to the currently logged in user’s ID if it is. Once we have the user ID, we retrieve the list of users $user_id follows by using pwuf_get_following(), which we wrote earlier.
If $following is not false (which it will be if there are no followers), we use the count() function to count the number of users in the array.
We then return $count through a filter called “pwuf_get_following_count” so that other developers can modify the count data before it is returned if they wish.
Now let’s write the pwuf_get_follower_count() function, which is nearly identical to pwuf_get_following_count():
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 | /** * Retrieve follower count * * Gets the total number of users that are following the specified user * * @access private * @since 1.0 * @param int $user_id - the ID of the user to retrieve a count for * @return int */ function pwuf_get_follower_count( $user_id = 0 ) { if ( empty( $user_id ) ) { $user_id = get_current_user_id(); } $followed_count = get_user_meta( $user_id, '_pwuf_followed_by_count', true ); $count = 0; if ( $followed_count ) { $count = $followed_count; } return (int) apply_filters( 'pwuf_get_follower_count', $count, $user_id ); } |
The only real difference between the two count functions is that one uses count() to count the number of items in the array and one simply returns the integer stored in the meta.
We have just three functions left.
First let’s write pwuf_increase_followed_by_count(), which is used to increase he number of of users that follow a user. The complete function looks like this:
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 | /** * Increase follower count * * Increments the total count for how many users a specified user is followed by * * @access private * @since 1.0 * @param int $user_id - the ID of the user to increease the count for * @return int */ function pwuf_increase_followed_by_count( $user_id = 0 ) { do_action( 'pwuf_pre_increase_followed_count', $user_id ); $followed_count = pwuf_get_follower_count( $user_id ); if ( $followed_count !== false ) { $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 ); } else { $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 ); } do_action( 'pwuf_post_increase_followed_count', $user_id ); return $new_followed_count; } |
At the top and bottom of the function, we place two action hooks. These are there so that other developers can tie into our function, just as we have done with the other functions above.
We retrieve the number of users that our user is followed by. If the $followed_count variable is not false (meaning this user has never been followed), we simply update the meta value with +1. If the user has never been followed, we simply set this meta value to 1.
At the end of the function we return the new count.
Our decrease count function is identical except that it takes the count down 1, instead of adding 1 to it:
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 | /** * Decrease follower count * * Decrements the total count for how many users a specified user is followed by * * @access private * @since 1.0 * @param int $user_id - the ID of the user to decrease the count for * @return int */ function pwuf_decrease_followed_by_count( $user_id ) { do_action( 'pwuf_pre_decrease_followed_count', $user_id ); $followed_count = pwuf_get_follower_count( $user_id ); if ( $followed_count ) { $count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) ); do_action( 'pwuf_post_increase_followed_count', $user_id ); } return $count; } |
And now our last and final function is the one that lets us check if a user is following another user:
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 | /** * Check if a user is following another * * Increments the total count for how many users a specified user is followed by * * @access private * @since 1.0 * @param int $user_id - the ID of the user doing the following * @param int $followed_user - the ID of the user to check if being followed by $user_id * @return int */ function pwuf_is_following( $user_id, $followed_user ) { $following = pwuf_get_following( $user_id ); $ret = false; // is not following by default if ( is_array( $following ) && in_array( $followed_user, $following ) ) { $ret = true; // is following } return (bool) apply_filters( 'pwuf_is_following', $user_id, $followed_user ); } |
All this function does is retrieve the list of users that follow $user_id and then check to see if $followed_user exists in the list. If the user is in the list, we return true, otherwise we return false.
Our return value is passed through a filter called “pwuf_is_following” so that other developers can modify this value if need be.
That’s it! All of our main functions for the User Follow Plugin are now complete. See you in part 4!
Hi Pippin,
Great tutorial here, man! I really appreciate the effort you put in making this kind of tutorials and following coding and design best practices in every line of code you write.
I went through part1, part2, and part3 of this tutorial and looking forward to part4.
Will you be releasing part 4 of this series anytime soon?
Thank you very much!
I’m planning to get the next part out on Monday. Sorry for being slow 🙂
Hi Pippin:
i can’t see the rest of the Part 3, it says i have to be logged in, but i’m already in… what do i have to do?
Thanks
Alejandro
Do you have a paid account?
So all these will go to actions.php right?
No, functions.