Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Counting non-hidden comments
sledov
New
Hello,
I am trying to write a plugin to hide/unhide comments and discussions. I created a custom flag in comments and discussions tables that can take three values (0 - hidden, 1 - visible and 2 - visible to author only).
Taking this into account, what would be the right way to get comment totals? I see in the templates it is taken directly from the database (echo $Discussion->CountComments;). Should I create a function and use it in every place $Discussion->CountComments is mentioned?
Thank you.
Tagged:
0
Comments
Normally you do not have to replace something at every place, but you can hook into a function. The comment count is calculated by a function in the comment model. If you look at that file, you'll find the function updateCommentCount and in this function there are some events fired. You can use them like that:
But as you can see this doesn't take into account who is viewing that discussion. An admin should see all comments and the author might see some of those comments so for both of them this count would be wrong and you would need to have additional columns in the discussion table for them, too.
You could add this information and display it in the discussion meta to those with appropriate rights: "x comments for everyone but you"/"x comments hidden for eveyone, y comments hidden for everyone but the discussion author."
I personally cannot think of an elegant solution for that problem...
And I wouldn't use flags in the database but complete words - that is more like Vanilla handles it.
If I write a function that calculates number of comments based on user type, where should it go? Into helper_functions.php in the theme?
How to check that plugin is enabled from within the theme? To say something like: if plugin is enabled show new comment count, otherwise show regular count.
You have multiple options. If you want to make that a feature of your theme, you can put all the code you need a the themehooks file of your theme.
You can also create a stand alone plugin, but you know that this would only makes sense if the plugin itself displays the information.
But just to answer your question: you can use
if c('EnabledPlugins'.'YourPluginName', false) {...
to check if a plugin is enabledI think I figured it out. Right before Discussion meta is shown, replace CountComments from Discussion table with a calculated one.