Forum

Thread tagged as: Question, Addons

PHP -check if a page is restricted to members

Hi,

I'm using the members app (v1.6.1). On a particular master page, I'd like a way to check whether the page instance is restricted to members (with any tag), so as to select whether to include perch_layout('global/social-share');. (The master page is otherwise identical for some pages outside of the members' area).

Is there a way I can write an if statement with its condition being whether the "Restrict access to members with tags" field is empty? Or is there a better way?

Thanks

Dave C

Dave C 0 points

  • 4 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Dave,

If you want to determine whether a member is logged in, you can use perch_member_logged_in(). If you want to check whether a member has a certain tag, you can use perch_member_has_tag(). Use what's appropriate to your case to determine whether the visitor/member should have access.

Documentation:

Dave C

Dave C 0 points

Hi Hussein,

Thanks but I don't think those functions cover what I need.

I have a master page and when it's used for pages inside the members area I want to exclude a line from the page. That way the social sharing buttons won't show for members-only pages, but will always display for other pages, no matter whether a user is logged in.

Maybe I'm missing something obvious?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Oh I see. Perhaps you can achieve this with page attributes?

sharing.html:

<perch:pages id="sharing" type="checkbox" label="Include social sharing links" value="1" suppress="true" divider-before="Social sharing" />

Add the following line to your templates/pages/attributes/default.html:

<perch:template path="sharing" />

In your master page:

$sharing = perch_page_attribute('sharing', [
    'template' => 'sharing.html'
], true);

if($sharing) {
    perch_layout('global/social-share');
}

Go to your page and click on the Details tab. At the bottom there should be a new checkbox. Unless it's checked, the global/social-share layout won't be included.

Dave C

Dave C 0 points

Thanks very much for that.

Might there be a method where the social sharing links automatically won't be included when the page is for members only? E.g. check whether the "Restrict access to members with tags" field is empty.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Of course you can. Change the logic of the above solution to your needs.

Or you can use the default attribute to have the checkbox checked by default:

<perch:pages id="sharing" type="checkbox" label="Include social sharing links" value="1" default="1" suppress="true" divider-before="Social sharing" />

This way it's checked for all pages by default, and you only have to uncheck it for the pages where you don't want to include the layout.

Dave C

Dave C 0 points

Sorry what I mean is:

When the user creates a members-only page in the admin, by filling in the "Restrict access to members with tags" field, rather than them also having to uncheck the checkbox for social sharing, is there instead a way to automatically detect (in an if) whether the "Restrict access to members with tags" field is empty? (I.e. bypass page attributes.)

Maybe it's a long shot but thought I'd ask

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

To know whether the checkbox field is unchecked you still need to call perch_page_attribute()

$sharing = perch_page_attribute('sharing', [
'template' => 'sharing.html'
], true);

if(!$sharing) {
// do something if checkbox is unchecked
}

Or:

if($sharing) {
// do something if checkbox is checked
perch_layout('global/social-share');
} else {
// do something if checkbox is unchecked
}

Not sure if this answers your question.

Dave C

Dave C 0 points

Is there an API method to tell whether a page has any member tags? If so, I could use this in the if statement. That way, rather than using page attributes, the person creating the page wouldn't have to select whether to display social sharing links.

If not, I'll go ahead and use page attributes

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I may have misunderstood your question. It is not entirely clear to me what you are trying to achieve. Do you want to restrict:

  • whole pages to members?
  • whole pages to members with certain tags?
  • a section of these pages to members?
  • a section of these pages to members with certain tags?

Either way it seems we have come back to perch_member_logged_in() and perch_member_has_tag() functions.

Check if a member is logged in with the perch_member_logged_in() function.

if (perch_member_logged_in()) {
     // do something for members
} else {
     // do something for other visitors
}

Check if a member has a specific tag with the perch_member_has_tag() function.

if (perch_member_has_tag('subscriber')) {
     // do something for members with 'subscriber' tag
} else {
     // do something for other visitors and members without 'subscriber' tag
}

Hope this helps.

Dave C

Dave C 0 points

In the settings tab for a page, the user can enter tags into a field marked "Restrict access to members with tags". (Obviously this is standard for the members app.)

I'd like a way to check whether the page is restricted to members -with any tag. That way, when the master page is used for a page in the members-only area, I can use an if to hide the social sharing links.

I feel like I could me missing an obvious solution but that's what I'm after.

Something like:

if (page_has_any_tag()) {
    // nothing
} else {
    perch_layout('global/social-share');
}

If this isn't possible, I assume the two options are

  • replicate the master page to make a new one for the members-only area and simply remove that line (which seems un-DRY)
  • or, use page attributes, which requires the user in making the page to select something to hide the social shares, when it feels like it should be automatic
Duncan Revell

Duncan Revell 78 points
Registered Developer

It sounds like you probably need to make a relatively simple app that creates one runtime function to look at the pageAccessTags column in the perch_pages table - if there is something in there for the page you're on, return true. Use that result to decide if the social links show.

Essentially, there's nothing built in to do that, as far as I'm aware.

EDIT: I should add that there is no public API available for this, so you would be going off piste a bit.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

In the settings tab for a page, the user can enter tags into a field marked "Restrict access to members with tags". (Obviously this is standard for the members app.)

Ok it sounds to me you are trying to define tags at the page level and don't want to hard-code it in the master page. For example, some pages might be restricted to members with 'subscriber' tag, other pages may be restricted to members with 'premium' tag and by default there is no restriction. And all these pages share a single master page. Is this correct?

If so, I think you can still use page attributes for this.

<perch:pages id="tag" type="text" label="Restrict access to members with tag" suppress="true" divider-before="Other" />

page.php:

$tag = perch_page_attribute('tag', [
    'template' => 'tag.html'
], true);


if(perch_member_has_tag($tag)) {
    // do something for members with tag entered in page settings
    // what you do here is only applied when a member is logged in and has the tag
} else {
    // do something for other instances e.g. other visitors, when no tag entered
}

As far as I'm aware perch_member_has_tag() checks against a single tag, so the above works for a single tag.

Or if you want to check whether a tag was entered in the settings rather than checking whether the logged-in member has entered the tag, i.e. this is to check whether the "page has tags":

if($tag) {
    // tag entered
} else {
    // no tag entered
}
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

or, use page attributes, which requires the user in making the page to select something to hide the social shares, when it feels like it should be automatic

Checking a checkbox is one click. Entering tags require more input from the user and is prone to error.

If you know you always check against one specific tag, you can hard-code it in the master page, use a checkbox field in your page attributes and rather than using a label like 'Hide sharing links' you can use 'Restrict to members' or 'Restrict to members with subscriber tag', so it feels "automatic" to the user.

There are various ways to handle this including building your own app like Duncan suggested. Hopefully you now have a good idea how to tackle this.

Dave C

Dave C 0 points

Hussein Al Hammad said:

For example, some pages might be restricted to members with 'subscriber' tag, other pages may be restricted to members with 'premium' tag and by default there is no restriction. And all these pages share a single master page. Is this correct?

Yes.

The field marked "Restrict access to members with tags" seems to come as standard with the members app, and if there is anything in that field, the page should not show social shares.