-
-
Save Lonsdale201/4ba7e21f5625ea6f1f75f45fee0521b3 to your computer and use it in GitHub Desktop.
JetEngine Custom macro - Fluent CRM get Contact users with selectable list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// place the code in the child theme functions.php or a custom code snippets plugin like FluentSnippets | |
// how to use | |
// Create a new Users Query in the Query builder | |
// Go to the Include/Exclude section | |
// click the Include section dynamic icon and choose the Fluent CRM Users with List | |
// HELP IMAGE https://prnt.sc/06FFNY9E24Sg | |
// This macro returns the identifiers of users who can be found in fluentCRM as both contacts and wordpress users. | |
// You can also select which CRM list you want to retrieve the users from | |
add_action( 'jet-engine/register-macros', function() { | |
class Fluent_CRM_Users_With_List extends \Jet_Engine_Base_Macros { | |
public function macros_tag() { | |
return 'fluent_crm_users_with_list'; | |
} | |
public function macros_name() { | |
return 'Fluent CRM Users with List'; | |
} | |
public function macros_args() { | |
$listApi = FluentCrmApi('lists'); | |
$lists = $listApi->all(); | |
$list_options = array(); | |
foreach ($lists as $list) { | |
$list_options[$list->id] = $list->title; | |
} | |
return array( | |
'contact_list' => array( | |
'label' => 'Contact List', | |
'type' => 'select', | |
'options' => $list_options, | |
'default' => key($list_options) | |
), | |
); | |
} | |
public function macros_callback( $args = array() ) { | |
$contact_list = !empty( $args['contact_list'] ) ? $args['contact_list'] : key($args['contact_list']); | |
$users = get_users( array( 'fields' => array( 'ID' ) ) ); | |
$crm_users = array(); | |
$contactApi = FluentCrmApi('contacts')->getInstance(); | |
$contacts = $contactApi->filterByLists([$contact_list])->get(); | |
foreach ( $users as $user ) { | |
foreach ( $contacts as $contact ) { | |
if ( $contact->user_id == $user->ID ) { | |
$crm_users[] = $user->ID; | |
break; | |
} | |
} | |
} | |
if ( empty( $crm_users ) ) { | |
return 'No CRM users found'; | |
} | |
return implode( ',', $crm_users ); | |
} | |
} | |
new Fluent_CRM_Users_With_List(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment