wpdb = $wpdb; $this->table = $wpdb->prefix . Database::$tableProfilesPermissions; $this->profilesTable = $wpdb->prefix . Database::$tableProfiles; } public function add($profileId, $role, $btnSlug) { if ( empty(intval($profileId)) || ((null !== $role && '' === strval($role))) || '' === strval($btnSlug) ) { throw new LabelPrintingError('Add profile permission invalid data.'); } $exists = $this->wpdb->get_var( $this->wpdb->prepare( "SELECT COUNT(*) FROM {$this->table} WHERE `profile_id` = %d AND `role` = %s AND `button_slug` = %s", $profileId, $role, $btnSlug ) ); if (null === $exists) { throw new LabelPrintingError('Profile permission check error.'); } if ($exists == 0) { $insertResult = $this->wpdb->insert( $this->table, array( 'profile_id' => $profileId, 'role' => $role, 'button_slug' => $btnSlug, ), array('%d', '%s', '%s') ); if (false === $insertResult) { throw new LabelPrintingError('Profile permission insert error.'); } } } public function deleteProfilePermissions($profileId) { $result = $this->wpdb->delete($this->table, array('profile_id' => $profileId)); if (false === $result) { throw new LabelPrintingError('Delete profile permissions error.'); } return $result; } public function getAvailableProfilesIds($btnSlug, $roles = array()) { $availableProfiles = array(); if (empty($roles)) { $userData = get_userdata(get_current_user_id()); $roles = !empty($userData->roles) && is_array($userData->roles) ? $userData->roles : array(); } if (empty($roles)) { return array(); } foreach ($roles as $role) { $condition = " pp.`id` IS NULL OR ( (pp.`role` = %s OR pp.`role` IS NULL) AND pp.`button_slug` = %s ) "; $args = array($role, $btnSlug); if ( UserSettingsHelper::getOption("showOnlyOwnedByUserProfiles", "0") && !current_user_can('manage_options') ) { $condition .= " AND p.author_id = %s "; $args[] = get_current_user_id(); } $roleProfiles = $this->wpdb->get_results( $this->wpdb->prepare(" SELECT p.*, pp.`role`, pp.`button_slug` FROM {$this->profilesTable} AS p LEFT JOIN {$this->table} AS pp ON pp.`profile_id` = p.`id` WHERE {$condition} ", $args), ARRAY_A ); $availableProfiles = array_merge($availableProfiles, $roleProfiles); } return array_unique(wp_list_pluck($availableProfiles, 'id')); } public function getAll() { $permissions = $this->wpdb->get_results( "SELECT * FROM {$this->table}", ARRAY_A ); if ($this->wpdb->last_error) { throw new LabelPrintingError('Get profiles permissions error.'); } return $permissions; } public function getByProfileId($profileId) { $profilePermissions = $this->wpdb->get_results( $this->wpdb->prepare("SELECT * FROM {$this->table} WHERE profile_id = %s", $profileId), ARRAY_A ); if ($this->wpdb->last_error) { throw new LabelPrintingError('Get profiles permissions error.'); } return $profilePermissions; } public function getWpRolesList($addDefault = true) { global $wp_roles; if ( ! isset( $wp_roles ) ) { $wp_roles = new \WP_Roles(); } $roles = wp_list_pluck($wp_roles->roles, 'name'); if ($addDefault) { $roles = array_merge(array('__default' => 'Default'), $roles); } return $roles; } public function getProfilesPermissionsGrouped() { $profilePermissions = array(); foreach ($this->getAll() as $permission) { if ($permission['role'] === null) { $permission['role'] = '__default'; } if (!isset($profilePermissions[$permission['role']])) { $profilePermissions[$permission['role']] = array(); } if (!isset($profilePermissions[$permission['role']] [$permission['button_slug']])) { $profilePermissions[$permission['role']] [$permission['button_slug']] = array(); } $profilePermissions[$permission['role']] [$permission['button_slug']] [] = $permission['profile_id']; } return $profilePermissions; } public function getCheckboxValue($roleSlug, $getSlug, $profileId) { $profilePermissionsGrouped = $this->getProfilesPermissionsGrouped(); return isset($profilePermissionsGrouped[$roleSlug][$getSlug]) && in_array($profileId, $profilePermissionsGrouped[$roleSlug][$getSlug]) ? '1' : '0'; } public function saveGrouped($profilePermissionsGrouped) { $regrouped = array(); foreach ($profilePermissionsGrouped as $role => $buttonsSlugs) { foreach ($buttonsSlugs as $btnSlug => $profileIds) { foreach ($profileIds as $profileId => $checked) { if (!isset($regrouped[$profileId])) { $regrouped[$profileId] = array(); } if (!isset($regrouped[$profileId][$role])) { $regrouped[$profileId][$role] = array(); } if ($checked === '1') { $regrouped[$profileId][$role][] = $btnSlug; } } } } $this->clearAll(); foreach ($regrouped as $profileId => $roleButtons) { foreach ($roleButtons as $role => $buttonSlugs) { foreach ($buttonSlugs as $buttonSlug) { if ('__default' === $role) { $role = null; } $this->add($profileId, $role, $buttonSlug); } } } } public function clearAll() { $this->wpdb->query("DELETE FROM {$this->table}"); } }