Skip to content

Commit

Permalink
Fix group membership
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavgreif authored and jakubboucek committed Feb 1, 2021
1 parent 5ad1d46 commit f3e1e71
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,14 @@ public function user_can_view_content( $args = [] ) {
// }


$group = new Group( $group_id );
$group = new Group( $group_id );
$membership = new Membership( get_current_user_id() );

if ($group_id) {
if ( ! is_user_logged_in() || ! $membership->is_valid_for_group( $group_id ) ) {
return false;
}
}

if ( $is_member == 'yes' ) {
// Check, if the user is logged in and is member of the group, if not, bail
Expand Down Expand Up @@ -354,7 +361,6 @@ public function user_can_view_content( $args = [] ) {
// Group check done, check if there are some days set and if is_member is yes
// it doesn't make sense to check days condition for users who should NOT be members of a group
if ( ! empty( $days_to_view ) && $is_member == 'yes' ) {
$membership = new Membership( get_current_user_id() );
$subscription_date = $membership->groups[ $group_id ]['subscription_date'];
// Compare against today's date
if ( date( 'Y-m-d' ) < date( 'Y-m-d', strtotime( "$subscription_date + $days_to_view days" ) ) ) {
Expand All @@ -373,7 +379,6 @@ public function user_can_view_content( $args = [] ) {
* @return mixed
*/
public function setup_nav_menu_item( $item ) {

if ( ! $this->user_can_view_post( $item->object_id ) ) {
$item->classes[] = 'ssc-hide';
$item->title = '';
Expand Down
27 changes: 27 additions & 0 deletions src/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,32 @@ public function get_subscription_date( $group_id ) {
public function get_valid_to( $group_id ) {
return get_user_meta( $this->user_id, '_ssc_group_subscription_valid_to_' . $group_id, true );
}

/**
* Check if the membership is valid for specific group
*
* @param $group_id
*
* @return bool
*/
public function is_valid_for_group( $group_id ) {
foreach ( $this->groups as $group ) {
if ( $group['group_id'] != $group_id ) {
continue;
}

if (!empty($group['subscription_date']) && $group['subscription_date'] >= date( 'Y-m-d' )) {
return false;
}

if (!empty($group['valid_to']) && $group['valid_to'] <= date( 'Y-m-d' )) {
return false;
}

return true;
}

return false;
}
}

0 comments on commit f3e1e71

Please sign in to comment.