latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

dynamic category protection

Home Forums Community Forum dynamic category protection

This topic contains 2 replies, has 2 voices. Last updated by  Raam Dev 4 years, 7 months ago.

Topic Author Topic
Posted: Tuesday Jun 5th, 2012 at 12:37 pm #15538
aphid
Username: slope

hello s2friends,

first of all, thank you for the wonderful plugin!

i am running a site where users can sign up to be part of a ‘silo’ whereas each silo can only access two specific categories. there are multiple silos, with each silo only having access to their two specific categories and none other.

this is being accomplished with custom capabilities: when users sign up, they are given two capabilities, each one being the name of the category they can access – e.g: pizza_a, sandwich_a

i have created a template_redirect action in my functions.php which is being used to redirect URIs like /pizza and /sandwich to their respective /pizza_a and /sandwich_a categories — this is working splendidly.

now i am tasked with preventing access to other categories. i have used the following, well-documented content protection scheme:

if(is_category('pizza_a') && !current_user_can ("access_s2member_ccap_pizza_a")) {
    header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
    exit ();
}
elseif (is_category('sandwich_a') && !current_user_can ("access_s2member_ccap_sandwich_a")) {
    header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
    exit ();
}

this works great, but do i really have to make one of these blocks for each silo i wish to protect? of course, i would like to discover some dynamic option by which i do not have to create a massive if/elseif block for each silo, and having to edit the code every time a silo is added (there are to be a multitude of silos)

any ideas or do i just need to hardcode this? thank you for your time.

List Of Topic Replies

Viewing 2 replies - 1 through 2 (of 2 total)
Author Replies
Author Replies
Posted: Tuesday Jun 5th, 2012 at 12:52 pm #15544
aphid
Username: slope

well sometimes writing out a question helps one discover a solution. i have attempted this, and as far as i can tell this seems to work:

// dynamically protect categories a-z
$letters = range('a', 'z');

foreach ($letters as $l) {
    
    if(is_category('pizza_' . $l) && !current_user_can ("access_s2member_ccap_pizza_" . $l)) {
        header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
        exit ();
    }
    elseif (is_category('sandwich_' . $l) && !current_user_can ("access_s2member_ccap_sandwich_" . $l)) {
        header ("Location: " . S2MEMBER_MEMBERSHIP_OPTIONS_PAGE_URL);
        exit ();
    }

}

i wonder about the performance considerations of this code, but something is perhaps better than nothing, eh?

take care my s2friends.

Posted: Wednesday Jun 6th, 2012 at 3:15 am #15648
Raam Dev
Username: Raam
Staff Member

Hi Aphid,

Glad you figured it out. :) Thank you for sharing the code!

Viewing 2 replies - 1 through 2 (of 2 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.