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