Created
September 25, 2019 01:32
-
-
Save messica/9eb4b2e5a6b4b78526c36ab8322ca14c to your computer and use it in GitHub Desktop.
Disable membership unless users have agreed to Terms of Service (requires Register Helper).
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 | |
/** | |
* Disable membership unless users have agreed to Terms of Service (requires Register Helper). | |
* | |
* - Adds a ToS checkbox and link to the user's profile page. | |
* - Overrides membership if users haven't agreed to updated ToS. | |
* - Filters non-member text if users haven't agreed to ToS. | |
* - Resets ToS user meta when ToS page is updated. | |
*/ | |
/** | |
* Register Helper Fields | |
*/ | |
function my_pmprorh_init_require_tos() { | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return; | |
} | |
$fields = array(); | |
$tospage = pmpro_getOption( "tospage" ); | |
if ( empty( $tospage ) ) { | |
return; | |
} | |
$tos_link = get_permalink( $tospage ); | |
$label = "I agree to the <a href={$tos_link} target=_blank>Terms of Service</a>."; | |
// Terms of Service Checkbox in Profile | |
$fields[] = new PMProRH_Field( 'pmpro_tos_checked', 'checkbox', array( | |
'label' => $label, | |
'profile' => true | |
) ); | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( 'just_profile', $field ); | |
} | |
} | |
add_action( 'init', 'my_pmprorh_init_require_tos' ); | |
/** | |
* Disable membership if ToS isn't accepted. | |
*/ | |
function my_pmpro_has_membership_access_filter_require_tos( $hasaccess, $post, $user, $levels ) { | |
global $pmpro_tos_check; | |
// Only continue if user already has access. | |
if ( ! $hasaccess || empty( $levels ) ) { | |
$pmpro_tos_checked = false; | |
return $hasaccess; | |
} | |
$tos_checked = get_user_meta( $user->ID, 'pmpro_tos_checked', true ); | |
$pmpro_tos_check = true; | |
return $tos_checked; | |
} | |
add_filter( 'pmpro_has_membership_access_filter', 'my_pmpro_has_membership_access_filter_require_tos', 10, 4 ); | |
/** | |
* Change non-member text when user hasn't agreed to ToS. | |
*/ | |
function my_pmpro_non_member_text_filter_require_tos( $text ) { | |
global $current_user, $pmpro_tos_check; | |
$tos_checked = get_user_meta( $current_user->ID, 'pmpro_tos_checked', true ); | |
if ( ! $tos_checked && $pmpro_tos_check ) { | |
$text = 'You must agree to the Terms of Service before accessing this content.'; | |
} | |
return $text; | |
} | |
add_filter( 'pmpro_non_member_text_filter', 'my_pmpro_non_member_text_filter_require_tos' ); | |
/** | |
* Reset ToS user meta when ToS page is updated. | |
*/ | |
function my_post_updated_require_tos( $post_id, $post_after, $post_before ) { | |
global $wpdb; | |
// Only continue if PMPro is loaded. | |
if ( ! function_exists( 'pmpro_getOption' ) ) { | |
return; | |
} | |
$tospage = pmpro_getOption( "tospage" ); | |
if ( ! empty( $tospage ) && $post_id == $tospage && $post_after->post_content != $post_before->post_content ) { | |
$sql = "UPDATE {$wpdb->usermeta} SET meta_value = '' WHERE meta_key = 'pmpro_tos_checked'"; | |
$wpdb->query( $sql ); | |
} | |
} | |
add_filter( 'post_updated', 'my_post_updated_require_tos', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment