-
Notifications
You must be signed in to change notification settings - Fork 2
/
members-privacy-caps.php
146 lines (127 loc) · 4.26 KB
/
members-privacy-caps.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Plugin Name: Members - Privacy Caps
* Plugin URI: https://themehybrid.com/plugins/members-privacy-caps
* Description: Creates additional capabilities for control over WordPress' privacy and personal data features.
* Version: 1.0.0
* Author: Justin Tadlock
* Author URI: https://themehybrid.com
* Text Domain: members-privacy-caps
* Domain Path: /resources/lang
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
* Fifth Floor, Boston, MA 02110-1301 USA
*
* @package MembersPrivacyCaps
* @version 1.0.0
* @author Justin Tadlock <[email protected]>
* @copyright Copyright (c) 2018, Justin Tadlock
* @link https://themehybrid.com/plugins/members-privacy-caps
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
namespace Members\AddOns\PrivacyCaps;
# Add actions and filters.
add_action( 'init', __NAMESPACE__ . '\load_textdomain', 0 );
add_action( 'members_register_caps', __NAMESPACE__ . '\register_caps' );
add_filter( 'map_meta_cap', __NAMESPACE__ . '\map_meta_cap', 95, 2 );
# Register activation/deactivation hooks.
register_activation_hook( __FILE__, __NAMESPACE__ . '\activation' );
/**
* Adds the privacy capabilities to the administrator role whenever the plugin
* is activated.
*
* On Multisite, we're not adding any caps to a role because these caps are, by
* default, only allowed for people with the `manage_network` capability, which
* is a Super Admin cap. If network owners want sub-site administrators to be
* able to have thse caps, they should be added to the role via the edit role
* screen in Members.
*
* @since 1.0.0
* @access public
* @return void
*/
function activation() {
if ( is_multisite() ) {
return;
}
$role = get_role( 'administrator' );
if ( $role ) {
$role->add_cap( 'export_others_personal_data' );
$role->add_cap( 'erase_others_personal_data' );
$role->add_cap( 'manage_privacy_options' );
}
}
/**
* Loads the plugin's textdomain.
*
* @since 1.0.0
* @access public
* @return void
*/
function load_textdomain() {
load_plugin_textdomain(
'members-privacy-caps',
false,
trailingslashit( dirname( plugin_basename( __FILE__ ) ) ) . '/resources/lang'
);
}
/**
* Registers capabilities with the Members plugin.
*
* @since 1.0.0
* @access public
* @return void
*/
function register_caps() {
members_register_cap( 'manage_privacy_options', [
'label' => __( 'Manage Privacy Options', 'members-privacy-caps' ),
'group' => 'general'
] );
members_register_cap( 'erase_others_personal_data', [
'label' => __( "Erase Others' Personal Data", 'members-privacy-caps' ),
'group' => 'user'
] );
members_register_cap( 'export_others_personal_data', [
'label' => __( "Export Others' Personal Data", 'members-privacy-caps' ),
'group' => 'user'
] );
}
/**
* The privacy caps are mapped to `manage_options` (or `manage_network` in the
* case of multisite) by default, effectively making them meta caps. We're
* turning the caps into primitive caps.
*
* @since 1.0.0
* @access public
* @param array $caps
* @param string $cap
* @return array
*/
function map_meta_cap( $caps, $cap ) {
$privacy_caps = [
'export_others_personal_data',
'erase_others_personal_data',
'manage_privacy_options'
];
if ( in_array( $cap, $privacy_caps ) ) {
// The cap should map back to itself.
$caps = [ $cap ];
// Core WP requires the 'delete_users' cap when erasing a user's
// personal data. This becomes even more important on multisite
// where even a sub-site admin might not be able to delete users.
// So, we're adding this as a required cap too.
if ( 'erase_others_personal_data' === $cap ) {
$caps[] = 'delete_users';
}
}
return $caps;
}