1: <?php
2:
3: 4: 5:
6: class Quform_Permissions
7: {
8: 9: 10: 11: 12:
13: public function getAllCapabilities()
14: {
15: return apply_filters('quform_all_capabilities', array(
16: 'quform_full_access',
17: 'quform_view_dashboard',
18: 'quform_list_forms',
19: 'quform_add_forms',
20: 'quform_edit_forms',
21: 'quform_delete_forms',
22: 'quform_view_entries',
23: 'quform_edit_entries',
24: 'quform_delete_entries',
25: 'quform_resend_notifications',
26: 'quform_view_tools',
27: 'quform_export_entries',
28: 'quform_export_forms',
29: 'quform_import_forms',
30: 'quform_settings',
31: 'quform_help'
32: ));
33: }
34:
35: 36: 37: 38: 39: 40: 41:
42: public function getAllCapabilitiesWithDescriptions()
43: {
44: return apply_filters('quform_all_capabilities_with_descriptions', array(
45: 'quform_full_access' => __('Full Access', 'quform'),
46: 'quform_view_dashboard' => __('View Dashboard', 'quform'),
47: 'quform_list_forms' => __('List Forms', 'quform'),
48: 'quform_add_forms' => __('Add Forms', 'quform'),
49: 'quform_edit_forms' => __('Edit Forms', 'quform'),
50: 'quform_delete_forms' => __('Delete Forms', 'quform'),
51: 'quform_view_entries' => __('View Entries', 'quform'),
52: 'quform_edit_entries' => __('Edit Entries', 'quform'),
53: 'quform_delete_entries' => __('Delete Entries', 'quform'),
54: 'quform_resend_notifications' => __('Resend Notifications', 'quform'),
55: 'quform_view_tools' => __('View Tools Page', 'quform'),
56: 'quform_export_entries' => __('Export Entries', 'quform'),
57: 'quform_export_forms' => __('Export Forms', 'quform'),
58: 'quform_import_forms' => __('Import Forms', 'quform'),
59: 'quform_settings' => __('Edit Settings', 'quform'),
60: 'quform_help' => __('View Help Page', 'quform')
61: ));
62: }
63:
64: 65: 66:
67: public function activate()
68: {
69: $role = get_role('administrator');
70:
71: if ($role instanceof WP_Role) {
72: foreach ($this->getAllCapabilities() as $cap) {
73: $role->add_cap($cap);
74: }
75: }
76:
77:
78: $user = wp_get_current_user();
79:
80: if ($user instanceof WP_User && $user->has_cap('activate_plugins')) {
81: $user->get_role_caps();
82: }
83: }
84:
85: 86: 87: 88: 89:
90: public function update(array $permissions)
91: {
92: $caps = $this->getAllCapabilities();
93:
94:
95: global $wp_roles;
96: $roles = $wp_roles->get_names();
97:
98: foreach ($roles as $key => $name) {
99: if ($key === 'administrator') {
100: continue;
101: }
102:
103: $role = get_role($key);
104:
105: if ( ! $role instanceof WP_Role) {
106: continue;
107: }
108:
109: foreach ($caps as $cap) {
110: $add = isset($permissions[$key][$cap]) && $permissions[$key][$cap];
111:
112: if ( ! $role->has_cap($cap) && $add) {
113: $role->add_cap($cap);
114: } elseif ($role->has_cap($cap) && ! $add) {
115: $role->remove_cap($cap);
116: }
117: }
118: }
119: }
120:
121: 122: 123:
124: public function uninstall()
125: {
126: $caps = $this->getAllCapabilities();
127:
128:
129: global $wp_roles;
130: $roles = $wp_roles->get_names();
131:
132: foreach ($roles as $key => $name) {
133: $role = get_role($key);
134:
135: if ( ! $role instanceof WP_Role) {
136: continue;
137: }
138:
139: foreach ($caps as $cap) {
140: if ($role->has_cap($cap)) {
141: $role->remove_cap($cap);
142: }
143: }
144: }
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154:
155: public function fullAccessCheck($allcaps, $caps, $args)
156: {
157: $capabilities = $this->getAllCapabilities();
158:
159: if (isset($args[0]) && array_search($args[0], $capabilities) !== false) {
160:
161: if (isset($allcaps['quform_full_access']) && $allcaps['quform_full_access']) {
162:
163: foreach ($capabilities as $capability) {
164: $allcaps[$capability] = true;
165: }
166: }
167: }
168:
169: return $allcaps;
170: }
171: }
172: