Skip to content

Instantly share code, notes, and snippets.

@tnog
Created April 27, 2021 20:11
Show Gist options
  • Save tnog/43b4d4f415dd908af95bcbe7bfddbd88 to your computer and use it in GitHub Desktop.
Save tnog/43b4d4f415dd908af95bcbe7bfddbd88 to your computer and use it in GitHub Desktop.
<?php
namespace To4Framework;
if (!defined('ABSPATH')) {
exit;
}
class CachingAds extends Caching
{
/**
* Static property to hold our singleton instance
*
*/
static $instance = false;
/**
* Custom constructor using static method
* @see https://carlalexander.ca/designing-class-wordpress-hooks/
* @return void
*/
public static function init()
{
$self = new self();
add_action('publish_advanced_ads', [$self, 'purgeAfterAdsUpdate'], 20, 2);
add_action('delete_post', [$self, 'purgeAfterAdsUpdate'], 20, 2);
add_filter('advanced-ads-update-placements', [$self, 'purgeAfterAdsPlacementUpdate'], 20);
add_filter('edited_advanced_ads_groups', [$self, 'purgeAfterAdGroupUpdate'], 20, 2);
add_filter('created_advanced_ads_groups', [$self, 'purgeAfterAdGroupUpdate'], 20, 2);
add_filter('delete_advanced_ads_groups', [$self, 'purgeAfterAdGroupDeletion'], 20, 4);
}
/**
* Trigger global cache clear when advanced ads banner is updated or published.
* @param int $post_id
* @param object $post
* @uses cache_flush() Clears entire WPE cache: Memcache, Varnish, MaxCDN
*/
public function purgeAfterAdsUpdate($post_id, $post) {
$screen = \get_current_screen();
$postType = get_post_type( $post_id );
if('advanced_ads' === $postType && 'advanced_ads' === $screen->id) {
parent::globalCacheFlush('Cache purge triggered after AA ad update.', false);
}
}
/**
* Trigger global cache clear when Advanced Ads placement is updated or created.
* @param mixed $success
* @return mixed $success
*/
public function purgeAfterAdsPlacementUpdate($success) {
// Check if $success has a value
if( isset($success) ) {
parent::globalCacheFlush('Cache purge triggered after AA placement update.', false);
}
return $success;
}
/**
* Trigger global cache clear when Advanced Ads group ad is updated or created
* @see https://developer.wordpress.org/reference/hooks/created_taxonomy/
* @see https://developer.wordpress.org/reference/hooks/edited_taxonomy/
* @param int $term_id
* @param int $tt_id
*/
public function purgeAfterAdGroupUpdate($term_id, $tt_id) {
parent::globalCacheFlush('Cache purge triggered after AA ad group update.', false);
}
/**
* Trigger global cache clear when Advanced Ads group ad is updated or created
* @see https://developer.wordpress.org/reference/hooks/delete_taxonomy/
* @param int $term_id
* @param int $tt_id
* @param WP_Term $deleted_term
* @param array $object_ids
*/
public function purgeAfterAdGroupDeletion($term_id, $tt_id, $deleted_term, $object_ids) {
parent::globalCacheFlush('Cache purge triggered after AA ad group deletion.', false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment