-
Notifications
You must be signed in to change notification settings - Fork 15
/
rcp-restrict-entire-site.php
76 lines (60 loc) · 1.86 KB
/
rcp-restrict-entire-site.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* Plugin Name: Restrict Content Pro - Restrict Entire Site
* Description: Restricts access to the entire site except for designated RCP pages such as registration, login, etc.
* Version: 1.0
* Author: Sandhills Development, LLC
* Author URI: https://sandhillsdev.com
* License: GPL2
*/
/**
* Redirect non-members to the "Redirect Page". This is set in Restrict > Settings > Misc.
*
* @since 1.0
* @return void
*/
function ag_rcp_restrict_entire_site() {
// If the current user is an admin, bail.
if ( current_user_can( 'manage_options' ) ) {
return;
}
if ( ! function_exists( 'rcp_user_has_active_membership' ) ) {
return;
}
// If current user has an active membership, bail.
if ( rcp_user_has_active_membership() ) {
return;
}
// Otherwise we need to redirect them.
global $rcp_options;
// Inactive users will be able to access these pages.
$whitelisted_post_ids = array(
$rcp_options['registration_page'],
$rcp_options['redirect'],
$rcp_options['account_page'],
$rcp_options['edit_profile'],
$rcp_options['update_card'],
$rcp_options['login_redirect'],
$rcp_options['redirect_from_premium']
);
// Whether or not inactive users should be able to access the homepage.
$whitelist_home = false; // Change this to TRUE if you want to allow access to the homepage.
if ( ! empty( $rcp_options['redirect_from_premium'] ) ) {
$redirect_url = get_permalink( $rcp_options['redirect_from_premium'] );
} else {
$redirect_url = home_url();
$whitelist_home = true;
}
// This is a whitelisted ID - bail.
if ( is_singular() && in_array( get_the_ID(), $whitelisted_post_ids ) ) {
return;
}
// Allow access to homepage if this is whitelisted.
if ( $whitelist_home && is_front_page() ) {
return;
}
// Otherwise redirect.
wp_safe_redirect( $redirect_url );
exit;
}
add_action( 'template_redirect', 'ag_rcp_restrict_entire_site' );