PluginProbe
UpStream: a Project Management Plugin for WordPress / 1.39.3
UpStream: a Project Management Plugin for WordPress v1.39.3
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / up-client-functions.php

up-client-functions.php in UpStream: a Project Management Plugin for WordPress 1.39.3, at includes/up-client-functions.php

185 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined('ABSPATH')) {
5 exit;
6 }
7
8
9 function upstream_get_client_id()
10 {
11 $client_id = (int)upstream_project_client_id();
12
13 if ($client_id === 0) {
14 $user_id = upstream_current_user_id();
15 $client_id = (int)upstream_get_users_client_id($user_id);
16 }
17
18 return $client_id > 0 ? $client_id : null;
19 }
20
21 function upstream_client_logo($client_id = 0)
22 {
23 $logoURL = "";
24
25 if ((int)$client_id === 0) {
26 $client_id = upstream_get_client_id();
27 }
28
29 if ($client_id > 0) {
30 global $wpdb, $table_prefix;
31
32 $logoURL = $wpdb->get_var(sprintf(
33 '
34 SELECT `meta_value`
35 FROM `%s`
36 WHERE `post_id` = "%s"
37 AND `meta_key` = "_upstream_client_logo"',
38 $table_prefix . 'postmeta',
39 $client_id
40 ));
41 }
42
43 return apply_filters('upstream_client_logo', $logoURL, $client_id);
44 }
45
46 /**
47 * Save post metadata when a post is saved.
48 * Mainly used to update user ids
49 *
50 * @param int $post_id The post ID.
51 * @param post $post The post object.
52 * @param bool $update Whether this is an existing post being updated or not.
53 */
54 function upstream_update_client_meta_values($post_id, $post, $update)
55 {
56 $slug = 'client';
57
58 // If this isn't a 'client' post, don't update it.
59 if ($slug != $post->post_type) {
60 return;
61 }
62
63 // update the overall progress of the project
64 if (isset($_POST['_upstream_client_users'])) :
65
66 $users = $_POST['_upstream_client_users']; // This is sanitized in the following lines
67
68 $i = 0;
69 if ($users && is_array($users)) :
70 foreach ($users as $user) {
71 if (!is_array($user) || ! isset($user['id']) || empty($user['id']) || sanitize_text_field($user['id']) == '') {
72 $users[$i]['id'] = upstream_admin_set_unique_id();
73 } else {
74 // already sanity checked is_array($users[$i]) && isset($users[$i]['id'])
75 $users[$i]['id'] = (int)$users[$i]['id'];
76 }
77 $i++;
78 }
79 else:
80 $users = [];
81 endif;
82
83 update_post_meta($post_id, '_upstream_client_users', $users);
84
85 endif;
86 }
87
88 add_action('save_post', 'upstream_update_client_meta_values', 99999, 3);
89
90 /**
91 * Retrieve all Client Users associated with a given client.
92 *
93 * @since 1.11.0
94 *
95 * @param int $client_id The reference id.
96 *
97 * @return array|bool Array in case of success or false in case the client_id is invalid.
98 */
99 function upstream_get_client_users($client_id)
100 {
101 $client_id = (int)$client_id;
102 if ($client_id <= 0) {
103 return false;
104 }
105
106 $usersList = [];
107 $clientUsersList = array_filter((array)get_post_meta($client_id, '_upstream_new_client_users', true));
108 if (count($clientUsersList) > 0) {
109 $usersIdsList = [];
110 foreach ($clientUsersList as $clientUser) {
111 if (isset($clientUser['user_id'])) {
112 $usersIdsList[] = (int)$clientUser['user_id'];
113 }
114 }
115
116 $rowset = (array)get_users([
117 'fields' => ['ID', 'display_name'],
118 'include' => $usersIdsList,
119 ]);
120
121 foreach ($rowset as $row) {
122 $usersList[(int)$row->ID] = [
123 'id' => (int)$row->ID,
124 'name' => $row->display_name,
125 ];
126 }
127 }
128
129 return $usersList;
130 }
131
132 /**
133 * Check if a given user is a Client User associated with a given client.
134 *
135 * @since 1.11.0
136 *
137 * @param int $client_user_id The client user id.
138 * @param int $client_id The client id.
139 *
140 * @return bool|null Bool or NULL in case the user is invalid.
141 */
142 function upstream_do_client_user_belongs_to_client($client_user_id, $client_id)
143 {
144 $client_user_id = (int)$client_user_id;
145 if ($client_user_id <= 0) {
146 return null;
147 }
148
149 $clientUsers = (array)upstream_get_client_users($client_id);
150
151 $clientUserBelongsToClient = isset($clientUsers[$client_user_id]);
152
153 return $clientUserBelongsToClient;
154 }
155
156 /**
157 * Retrieve all client user permissions.
158 *
159 * @since 1.11.0
160 *
161 * @param int $client_user_id The client user id.
162 *
163 * @return array|bool A permissions array or false if user doesn't exist.
164 */
165 function upstream_get_client_user_permissions($client_user_id)
166 {
167 $clientUser = new \WP_User((int)$client_user_id);
168 if ($clientUser->ID === 0) {
169 return false;
170 }
171
172 $permissions = upstream_get_client_users_permissions();
173 foreach ($permissions as $permissionIndex => $permission) {
174 if (isset($clientUser->caps[$permission['key']])) {
175 $permission['value'] = $clientUser->caps[$permission['key']];
176 } elseif (isset($clientUser->allcaps[$permission['key']])) {
177 $permission['value'] = $clientUser->allcaps[$permission['key']];
178 }
179
180 $permissions[$permissionIndex] = $permission;
181 }
182
183 return $permissions;
184 }
185