Filter uncanny-automator-pro

uap_option_get_all_gh_contacts

Filters arguments for fetching all Groundhogg contacts, allowing modification of the query before retrieval.

add_filter( 'uap_option_get_all_gh_contacts', $callback, 10, 1 );

Description

This filter hook allows developers to modify the array of Groundhogg contacts before they are displayed in a dropdown. It fires after all Groundhogg contacts have been retrieved, and developers can use it to add, remove, or alter contact options. Note that the `$args` parameter contains the `options` array.


Usage

add_filter( 'uap_option_get_all_gh_contacts', 'your_function_name', 10, 1 );

Parameters

$args (mixed)
This parameter contains an array of arguments that can be used to customize the retrieval and display of Groundhogg contacts.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the 'uap_option_get_all_gh_contacts' hook
 * to potentially add custom logic or modify the list of Groundhogg contacts
 * before they are displayed in Uncanny Automator.
 *
 * In this example, we'll demonstrate how to:
 * 1. Add a specific contact (if it doesn't already exist) based on an ID.
 * 2. Remove a specific contact from the list if a certain condition is met.
 *
 * @param array $args An array containing arguments for fetching contacts.
 *                    Expected to have keys like 'is_all' and 'options' (which will be populated).
 * @return array The modified arguments array, with potentially altered 'options'.
 */
function my_custom_groundhogg_contacts( $args ) {

	// Define a specific contact ID and name to potentially add.
	$custom_contact_id = '9999';
	$custom_contact_name = 'My Special Contact';

	// Check if the 'options' key exists and is an array.
	if ( isset( $args['options'] ) && is_array( $args['options'] ) ) {
		$current_contacts = $args['options'];

		// 1. Add a custom contact if it's not already present.
		// We'll check if the 'is_all' flag is true, as this filter is often used when
		// a comprehensive list is being generated.
		if ( true === $args['is_all'] ) {
			if ( ! array_key_exists( $custom_contact_id, $current_contacts ) ) {
				// Add the custom contact to the beginning of the array.
				$current_contacts = array( $custom_contact_id => $custom_contact_name ) + $current_contacts;
			}
		}

		// 2. Remove a specific contact if a certain condition is met.
		// For demonstration, let's say we want to remove any contact with the ID '100'.
		$contact_to_remove_id = '100';
		if ( array_key_exists( $contact_to_remove_id, $current_contacts ) ) {
			unset( $current_contacts[ $contact_to_remove_id ] );
		}

		// Update the 'options' in the $args array with our modified contacts.
		$args['options'] = $current_contacts;
	}

	// Always return the $args array, whether modified or not.
	return $args;
}
add_filter( 'uap_option_get_all_gh_contacts', 'my_custom_groundhogg_contacts', 10, 1 );
?>

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/integrations/groundhogg/helpers/groundhogg-pro-helpers.php:91

public function get_all_gh_contacts( $args = array() ) {


		$defaults = array(
			'option_code'           => 'GH_CONTACT',
			'label'                 => esc_attr__( 'Contact', 'uncanny-automator' ),
			'input_type'            => 'select',
			'required'              => true,
			'options_show_id'       => false,
			'is_any'                => false,
			'is_all'                => false,
			'supports_custom_value' => false,
			'relevant_tokens'       => array(),
			'options'               => array(),
		);

		$args         = wp_parse_args( $args, $defaults );
		$all_contacts = array();

		try {
			// Try using the get_db function first (simpler approach)
			if ( function_exists( 'Groundhoggget_db' ) ) {
				$contacts_db = Groundhoggget_db( 'contacts' );
				$contacts = $contacts_db->query( array(
					'number'     => 1000,
					'offset'     => 0,
					'orderby'    => 'last_name',
					'order'      => 'ASC'
				) );
			} else {
				// Fallback to Contact_Query
				$query_args = array(
					'number'     => 1000,
					'offset'     => 0,
					'orderby'    => 'last_name',
					'order'      => 'ASC',
					'no_found_rows' => true
				);

				$contacts_db = new GroundhoggContact_Query( $query_args );
				$contacts = $contacts_db->query( $query_args );
			}

		} catch ( Error $e ) {
			// Keep $all_contacts as empty array
		} catch ( Exception $e ) {
			// Keep $all_contacts as empty array
		}

		if ( ! empty( $contacts ) ) {
			foreach ( $contacts as $contact ) {
				// Handle both object and array formats
				$contact_id = is_object( $contact ) ? $contact->ID : $contact['ID'];
				$contact_email = is_object( $contact ) ? $contact->email : $contact['email'];
				
				if ( ! empty( $contact_email ) ) {
					$all_contacts[ $contact_id ] = $contact_email;
				}
			}
		}

		if ( true === $args['is_any'] ) {
			$all_contacts = array( '-1' => _x( 'Any contact', 'Groundhogg', 'uncanny-automator' ) ) + $all_contacts;
		}

		if ( true === $args['is_all'] ) {
			$all_contacts = array( '-1' => _x( 'All contacts', 'Groundhogg', 'uncanny-automator' ) ) + $all_contacts;
		}

		$args['options'] = $all_contacts;

		return apply_filters( 'uap_option_get_all_gh_contacts', $args );

	}

Scroll to Top