View difference between Paste ID: EBe4WJPr and 9AjKnBW5
SHOW: | | - or go back to the newest paste.
1
<?php
2
function healthexcel_wpf_infusionsoft_tag_staff_members() {
3
4
	if ( empty( $_REQUEST['contactId'] ) ) {
5
		return false;
6
		// wp_die( '<h3>Error</h3>Referring Practitioner Contact ID not provided! Aborting.', 'Error', 500 );
7
	} else if ( empty( $_REQUEST['isTagId'] ) ) {
8
		return false;
9
		// wp_die( '<h3>Error</h3>Target Tag ID not provided! Aborting.', 'Error', 500 );
10
	}
11
12
	$app = tboc_healthexcel_isdk_api();
13
	if ( is_wp_error( $app ) ) {
14
		return;
15
	}
16
	$practitioner_contact_id = (int) $_REQUEST['contactId'];
17
18
	// Search for contacts with Referring Practitioner field value equal to contactId value
19
	$returnFields = array('Id');
20
	$staffMembers = $app->dsFind(
21
		'Contact',                // table
22
		1,                        // limit
23
		0,                        // page
24
		'_ReferringPractitioner', // fieldName
25
		$practitioner_contact_id, // fieldValue
26
		$returnFields             // returnFields
27
	);
28
29
	// Check do we have success array with contacts from IS?
30
	if ( empty( $staffMembers ) ) {
31
		return true;
32
		// wp_die( '<h3>Nothing to do!</h3>No staff members registered by this practitioner. Aborting.', 'OK', 200 );
33
	}
34
35
	// Extract staff member contactId
36
	$contact_id = (int) $staffMembers[0]['Id'];
37
38
	// Extract IS Tag ID's
39
	$is_tag_id = $_REQUEST['isTagId'];
40
	$tag_ids = explode(',', $is_tag_id);
41
42-
	// Apply IS tag(s)
42+
	// Get WordPress user ID
43-
	$ret = wp_fusion()->crm->apply_tags( $tag_ids, $contact_id );
43+
	$user_id = wp_fusion()->user->get_user_id( $contact_id );
44
45-
	if ( true === $ret ) {
45+
	// If no matching user found apply tags directly in Infusionsoft
46
	if( $user_id == false ) {
47
48
		$ret = wp_fusion()->crm->apply_tags( $tag_ids, $contact_id );
49
50
	} else {
51
52
		$ret = wp_fusion()->user->apply_tags( $tag_ids, $user_id );
53
54
	}
55
56
	if ( is_wp_error( $ret ) || $ret == false ) {
57
		error_log('Error performing request');
58
		error_log(print_r($ret, true));
59
	}
60
61
	wp_die();
62
63
} // END function healthexcel_wpf_infusionsoft_tag_staff_members() {
64
add_action( 'wp_ajax_nopriv_wpf_tag_staff_members', 'healthexcel_wpf_infusionsoft_tag_staff_members' );