Advanced Ads Pro comes with Passive Cache-Busting, a performance-optimized method to deliver ads on cached websites. It basically performs the checks needed to choose the correct ad for a visitor using JavaScript. This includes most Visitor Conditions.

While it is rather simple to extend Visitor Conditions with PHP based checks by just looking at the basic code, it is more complex to add the appropriate JavaScript code.

Find a full example on how to add a new Visitor Condition including a JavaScript-based check for passive cache-busting below.

Please note that this code needs adjustments. Only developers should use it and we can’t help customizing it. If you find an obvious issue then please let us know.

// Add new visitor condition.
add_filter( 'advanced-ads-visitor-conditions', function( $conditions ) {
    $conditions['test_condition'] = array(
        'label' => __( 'test', 'advanced-ads' ),
        'description' => __( 'test description', 'advanced-ads' ),
        'metabox' => array( 'Test_Visitor_Conditions', 'metabox' ), // callback to generate the metabox
        'check' => array( 'Test_Visitor_Conditions', 'check' ) // callback for frontend check
    );
    return $conditions;
} );

// Implement the condition options on the ad edit page
class Test_Visitor_Conditions {
    static function metabox( $options, $index = 0 ) {

	    if ( ! isset ( $options['type'] ) || '' === $options['type'] ) { return; }

    	$type_options = Advanced_Ads_Visitor_Conditions::get_instance()->conditions;

	    if ( ! isset( $type_options[ $options['type'] ] ) ) {
    	    return;
	    }

    	// form name basis
	    $name = Advanced_Ads_Visitor_Conditions::FORM_NAME . '[' . $index . ']';
    	// options
	    $value = isset( $options['value']  ) ? $options['value'] : '' ;

    	?><input type="text" name="<?php echo $name; ?>[value]" value="<?php echo $value; ?>"/>
        <input type="hidden" name="<?php echo $name; ?>[type]" value="<?php echo $options['type']; ?>"/>
        <p class="description"><?php echo $type_options[ $options['type'] ]['description']; ?></p>
        <?php
    }

	// Checks with PHP if the condition is true in the frontend when no or AJAX cache-busting is used
    static function check( $options = array() ) {
        if ( isset( $options['value'] ) && $options['value'] === 'test string' ) {
            return true;
        }

        return false;
    }

}

// Notify passive cache-busting that this condition can be checked using JavaScript.
add_filter( 'advanced-ads-js-visitor-conditions', function( $conditions ) {
    $conditions[] = 'test_condition';
    return $conditions;
} );

// Add a check using JavaScript.
add_action( 'wp_footer', function() { ?>
<script>
jQuery( document ).on( 'advads-passive-cb-conditions', function( e, Advads_passive_cb_Conditions ) {
    Advads_passive_cb_Conditions.conditions['test_condition'] = function ( options, ad ) {
        return options.value === 'test string'
    };
});
</script>
<?php
} );