PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.3.3
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.3.3
3.3.3 3.3.2 3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 158 releases
← All changes | includes/Core/WPDRoleManagement.php +116 -0 0.2.5.7 → 3.3.3 View file →
@@ -1,0 +1,116 @@
1 +<?php
2 +
3 +namespace NotificationX\Core;
4 +
5 +
6 +/**
7 + * Role Management Class for WPDev
8 + */
9 +class WPDRoleManagement {
10 + /**
11 + * [
12 + * 'read_notificationx' => [
13 + * 'roles' => ['administrator'],
14 + * 'map' => [],
15 + * ],
16 + * 'edit_notificationx' => [
17 + * 'roles' => ['administrator'],
18 + * 'map' => ['read_notificationx'],
19 + * ],
20 + * 'edit_notificationx_settings' => [
21 + * 'roles' => ['administrator'],
22 + * 'map' => ['read_notificationx'],
23 + * ],
24 + * 'read_notificationx_analytics' => [
25 + * 'roles' => ['administrator'],
26 + * 'map' => ['read_notificationx'],
27 + * ],
28 + * ];
29 + *
30 + * @var array
31 + */
32 + public $cap_roles = [];
33 +
34 + /**
35 + * Initial Invoked
36 + */
37 + public function __construct($cap_roles){
38 + $this->cap_roles = $cap_roles;
39 + add_filter('user_has_cap', array($this, 'allow_admin'), 10, 4);
40 + add_filter('map_meta_cap', array($this, 'map_meta_cap'), 10, 4);
41 + add_action('wpd_add_cap', array($this, 'add_role_caps'), 10);
42 + }
43 +
44 + /**
45 + * Add caps to selected roles on settings save.
46 + *
47 + * @param array $cap_roles
48 + * @return void
49 + */
50 + public function add_role_caps($cap_roles) {
51 + // Try global first
52 + global $wp_roles;
53 +
54 + // Fallback if not set
55 + if ( ! isset( $wp_roles ) || ! is_object( $wp_roles ) ) {
56 + $wp_roles = wp_roles(); // Safe fallback
57 + }
58 +
59 + if ( isset($wp_roles->role_objects) && is_array($wp_roles->role_objects) ) {
60 + foreach ( $wp_roles->role_objects as $role_name => $role ) {
61 + foreach ( $cap_roles as $cap => $_role ) {
62 + if ( $role_name === 'administrator' ) {
63 + $role->add_cap($cap);
64 + } elseif ( in_array( $role_name, $_role['roles'], true ) ) {
65 + $role->add_cap($cap);
66 + } else {
67 + $role->remove_cap($cap);
68 + }
69 + }
70 + }
71 + }
72 + }
73 +
74 +
75 + /**
76 + * Enable caps based on passed cap_roles.
77 + *
78 + *
79 + * @return array
80 + */
81 + public function allow_admin($allcaps, $caps, $args, $user) {
82 + foreach ($this->cap_roles as $cap => $_role) {
83 + // admin has all caps.
84 + if(!empty($allcaps['administrator'])){
85 + $allcaps[$cap] = true;
86 + }
87 + // if user has cap then also add the mapped(dependency) cap.
88 + elseif(!empty($allcaps[$cap]) && !empty($_role['map']) && is_array($_role['map'])){
89 + foreach ($_role['map'] as $key => $map) {
90 + if(in_array($map, $caps)){
91 + $allcaps[$map] = true;
92 + }
93 + }
94 + }
95 + }
96 + return $allcaps;
97 + }
98 +
99 + /**
100 + * Add additional cap for specified cap, based on passed cap_roles.
101 + *
102 + *
103 + * @return array
104 + */
105 + public function map_meta_cap($caps, $cap, $user_id, $args) {
106 + foreach ($this->cap_roles as $_cap => $_role) {
107 + if($cap == $_cap && !empty($_role['map']) && is_array($_role['map'])){
108 + foreach ($_role['map'] as $key => $cap) {
109 + $caps[] = $cap;
110 + }
111 + }
112 + }
113 + return $caps;
114 + }
115 +
116 +}