Skip to content

Add youth member role - #73

Open
steff-mueller wants to merge 3 commits into
bcc-code:masterfrom
steff-mueller:master
Open

Add youth member role#73
steff-mueller wants to merge 3 commits into
bcc-code:masterfrom
steff-mueller:master

Conversation

@steff-mueller

@steff-muellersteff-mueller commented Jan 15, 2023

Copy link
Copy Markdown

Implemented a youth member role for content access which is assigned if the visitor is a member of your local church and has an age <=36.

Implemented a youth member role which is assigned if the visitor is a member
and has an age <=36.
@cs1m0n

Copy link
Copy Markdown
Member

Hey Steffan,
and thank you so much for your contribution!

The code looks fine to me, but I wonder if that's the way we actually want to go ..
E.g. what happens if you want to target a youth member only from your local church? Will we then add another role for that?

I fear that without a clear goal for this, we'll continue adding more and more roles and end up having no control over the logic ..

Can you maybe describe the reason you need to identify youth members so we understand your use case?
Another solution to this is to have the logic inside the widget / plugin itself.

Only my 2 cents ..

@steff-mueller

steff-mueller commented Jan 16, 2023

Copy link
Copy Markdown
Author

Hi @cs1m0n, as I have implemented it right now, the role only targets youth members from your local church. I agree with you that a more flexible logic is the way to go. We needed a quick-n-dirty solution to limit access of our youth meeting livestreams to youth members of our church.

Comment threadplugins/bcc-login/bcc-login.php
@steff-mueller

Copy link
Copy Markdown
Author

I found a way to accomplish our use case without changing the bcc-login plugin. I created a new separate plugin which applies the age restriction on the post/page. Maybe you want to add this snippet to the documentation?

wp-content/plugins/bcc-login-age-restriction/bcc-login-age-restriction.php:

<?php/*Plugin Name: BCC Login Age RestrictionExtension plugin for https://github.com/bcc-code/bcc-wp*/constdwjz_post_id = 1758; // the id of the post where to apply the age restrictionconstdwjz_max_age = 36;
functiondwjz_on_template_redirect() {
// check post idif (get_the_ID() === dwjz_post_id && !dwjz_is_youth()) {
returndwjz_not_allowed_to_view_page();
}
}
add_action( 'template_redirect', 'dwjz_on_template_redirect', 1 );
functiondwjz_filter_menu_items( $items ) {
foreach ( $itemsas$key => $item ) {
if ($item->object_id == dwjz_post_id && !dwjz_is_youth()) {
unset( $items[ $key ] );
}
}
return$items;
}
add_filter( 'wp_get_nav_menu_items', 'dwjz_filter_menu_items', 21 );
/*Returns `true` if the current user's age is <= `dwjz_max_age`,otherwise the function returns `false`.*/functiondwjz_is_youth() {
// Check if user is logged in to Wordpressif (!is_user_logged_in()) {
returnfalse;
}
// Check if oidc token is availableif (!isset($_COOKIE['oidc_token_id'])) {
returnfalse;
}
// Check if oidc token is valid (still stored in transient)$token_id = $_COOKIE['oidc_token_id'];
if ( ! empty( $token_id ) ) {
$token = get_transient( 'oidc_id_token_' . $token_id );
}
if ( empty( $token ) ) {
returnfalse;
}
// Get token claims$claims = BCC_Login_Token_Utility::get_token_claims( $token );
$birthdate = date_create($claims['birthdate']);
$current_date = date_create();
$diff = date_diff($birthdate, $current_date);
$age = $diff->y;
return$age <= dwjz_max_age;
}
functiondwjz_not_allowed_to_view_page(){
wp_die(
sprintf(
'%s<br><a href="%s">%s</a>',
__( 'Sorry, you are not allowed to view this page.', 'bcc-login' ),
site_url(),
__( 'Go to the front page', 'bcc-login' )
),
__( 'Unauthorized' ),
array(
'response' => 401,
)
);
}

@victorienfotsing

Copy link
Copy Markdown

I have created a new WP plugin for this use case, offering more flexibility. It allows the administrator to define minimum and maximum age limits for each page whose content needs to be restricted.

https://github.com/victorienfotsing/bcc-login-age-restriction

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@steff-mueller@cs1m0n@victorienfotsing