Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Created August 2, 2024 06:34
Show Gist options
  • Save JarrydLong/d20174973213856c28df5ea21d47de98 to your computer and use it in GitHub Desktop.
Save JarrydLong/d20174973213856c28df5ea21d47de98 to your computer and use it in GitHub Desktop.
<?php //do not copy
function my_pmprorh_init_pause_access() {
// Don't break if Register Helper is not loaded.
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// Bail if not in administrative area.
if ( ! is_admin() ) {
return;
}
pmprorh_add_checkout_box( 'pause_hasaccess', 'Access to Member Content' );
// Define the field.
$fields = array();
$fields[] = new PMProRH_Field(
'pmpro_paused_user',
'checkbox',
array(
'label' => 'Pause User',
'text' => 'Deny Access to Member Content',
'profile' => 'admins',
)
);
// Add the fields into the checkout_boxes are of the checkout page.
foreach ( $fields as $field ) {
pmprorh_add_registration_field(
'pause_hasaccess',
$field
);
}
}
add_action( 'init', 'my_pmprorh_init_pause_access', 10, 1 );
/**
* Deny access to member content if user is 'paused'.
*/
function paused_member_pmpro_has_membership_access_filter( $access, $post, $user, $levels ) {
//if we don't have access now, we still won't
if ( ! $access ) {
return $access;
}
//no user, this must be open to everyone
if ( empty( $user ) || empty( $user->ID ) ) {
return $access;
}
//no levels, must be open
if ( empty( $levels ) ) {
return $access;
}
//Pause account access anyway
$paused_user = mypmpro_is_parent_account_paused( $user->ID );
if ( $paused_user ) {
return false;
}
return $access;
}
add_filter( 'pmpro_has_membership_access_filter', 'paused_member_pmpro_has_membership_access_filter', 20, 4 );
/**
* Show a different message for users that have their membership paused.
*/
function paused_member_pmpro_non_member_text_filter( $text ) {
global $current_user, $has_access;
//get current user's level ID
$paused_user = mypmpro_is_parent_account_paused( $current_user->ID );
//if a user does not have a membership level, return default text.
if ( ! empty( $paused_user ) ) {
$text = __( '<p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a>', 'paid-memberships-pro' );
}
return $text;
}
//Deprecated as of 3.1
// add_filter( 'pmpro_non_member_text_filter', 'paused_member_pmpro_non_member_text_filter' );
function mypmpro_pmpro_no_access_message_body( $body, $level_ids ) {
global $current_user, $has_access;
if( $current_user->ID === 0 ) {
return $body;
}
$paused_message = __( '<p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a>', 'paid-memberships-pro' );
$paused_user = mypmpro_is_parent_account_paused( $current_user->ID );
//if a user does not have a membership level, return default text.
if ( $paused_user ) {
return $paused_message;
}
return $body;
}
add_filter( 'pmpro_no_access_message_body', 'mypmpro_pmpro_no_access_message_body', 10, 2 );
/**
* Filter the content of the Membership Account page to show message to paused user.
*
*/
function paused_member_pmpro_membership_account_filter( $content ) {
global $pmpro_pages, $current_user;
// Check if current user is paused.
if ( is_user_logged_in() ) {
$paused_user = mypmpro_is_parent_account_paused( $current_user->ID );
// $paused_user = get_user_meta( $current_user->ID, 'pmpro_paused_user', true );
}
if ( is_page( $pmpro_pages[ 'account' ] ) && ! empty( $paused_user ) ) {
$newcontent = __( '<div class="pmpro_content_message"><p>Your membership is paused. Please contact us to reinstate your membership.</p><a href="/contact/">Contact Us</a></div>', 'paid-memberships-pro' );
$content = $newcontent . $content;
}
return $content;
}
add_filter( 'the_content', 'paused_member_pmpro_membership_account_filter', 10 );
function mypmpro_is_parent_account_paused( $user_id ) {
if( ! class_exists( 'PMProGroupAcct_Group_Member' ) ) {
return false;
}
if( $user_id === 0 ) {
return false;
}
//Is this user paused?
$paused_user = get_user_meta( $user_id, 'pmpro_paused_user', true );
if ( ! empty( $paused_user ) ) {
return true;
}
//Lets check if they're a child account and their parent is paused.
$groupaccount = new PMProGroupAcct_Group_Member( $user_id );
$child = $groupaccount->get_group_members( array( 'group_child_user_id' => $user_id ) );
if( empty( $child ) ) {
return false;
}
foreach( $child as $ch ) {
$group_id = $ch->group_id;
$groups = new PMProGroupAcct_Group( $group_id );
$parent_id = $groups->group_parent_user_id;
//now we need to check if the user is approved for ANY of the $levels
$paused_user = get_user_meta( $parent_id, 'pmpro_paused_user', true );
if ( ! empty( $paused_user ) ) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment