UserRepository.php
142 lines
| 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 | * Can the user publish to post type |
| 95 | */ |
| 96 | public function canPublish($post_type = 'post') |
| 97 | { |
| 98 | if ( $post_type == 'page' ) { |
| 99 | return ( current_user_can('publish_pages') ) ? true : false; |
| 100 | } |
| 101 | if ( current_user_can('publish_posts') ) return true; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get an array of all users/ids |
| 107 | * @since 1.3.0 |
| 108 | * @return array |
| 109 | */ |
| 110 | public function allUsers() |
| 111 | { |
| 112 | $users = get_users([ |
| 113 | 'fields' => ['ID', 'display_name'] |
| 114 | ]); |
| 115 | return $users; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get User's Visible Pages |
| 120 | * @since 1.3.4 |
| 121 | * @return array - array of pages user has toggled visible |
| 122 | */ |
| 123 | public function getVisiblePages() |
| 124 | { |
| 125 | return unserialize(get_user_meta(get_current_user_id(), 'np_visible_posts', true)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Update User's Visible Pages |
| 130 | */ |
| 131 | public function updateVisiblePages($post_type, $ids) |
| 132 | { |
| 133 | $visible = $this->getVisiblePages(); |
| 134 | if ( $this->integrations->plugins->wpml->installed ) $ids = $this->integrations->plugins->wpml->getAllTranslatedIds($ids); |
| 135 | $visible[$post_type] = $ids; |
| 136 | update_user_meta( |
| 137 | get_current_user_id(), |
| 138 | 'np_visible_posts', |
| 139 | serialize($visible) |
| 140 | ); |
| 141 | } |
| 142 | } |