-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrcp-exclude-post-type-restrictions.php
120 lines (94 loc) · 3.23 KB
/
rcp-exclude-post-type-restrictions.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Plugin Name: Restrict Content Pro - Exclude from Post Type Restrictions
* Description: Allows you to exclude a post from global post type restrictions.
* Version: 1.0
* Author: Sandhills Development, LLC
* Author URI: https://sandhillsdev.com
* License: GPL2
*/
/**
* If this post has post type restrictions, add a checkbox allowing you to exclude this post.
*
* @since 1.0
* @return void
*/
function ag_rcp_exclude_from_restrictions_meta() {
$pt_restrictions = rcp_get_post_type_restrictions( get_post_type( get_the_ID() ) );
// Bail if this post type is not restricted.
if ( empty( $pt_restrictions ) ) {
return;
}
// Otherwise, show the checkbox.
$override = get_post_meta( get_the_ID(), 'rcp_override_pt_restrictions', true );
?>
<div class="rcp-metabox-field">
<p><strong><?php _e( 'Override' ); ?></strong></p>
<label for="rcp-override-pt-restrictions">
<input type="checkbox" name="rcp_override_pt_restrictions" id="rcp-override-pt-restrictions" value="1"<?php checked( $override ); ?>/>
<?php _e( 'Override post type restrictions and make this post available to everyone.', 'rcp' ); ?>
</label>
</div>
<?php
wp_nonce_field( 'rcp_save_post_type_restriction_override', 'rcp_post_type_restriction_override_nonce' );
}
add_action( 'rcp_metabox_additional_options_before', 'ag_rcp_exclude_from_restrictions_meta' );
/**
* Save checkbox.
*
* @param int $post_id ID of the post being saved.
*
* @since 1.0
* @return void
*/
function ag_rcp_save_pt_override( $post_id ) {
// Verify nonce.
if ( empty( $_POST['rcp_post_type_restriction_override_nonce'] ) || ! wp_verify_nonce( $_POST['rcp_post_type_restriction_override_nonce'], 'rcp_save_post_type_restriction_override' ) ) {
return;
}
// Check autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check permissions.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( ! empty( $_POST['rcp_override_pt_restrictions'] ) ) {
update_post_meta( $post_id, 'rcp_override_pt_restrictions', 1 );
} else {
delete_post_meta( $post_id, 'rcp_override_pt_restrictions' );
}
}
add_action( 'save_post', 'ag_rcp_save_pt_override' );
/**
* If this post has a global post type restriction and the override box is checked, grant the user access.
*
* @param bool $can_access Whether or not the user can access this post.
* @param int $user_id ID of the user being checked.
* @param int $post_id ID of the post being checked.
* @param RCP_Member $member Member object.
*
* @since 1.0
* @return bool
*/
function ag_rcp_maybe_override_pt_restrictions( $can_access, $user_id, $post_id, $member ) {
if ( $can_access ) {
return $can_access;
}
$post_type_restrictions = rcp_get_post_type_restrictions( get_post_type( $post_id ) );
// Post type isn't restricted - bail.
if ( empty( $post_type_restrictions ) ) {
return $can_access;
}
$override = get_post_meta( $post_id, 'rcp_override_pt_restrictions', true );
if ( ! empty( $override ) ) {
return true;
}
return $can_access;
}
add_filter( 'rcp_member_can_access', 'ag_rcp_maybe_override_pt_restrictions', 99999, 4 );