PluginProbe
Nested Pages / 3.0.4
Nested Pages v3.0.4
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 All 106 releases
wp-nested-pages / app / Entities / User / UserRepository.php
UserRepository.php
130 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace NestedPages\Entities\User;
3
4 use NestedPages\Entities\PluginIntegration\IntegrationFactory;
5
6 /**
7 * User Repository
8 * @since 1.1.7
9 */
10 class UserRepository
11 {
12 /**
13 * Plugin Integrations
14 * @var object
15 */
16 private $integrations;
17
18 public function __construct()
19 {
20 $this->integrations = new IntegrationFactory;
21 }
22
23 /**
24 * Return Current User's Roles
25 * @return array
26 * @since 1.1.7
27 */
28 public function getRoles()
29 {
30 global $current_user;
31
32 // If current user is superadmin (WP Multisite) add administrator to the roles array
33 if (function_exists('is_multisite') && is_multisite() && is_super_admin()) {
34 $current_user->roles[] = 'administrator';
35 }
36
37 return $current_user->roles;
38 }
39
40 /**
41 * Get all roles that arent admin, contributor or subscriber
42 * @return array
43 * @since 1.1.7
44 */
45 public function allRoles($exclude = array('Administrator', 'Contributor', 'Subscriber', 'Author') )
46 {
47 global $wp_roles;
48 $all_roles = $wp_roles->roles;
49 $editable_roles = apply_filters('editable_roles', $all_roles);
50 $roles = [];
51 if ( !is_array($exclude) ) $exclude = [];
52 foreach($editable_roles as $key=>$editable_role){
53 if ( !in_array($editable_role['name'], $exclude) ){
54 $role = [
55 'name' => $key,
56 'label' => $editable_role['name']
57 ];
58 array_push($roles, $role);
59 }
60 }
61 return $roles;
62 }
63
64 /**
65 * Get a single role
66 * @since 3.0
67 */
68 public function getSingleRole($role = 'administrator')
69 {
70 global $wp_roles;
71 if ( isset($wp_roles->roles[$role]) ) return $wp_roles->roles[$role];
72 return false;
73 }
74
75 /**
76 * Can current user sort pages
77 * @return boolean
78 * @since 1.1.7
79 */
80 public function canSortPages()
81 {
82 $roles = $this->getRoles();
83 $cansort = get_option('nestedpages_allowsorting', []);
84 if ( $cansort == "" ) $cansort = [];
85
86 foreach($roles as $role){
87 if ( $role == 'administrator' ) return true;
88 if ( in_array($role, $cansort) ) return true;
89 }
90 return false;
91 }
92
93 /**
94 * Get an array of all users/ids
95 * @since 1.3.0
96 * @return array
97 */
98 public function allUsers()
99 {
100 $users = get_users([
101 'fields' => ['ID', 'display_name']
102 ]);
103 return $users;
104 }
105
106 /**
107 * Get User's Visible Pages
108 * @since 1.3.4
109 * @return array - array of pages user has toggled visible
110 */
111 public function getVisiblePages()
112 {
113 return unserialize(get_user_meta(get_current_user_id(), 'np_visible_posts', true));
114 }
115
116 /**
117 * Update User's Visible Pages
118 */
119 public function updateVisiblePages($post_type, $ids)
120 {
121 $visible = $this->getVisiblePages();
122 if ( $this->integrations->plugins->wpml->installed ) $ids = $this->integrations->plugins->wpml->getAllTranslatedIds($ids);
123 $visible[$post_type] = $ids;
124 update_user_meta(
125 get_current_user_id(),
126 'np_visible_posts',
127 serialize($visible)
128 );
129 }
130 }