-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add youth member role #73
base: master
Are you sure you want to change the base?
Conversation
Implemented a youth member role which is assigned if the visitor is a member and has an age <=36.
Hey Steffan, The code looks fine to me, but I wonder if that's the way we actually want to go .. 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? Only my 2 cents .. |
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. |
I found a way to accomplish our use case without changing the
<?php
/*
Plugin Name: BCC Login Age Restriction
Extension plugin for https://github.com/bcc-code/bcc-wp
*/
const dwjz_post_id = 1758; // the id of the post where to apply the age restriction
const dwjz_max_age = 36;
function dwjz_on_template_redirect() {
// check post id
if (get_the_ID() === dwjz_post_id && !dwjz_is_youth()) {
return dwjz_not_allowed_to_view_page();
}
}
add_action( 'template_redirect', 'dwjz_on_template_redirect', 1 );
function dwjz_filter_menu_items( $items ) {
foreach ( $items as $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`.
*/
function dwjz_is_youth() {
// Check if user is logged in to Wordpress
if (!is_user_logged_in()) {
return false;
}
// Check if oidc token is available
if (!isset($_COOKIE['oidc_token_id'])) {
return false;
}
// 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 ) ) {
return false;
}
// 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;
}
function dwjz_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,
)
);
} |
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.