Filter uncanny-automator-pro

uap_option_get_affiliates

Filters the retrieved affiliate data before it's returned, allowing for modification of affiliate information.

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

Description

Fires when Uncanny Automator Pro retrieves a list of AffiliateWP affiliates for use in automations. Developers can filter the returned `$option` array to modify the affiliate list, add custom values, or change how they are presented, particularly useful for dynamic filtering or custom integrations.


Usage

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

Parameters

$option (mixed)
This parameter is not used by the `uap_option_get_affiliates` filter hook and can be ignored.

Return Value

The filtered value.


Examples

/**
 * Filter hook: uap_option_get_affiliates
 *
 * This example demonstrates how to modify the options array passed to the 'uap_option_get_affiliates'
 * filter. It adds a custom affiliate to the list if certain conditions are met.
 *
 * @param array $option The array of affiliate options.
 * @return array The modified array of affiliate options.
 */
add_filter( 'uap_option_get_affiliates', function( $option ) {

    // Check if the current user is an administrator.
    if ( current_user_can( 'manage_options' ) ) {

        // Add a custom affiliate option. This could be an affiliate with a specific ID,
        // or a placeholder for a special scenario.
        $custom_affiliate_id = 123; // Example custom affiliate ID
        $custom_affiliate_name = 'Special Referral Program'; // Example custom name

        // Check if the custom affiliate already exists in the options to avoid duplicates.
        $affiliate_exists = false;
        if ( ! empty( $option['options'] ) && is_array( $option['options'] ) ) {
            foreach ( $option['options'] as $affiliate_data ) {
                if ( isset( $affiliate_data['value'] ) && $affiliate_data['value'] == $custom_affiliate_id ) {
                    $affiliate_exists = true;
                    break;
                }
            }
        }

        // If the custom affiliate doesn't exist, add it to the options.
        if ( ! $affiliate_exists ) {
            $option['options'][] = array(
                'label' => $custom_affiliate_name,
                'value' => $custom_affiliate_id,
            );
        }
    }

    return $option;
}, 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/affiliate-wp/helpers/affwp-pro-helpers.php:202

public function get_affiliates( $label = null, $option_code = 'ALLAFFILIATES', $args = array() ) {
		if ( ! $label ) {
			$label = __( 'Affiliate', 'uncanny-automator-pro' );
		}

		$token        = key_exists( 'token', $args ) ? $args['token'] : true;
		$is_ajax      = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
		$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
		$end_point    = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
		$any_option   = key_exists( 'any_option', $args ) ? $args['any_option'] : false;
		$options      = array();

		if ( $any_option ) {
			$options['-1'] = esc_attr__( 'Any affiliate', 'uncanny-automator-pro' );
		}

		global $wpdb;
		$affiliates = $wpdb->get_results( "SELECT affiliate_id FROM {$wpdb->prefix}affiliate_wp_affiliates" );

		if ( is_array( $affiliates ) ) {
			foreach ( $affiliates as $affiliate ) {
				$options[ $affiliate->affiliate_id ] = affwp_get_affiliate_name( $affiliate->affiliate_id );
			}
		}

		$option = array(
			'option_code'           => $option_code,
			'label'                 => $label,
			'input_type'            => 'select',
			'required'              => true,
			'supports_tokens'       => $token,
			'supports_custom_value' => true,
			'is_ajax'               => $is_ajax,
			'fill_values_in'        => $target_field,
			'endpoint'              => $end_point,
			'options'               => $options,
		);

		return apply_filters( 'uap_option_get_affiliates', $option );
	}

Scroll to Top