I need a table to log certain actions users make in WordPress.
As of now, this is the database schema I have camp up with:
id bigint(20) NOT NULL AUTO_INCREMENT,
uid bigint(20) NOT NULL,
type VARCHAR(256) NOT NULL,
data1 TEXT NOT NULL,
data2 TEXT NOT NULL,
data3 TEXT NOT NULL,
timestamp bigint(20) NOT NULL,
UNIQUE KEY id (id)
Let me clarify:
uid: User ID of the wordpress user
type: Type of action the user made (can be 'comment', 'new_post', 'login', etc)
data1/2/3: additional data (for example, ID of comment or post made)
To display the logs, I would query the database and run through a certain filter to get the text to display for that particular log. So it works something like this:
if( $type == 'comment') {
$comment = get_comment( $data1 );
$user = get_user($uid);
echo "User {$user->name} has made a <a href='{$comment->permalink}'>comment</a>";
}
Is this the most efficient way of doing things? It seems quite fine to me as I do not want to just store HTML in the logs table to be outputted.
However, the problem comes where I want to hide a particular log entry when certain conditions are met. Like, for example, if a comment no longer exists, I want to hide that entry. This would pose some problems with pagination. Any suggestions on how I can overcome this?
Thanks!
EDIT:
myplugin_transactions
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
type VARCHAR(256) NOT NULL,
timestamp bigint(20) NOT NULL,
UNIQUE KEY id (id)
myplugin_meta
id bigint(20) NOT NULL AUTO_INCREMENT,
txn_id bigint(20) NOT NULL,
key VARCHAR(256) NOT NULL,
data TEXT NOT NULL,
UNIQUE KEY id (id)
Lets say I want to select * from myplugin_transactions where data1 would usually have had been ‘x’ and data2 been ‘y’. How should I do it in this case?
SELECT * FROM myplugin_transactions LEFT JOIN myplugin_meta ON myplugin_transactions.id = myplugin_meta.txn_id WHERE ( ... ? )
This answer is going to be very generic as it doesn’t provide any code, but it’s also too long for a comment.
Firstly, you shouldn’t be storing additional data in those
data1,data2,data3fields. You’re using MySQL, so you’ve got the power of relational databases. Use them.You should simply have another table, which has an ID field (the ID of the action), and a data field. That way you can store 0 to as-many-items-as-you-want pieces of metadata. I mean, wordpress already does this with metadata right?
Secondly, if a comment is deleted, do you simply want to delete the action related to it? If so, simply hook into the API. I believe there is a hook for
delete_comment: http://codex.wordpress.org/Plugin_API/Action_Reference#Comment.2C_Ping.2C_and_Trackback_ActionsOtherwise if you want to keep the action, you can either add an extra field or piece of metadata called, say,
deleted. When a comment is deleted, as above: hook into thedelete_commentcall and update the action todeleted = true. Then when you run your query on all the actions, exclude the deleted statements, eg... WHERE deleted = NULL ...etc.EDIT2:
To answer your select statement, something like this could work:
Obviously replacing the
data1anddata2keywords with meaningful descriptions.