Skip to content

Instantly share code, notes, and snippets.

@tobeyadr
Last active December 20, 2023 17:34
Show Gist options
  • Save tobeyadr/23f23dfe5f68b132fe1b8e34cf07b014 to your computer and use it in GitHub Desktop.
Save tobeyadr/23f23dfe5f68b132fe1b8e34cf07b014 to your computer and use it in GitHub Desktop.
Prevent sales reps from importing the owner
<?php
add_action( 'groundhogg/generate_contact_with_map/before', 'prevent_sales_reps_from_importing_owner', 10, 2 );
/**
* Removes the field mapping for the contact owner if the current user does not have edit_others_contacts permissions
*
* @param $fields array
* @param $map array
*
* @return void
*/
function prevent_sales_reps_from_importing_owner( &$fields, &$map ){
// No user is logged in currently or the current user can edit others contacts
if ( ! is_user_logged_in() || current_user_can( 'edit_others_contacts' ) ){
return;
}
$owner_keys = [
'owner_email',
'owner_id'
];
foreach ( $owner_keys as $owner_key ){
if ( ! in_array( $owner_key, $map ) ){
continue;
}
unset( $map[ array_search( $owner_key, $map ) ] );
}
}
add_filter( 'groundhogg/mappable_fields', 'remove_owner_from_mappable_fields' );
/**
* Removes owner from the field mapping options
*
* @param $fields
*
* @return array[]
*/
function remove_owner_from_mappable_fields( $fields ){
if ( ! is_user_logged_in() || current_user_can( 'edit_others_contacts' ) ){
return $fields;
}
unset( $fields[ __( 'Contact Owner', 'groundhogg' ) ] );
return $fields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment