wpf_api_{$method_name}

#Overview

This filter allows you to override calls to any of WP Fusion’s CRM API methods. It’s inspired by the get_{$meta_type}_metadata filter in WordPress core.

If a non-null value is returned from this filter, it will bypass sending any API calls to your CRM, and instead use your custom function to handle the API call.

This can be used to override any of WP Fusion’s CRM API methods with your own custom functionality.

#Available method names

For more information see the CRM API documentation.

  • get_contact_id
  • get_tags
  • apply_tags
  • remove_tags
  • add_contact
  • update_contact
  • load_contact
  • load_contacts
  • track_event (with Event Tracking)

#Parameters

#Examples

#Enhanced contact lookup with name matching

This example overrides the contact ID lookup with ActiveCampaign so the ID is only returned if the email address, first name, and last name match (for registered users).

/**
 * Get the Contact ID for a given email address, with an additional search by name
 * for registered users.
 *
 * @param null  $result The result. Return a non-null value to override the API call.
 * @param array $args   The arguments passed to the CRM API function.
 * @return string The Contact ID, if found.
 */
function custom_wpf_get_contact_id( $result = null, $args ) {

	// $email_address is the only parameter passed to wp_fusion()->crm->get_contact_id(), so it's stored at $args[0].
	$email_address = $args[0];

	$request_uri = wp_fusion()->crm->api_url . 'api/3/contacts?email=' . rawurlencode( $email_address );

	$user = get_user_by( 'email', $email_address );

	if ( $user ) {

		// If it's a registered user, let's also search by their name.

		$request_uri .= '&search=' . rawurlencode( $user->first_name . ' ' . $user->last_name );

	}

	// Make the API call.

	$response = wp_safe_remote_get( $request_uri, wp_fusion()->crm->get_params() );

	if ( is_wp_error( $response ) ) {
		return $response;
	}

	$response = json_decode( wp_remote_retrieve_body( $response ) );

	if ( empty( $response->contacts ) ) {
		// No matching contact found.
		return false;
	} else {
		// Return the ID of the contact.
		return $response->contacts[0]->id;
	}
}

add_filter( 'wpf_api_get_contact_id', 'custom_wpf_get_contact_id', 10, 2 );

#Cache contact IDs to improve performance

Cache contact ID lookups to reduce API calls and improve performance on high-traffic sites.

function cache_contact_id_lookups( $result = null, $args ) {
    
    $email_address = $args[0];
    $cache_key = 'wpf_contact_id_' . md5( $email_address );
    
    // Check cache first
    $cached_id = wp_cache_get( $cache_key );
    if ( false !== $cached_id ) {
        return $cached_id;
    }
    
    // Let WP Fusion do the normal lookup
    return $result; // null = continue with normal process
}

// Cache the result after normal lookup
function cache_contact_id_result( $contact_id, $email ) {
    if ( $contact_id && ! is_wp_error( $contact_id ) ) {
        $cache_key = 'wpf_contact_id_' . md5( $email );
        wp_cache_set( $cache_key, $contact_id, '', HOUR_IN_SECONDS );
    }
}

add_filter( 'wpf_api_get_contact_id', 'cache_contact_id_lookups', 10, 2 );
add_action( 'wpf_got_contact_id', 'cache_contact_id_result', 10, 2 );

#Log all API calls for debugging

Override API calls to add comprehensive logging while still executing the original functionality.

function log_all_api_calls( $result = null, $args ) {
    
    // Get the method name from the current filter
    $current_filter = current_filter();
    $method_name = str_replace( 'wpf_api_', '', $current_filter );
    
    // Log the API call attempt
    if ( function_exists( 'wpf_log' ) ) {
        wpf_log( 'info', 0, "API Call: {$method_name}", array( 
            'meta_array' => array(
                'method' => $method_name,
                'args' => $args,
                'timestamp' => current_time( 'mysql' )
            )
        ));
    }
    
    // Return null to continue with normal processing
    return $result;
}

// Apply to all API methods
$api_methods = array( 'get_contact_id', 'get_tags', 'apply_tags', 'remove_tags', 
                     'add_contact', 'update_contact', 'load_contact' );

foreach ( $api_methods as $method ) {
    add_filter( "wpf_api_{$method}", 'log_all_api_calls', 5, 2 );
}

#Prevent tag application during maintenance

Temporarily disable tag applications during site maintenance or bulk operations.

function prevent_tags_during_maintenance( $result = null, $args ) {
    
    // Check if maintenance mode is active
    if ( defined( 'WP_MAINTENANCE_MODE' ) && WP_MAINTENANCE_MODE ) {
        wpf_log( 'notice', 0, 'Tag application blocked - maintenance mode active' );
        return true; // Return success without actually applying tags
    }
    
    // Check for bulk operations
    if ( doing_action( 'wpf_batch_process' ) ) {
        // Allow normal processing during batch operations
        return $result;
    }
    
    // Check if it's during specified maintenance hours (e.g., 2-4 AM)
    $current_hour = (int) date( 'H' );
    if ( $current_hour >= 2 && $current_hour <= 4 ) {
        wpf_log( 'notice', 0, 'Tag application deferred - maintenance window' );
        
        // Queue the tag application for later
        wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wpf_delayed_apply_tags', $args );
        
        return true;
    }
    
    return $result;
}

add_filter( 'wpf_api_apply_tags', 'prevent_tags_during_maintenance', 10, 2 );

Was this helpful?