WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#11329 closed enhancement (wontfix)

Allow _wp_get_comment_list() to return comments only for specific users

Reported by: pampfelimetten Owned by: Denis-de-Bernardy
Milestone: Priority: normal
Severity: normal Version: 2.8.5
Component: Query Keywords: has-patch 2nd-opinion close
Focuses: Cc:

Description

Right now I can limit the post edit screen to only show comments of the currently logged in user (useful in cms-like environments - especially if you have more than 400 users like we do). See http://wordpress.org/support/topic/287591?replies=25#post-1222413

But I can't do the same thing with comments, and it looks like I'm not the only one missing this feature.

After looking into the source code, I think _wp_get_comment_list() should get modified, or at least get a filter.

Attachments (7)

11329.001.diff (724 bytes) - added by cnorris23 4 years ago.
11329.002.diff (731 bytes) - added by cnorris23 4 years ago.
code cleanup
11329.003.diff (1013 bytes) - added by pampfelimetten 4 years ago.
11329.004.diff (1.9 KB) - added by cnorris23 4 years ago.
coding standards
11329.005.diff (2.0 KB) - added by cnorris23 4 years ago.
11329.006.diff (2.0 KB) - added by pampfelimetten 4 years ago.
11329.007.diff (2.7 KB) - added by pampfelimetten 4 years ago.

Download all attachments as: .zip

Change History (32)

comment:1 cnorris234 years ago

  • Keywords needs-patch added; comment_list admin removed
  • Type changed from feature request to enhancement

comment:2 cnorris234 years ago

  • Keywords has-patch needs-testing added; needs-patch removed

cnorris234 years ago

comment:3 cnorris234 years ago

Forgot to mention that this only accepts one user id.

pampfelimetten, if you, or someone else, needs to pass more than one user id, let me know and I can adjust the patch. since, you only asked to be able to get the comments for the current user, I only made the patch to accept one user id.

comment:4 pampfelimetten4 years ago

Hi cnorris,

thanks a lot! I will test it at the weekend and report back.
Right now, I only need it for one user id, but maybe there are some people out there who would like to be able to handle more than one id, who knows.

comment:5 pampfelimetten4 years ago

Hi cnorris,

i was curios so I tested it right away, but it isnt working. I only get a blank list in the backend, tested against 2.9.1
Did you test it on your own setup and did it work?

I tested it with the filter:

function cba_get_comment_list_by_user($userid) {
	$userid = 17;
	return $userid;
};

add_filter('get_comment_list_by_user', 'cba_get_comment_list_by_user');

...aswell as hardcoded, same thing.

comment:6 cnorris234 years ago

I made and tested the patch on trunk, as it's highly unlikely that it will get committed to the 2.9 branch. I just tested the patch on 2.9.1 and XAMPP, and it worked fine. I used your exact function, and since my test site doesn't have a user with an ID of '17', I got "No results found" instead of a comment list. Is this what you got? Using an existing user ID on my test site, the patch worked and returned results from only that user.

What do you mean by "as well as hardcoded?" The function you have above has the ID hardcoded already. Did you hardcode in template.php (if so, where)?

It may somehow be conflicting with another plugin you have installed. Try the code below, and see if that works. It's what I initially used to test the filter. All it does is get the ID of the user you used to log into WordPress, then passes it to the filter.

function cba_get_comment_list_by_user($userid) {
	global $user_ID;
	return $user_ID;
};

add_filter('get_comment_list_by_user', 'cba_get_comment_list_by_user');

cnorris234 years ago

code cleanup

comment:7 pampfelimetten4 years ago

Hi Cnorris,

sorry, I think we have a mix up, which occurs to me, when I look closely at your code =).
My initial enhancement wish was to show comments of POSTS of a certain user - so in a multi-author environment authors see directly comments which are concerning their own posts.
Your code now shows comments by a certain user, which is a completely different thing.

I think we have to make a join with the posts table, actually.

Are you motivated to give it a try?
Thanks

comment:8 pampfelimetten4 years ago

Okay, I tried and tested it, seems to work, even with an array now (usefull for enabling different authors to cooperate, what is what we want to do as well)

 	if ( !empty($userid) ) {
	    if(!is_array($userid)) {
			$query .= "AND p.post_author = '$userid' ";
		} else {
			$query .= "AND p.post_author IN ('".implode("', '", $userid)."') ";
		}
	}

It has one flaw: the total count still includes all of the comments, and paging also is including pages for all comments, and returning empty pages if going back too far.

I don't know anything about svn, sadly, can you test it and include a new patch file?

comment:9 pampfelimetten4 years ago

okay, also solved the total count thing:

 	if ( !empty($userid) ) {
	    if(!is_array($userid)) {
			$query .= "AND p.post_author = '$userid' ";
			$total = '';
		} else {
			$query .= "AND p.post_author IN ('".implode("', '", $userid)."') ";
			$total = '';			
		}
	}

comment:10 pampfelimetten4 years ago

which of course can be even shorter:

if ( !empty($userid) ) {

if(!is_array($userid)) {

$query .= "AND p.post_author = '$userid' ";

} else {

$query .= "AND p.post_author IN ('".implode("', '", $userid)."') ";

}

$total = ;

}


comment:11 pampfelimetten4 years ago

sry:

	if ( !empty($userid) ) {
	    if(!is_array($userid)) {
			$query .= "AND p.post_author = '$userid' ";
		} else {
			$query .= "AND p.post_author IN ('".implode("', '", $userid)."') ";
		}
		$total = '';
	}    

pampfelimetten4 years ago

comment:12 pampfelimetten4 years ago

  • Component changed from Accessibility to Query
  • Owner set to Denis-de-Bernardy
  • Status changed from new to reviewing

He, first patch I ever submitted, I hope it's working well.
Can somebody from the core developers review it?

comment:13 cnorris234 years ago

  • Keywords dev-feedback tested added; has-patch needs-testing removed

sorry, I think we have a mix up, which occurs to me, when I look closely at your code
=). My initial enhancement wish was to show comments of POSTS of a certain user - so
in a multi-author environment authors see directly comments which are concerning their
own posts. Your code now shows comments by a certain user, which is a completely
different thing.

Yeah, I was a little unsure as to what you were looking for, but I thought I had it. Clearly, I was wrong.

I've tested the patch on trunk r12813 and my XAMPP install. It looks good, and works for me. I've updated the patch to conform to coding standards.

cnorris234 years ago

coding standards

comment:14 nacin4 years ago

  • Keywords needs-patch added; dev-feedback tested removed

The patch needs some improvement as it stands. At the very least, we should absint() the user id, and for when it is an array, array_map() on absint(). The query thus should not quote user ids in SQL, as they are of the numeric type. The filter should be false by default, instead of a string, as it doesn't expect a string -- it expects an user ID (integer) or array of integers.

cnorris234 years ago

comment:15 cnorris234 years ago

  • Keywords has-patch added; needs-patch removed

@pampfelimetten,
I've updated the patch. Can you check to make sure everything still works for you?

@nacin
Thanks for the feedback.

pampfelimetten4 years ago

comment:16 pampfelimetten4 years ago

@Cnorris: You forgot a semicolon, I corrected it, see patch .006.
Otherwise it still works perfectly.

comment:17 Denis-de-Bernardy4 years ago

  • Keywords 2nd-opinion added

suggesting we punt and massage this a bit more: a filter just seems wrong.

we're fetching posts using get_posts(), if memory serves. we ought to retrieve comments using a get_comments() function.

comment:18 pampfelimetten4 years ago

@denis-de-bernardy:
if you suggest an alternative, I would be happy to try to program it.
I tried another solution, please give me feedback if you like this solution more, see diff 007

pampfelimetten4 years ago

comment:19 nacin4 years ago

  • Milestone changed from 3.0 to Future Release

comment:20 maliska4 years ago

  • Cc maliska added

Hello,

I am new to word press but I have an ok understand of how it works. The patch is something I do need. I am running wordpress version 3.0.1 and this feature is not there as far as I can see. Can someone point me in the direction of who to update this patch so I can get this in my system?

Kind Regards

Maliska

comment:21 maliska4 years ago

OK, I made the changes to the files listed in 11329.007.diff but what this gave me is the users only see their comments when the are looking at a post. What I am really looking for is when I user logs in they go to the dashboard and when they click comments they see only comments directed to them. So basically if an author logs in and goes to the comment area they see only comments that have been posted of their posts.

comment:22 pampfelimetten4 years ago

hi maliska,

my patch only handles the standard comment view, not the dashboard widget. But that would be a nice thing to have as well.

comment:23 cnorris233 years ago

  • Keywords close added

Suggest close as _wp_get_comment_list() has been replaced by WP_Comments_List_Table.

comment:24 pampfelimetten3 years ago

  • Resolution set to wontfix
  • Status changed from reviewing to closed

Should others try to achive the same, with 3.1 it's possible to do it with a filter like this:

function cba_get_comment_list_by_user($clauses) {
	if (is_admin()) {
		global $user_ID, $wpdb;
		$clauses['join'] = ", cba_posts";
		$clauses['where'] .= " AND ".$wpdb->prefix."posts.post_author = ".$user_ID." AND ".$wpdb->prefix."comments.comment_post_ID = ".$wpdb->prefix."posts.ID";
	};
	return $clauses;
};
add_filter('comments_clauses', 'cba_get_comment_list_by_user');

Closing the ticket.

comment:25 ocean903 years ago

  • Milestone Future Release deleted
Note: See TracTickets for help on using tickets.