UserRepository.php
242 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 | * Can the user perform bulk actions? |
| 165 | */ |
| 166 | public function canPerformBulkActions($post_type_settings) |
| 167 | { |
| 168 | if ( !isset($post_type_settings->bulk_edit_roles) || !is_array($post_type_settings->bulk_edit_roles) ) return true; |
| 169 | if ( empty($post_type_settings->bulk_edit_roles) ) return true; |
| 170 | $allowed = false; |
| 171 | foreach ( $post_type_settings->bulk_edit_roles as $role ){ |
| 172 | if ( current_user_can($role) ) $allowed = true; |
| 173 | break; |
| 174 | } |
| 175 | return $allowed; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get an array of all users/ids |
| 180 | * @since 1.3.0 |
| 181 | * @return array |
| 182 | */ |
| 183 | public function allUsers() |
| 184 | { |
| 185 | $users = get_users([ |
| 186 | 'fields' => ['ID', 'display_name'] |
| 187 | ]); |
| 188 | return $users; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get User's Visible Pages |
| 193 | * @since 1.3.4 |
| 194 | * @return array - array of pages user has toggled visible |
| 195 | */ |
| 196 | public function getVisiblePages() |
| 197 | { |
| 198 | return unserialize(get_user_meta(get_current_user_id(), 'np_visible_posts', true)); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Update User's Visible Pages |
| 203 | */ |
| 204 | public function updateVisiblePages($post_type, $ids) |
| 205 | { |
| 206 | $visible = $this->getVisiblePages(); |
| 207 | if ( $this->integrations->plugins->wpml->installed ) $ids = $this->integrations->plugins->wpml->getAllTranslatedIds($ids); |
| 208 | $visible[$post_type] = $ids; |
| 209 | update_user_meta( |
| 210 | get_current_user_id(), |
| 211 | 'np_visible_posts', |
| 212 | serialize($visible) |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Update User's Status Preference (All/Published/Draft) |
| 218 | */ |
| 219 | public function updateStatusPreference($post_type, $status) |
| 220 | { |
| 221 | $preference = get_user_meta(get_current_user_id(), 'np_status_preference', true); |
| 222 | if ( !$preference ) $preference = []; |
| 223 | $preference[$post_type] = sanitize_text_field($status); |
| 224 | update_user_meta( |
| 225 | get_current_user_id(), |
| 226 | 'np_status_preference', |
| 227 | $preference |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get the current user's status preference for a post type |
| 233 | * (show all/published/draft) |
| 234 | */ |
| 235 | public function getStatusPreference($post_type) |
| 236 | { |
| 237 | $preference = get_user_meta(get_current_user_id(), 'np_status_preference', true); |
| 238 | if ( !$preference || !is_array($preference) || !isset($preference[$post_type])) return 'all'; |
| 239 | return $preference[$post_type]; |
| 240 | } |
| 241 | } |
| 242 |