Page 1 of 1

Question about current_user_can

PostPosted: June 25th, 2010, 4:24 pm
by pcwriter
Hi all, php noob here :D

I've set up a number of subscription packages on my WP/Buddypress install which allow/restrict the type of content members can create/upload using custom capabilities (Blog creation package, Media upload package, Group creation package).

My subscription plan structure looks like this:
Lev 1 plans - Blogs package only, Media package only or Groups package only
Lev 2 plans - Blogs & Media, Blogs & Groups, Media & Groups
Lev 3 plan - Blogs, Media & Groups

For subscription modification/upgrades, I want to code the membership options page so that the current plan to which the member is subscribed is identified according to packages included in the plan, and display only modification buttons for those packages NOT included.

For Level 1 (single) plans, it's pretty straightforward... the code is right there in the documentation:

Code: Select all
<?php if (is_user_logged_in() && current_user_can("access_s2member_level1")){ ?>
    <?php if (current_user_can("access_s2member_ccap_blogs")){ ?>
Show subscription modification buttons to add Media OR Group packages
    <?php } ?>
    <?php if (current_user_can("access_s2member_ccap_media")){ ?>
Show subscription modification buttons to add Blogs OR Group packages
    <?php } ?>
    <?php if (current_user_can("access_s2member_ccap_groups")){ ?>
Show subscription modification buttons to add Blogs OR Media packages
    <?php } ?>
<?php } ?>


For Level 2 (double) plans, I need to identify which of the 3 packages the member is currently NOT subscribed to. So, what would be the better way to accomplish this?

Code: Select all
<?php if (is_user_logged_in() && current_user_can("access_s2member_level2")){ ?>
<?php if (!current_user_can("access_s2member_ccap_blogs")){ ?>
Show subscription modification buttons to add Blogs package
<?php } ?>
<?php } ?>


----OR----

Code: Select all
<?php if (is_user_logged_in() && current_user_can("access_s2member_level2") && !current_user_can("access_s2member_ccap_blogs")){ ?>
Show subscription modification buttons to add Blogs package
<?php } ?>


----OR-----

Code: Select all
<?php if (is_user_logged_in() && current_user_can("access_s2member_level2") && current_user_can("access_s2member_ccap_media") && current_user_can("access_s2member_ccap_groups")){ ?>
Show subscription modification buttons to add Blogs package
<?php } ?>


...etc...

So, there seem to be actually several ways I could do this. Which would be better?

Re: Question about current_user_can

PostPosted: June 26th, 2010, 1:10 am
by Jason Caldwell
Ok, so your Level 2 Members will usually have 2 Custom Capabilities, out of 3 total that you're offering; and you need to determine which one they do NOT have in their current package.

Here is how I would do that:

Code: Select all
<?php if(is_user_logged_in() && current_user_can("access_s2member_level2")){ ?>

    <?php if(!current_user_can("access_s2member_ccap_blogs")){ ?>

        Upgrade now to Level #3, add the Blogs Capability to your Membership!

    <?php } else if(!current_user_can("access_s2member_ccap_media")){ ?>

        Upgrade now to Level #3, add the Media Capability to your Membership!

    <?php } else if(!current_user_can("access_s2member_ccap_groups")){ ?>

        Upgrade now to Level #3, add the Groups Capability to your Membership!

    <?php } ?>

<?php } ?>

Re: Question about current_user_can

PostPosted: June 26th, 2010, 1:19 am
by Jason Caldwell
There is another method that is NOT documented by s2Member.
Using this method, you could pull information about someone that is not currently logged into your site. For instance, if you were writing some sort of CRON job, integrating s2Member into a theme, or writing another plugin perhaps.

Code: Select all
<?php
        $user = new WP_User("johndoe22");

        if(!$user->has_cap("access_s2member_ccap_blogs"))
            echo 'They do NOT have the Blogs capability.';

        else if(!$user->has_cap("access_s2member_ccap_media"))
            echo 'They do NOT have the Media capability.';

        else if(!$user->has_cap("access_s2member_ccap_groups"))
            echo 'They do NOT have the Groups capability.';
?>

Note the call to:
Code: Select all
$user = new WP_User("johndoe22");

That could also reference a Member by their WordPress® database ID.
Code: Select all
$user = new WP_User(54);

Re: Question about current_user_can

PostPosted: June 26th, 2010, 5:08 am
by pcwriter
Thanks Jason,

I forgot about "else if". Makes the code a lot neater ;)

BTW, that second suggestion is a bit over my head as I'm still a novice at php... but I'm really good at copy/paste :D

Re: Question about current_user_can

PostPosted: June 26th, 2010, 7:36 am
by pcwriter
Hi again Jason,

I figured I'd post this as sample code to play with for other noobs who want to customize the info displayed on their membership options page according to s2member levels+ccap.

Strange, but "else if" spat out parse errors. So, slight modifications to the code as below and it works like a charm. I tested by assigning various roles and custom capabilities to a test user account (through WPMU dashboard), and the correct upgrade info is displayed on the membership page for each level and ccap bundle.

The first part is the default page display seen by site visitors or not-logged-in members (shows options for all membership levels including free and paid). The 2nd part identifies users who are free members (no ccap access) and displays only options to upgrade to a paid subscription. The 3rd part identifies users who have access to only 1 ccap (Level 1 subscribers) and displays only options to add a 2nd ccap to their subscription (upgrade to Level 2). The 4th part identifies Level 2 subscribers and displays info to add which of the 3 available ccap they don't have access to (upgrade to Level 3). The 5th part identifies members who have subscribed to all 3 ccap options (Level 3) and offers an upgrade to Level 4 Lifetime membership. (This one's gonna be a bit tricky, 'cuz I'll need to figure out a way to get around PayPal's nasty 20% subscription modification limit. But that's a challenge for another day.) The last part simply identifies Level 4 subscribers and gives them a high-5 :)

Thanks again for a fantastic plugin! (Donation on it's way... :D )

Code: Select all
<?php if (!S2MEMBER_CURRENT_USER_IS_LOGGED_IN){ ?>
Show all membership packages and options (free and paid)
<?php } ?>

<?php if (is_user_logged_in() && !current_user_can("access_s2member_level1")){ ?>
This is a free member, so display ALL paid subscription options
<?php } ?>

<?php if (is_user_logged_in() && !current_user_can("access_s2member_level2")){ ?>
    <?php if (current_user_can("access_s2member_ccap_blogs")){ ?>
Subscription modification buttons (Add Media to your Blogs package) OR (Add Groups to your Blogs package)
    <? } ?>
    <?php if (current_user_can("access_s2member_ccap_media")){ ?>
Subscription modification buttons (Add Blogs to your Media package) OR (Add Groups to your Media package)
    <? } ?>
    <?php if (current_user_can("access_s2member_ccap_groups")){ ?>
Subscription modification buttons (Add Blogs to your Groups package) OR (Add Media to your Groups package)
    <?php } ?>
<?php } ?>

<?php if (is_user_logged_in() && current_user_can("access_s2member_level2")){ ?>
    <?php if (!current_user_can("access_s2member_ccap_blogs")){ ?>
Subscription modification button (Add Blogs to your Media+Groups package)
    <? } ?>
    <?php if (!current_user_can("access_s2member_ccap_media")){ ?>
Subscription modification buttons (Add Media to your Blogs+Groups package)
    <? } ?>
    <?php if (!current_user_can("access_s2member_ccap_groups")){ ?>
Subscription modification buttons (Add Groups to your Blogs+Media package)
    <?php } ?>
<?php } ?>

<?php if (is_user_logged_in() && current_user_can("access_s2member_level3") && !current_user_can("access_s2member_level4")){ ?>
ALL options already included in subscription, so offer upgrade to Lev4 Lifetime membership
<?php } ?>

<?php if (is_user_logged_in() && current_user_can("access_s2member_level4")){ ?>
You are already a Lifetime member. Awesome, dude!
<?php } ?>


EDIT: Doh! Reading through the API constants, I just discovered: S2MEMBER_CURRENT_USER_ACCESS_LEVEL.
Could simplify things... :mrgreen:

Re: Question about current_user_can

PostPosted: July 22nd, 2010, 11:44 pm
by Jason Caldwell
Marking this resolved. ~Thanks.