This entry is part 2 of 7 in the Creating a User Follow System Plugin Series
Part 1 of the User Follow System series looked at laying out the main structure of the plugin. For part 2, we’re going to continue that and lay out the main skeleton functions that we need for our plugin. These will include the functions for following a user, unfollowing a user, checking if a user followers another, and other similar functions.
There are several main tasks that happen in a plugin like this, and that’s primarily what I want to walk you through in this part of the series. We won’t write a lot of code here (we will in part 3) because it’s really important that you have a good understanding of how the logic of the system works before you jump into the code.
The Storage Method Used
There are a lot of ways to store data about users (primarily data about who is following who) and it’s important that you think about it before simply choosing one. In this tutorial series we’ll be storing the info in the WordPress user meta table. Now, there are a couple of really important notes that you need to know about this:
1. When storing in the user meta table, it is possible to make user meta global for multi site installs, or localized to individual installs. The add/get/update_user_option() set of functions is (by default) localized to individual sites. The add/get/update_user_meta() set of functions, however, is global for multi site installs, meaning that meta stored for a user on site #4 will also exist for that same user on site #7. We will be using the global option for this series, but in your own applications, this may not work.
2. By storing the data in the user meta tables, our system is persistent, meaning that all data is stored forever. Due to the way the data is stored, it is technically limited in size. All of the user IDs that one particular user is following, will be stored in the same database column and row, which means that at some point, it is possible the space will run out and no more users will be able to be followed (or some other nasty effect). In order for this to happen, a user would have to follow an exorbitant number of other users (thousands and thousands), so it is very unlikely, but still possible. If you are in a scenario where this could cause an issue, you should consider an alternate storage method.
There are three separate meta keys that will be using for storing information. One for the other users that a user is following; one for the other users that are followed by a user; one for the total number of users a user is followed by. The meta IDs will be as follows:
- _pwuf_following
- _pwuf_followers
- _pwuf_followed_by_count
The Follow Process
When a user chooses to follow another user, there are a few things that happen. First of all, the user that is doing the following, will be logged into the site. We will refer to this user as the “current” user. The user that is getting followed by the current user, can be anyone and is simply referenced via their user ID.
In order to follow a user, there will be a link displayed which we will create later. This link will contain two pieces of information:
1. The action to perform (follow or unfollow)
2. The user ID that is getting followed
When the link is clicked, a “listener” that is running in the background will pick up the click and then trigger an action that marks the user as being followed by the current user. Now, when a user is marked as being followed, there are a couple of things that happen, and they happen in order:
- The server checks to see if the current user is already following the user being followed. A user can only be followed once.
- If the user is not already being followed by the currently logged-in user, a list of all users that are being followed by our current user is retrieved.
- Once we have a list of followed users, we append our new followed user ID to the list and update the list in the database.
- The user is now considered followed.
The Unfollow Process
When a user chooses to unfollow another user (meaning that they were already following them), a few things happen. The process is nearly identical to the follow process, except that we remove the new user, instead of adding them:
- The server checks to see if the current user is already following the user being unfollowed. A user can only unfollow another user if they are already following them.
- If the user is already being followed by the currently logged-in user, a list of all users that are being followed by our current user is retrieved.
- Once we have a list of followed users, we remove our new unfollowed user ID from the list and update the list in the database.
- The user is now considered unfollowed.
The Shell Functions
We now know the basic process that happens each time a user is followed or unfollowed, so now let’s lay out the shell functions we need for all of this to happen.
By “shell functions” I mean the function names and parameters. The idea behind this is to fully think out how the system works before writing a single line of any function. So we’ll write the function names, but nothing us. This makes it much easier to conceptualize the process and get a better idea of exactly what each function does.
These are all of the functions that will be placed in includes/follow-functions.php
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | <?php /** * Follow Functions * * @package User Following System * @subpackage Follow Functions * @copyright Copyright (c) 2012, Pippin Williamson * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 1.0 */ /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } /** * 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 ) { } |
It should be pretty clear what each of these functions is intended to do, which is something you should always strive for. Meaningfully named functions make your system dramatically easier for an outside developer to learn and understand, and also significantly helps you in your own development.
See you for part 3!
Want to learn