Page 1 of 1

Hiding protected posts

PostPosted: September 7th, 2010, 5:24 am
by camillemm
Hello,
I use the Comicpress theme for my blog and want to protect one specific category. That's OK :-)
The problem: when a post belongs to the protected category, the visitor is directly moved to the Membershtip Sign Up page. It's the normal behaviour but as my protected posts will be "mixed" with the regular posts, the navigation (it's a "comics" navigation: go to next post, go to previous post) will be very hard for the non registered users.
I want to hide the protected posts to non registered users and that the post shows for the registered. I tried to put "private" for the protected posts (and put a function to show the "private posts" to registered users) but I get some little problems (I wanted to show the count of the post in the protected category and private posts cannot be count).
So I will try some plugin like http://multinc.com/social-privacy/ but I'm afraid they could be interfer with S2member.
If somebody has an idea for my problem, I will be glad to read it :-)

Re: Hiding protected posts

PostPosted: September 7th, 2010, 10:42 am
by Elizabeth
Hi,

Check out the Advanced Query Conditionals.
Dashboard -> API /Scripting -> Advanced Query Conditionals
Advanced Query Conditionals ( some PHP scripting required )

s2Member provides several built-in API Functions that are tailored to meet the needs of developers integrating s2Member into their themes. Such as: is_protected_by_s2member($id, "[category,tag,post,page,singular,uri]"), is_permitted_by_s2member($id, "[category,tag,post,page,singular,uri]"), is_category_protected_by_s2member($cat_id), is_category_permitted_by_s2member($cat_id), is_tag_protected_by_s2member($tag_id [slug or tag name]), is_tag_permitted_by_s2member($tag_id [slug or tag name]), is_post_protected_by_s2member($post_id), is_post_permitted_by_s2member($post_id), is_page_protected_by_s2member($page_id), is_page_permitted_by_s2member($page_id), is_uri_protected_by_s2member($uri [or full url]), is_uri_permitted_by_s2member($uri [ or full url]).

In addition, there are two special functions that can be applied by theme authors before making custom queries: attach_s2member_query_filters(), detach_s2member_query_filters(). These can be used before and after a call to query_posts() for example. s2Member will automatically filter all protected content ( not available to the current User/Member ).

Code: Select all
Example #1: Pre-filtering custom queries in WordPress®.

<?php
attach_s2member_query_filters();
    query_posts("posts_per_page=5");

    if (have_posts()):
        while (have_posts()):
            the_post();
        /*
        Protected content will be excluded automatically.
        ( based on the current User/Member status )
        */
        endwhile;
    endif;

    wp_reset_query();
detach_s2member_query_filters();
?>

Example #2: OR, instead of pre-filtering; check Access Restrictions in The Loop.

<?php
detach_s2member_query_filters();
    query_posts("posts_per_page=5");

    if (have_posts()):
        while (have_posts()):
            the_post();

            if(!is_permitted_by_s2member())
                continue;
            /* Skip it. The current User/Member has NO access. */

        endwhile;
    endif;

    wp_reset_query();
?>

Re: Hiding protected posts

PostPosted: September 7th, 2010, 11:23 am
by camillemm
Well, I am not the author of the theme but I will look carefully at this code. Thx for quick answer !

Re: Hiding protected posts

PostPosted: September 8th, 2010, 12:12 am
by Jason Caldwell
Hi there. Thanks for the great question.
I recently did a ComicPress installation for a client. It's a very nice theme design.

It sounds to me like you should go with Tag Level Access, instead of Category Level Access.
In other words, if you protect an entire Category, you're blocking the archive view that is provided by ComicPress. Instead, I would apply s2Member's protection to a special Tag, like: members only or something similar. Then apply that Tag to the Posts that you'd like to block access to. This will allow excerpts of each Post to be visible in archive views, but the Permalinks will still be protected.

In the latest release of s2Member, there is a new form of protection called
"Alternative View Protection". You may also want to look over that section.
In your Dashboard, see: s2 -> General Options -> Alternative View Protection.

Other related topics:
viewtopic.php?f=4&t=640&p=2719&hilit=permalink#p2719
viewtopic.php?f=4&t=587&p=2615&hilit=alternative+view+protection#p2615

Re: Hiding protected posts

PostPosted: September 8th, 2010, 1:11 am
by camillemm
Thanks for the suggestions. I use a subcategory of comics as protected category but I will have a look at the tag protection but I don't think it will be good for my purpose as I use tags as categories.

As the Alternative protection hide everything, I'm not interested. The purpose is to show something (here a very little thumbnail) in RSS and Tags to say to people it's worth to pay :-) and that they miss something if they don't pay. But it seems I speak to fast. I will have a look to this Alternative protection.

Re: Hiding protected posts

PostPosted: September 8th, 2010, 1:29 am
by camillemm
After reading the options of Alternative View, it seems it's the opposite of my wish :-) because I want to hide the protect post in home and single view but show their existence in all the other views.

Re: Hiding protected posts

PostPosted: September 8th, 2010, 2:16 am
by camillemm
Well,
it seems I cannot make it work with the code you gave me Elizabeth.
Here is my single.php
Code: Select all
<?php
get_header();
attach_s2member_query_filters();
if (have_posts()) :
   while (have_posts()) : the_post();
      if (comicpress_in_comic_category()) {
         if (!comicpress_themeinfo('disable_comic_blog_single')) {
            comicpress_display_post();
            $cur_date = mysql2date('Y-m-j', $post->post_date);
            $next_comic = comicpress_get_next_comic();
            if (!empty($next_comic)) {
               $next_comic = (array)$next_comic;
               $next_date = mysql2date('Y-m-j', $next_comic['post_date']);
            }
            $blog_query = 'showposts='.comicpress_themeinfo('blog_postcount').'&order=asc&cat='.comicpress_exclude_comic_categories();
         }
      } else {
         comicpress_display_post();
      }
      comments_template('', true);
      
   endwhile;

   
   if (is_active_sidebar('blog')) get_sidebar('blog');
   
   if (comicpress_themeinfo('static_blog') && comicpress_in_comic_category()) {
      if (!comicpress_themeinfo('split_column_in_two')) {
         $blog_query = 'showposts='.comicpress_themeinfo('blog_postcount').'&cat='.comicpress_exclude_comic_categories().'&paged='.$paged;
         
         $posts = &query_posts($blog_query);
      if (have_posts()) { ?>
      
         <?php if (!comicpress_themeinfo('disable_blogheader')) { ?>
            <div id="blogheader"><!-- This area can be used for a heading above your main page blog posts --></div>
         <?php } ?>
         
         <div class="blogindex-head"></div>
         <div class="blogindex">
            <?php while (have_posts()) : the_post();
               
               comicpress_display_post();   
            
         endwhile; ?>
         </div>
         <div class="blogindex-foot"></div>
         <?php }
         comicpress_pagination();
      } else {
         comicpress_dual_columns();
      }
   } else {
      if (comicpress_themeinfo('blogposts_with_comic')) {
         
         $temppost = $post;
         $temp_query = $wp_query;      
         
         if (comicpress_in_comic_category()) {
            function filter_where($where = '') {
               global $cur_date, $next_date;
               if (!empty($next_date)) {
                  $where .= " AND post_date >= '".$cur_date."' AND post_date <= '".$next_date."'";
               } else {
                  $where .= " AND post_date >= '".$cur_date."'";
               }
               return $where;
            }
            add_filter('posts_where', 'filter_where');
            $posts = &query_posts($blog_query);
            if (have_posts()) { while (have_posts()) : the_post();
                  comicpress_display_post();
                  comments_template('', true);
            endwhile; }
         }
         $post = $temppost; $wp_query = $temp_query; $temppost = null; $temp_query = null;
      }
   }
else:

?>
   <div <?php post_class(); ?>>
      <div class="post-head"></div>
      <div class="post">
         <p><?php _e('Sorry, no posts matched your criteria.','comicpress'); ?></p>
         <div class="clear"></div>
      </div>
      <div class="post-foot"></div>
   </div>
   <?php

endif;
       wp_reset_query();
detach_s2member_query_filters();
   
if (is_active_sidebar('under-blog')) get_sidebar('underblog');

get_footer();
?>


And my index.php

Code: Select all
<?php
get_header();
attach_s2member_query_filters();
if (!comicpress_themeinfo('disable_comic_frontpage') && !comicpress_themeinfo('disable_comic_blog_frontpage') && !is_paged() )  {
   $wp_query->in_the_loop = true; $comicFrontpage = new WP_Query(); $comicFrontpage->query('showposts=1&cat='.comicpress_all_comic_categories_string());
   while ($comicFrontpage->have_posts()) : $comicFrontpage->the_post();
      comicpress_display_post();
   endwhile;
   wp_reset_query();
detach_s2member_query_filters();
}

if (is_active_sidebar('blog')) get_sidebar('blog');

if (!comicpress_themeinfo('disable_blogheader')) { ?>
   <div id="blogheader"><?php echo comicpress_themeinfo('blogheader_text'); ?></div>
<?php
}

if (!comicpress_themeinfo('disable_blog_frontpage')) {
   Protect();
   if (!comicpress_themeinfo('split_column_in_two')) {
      $paged = get_query_var('paged');
      $blog_query = 'showposts='.comicpress_themeinfo('blog_postcount') .'&cat='.comicpress_exclude_comic_categories().'&paged='.$paged;
      $posts = &query_posts($blog_query);
      if (have_posts()) { ?>
         <div class="blogindex-head"></div>
         <div class="blogindex">
         <?php while (have_posts()) : the_post();
            comicpress_display_post();
         endwhile; ?>
         </div>
         <div class="blogindex-foot"></div>
      <?php }
      comicpress_pagination();
   } else {
      comicpress_dual_columns();
   }
   UnProtect();
}

if (is_active_sidebar('under-blog')) get_sidebar('underblog');

get_footer();
?>


If you have some suggestion...

Re: Hiding protected posts

PostPosted: September 9th, 2010, 4:24 am
by camillemm
Ooch, as my tests were wrong (see my post about "manuel new member"), my solution with "private" posts do not work. The profiles created by S2member are not recognize as "traditionnal member" by WP. So the "hide categories" plugins do not work either.

Re: Hiding protected posts

PostPosted: September 9th, 2010, 5:07 am
by Jason Caldwell
camillemm wrote:After reading the options of Alternative View, it seems it's the opposite of my wish :-) because I want to hide the protect post in home and single view but show their existence in all the other views.

Gotchya. So you DO want to enable Alternative View Protection, but ONLY on your Home Page and/or in special queries inside single.php. So here is what I recommend, please report back on this if you can.

1. Alternative View Protection = off
2. Use attach_s2member_query_filters() and detach_s2member_query_filters()
Please note that in order for these to work, you MUST call upon query_posts() after you attach the filters. In your example of single.php, this is not the case. So you'll need to work some magic using something like the following example, being careful not to affect the wrong parts of your theme at the wrong time.

Code: Select all

global $query_string
;
attach_s2member_query_filters();
/* Now re-query -> */ query_posts($query_string);

while (have_posts()): the_post();
     ... something, something
endwhile;

wp_reset_query();
detach_s2member_query_filters();
 

Make sure you're logged OUT whenever you test this. Also be sure that you've protected some content first, and that you're performing a query that would actually result in something being filtered.
This can be a little tricky at times ( depending on pagination, the specific archive view, etc ).

* NOTE: the home page sample you sent over, looks to me like it should be working as expected.
* NOTE: Make sure that your queries do NOT have this in the query string:
query_posts("suppress_filters=true");

Related article on query_posts()
[ http://codex.wordpress.org/Function_Ref ... uery_posts ]

Re: Hiding protected posts

PostPosted: September 9th, 2010, 8:27 am
by camillemm
Thank you for reply. You are right, the homepage works... on a test site. With the same theme, the same plugins, it does not works on my usual blog. It's a complete mystery to me. Maybe I will find the little difference between the two...
For the single.php file, I did not manage (even on the test site :-)) to make it work. For the moment, I stop searching. I have contacted a plugin creator who made a "hide category to non connected people" plugin and I hope he will find a trick to make it works with S2member :-)
Last solution: changing the theme (aarrr !)

Re: Hiding protected posts

PostPosted: September 9th, 2010, 10:40 am
by Jason Caldwell
Thanks for reporting back.
Sure. You might want to steer your developer into the direction of s2Member's API Scripting section.
In your Dashboard, see: s2Member -> API Scripting -> Query Conditionals

Hope this helps. Please keep us posted if you can.
~ Much appreciated.

Re: Hiding protected posts

PostPosted: September 13th, 2010, 4:13 am
by camillemm
Hello again. As the Comicpress Theme is too hard for me to modify, I am working on a new theme much simpler.
I created a simple condition:
Code: Select all
<?php if(in_category(3) && is_user_not_logged_in()){ ?>

where "3" is my protected category. I did not find the eventual function used by S2member to get the protected categories ID. Does it exist ?