Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 481 |
|
2.13% |
1 / 47 |
CRAP | n/a |
0 / 0 |
|
| zeroBSCRM_clearUserRoles | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBSCRM_addUserRoles | |
0.00% |
0 / 227 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsIsZBSUser | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| zeroBSCRM_permsIsZBSBackendUser | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| zeroBSCRM_permsIsZBSUserOrAdmin | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| zeroBSCRM_isZBSAdmin | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_isWPAdmin | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_isZBSAdminOrAdmin | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| zeroBSCRM_wooCommerceRemoveBlock | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_getWordPressRoles | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBSCRM_getCurrentUserCaps | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsObjType | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
110 | |||
| jpcrm_can_user_manage_contacts | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBSCRM_permsCustomers | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| zeroBSCRM_permsSendEmailContacts | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsViewCustomers | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsCustomersTags | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsQuotes | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsViewQuotes | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsInvoices | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsViewInvoices | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsTransactions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsViewTransactions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsMailCampaigns | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsForms | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_perms_tasks | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsNotify | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsExport | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsLogsAddEdit | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsLogsDelete | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsClient | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBSCRM_permsWPEditPosts | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| zeroBS_getPossibleCustomerOwners | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBS_getPossibleCompanyOwners | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBS_getPossibleQuoteOwners | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBS_getPossibleInvoiceOwners | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBS_getPossibleTransactionOwners | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBS_getPossibleTaskOwners | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| zeroBSCRM_isRole | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| jpcrm_role_check | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
182 | |||
| zeroBS_getPossibleOwners | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
| jpcrm_can_wp_user_view_object | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
342 | |||
| jpcrm_can_current_wp_user_view_object | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| jpcrm_can_access_portal_via_hash | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| jpcrm_get_easy_access_security_request_name_by_obj_type | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| jpcrm_perms_error | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| jpcrm_is_allowed_path | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | /* |
| 3 | * Jetpack CRM |
| 4 | * https://jetpackcrm.com |
| 5 | * V1.20 |
| 6 | * |
| 7 | * Copyright 2020 Automattic |
| 8 | * |
| 9 | * Date: 01/11/16 |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ZEROBSCRM_PATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Remove user roles |
| 18 | */ |
| 19 | function zeroBSCRM_clearUserRoles() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
| 20 | remove_role( 'zerobs_admin' ); |
| 21 | remove_role( 'zerobs_customermgr' ); |
| 22 | remove_role( 'zerobs_quotemgr' ); |
| 23 | remove_role( 'zerobs_invoicemgr' ); |
| 24 | remove_role( 'zerobs_transactionmgr' ); |
| 25 | remove_role( 'zerobs_customer' ); |
| 26 | remove_role( 'zerobs_mailmgr' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Build User Roles |
| 31 | */ |
| 32 | function zeroBSCRM_addUserRoles() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
| 33 | |
| 34 | // Jetpack CRM Admin |
| 35 | add_role( |
| 36 | 'zerobs_admin', |
| 37 | __( 'Jetpack CRM Admin (Full CRM Permissions)', 'zero-bs-crm' ), |
| 38 | array( |
| 39 | 'read' => true, // true allows this capability |
| 40 | 'edit_posts' => false, // Allows user to edit their own posts |
| 41 | 'edit_pages' => false, // Allows user to edit pages |
| 42 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 43 | 'create_posts' => false, // Allows user to create new posts |
| 44 | 'manage_categories' => false, // Allows user to manage post categories |
| 45 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 46 | ) |
| 47 | ); |
| 48 | |
| 49 | // gets the role from WP |
| 50 | $role = get_role( 'zerobs_admin' ); |
| 51 | |
| 52 | $role->add_cap( 'read' ); |
| 53 | $role->remove_cap( 'edit_posts' ); |
| 54 | $role->add_cap( 'upload_files' ); // added 21/5/18 to ensure can upload media |
| 55 | $role->add_cap( 'admin_zerobs_usr' ); #} For all zerobs users :) |
| 56 | // NOTE. Adding this adds a random "Post categories / not posts" to menu |
| 57 | // will have to remove programattically :( |
| 58 | $role->add_cap( 'manage_categories' ); |
| 59 | $role->add_cap( 'manage_sales_dash' ); #mike added |
| 60 | $role->add_cap( 'admin_zerobs_mailcampaigns' ); |
| 61 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 62 | |
| 63 | // give permission to edit settings |
| 64 | $role->add_cap( 'admin_zerobs_manage_options' ); |
| 65 | |
| 66 | // CRM object view capabilities |
| 67 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 68 | $role->add_cap( 'admin_zerobs_view_quotes' ); |
| 69 | $role->add_cap( 'admin_zerobs_view_invoices' ); |
| 70 | $role->add_cap( 'admin_zerobs_view_events' ); |
| 71 | $role->add_cap( 'admin_zerobs_view_transactions' ); |
| 72 | |
| 73 | // CRM object edit capabilities |
| 74 | $role->add_cap( 'admin_zerobs_customers' ); |
| 75 | $role->add_cap( 'admin_zerobs_customers_tags' ); |
| 76 | $role->add_cap( 'admin_zerobs_quotes' ); |
| 77 | $role->add_cap( 'admin_zerobs_events' ); |
| 78 | $role->add_cap( 'admin_zerobs_invoices' ); |
| 79 | $role->add_cap( 'admin_zerobs_transactions' ); |
| 80 | $role->add_cap( 'admin_zerobs_forms' ); |
| 81 | |
| 82 | // logs |
| 83 | $role->add_cap( 'admin_zerobs_logs_addedit' ); |
| 84 | $role->add_cap( 'admin_zerobs_logs_delete' ); |
| 85 | |
| 86 | // emails |
| 87 | $role->add_cap( 'admin_zerobs_sendemails_contacts' ); |
| 88 | |
| 89 | // paranoia |
| 90 | unset( $role ); |
| 91 | |
| 92 | // give WP admins extra capabilities |
| 93 | $role = get_role( 'administrator' ); |
| 94 | |
| 95 | // this is for users who've removed 'administrator' role type |
| 96 | // WH temp catch anyhow, for Nimitz. |
| 97 | if ( $role !== null ) { |
| 98 | |
| 99 | // Caps |
| 100 | $role->add_cap( 'manage_sales_dash' ); |
| 101 | $role->add_cap( 'admin_zerobs_mailcampaigns' ); |
| 102 | $role->add_cap( 'admin_zerobs_forms' ); |
| 103 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 104 | |
| 105 | // give permission to edit settings |
| 106 | $role->add_cap( 'admin_zerobs_manage_options' ); |
| 107 | |
| 108 | // CRM object view capabilities |
| 109 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 110 | $role->add_cap( 'admin_zerobs_view_quotes' ); |
| 111 | $role->add_cap( 'admin_zerobs_view_invoices' ); |
| 112 | $role->add_cap( 'admin_zerobs_view_events' ); |
| 113 | $role->add_cap( 'admin_zerobs_view_transactions' ); |
| 114 | |
| 115 | // CRM object edit capabilities |
| 116 | $role->add_cap( 'admin_zerobs_customers' ); |
| 117 | $role->add_cap( 'admin_zerobs_customers_tags' ); |
| 118 | $role->add_cap( 'admin_zerobs_quotes' ); |
| 119 | $role->add_cap( 'admin_zerobs_invoices' ); |
| 120 | $role->add_cap( 'admin_zerobs_events' ); |
| 121 | $role->add_cap( 'admin_zerobs_transactions' ); |
| 122 | |
| 123 | // needed for notifications |
| 124 | $role->add_cap( 'admin_zerobs_notifications' ); |
| 125 | |
| 126 | // logs |
| 127 | $role->add_cap( 'admin_zerobs_logs_addedit' ); |
| 128 | $role->add_cap( 'admin_zerobs_logs_delete' ); |
| 129 | |
| 130 | // all users |
| 131 | $role->add_cap( 'admin_zerobs_usr' ); |
| 132 | |
| 133 | // emails |
| 134 | $role->add_cap( 'admin_zerobs_sendemails_contacts' ); |
| 135 | |
| 136 | // paranoia |
| 137 | unset( $role ); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | // CRM Contact Manager |
| 142 | add_role( |
| 143 | 'zerobs_customermgr', |
| 144 | __( 'Jetpack CRM Contact Manager', 'zero-bs-crm' ), |
| 145 | array( |
| 146 | 'read' => true, // true allows this capability |
| 147 | 'edit_posts' => false, // Allows user to edit their own posts |
| 148 | 'edit_pages' => false, // Allows user to edit pages |
| 149 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 150 | 'create_posts' => false, // Allows user to create new posts |
| 151 | 'manage_categories' => false, // Allows user to manage post categories |
| 152 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 153 | ) |
| 154 | ); |
| 155 | |
| 156 | // gets the role from WP |
| 157 | $role = get_role( 'zerobs_customermgr' ); |
| 158 | |
| 159 | // caps |
| 160 | $role->add_cap( 'read' ); |
| 161 | $role->remove_cap( 'edit_posts' ); |
| 162 | $role->add_cap( 'upload_files' ); // added 21/5/18 to ensure can upload media |
| 163 | $role->add_cap( 'admin_zerobs_usr' ); #} For all zerobs users :) |
| 164 | $role->add_cap( 'manage_categories' ); |
| 165 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 166 | |
| 167 | // CRM object view capabilities |
| 168 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 169 | $role->add_cap( 'admin_zerobs_view_quotes' ); |
| 170 | $role->add_cap( 'admin_zerobs_view_invoices' ); |
| 171 | $role->add_cap( 'admin_zerobs_view_events' ); |
| 172 | $role->add_cap( 'admin_zerobs_view_transactions' ); |
| 173 | |
| 174 | // CRM object edit capabilities |
| 175 | $role->add_cap( 'admin_zerobs_customers' ); |
| 176 | $role->add_cap( 'admin_zerobs_customers_tags' ); |
| 177 | $role->add_cap( 'admin_zerobs_quotes' ); |
| 178 | $role->add_cap( 'admin_zerobs_events' ); |
| 179 | $role->add_cap( 'admin_zerobs_invoices' ); |
| 180 | $role->add_cap( 'admin_zerobs_transactions' ); |
| 181 | |
| 182 | // needed for notifications |
| 183 | $role->add_cap( 'admin_zerobs_notifications' ); |
| 184 | |
| 185 | // logs |
| 186 | $role->add_cap( 'admin_zerobs_logs_addedit' ); |
| 187 | |
| 188 | // emails |
| 189 | $role->add_cap( 'admin_zerobs_sendemails_contacts' ); |
| 190 | |
| 191 | unset( $role ); |
| 192 | |
| 193 | // CRM Quote Manager |
| 194 | add_role( |
| 195 | 'zerobs_quotemgr', |
| 196 | __( 'Jetpack CRM Quote Manager', 'zero-bs-crm' ), |
| 197 | array( |
| 198 | 'read' => true, // true allows this capability |
| 199 | 'edit_posts' => false, // Allows user to edit their own posts |
| 200 | 'edit_pages' => false, // Allows user to edit pages |
| 201 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 202 | 'create_posts' => false, // Allows user to create new posts |
| 203 | 'manage_categories' => false, // Allows user to manage post categories |
| 204 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 205 | ) |
| 206 | ); |
| 207 | |
| 208 | // gets the role from WP |
| 209 | $role = get_role( 'zerobs_quotemgr' ); |
| 210 | |
| 211 | // caps |
| 212 | $role->add_cap( 'read' ); |
| 213 | $role->remove_cap( 'edit_posts' ); |
| 214 | $role->add_cap( 'upload_files' ); // added 21/5/18 to ensure can upload media |
| 215 | $role->add_cap( 'admin_zerobs_usr' ); #} For all zerobs users :) |
| 216 | $role->add_cap( 'manage_categories' ); |
| 217 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 218 | |
| 219 | // CRM object view capabilities |
| 220 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 221 | $role->add_cap( 'admin_zerobs_view_quotes' ); |
| 222 | |
| 223 | // CRM object edit capabilities |
| 224 | $role->add_cap( 'admin_zerobs_customers' ); |
| 225 | $role->add_cap( 'admin_zerobs_quotes' ); |
| 226 | |
| 227 | // needed for notifications |
| 228 | $role->add_cap( 'admin_zerobs_notifications' ); |
| 229 | |
| 230 | // logs |
| 231 | $role->add_cap( 'admin_zerobs_logs_addedit' ); |
| 232 | |
| 233 | // paranoia |
| 234 | unset( $role ); |
| 235 | |
| 236 | // CRM Invoice Manager |
| 237 | add_role( |
| 238 | 'zerobs_invoicemgr', |
| 239 | __( 'Jetpack CRM Invoice Manager', 'zero-bs-crm' ), |
| 240 | array( |
| 241 | 'read' => true, // true allows this capability |
| 242 | 'edit_posts' => false, // Allows user to edit their own posts |
| 243 | 'edit_pages' => false, // Allows user to edit pages |
| 244 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 245 | 'create_posts' => false, // Allows user to create new posts |
| 246 | 'manage_categories' => false, // Allows user to manage post categories |
| 247 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 248 | ) |
| 249 | ); |
| 250 | |
| 251 | // gets the role from WP |
| 252 | $role = get_role( 'zerobs_invoicemgr' ); |
| 253 | |
| 254 | // caps |
| 255 | $role->add_cap( 'read' ); |
| 256 | $role->remove_cap( 'edit_posts' ); |
| 257 | $role->add_cap( 'upload_files' ); // added 21/5/18 to ensure can upload media |
| 258 | $role->add_cap( 'admin_zerobs_usr' ); #} For all zerobs users :) |
| 259 | $role->add_cap( 'manage_categories' ); |
| 260 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 261 | |
| 262 | // CRM object view capabilities |
| 263 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 264 | $role->add_cap( 'admin_zerobs_view_invoices' ); |
| 265 | $role->add_cap( 'admin_zerobs_view_transactions' ); |
| 266 | |
| 267 | // CRM object edit capabilities |
| 268 | $role->add_cap( 'admin_zerobs_customers' ); |
| 269 | $role->add_cap( 'admin_zerobs_invoices' ); |
| 270 | $role->add_cap( 'admin_zerobs_transactions' ); |
| 271 | |
| 272 | // needed for notifications |
| 273 | $role->add_cap( 'admin_zerobs_notifications' ); |
| 274 | |
| 275 | // logs |
| 276 | $role->add_cap( 'admin_zerobs_logs_addedit' ); |
| 277 | |
| 278 | // paranoia |
| 279 | unset( $role ); |
| 280 | |
| 281 | // CRM Transaction Manager |
| 282 | add_role( |
| 283 | 'zerobs_transactionmgr', |
| 284 | __( 'Jetpack CRM Transaction Manager', 'zero-bs-crm' ), |
| 285 | array( |
| 286 | 'read' => false, // true allows this capability |
| 287 | 'edit_posts' => false, // Allows user to edit their own posts |
| 288 | 'edit_pages' => false, // Allows user to edit pages |
| 289 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 290 | 'create_posts' => false, // Allows user to create new posts |
| 291 | 'manage_categories' => false, // Allows user to manage post categories |
| 292 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 293 | ) |
| 294 | ); |
| 295 | |
| 296 | // gets the role from WP |
| 297 | $role = get_role( 'zerobs_transactionmgr' ); |
| 298 | |
| 299 | // caps |
| 300 | $role->add_cap( 'read' ); |
| 301 | $role->remove_cap( 'edit_posts' ); |
| 302 | $role->add_cap( 'upload_files' ); // added 21/5/18 to ensure can upload media |
| 303 | $role->add_cap( 'admin_zerobs_usr' ); #} For all zerobs users :) |
| 304 | $role->add_cap( 'manage_categories' ); |
| 305 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 306 | |
| 307 | // CRM object view capabilities |
| 308 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 309 | $role->add_cap( 'admin_zerobs_view_transactions' ); |
| 310 | |
| 311 | // CRM object edit capabilities |
| 312 | $role->add_cap( 'admin_zerobs_customers' ); |
| 313 | $role->add_cap( 'admin_zerobs_transactions' ); |
| 314 | |
| 315 | // needed for notifications |
| 316 | $role->add_cap( 'admin_zerobs_notifications' ); |
| 317 | |
| 318 | // logs |
| 319 | $role->add_cap( 'admin_zerobs_logs_addedit' ); |
| 320 | |
| 321 | // paranoia |
| 322 | unset( $role ); |
| 323 | |
| 324 | // CRM Customer |
| 325 | add_role( |
| 326 | 'zerobs_customer', |
| 327 | __( 'Jetpack CRM Contact', 'zero-bs-crm' ), |
| 328 | array( |
| 329 | 'read' => true, // true allows this capability |
| 330 | 'edit_posts' => false, // Allows user to edit their own posts |
| 331 | 'edit_pages' => false, // Allows user to edit pages |
| 332 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 333 | 'create_posts' => false, // Allows user to create new posts |
| 334 | 'manage_categories' => false, // Allows user to manage post categories |
| 335 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 336 | ) |
| 337 | ); |
| 338 | |
| 339 | // paranoia |
| 340 | unset( $role ); |
| 341 | |
| 342 | // CRM Mail Manager - Manages campaigns, customers / companies |
| 343 | add_role( |
| 344 | 'zerobs_mailmgr', |
| 345 | __( 'Jetpack CRM Mail Manager', 'zero-bs-crm' ), |
| 346 | array( |
| 347 | 'read' => false, // true allows this capability |
| 348 | 'edit_posts' => false, // Allows user to edit their own posts |
| 349 | 'edit_pages' => false, // Allows user to edit pages |
| 350 | 'edit_others_posts' => false, // Allows user to edit others posts not just their own |
| 351 | 'create_posts' => false, // Allows user to create new posts |
| 352 | 'manage_categories' => false, // Allows user to manage post categories |
| 353 | 'publish_posts' => false, // Allows the user to publish, otherwise posts stays in draft mode |
| 354 | ) |
| 355 | ); |
| 356 | |
| 357 | // gets the role from WP |
| 358 | $role = get_role( 'zerobs_mailmgr' ); |
| 359 | |
| 360 | // caps |
| 361 | $role->add_cap( 'read' ); |
| 362 | $role->remove_cap( 'edit_posts' ); |
| 363 | $role->add_cap( 'upload_files' ); // added 21/5/18 to ensure can upload media |
| 364 | $role->add_cap( 'admin_zerobs_usr' ); #} For all zerobs users :) |
| 365 | $role->add_cap( 'admin_zerobs_mailcampaigns' ); |
| 366 | $role->add_cap( 'manage_categories' ); |
| 367 | $role->add_cap( 'zbs_dash' ); # WH added 1.2 - has rights to view ZBS Dash |
| 368 | |
| 369 | // CRM object view capabilities |
| 370 | $role->add_cap( 'admin_zerobs_view_customers' ); |
| 371 | $role->add_cap( 'admin_zerobs_view_quotes' ); |
| 372 | $role->add_cap( 'admin_zerobs_view_invoices' ); |
| 373 | $role->add_cap( 'admin_zerobs_view_events' ); |
| 374 | $role->add_cap( 'admin_zerobs_view_transactions' ); |
| 375 | |
| 376 | // CRM object edit capabilities |
| 377 | $role->add_cap( 'admin_zerobs_customers' ); |
| 378 | $role->add_cap( 'admin_zerobs_customers_tags' ); |
| 379 | $role->add_cap( 'admin_zerobs_events' ); |
| 380 | |
| 381 | // needed for notifications |
| 382 | $role->add_cap( 'admin_zerobs_notifications' ); |
| 383 | |
| 384 | // emails |
| 385 | $role->add_cap( 'admin_zerobs_sendemails_contacts' ); |
| 386 | |
| 387 | unset( $role ); |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | ====================================================== |
| 392 | / Add + Remove Roles |
| 393 | ====================================================== */ |
| 394 | |
| 395 | /* |
| 396 | ====================================================== |
| 397 | Role Helpers |
| 398 | ====================================================== */ |
| 399 | |
| 400 | // note this returns true if is any ZBS role, INCLUDING zbs customer |
| 401 | // if need just 'backend' user, use zeroBSCRM_permsIsZBSBackendUser |
| 402 | function zeroBSCRM_permsIsZBSUser() { |
| 403 | |
| 404 | #} Set a global var for this load, (sometimes multi-called) |
| 405 | global $zeroBSCRM_isZBSUser; |
| 406 | |
| 407 | if ( isset( $zeroBSCRM_isZBSUser ) ) { |
| 408 | return $zeroBSCRM_isZBSUser; |
| 409 | } |
| 410 | |
| 411 | #} ... else |
| 412 | $zeroBSCRM_isZBSUser = false; |
| 413 | $cu = wp_get_current_user(); |
| 414 | if ( $cu->has_cap( 'admin_zerobs_usr' ) ) { |
| 415 | $zeroBSCRM_isZBSUser = true; |
| 416 | } |
| 417 | if ( $cu->has_cap( 'zerobs_customer' ) ) { |
| 418 | $zeroBSCRM_isZBSUser = true; |
| 419 | } |
| 420 | |
| 421 | return $zeroBSCRM_isZBSUser; |
| 422 | } |
| 423 | // note this returns true if is any wp-admin based zbs user |
| 424 | // if want zbs customer roles too, use zeroBSCRM_permsIsZBSUser |
| 425 | function zeroBSCRM_permsIsZBSBackendUser() { |
| 426 | |
| 427 | #} Set a global var for this load, (sometimes multi-called) |
| 428 | global $zeroBSCRM_isZBSBackendUser; |
| 429 | |
| 430 | if ( isset( $zeroBSCRM_isZBSBackendUser ) ) { |
| 431 | return $zeroBSCRM_isZBSBackendUser; |
| 432 | } |
| 433 | |
| 434 | #} ... else |
| 435 | $zeroBSCRM_isZBSBackendUser = false; |
| 436 | $cu = wp_get_current_user(); |
| 437 | if ( $cu->has_cap( 'admin_zerobs_usr' ) ) { |
| 438 | $zeroBSCRM_isZBSBackendUser = true; |
| 439 | } |
| 440 | |
| 441 | return $zeroBSCRM_isZBSBackendUser; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Checks whether current user (or specified WP ID) is backend user, or wp admin |
| 446 | * |
| 447 | * @param $wordpress_users_id - bool|int; if passed, will check this WordPress user ID rather than current user |
| 448 | */ |
| 449 | function zeroBSCRM_permsIsZBSUserOrAdmin( $wordpress_user_id = false ) { |
| 450 | |
| 451 | // param passed? |
| 452 | if ( $wordpress_user_id == false ) { |
| 453 | |
| 454 | // if using current WordPress user: |
| 455 | |
| 456 | // Maintain a global var for this load, (sometimes called multiple times) |
| 457 | // (Only for current user checks) |
| 458 | global $zeroBSCRM_isZBSBackendUser; |
| 459 | |
| 460 | // check if already checked |
| 461 | if ( isset( $zeroBSCRM_isZBSBackendUser ) ) { |
| 462 | return $zeroBSCRM_isZBSBackendUser; |
| 463 | } |
| 464 | |
| 465 | // else check: |
| 466 | $zeroBSCRM_isZBSBackendUser = false; |
| 467 | $user = wp_get_current_user(); |
| 468 | |
| 469 | } else { |
| 470 | |
| 471 | // using passed user id |
| 472 | |
| 473 | $user = get_userdata( $wordpress_user_id ); |
| 474 | |
| 475 | } |
| 476 | |
| 477 | // user isn't logged in, or the passed user ID no longer exists or is an invalid value |
| 478 | if ( ! $user ) { |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // crm user check |
| 483 | if ( $user->has_cap( 'admin_zerobs_usr' ) ) { |
| 484 | $zeroBSCRM_isZBSBackendUser = true; |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | // admin check |
| 489 | if ( $user->has_cap( 'manage_options' ) ) { |
| 490 | return true; |
| 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | function zeroBSCRM_isZBSAdmin() { |
| 497 | |
| 498 | $cu = wp_get_current_user(); |
| 499 | // https://wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role |
| 500 | if ( in_array( 'zerobs_admin', (array) $cu->roles ) ) { |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | function zeroBSCRM_isWPAdmin() { |
| 508 | |
| 509 | $cu = wp_get_current_user(); |
| 510 | |
| 511 | #} adm |
| 512 | if ( $cu->has_cap( 'manage_options' ) ) { |
| 513 | return true; |
| 514 | } |
| 515 | |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | function zeroBSCRM_isZBSAdminOrAdmin() { |
| 520 | |
| 521 | $cu = wp_get_current_user(); |
| 522 | // https://wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role |
| 523 | if ( in_array( 'zerobs_admin', (array) $cu->roles ) ) { |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | #} or adm |
| 528 | if ( $cu->has_cap( 'manage_options' ) ) { |
| 529 | return true; |
| 530 | } |
| 531 | |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | function zeroBSCRM_wooCommerceRemoveBlock() { |
| 536 | #} Add Filter for WooCommerce |
| 537 | $cu = wp_get_current_user(); |
| 538 | if ( $cu->has_cap( 'admin_zerobs_usr' ) ) { |
| 539 | add_filter( 'woocommerce_prevent_admin_access', '__return_false' ); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | function zeroBSCRM_getWordPressRoles() { |
| 544 | |
| 545 | global $wp_roles; |
| 546 | |
| 547 | $all_roles = $wp_roles->roles; |
| 548 | |
| 549 | return $all_roles; |
| 550 | } |
| 551 | |
| 552 | // return current user capabilities |
| 553 | function zeroBSCRM_getCurrentUserCaps() { |
| 554 | |
| 555 | $data = get_userdata( get_current_user_id() ); |
| 556 | |
| 557 | if ( is_object( $data ) ) { |
| 558 | return array_keys( $data->allcaps ); |
| 559 | } |
| 560 | |
| 561 | return array(); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Determine if the current user is allowed to manage contacts. |
| 566 | * |
| 567 | * @param int $obj_type_id Object type ID. |
| 568 | * |
| 569 | * @return bool |
| 570 | */ |
| 571 | function zeroBSCRM_permsObjType( $obj_type_id = -1 ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
| 572 | |
| 573 | switch ( $obj_type_id ) { |
| 574 | case ZBS_TYPE_CONTACT: |
| 575 | case ZBS_TYPE_COMPANY: |
| 576 | case ZBS_TYPE_SEGMENT: |
| 577 | return zeroBSCRM_permsCustomers(); |
| 578 | |
| 579 | case ZBS_TYPE_QUOTE: |
| 580 | case ZBS_TYPE_QUOTETEMPLATE: |
| 581 | return zeroBSCRM_permsQuotes(); |
| 582 | |
| 583 | case ZBS_TYPE_INVOICE: |
| 584 | return zeroBSCRM_permsInvoices(); |
| 585 | |
| 586 | case ZBS_TYPE_TRANSACTION: |
| 587 | return zeroBSCRM_permsTransactions(); |
| 588 | |
| 589 | case ZBS_TYPE_FORM: |
| 590 | return zeroBSCRM_permsForms(); |
| 591 | |
| 592 | case ZBS_TYPE_TASK: |
| 593 | return zeroBSCRM_perms_tasks(); |
| 594 | |
| 595 | } |
| 596 | |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Determine if a user is allowed to manage contacts. |
| 602 | * |
| 603 | * @since 6.1.0 |
| 604 | * |
| 605 | * @param WP_User $user The WP User to check permission access for. |
| 606 | * @param int $contact_id (Optional) The ID of the CRM contact. |
| 607 | * @return bool Returns a bool representing a user permission state. |
| 608 | */ |
| 609 | function jpcrm_can_user_manage_contacts( WP_User $user, $contact_id = null ) { |
| 610 | /** |
| 611 | * Allow third party plugins to modify the permission conditions for contacts. |
| 612 | * |
| 613 | * @since 6.1.0 |
| 614 | * |
| 615 | * @param boolean $allowed A boolean that represents the permission state. |
| 616 | * @param WP_User $user The WP User to check permission access for. |
| 617 | * @param int|null $contact_id (Optional) The ID of the CRM contact. |
| 618 | */ |
| 619 | return (bool) apply_filters( |
| 620 | 'jpcrm_can_user_manage_contacts', |
| 621 | $user->has_cap( 'admin_zerobs_customers' ), |
| 622 | $user, |
| 623 | $contact_id |
| 624 | ); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Determine if the current user is allowed to manage contacts. |
| 629 | * |
| 630 | * @deprecated 6.1.0 Use jpcrm_can_user_manage_contacts() |
| 631 | * |
| 632 | * @return bool |
| 633 | * |
| 634 | * @phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 635 | */ |
| 636 | function zeroBSCRM_permsCustomers() { |
| 637 | $current_user = wp_get_current_user(); |
| 638 | |
| 639 | if ( ! $current_user instanceof WP_User ) { |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | return jpcrm_can_user_manage_contacts( $current_user ) === true; |
| 644 | } |
| 645 | |
| 646 | function zeroBSCRM_permsSendEmailContacts() { |
| 647 | |
| 648 | $cu = wp_get_current_user(); |
| 649 | if ( $cu->has_cap( 'admin_zerobs_sendemails_contacts' ) ) { |
| 650 | return true; |
| 651 | } |
| 652 | return false; |
| 653 | } |
| 654 | |
| 655 | function zeroBSCRM_permsViewCustomers() { |
| 656 | |
| 657 | $cu = wp_get_current_user(); |
| 658 | if ( $cu->has_cap( 'admin_zerobs_view_customers' ) ) { |
| 659 | return true; |
| 660 | } |
| 661 | return false; |
| 662 | } |
| 663 | function zeroBSCRM_permsCustomersTags() { |
| 664 | |
| 665 | $cu = wp_get_current_user(); |
| 666 | if ( $cu->has_cap( 'admin_zerobs_customers_tags' ) ) { |
| 667 | return true; |
| 668 | } |
| 669 | return false; |
| 670 | } |
| 671 | function zeroBSCRM_permsQuotes() { |
| 672 | |
| 673 | $cu = wp_get_current_user(); |
| 674 | if ( $cu->has_cap( 'admin_zerobs_quotes' ) ) { |
| 675 | return true; |
| 676 | } |
| 677 | return false; |
| 678 | } |
| 679 | function zeroBSCRM_permsViewQuotes() { |
| 680 | |
| 681 | $cu = wp_get_current_user(); |
| 682 | if ( $cu->has_cap( 'admin_zerobs_view_quotes' ) ) { |
| 683 | return true; |
| 684 | } |
| 685 | return false; |
| 686 | } |
| 687 | function zeroBSCRM_permsInvoices() { |
| 688 | |
| 689 | $cu = wp_get_current_user(); |
| 690 | if ( $cu->has_cap( 'admin_zerobs_invoices' ) ) { |
| 691 | return true; |
| 692 | } |
| 693 | return false; |
| 694 | } |
| 695 | function zeroBSCRM_permsViewInvoices() { |
| 696 | |
| 697 | $cu = wp_get_current_user(); |
| 698 | if ( $cu->has_cap( 'admin_zerobs_view_invoices' ) ) { |
| 699 | return true; |
| 700 | } |
| 701 | return false; |
| 702 | } |
| 703 | function zeroBSCRM_permsTransactions() { |
| 704 | |
| 705 | $cu = wp_get_current_user(); |
| 706 | if ( $cu->has_cap( 'admin_zerobs_transactions' ) ) { |
| 707 | return true; |
| 708 | } |
| 709 | return false; |
| 710 | } |
| 711 | function zeroBSCRM_permsViewTransactions() { |
| 712 | |
| 713 | $cu = wp_get_current_user(); |
| 714 | if ( $cu->has_cap( 'admin_zerobs_view_transactions' ) ) { |
| 715 | return true; |
| 716 | } |
| 717 | return false; |
| 718 | } |
| 719 | function zeroBSCRM_permsMailCampaigns() { |
| 720 | |
| 721 | $cu = wp_get_current_user(); |
| 722 | if ( $cu->has_cap( 'admin_zerobs_mailcampaigns' ) ) { |
| 723 | return true; |
| 724 | } |
| 725 | return false; |
| 726 | } |
| 727 | function zeroBSCRM_permsForms() { |
| 728 | |
| 729 | $cu = wp_get_current_user(); |
| 730 | if ( $cu->has_cap( 'admin_zerobs_forms' ) ) { |
| 731 | return true; |
| 732 | } |
| 733 | return false; |
| 734 | } |
| 735 | |
| 736 | function zeroBSCRM_perms_tasks() { |
| 737 | |
| 738 | $cu = wp_get_current_user(); |
| 739 | if ( $cu->has_cap( 'admin_zerobs_events' ) ) { |
| 740 | return true; |
| 741 | } |
| 742 | return false; |
| 743 | } |
| 744 | function zeroBSCRM_permsNotify() { |
| 745 | |
| 746 | $cu = wp_get_current_user(); |
| 747 | if ( $cu->has_cap( 'admin_zerobs_notifications' ) ) { |
| 748 | return true; |
| 749 | } |
| 750 | return false; |
| 751 | } |
| 752 | // NEEDS it's own cap when we get granular. |
| 753 | function zeroBSCRM_permsExport() { |
| 754 | |
| 755 | $cu = wp_get_current_user(); |
| 756 | if ( $cu->has_cap( 'admin_zerobs_customers' ) ) { |
| 757 | return true; |
| 758 | } |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | // LOGS |
| 763 | |
| 764 | // can add/edit logs |
| 765 | function zeroBSCRM_permsLogsAddEdit() { |
| 766 | |
| 767 | $cu = wp_get_current_user(); |
| 768 | if ( $cu->has_cap( 'admin_zerobs_logs_addedit' ) ) { |
| 769 | return true; |
| 770 | } |
| 771 | return false; |
| 772 | } |
| 773 | |
| 774 | // can delete logs |
| 775 | function zeroBSCRM_permsLogsDelete() { |
| 776 | |
| 777 | $cu = wp_get_current_user(); |
| 778 | if ( $cu->has_cap( 'admin_zerobs_logs_delete' ) ) { |
| 779 | return true; |
| 780 | } |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | function zeroBSCRM_permsClient() { |
| 785 | // using Client to not confuse with Customer and Customer Manager |
| 786 | |
| 787 | $cu = wp_get_current_user(); |
| 788 | if ( $cu->has_cap( 'zerobs_customer' ) ) { |
| 789 | return true; |
| 790 | } |
| 791 | return false; |
| 792 | } |
| 793 | function zeroBSCRM_permsWPEditPosts() { |
| 794 | |
| 795 | $cu = wp_get_current_user(); |
| 796 | if ( $cu->has_cap( 'edit_posts' ) ) { |
| 797 | return true; |
| 798 | } |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | function zeroBS_getPossibleCustomerOwners() { |
| 803 | return zeroBS_getPossibleOwners( array( 'zerobs_admin', 'zerobs_customermgr' ) ); } |
| 804 | function zeroBS_getPossibleCompanyOwners() { |
| 805 | return zeroBS_getPossibleOwners( array( 'zerobs_admin', 'zerobs_customermgr' ) ); } |
| 806 | function zeroBS_getPossibleQuoteOwners() { |
| 807 | return zeroBS_getPossibleOwners( array( 'zerobs_admin', 'zerobs_customermgr' ) ); } |
| 808 | function zeroBS_getPossibleInvoiceOwners() { |
| 809 | return zeroBS_getPossibleOwners( array( 'zerobs_admin', 'zerobs_customermgr' ) ); } |
| 810 | function zeroBS_getPossibleTransactionOwners() { |
| 811 | return zeroBS_getPossibleOwners( array( 'zerobs_admin', 'zerobs_customermgr' ) ); } |
| 812 | function zeroBS_getPossibleTaskOwners() { |
| 813 | return zeroBS_getPossibleOwners( array( 'zerobs_admin', 'admin_zerobs_events' ) ); } |
| 814 | |
| 815 | // added this because Multi-site doesn't reliably |
| 816 | // return on current_user_can('zerobs_customer') |
| 817 | // https://wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role |
| 818 | function zeroBSCRM_isRole( $role = '' ) { |
| 819 | |
| 820 | $user = wp_get_current_user(); |
| 821 | if ( in_array( $role, (array) $user->roles ) ) { |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | return false; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Checks a user object (or current user if false passed) |
| 830 | * has roles in first array, and none of the roles in second array |
| 831 | **/ |
| 832 | function jpcrm_role_check( $user_object = false, $has_roles = array(), $hasnt_roles = array(), $only_these_roles = array() ) { |
| 833 | |
| 834 | // load current user if not passed |
| 835 | if ( ! $user_object ) { |
| 836 | |
| 837 | $user_object = wp_get_current_user(); |
| 838 | |
| 839 | } |
| 840 | |
| 841 | // got object? |
| 842 | if ( ! $user_object ) { |
| 843 | |
| 844 | return false; |
| 845 | |
| 846 | } |
| 847 | |
| 848 | // verify has_roles |
| 849 | if ( count( $has_roles ) > 0 ) { |
| 850 | |
| 851 | foreach ( $has_roles as $role ) { |
| 852 | |
| 853 | if ( ! in_array( $role, $user_object->roles ) ) { |
| 854 | |
| 855 | return false; |
| 856 | |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | // verify hasnt_roles |
| 862 | if ( count( $hasnt_roles ) > 0 ) { |
| 863 | |
| 864 | foreach ( $hasnt_roles as $role ) { |
| 865 | |
| 866 | if ( in_array( $role, $user_object->roles ) ) { |
| 867 | |
| 868 | return false; |
| 869 | |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | // verify only_roles |
| 875 | if ( count( $only_these_roles ) > 0 ) { |
| 876 | |
| 877 | $role_match_count = 0; |
| 878 | |
| 879 | foreach ( $user_object->roles as $role ) { |
| 880 | |
| 881 | if ( ! in_array( $role, $only_these_roles ) ) { |
| 882 | |
| 883 | return false; |
| 884 | |
| 885 | } else { |
| 886 | |
| 887 | ++$role_match_count; |
| 888 | |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | if ( $role_match_count != count( $only_these_roles ) ) { |
| 893 | |
| 894 | return false; |
| 895 | |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | return true; |
| 900 | } |
| 901 | |
| 902 | function zeroBS_getPossibleOwners( $permsReq = '', $simplify = false ) { |
| 903 | |
| 904 | // https://codex.wordpress.org/Function_Reference/get_users |
| 905 | /* |
| 906 | possible args.. |
| 907 | $args = array( |
| 908 | 'blog_id' => $GLOBALS['blog_id'], |
| 909 | 'role' => '', |
| 910 | 'role__in' => array('administrator','zerobs_admin','zerobs_customermgr','zerobs_quotemgr','zerobs_invoicemgr','zerobs_transactionmgr',''), |
| 911 | 'role__not_in' => array(), |
| 912 | 'meta_key' => '', |
| 913 | 'meta_value' => '', |
| 914 | 'meta_compare' => '', |
| 915 | 'meta_query' => array(), |
| 916 | 'date_query' => array(), |
| 917 | 'include' => array(), |
| 918 | 'exclude' => array(), |
| 919 | 'orderby' => 'login', |
| 920 | 'order' => 'ASC', |
| 921 | 'offset' => '', |
| 922 | 'search' => '', |
| 923 | 'number' => '', |
| 924 | 'count_total' => false, |
| 925 | 'fields' => 'all', |
| 926 | 'who' => '', |
| 927 | ); |
| 928 | |
| 929 | */ |
| 930 | |
| 931 | if ( empty( $permsReq ) || ! in_array( $permsReq, array( 'zerobs_admin', 'zerobs_customermgr', 'zerobs_quotemgr', 'zerobs_invoicemgr', 'zerobs_transactionmgr' ) ) ) { |
| 932 | |
| 933 | // all zbs users + admin |
| 934 | $args = array( 'role__in' => array( 'administrator', 'zerobs_admin', 'zerobs_customermgr', 'zerobs_quotemgr', 'zerobs_invoicemgr', 'zerobs_transactionmgr' ) ); |
| 935 | |
| 936 | } else { |
| 937 | |
| 938 | // specific roles :) (+- admin?) |
| 939 | $args = array( 'role__in' => array( 'administrator', $permsReq ) ); |
| 940 | |
| 941 | } |
| 942 | |
| 943 | $users = get_users( $args ); |
| 944 | |
| 945 | // this is used by inline editing on list view, be careful if editing |
| 946 | if ( $simplify ) { |
| 947 | |
| 948 | if ( is_array( $users ) ) { |
| 949 | |
| 950 | $ret = array(); |
| 951 | |
| 952 | foreach ( $users as $u ) { |
| 953 | |
| 954 | $ret[] = array( |
| 955 | |
| 956 | 'id' => $u->ID, |
| 957 | 'name' => $u->data->display_name, |
| 958 | 'email' => $u->data->user_email, |
| 959 | |
| 960 | ); |
| 961 | } |
| 962 | |
| 963 | $users = $ret; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | return $users; |
| 968 | } |
| 969 | /* |
| 970 | ====================================================== |
| 971 | / Role Helpers |
| 972 | ====================================================== */ |
| 973 | |
| 974 | /** |
| 975 | * Checks whether a WP user has permissions to view an object |
| 976 | * |
| 977 | * @param \WP_User $wp_user - WP user object. |
| 978 | * @param int $obj_id - Object ID. |
| 979 | * @param int $obj_type_id - Object type ID. |
| 980 | * |
| 981 | * @return bool indicating whether the WP user can view the current object |
| 982 | */ |
| 983 | function jpcrm_can_wp_user_view_object( $wp_user, $obj_id, $obj_type_id ) { |
| 984 | |
| 985 | // unsupported object type |
| 986 | if ( ! in_array( $obj_type_id, array( ZBS_TYPE_QUOTE, ZBS_TYPE_INVOICE ), true ) ) { |
| 987 | return false; |
| 988 | } |
| 989 | |
| 990 | global $zbs; |
| 991 | |
| 992 | // retrieve object |
| 993 | switch ( $obj_type_id ) { |
| 994 | case ZBS_TYPE_QUOTE: |
| 995 | $is_quote_admin = $wp_user->has_cap( 'admin_zerobs_quotes' ); |
| 996 | $obj_data = zeroBS_getQuote( $obj_id ); |
| 997 | // draft quote |
| 998 | if ( is_array( $obj_data ) && $obj_data['template'] == -1 && ! $is_quote_admin ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual |
| 999 | return false; |
| 1000 | } |
| 1001 | $assigned_contact_id = $zbs->DAL->quotes->getQuoteContactID( $obj_id ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 1002 | break; |
| 1003 | case ZBS_TYPE_INVOICE: |
| 1004 | $is_invoice_admin = $wp_user->has_cap( 'admin_zerobs_invoices' ); |
| 1005 | $obj_data = zeroBS_getInvoice( $obj_id ); |
| 1006 | // draft invoice |
| 1007 | if ( is_array( $obj_data ) && $obj_data['status'] === 'Draft' && ! $is_invoice_admin ) { |
| 1008 | return false; |
| 1009 | } |
| 1010 | $assigned_contact_id = $zbs->DAL->invoices->getInvoiceContactID( $obj_id ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | // no such object! |
| 1015 | if ( ! $obj_data ) { |
| 1016 | return false; |
| 1017 | } |
| 1018 | |
| 1019 | // not logged in |
| 1020 | if ( ! $wp_user ) { |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
| 1024 | // grant access if user has full permissions to view object type |
| 1025 | if ( |
| 1026 | $obj_type_id == ZBS_TYPE_QUOTE && $is_quote_admin |
| 1027 | || $obj_type_id == ZBS_TYPE_INVOICE && $is_invoice_admin |
| 1028 | ) { |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | // object is not assigned |
| 1033 | if ( ! $assigned_contact_id ) { |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | // verify current user is assigned user |
| 1038 | $contact_id = zeroBS_getCustomerIDWithEmail( $wp_user->user_email ); |
| 1039 | if ( $assigned_contact_id != $contact_id ) { |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
| 1043 | // passed all checks, so go for liftoff |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Pass-thru helper function to get current user for use with jpcrm_can_wp_user_view_object() |
| 1049 | * Particularly useful for client portal functions. |
| 1050 | * |
| 1051 | * @param int $obj_id |
| 1052 | * @param int $obj_type_id |
| 1053 | * |
| 1054 | * @return bool indicating whether the current user can view the current object |
| 1055 | */ |
| 1056 | function jpcrm_can_current_wp_user_view_object( $obj_id, $obj_type_id ) { |
| 1057 | $current_wp_user = wp_get_current_user(); |
| 1058 | return jpcrm_can_wp_user_view_object( $current_wp_user, $obj_id, $obj_type_id ); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * Determines if client portal access is allowed via easy-access hashes. |
| 1063 | * |
| 1064 | * @param int $obj_type_id |
| 1065 | * |
| 1066 | * @return bool |
| 1067 | */ |
| 1068 | function jpcrm_can_access_portal_via_hash( $obj_type_id ) { |
| 1069 | |
| 1070 | // easy access is disabled |
| 1071 | if ( zeroBSCRM_getSetting( 'easyaccesslinks' ) != 1 ) { |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | $security_request_name = jpcrm_get_easy_access_security_request_name_by_obj_type( $obj_type_id ); |
| 1076 | |
| 1077 | // unsupported object type |
| 1078 | if ( ! $security_request_name ) { |
| 1079 | return false; |
| 1080 | } |
| 1081 | |
| 1082 | // fail if already blocked (this is a nefarious user or bot) |
| 1083 | if ( zeroBSCRM_security_blockRequest( $security_request_name ) ) { |
| 1084 | return false; |
| 1085 | } |
| 1086 | // access via hash is allowed |
| 1087 | return true; |
| 1088 | } |
| 1089 | |
| 1090 | /** |
| 1091 | * Returns security request name by object type. |
| 1092 | * |
| 1093 | * @param int $obj_type_id |
| 1094 | * |
| 1095 | * @return string|bool request name or false if no match |
| 1096 | */ |
| 1097 | function jpcrm_get_easy_access_security_request_name_by_obj_type( $obj_type_id ) { |
| 1098 | |
| 1099 | switch ( $obj_type_id ) { |
| 1100 | case ZBS_TYPE_INVOICE: |
| 1101 | $security_request_name = 'inveasy'; |
| 1102 | break; |
| 1103 | case ZBS_TYPE_QUOTE: |
| 1104 | $security_request_name = 'quoeasy'; |
| 1105 | break; |
| 1106 | default: |
| 1107 | $security_request_name = false; |
| 1108 | } |
| 1109 | |
| 1110 | return $security_request_name; |
| 1111 | } |
| 1112 | |
| 1113 | // show an error if bad permissions |
| 1114 | function jpcrm_perms_error() { |
| 1115 | echo zeroBSCRM_UI2_messageHTML( |
| 1116 | 'warning', |
| 1117 | __( 'Access Denied', 'zero-bs-crm' ), |
| 1118 | __( 'You do not have permission to access this page.', 'zero-bs-crm' ), |
| 1119 | 'disabled warning sign', |
| 1120 | 'bad_perms' |
| 1121 | ); |
| 1122 | die( 0 ); |
| 1123 | } |
| 1124 | |
| 1125 | /** |
| 1126 | * Verifies a given path is within allowed base paths. |
| 1127 | * |
| 1128 | * @param string $path Path to check. |
| 1129 | * @param array|string $allowed_base_paths Paths to check. |
| 1130 | * |
| 1131 | * @return bool True if it's an allowed path, false if not. |
| 1132 | */ |
| 1133 | function jpcrm_is_allowed_path( $path, $allowed_base_paths ) { |
| 1134 | |
| 1135 | // Convert to array if not already one. |
| 1136 | if ( ! is_array( $allowed_base_paths ) ) { |
| 1137 | $allowed_base_paths = array( $allowed_base_paths ); |
| 1138 | } |
| 1139 | |
| 1140 | $real_path = realpath( $path ); |
| 1141 | |
| 1142 | // Invalid path. |
| 1143 | if ( ! $real_path ) { |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | foreach ( $allowed_base_paths as $base_path ) { |
| 1148 | $real_base_path = realpath( $base_path ); |
| 1149 | |
| 1150 | // If file belongs to a valid base_path, all is well. |
| 1151 | // This base path is sometimes a non-existent path, so we test for its existence as well. |
| 1152 | if ( $real_base_path && strpos( $real_path, $real_base_path ) === 0 ) { |
| 1153 | return true; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | // doesn't match an allowed base path |
| 1158 | return false; |
| 1159 | } |