Created
February 18, 2021 11:49
-
-
Save ipokkel/55034deaff6c4bcb90276288f161f0dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This recipe allows members to renew a locked membership level | |
* if the level was locked with PMPro Lock Membership Levels Add On. | |
* | |
* The member will only be able to checkout and renew for their previous | |
* level that was locked and not other levels. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_init_switch_pmprolml_redirect_with_custom_redirect() { | |
if ( function_exists( 'pmprolml_template_redirect' ) ) { | |
remove_action( 'template_redirect', 'pmprolml_template_redirect' ); | |
add_action( 'template_redirect', 'my_pmprolml_template_redirect' ); | |
} | |
} | |
add_action( 'init', 'my_init_switch_pmprolml_redirect_with_custom_redirect' ); | |
function my_pmprolml_template_redirect() { | |
global $pmpro_pages, $current_user, $wpdb; | |
if ( empty( $pmpro_pages ) || empty( $pmpro_pages['membership_locked'] ) || ! is_user_logged_in() || ! function_exists( 'pmprolml_getUserOptions' ) ) { | |
return; | |
} | |
$user_lock_options = pmprolml_getUserOptions( $current_user->ID ); | |
$sql = "SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE `user_id` = '$current_user->ID' ORDER BY `modified` DESC limit 1"; | |
$previous_level = intval( $wpdb->get_var( $sql ) ); | |
$previous_level_locked = pmprolml_getLevelOptions( $previous_level )['lock']; | |
//Redirect away from the membership locked page if user isn't locked. | |
if ( is_page( $pmpro_pages['membership_locked'] ) && ( empty( $user_lock_options ) || empty( $user_lock_options['locked'] ) ) ) { | |
if ( pmpro_hasMembershipLevel() ) { | |
wp_redirect( pmpro_url( 'account' ) ); | |
exit; | |
} elseif ( $previous_level_locked ) { | |
return; | |
} else { | |
wp_redirect( home_url() ); | |
exit; | |
} | |
} | |
//Redirect to the membership locked page if user is locked. | |
$locked_pages = array( | |
$pmpro_pages['levels'], | |
$pmpro_pages['cancel'], | |
); | |
if ( is_page( $locked_pages ) ) { | |
if ( ( ! empty( $user_lock_options ) && ! empty( $user_lock_options['locked'] ) ) || $previous_level_locked ) { | |
if ( ! empty( $pmpro_pages['membership_locked'] ) ) { | |
wp_redirect( pmpro_url( 'membership_locked' ) ); | |
exit; | |
} else { | |
wp_redirect( home_url() ); | |
exit; | |
} | |
} | |
} | |
if ( is_page( $pmpro_pages['checkout'] ) ) { | |
$level_id = $_REQUEST['level']; | |
if ( ! empty( $level_id ) && ! pmpro_hasMembershipLevel( $level_id ) ) { | |
if ( $previous_level_locked && intval( $level_id ) !== $previous_level ) { | |
wp_redirect( pmpro_url( 'membership_locked' ) ); | |
exit; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment