Filter uncanny-automator-pro

automator_advanced_ads_pro_validate_trigger_meta_pieces_common

Filters trigger meta pieces for Advanced Ads Pro when ad status changes, allowing custom validation logic.

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

Description

Filters the validation pieces for Advanced Ads Pro trigger meta. Developers can add or modify validation codes here to customize how Advanced Ads Pro trigger data is processed. This hook fires after initial piece validation.


Usage

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

Parameters

$pieces (mixed)
This parameter is an array containing a specific status code used for validating Advanced Ads trigger meta.

Return Value

The filtered value.


Examples

add_filter( 'automator_advanced_ads_pro_validate_trigger_meta_pieces_common', 'my_advads_pro_trigger_validation', 10, 2 );

/**
 * Custom validation for Advanced Ads Pro trigger meta pieces.
 *
 * This function checks if the provided 'AD_STATUS_CHANGED_CODE' trigger
 * has a valid ad ID specified. If not, it adds an error message to the
 * validation results.
 *
 * @param array $pieces The validation pieces. Expected to be an array where the first element is the validation code.
 * @param array $data   An array containing recipe and trigger data.
 * @return array The modified validation pieces with potential error messages.
 */
function my_advads_pro_trigger_validation( $pieces, $data ) {

    // Ensure we're dealing with the expected validation code.
    if ( ! empty( $pieces ) && $pieces[0] === 'AD_STATUS_CHANGED_CODE' ) {

        // Extract relevant data from the $data array.
        $trigger_meta = isset( $data['trigger_meta'] ) ? $data['trigger_meta'] : array();

        // The 'AD_STATUS_CHANGED_CODE' trigger typically requires an ad ID.
        // We expect the ad ID to be in a specific key within the trigger meta.
        // The exact key might vary depending on the Uncanny Automator integration.
        // For this example, let's assume it's 'advanced_ads_ad_id'.
        $ad_id = isset( $trigger_meta['advanced_ads_ad_id'] ) ? absint( $trigger_meta['advanced_ads_ad_id'] ) : 0;

        // If no valid ad ID is found, add an error.
        if ( empty( $ad_id ) ) {
            // Add an error message to the $pieces array.
            // The format for error messages can vary, but typically it involves
            // adding an array with an 'error' key.
            $pieces[] = array(
                'error' => __( 'Please select a valid Advanced Ads Pro Ad.', 'your-text-domain' ),
            );
        }
    }

    // Always return the $pieces array, whether modified or not.
    return $pieces;
}

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/advanced-ads/tokens/advads-pro-tokens.php:74

public function parse_advads_pro_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {

		if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
			return $value;
		}

		$trigger_meta_validations = apply_filters(
			'automator_advanced_ads_pro_validate_trigger_meta_pieces_common',
			array( 'AD_STATUS_CHANGED_CODE' ),
			array(
				'pieces'       => $pieces,
				'recipe_id'    => $recipe_id,
				'trigger_data' => $trigger_data,
				'user_id'      => $user_id,
				'replace_args' => $replace_args,
			)
		);

		if ( ! array_intersect( $trigger_meta_validations, $pieces ) ) {
			return $value;
		}

		$to_replace = $pieces[2];

		switch ( $to_replace ) {
			case 'AD_OLD_STATUS':
				$value = maybe_unserialize( Automator()->db->token->get( 'AD_OLD_STATUS', $replace_args ) );
				break;
		}

		return $value;
	}

Scroll to Top