[wp-hackers] Plugin Port to 2.0: Showing private posts in single-view

Ryan Boren ryan at boren.nu
Wed Dec 21 16:34:18 GMT 2005


On Tue, 2005-12-20 at 19:45 -0800, Filipe Fortes wrote:
...
> I've gone through the code, and it seems like I should try to use the
> "user_has_cap" filter which is called in WP_User.has_cap. I've managed
> to get this working with the following hacky filter:
> 
> function postlevel_has_cap($capabilities)
> {
>   global $wp_query, $user_level, $postlevel_key;
>   if (($wp_query->is_single) && (get_post_status($wp_query->posts[0])
> == 'private'))
>   {
>     $post = $wp_query->posts[0];
>     if (($post->meta_key == $postlevel_key) && (intval($user_level) >
> $post->meta_value))
>     {
>       $capabilities['read_private_posts'] = 1;
>     }
>   }
>   return $capabilities;
> }
> 
> Questions for this list:
> 
> 1. Is this code at all future proof? It works now because get_posts
> only calls current_user_can for single posts? Should I be making my
> filter more robust?

Other plugins might loop over the posts in the query and call
current_user_can() for each, so you should take that into account.

> 2. My filter only recieves one argument, although the apply_filter
> which calls it passes three. Known issue?

add_filter('user_has_cap', 'postlevel_has_cap', 10, 3);

The fourth argument specifies the number of arguments you function will
accept.

> 3. Does any one see any red flags in this implementation?

Looks okay.

Here's a link to my demo post sharing plugin in case there is something
in there that is helpful to you.

http://boren.nu/downloads/post_sharing.phps

If there are any extra hooks you need added, let us know now and we can
try to get them into 2.0.

Ryan




More information about the wp-hackers mailing list