PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 3.2.3
Nested Pages v3.2.3
3.3.1 3.2.15 3.2.14 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.3.1 1.6.3.2 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.6 1.6.7 1.6.8 1.7.0 1.7.1 2.0.1 2.0.2 2.0.3 2.0.4 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.21 3.1.22 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7
wp-nested-pages / app / Entities / User / UserRepository.php
wp-nested-pages / app / Entities / User Last commit date
UserCapabilities.php 6 years ago UserRepository.php 4 years ago
UserRepository.php
226 lines
1 <?php
2 namespace NestedPages\Entities\User;
3
4 use NestedPages\Config\SettingsRepository;
5 use NestedPages\Entities\PluginIntegration\IntegrationFactory;
6
7 /**
8 * User Repository
9 * @since 1.1.7
10 */
11 class UserRepository
12 {
13 /**
14 * Settings Repository
15 * @var object
16 */
17 private $settings;
18
19 /**
20 * Plugin Integrations
21 * @var object
22 */
23 private $integrations;
24
25 public function __construct()
26 {
27 $this->settings = new SettingsRepository;
28 $this->integrations = new IntegrationFactory;
29 }
30
31 /**
32 * Return Current User's Roles
33 * @return array
34 * @since 1.1.7
35 */
36 public function getRoles()
37 {
38 global $current_user;
39
40 // If current user is superadmin (WP Multisite) add administrator to the roles array
41 if (function_exists('is_multisite') && is_multisite() && is_super_admin()) {
42 $current_user->roles[] = 'administrator';
43 }
44
45 return $current_user->roles;
46 }
47
48 /**
49 * Get all roles that arent admin, contributor or subscriber
50 * @return array
51 * @since 1.1.7
52 */
53 public function allRoles($exclude = array('Administrator', 'Contributor', 'Subscriber', 'Author') )
54 {
55 global $wp_roles;
56 $all_roles = $wp_roles->roles;
57 $editable_roles = apply_filters('editable_roles', $all_roles);
58 $roles = [];
59 if ( !is_array($exclude) ) $exclude = [];
60 foreach($editable_roles as $key=>$editable_role){
61 if ( !in_array($editable_role['name'], $exclude) ){
62 $role = [
63 'name' => $key,
64 'label' => $editable_role['name']
65 ];
66 array_push($roles, $role);
67 }
68 }
69 return $roles;
70 }
71
72 /**
73 * Get a single role
74 * @since 3.0
75 */
76 public function getSingleRole($role = 'administrator')
77 {
78 global $wp_roles;
79 if ( isset($wp_roles->roles[$role]) ) return $wp_roles->roles[$role];
80 return false;
81 }
82
83 /**
84 * Get the capabilities for a role
85 */
86 public function getSingleRoleCapabilities($role = 'administrator')
87 {
88 global $wp_roles;
89 if ( isset($wp_roles->roles[$role]) ) :
90 $capabilities = $wp_roles->roles[$role]['capabilities'];
91 if ( $role == 'administrator' && class_exists('GFCommon') ) $capabilities['gform_full_access'] = true;
92 return apply_filters('nestedpages_capabilities', $capabilities, $role);
93 endif;
94 return false;
95 }
96
97 /**
98 * Can current user sort pages
99 * @return boolean
100 * @since 1.1.7
101 * @see NestedPages\Entities\User\UserCapabilities
102 */
103 public function canSortPosts($post_type = 'page')
104 {
105 $roles = $this->getRoles();
106 $user_can_sort = false;
107 $roles_cansort = get_option('nestedpages_allowsorting', []);
108 if ( $roles_cansort == "" ) $roles_cansort = [];
109
110 foreach($roles as $role){
111 if ( $role == 'administrator' ) return true;
112 if ( in_array($role, $roles_cansort) ) $user_can_sort = true; // Plugin Option
113 $role_obj = get_role($role);
114 if ( $role_obj->has_cap("nestedpages_sorting_$post_type") ) $user_can_sort = true; // Custom Capability
115 }
116
117 return $user_can_sort;
118 }
119
120 /**
121 * Can current user view the Nested Pages Sort View
122 * @return boolean
123 * @since 3.1.9
124 */
125 public function canViewSorting($post_type = 'page')
126 {
127 $roles = $this->getRoles();
128 $viewable_roles = $this->settings->sortViewEnabled();
129 $user_can_view = false;
130
131 foreach($roles as $role){
132 if ( $role == 'administrator' ) return true;
133 if ( in_array($role, $viewable_roles) ) $user_can_view = true; // Custom Capability
134 }
135
136 return $user_can_view;
137 }
138
139 /**
140 * Can the user publish to post type
141 */
142 public function canPublish($post_type = 'post')
143 {
144 if ( $post_type == 'page' ) {
145 return ( current_user_can('publish_pages') ) ? true : false;
146 }
147 if ( current_user_can('publish_posts') ) return true;
148 return false;
149 }
150
151 /**
152 * Can the user add new posts for review?
153 */
154 public function canSubmitPending($post_type = 'post')
155 {
156 if ( $post_type == 'page' ) {
157 return ( current_user_can('edit_pages') ) ? true : false;
158 }
159 if ( current_user_can('edit_posts') ) return true;
160 return false;
161 }
162
163 /**
164 * Get an array of all users/ids
165 * @since 1.3.0
166 * @return array
167 */
168 public function allUsers()
169 {
170 $users = get_users([
171 'fields' => ['ID', 'display_name']
172 ]);
173 return $users;
174 }
175
176 /**
177 * Get User's Visible Pages
178 * @since 1.3.4
179 * @return array - array of pages user has toggled visible
180 */
181 public function getVisiblePages()
182 {
183 return unserialize(get_user_meta(get_current_user_id(), 'np_visible_posts', true));
184 }
185
186 /**
187 * Update User's Visible Pages
188 */
189 public function updateVisiblePages($post_type, $ids)
190 {
191 $visible = $this->getVisiblePages();
192 if ( $this->integrations->plugins->wpml->installed ) $ids = $this->integrations->plugins->wpml->getAllTranslatedIds($ids);
193 $visible[$post_type] = $ids;
194 update_user_meta(
195 get_current_user_id(),
196 'np_visible_posts',
197 serialize($visible)
198 );
199 }
200
201 /**
202 * Update User's Status Preference (All/Published/Draft)
203 */
204 public function updateStatusPreference($post_type, $status)
205 {
206 $preference = get_user_meta(get_current_user_id(), 'np_status_preference', true);
207 if ( !$preference ) $preference = [];
208 $preference[$post_type] = sanitize_text_field($status);
209 update_user_meta(
210 get_current_user_id(),
211 'np_status_preference',
212 $preference
213 );
214 }
215
216 /**
217 * Get the current user's status preference for a post type
218 * (show all/published/draft)
219 */
220 public function getStatusPreference($post_type)
221 {
222 $preference = get_user_meta(get_current_user_id(), 'np_status_preference', true);
223 if ( !$preference || !is_array($preference) || !isset($preference[$post_type])) return 'all';
224 return $preference[$post_type];
225 }
226 }