<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations\Plugins;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class KinstaMUPlugins
 *
 * Plugin Name: Kinsta Must-use Plugins
 * Plugin URI: https://kinsta.com/knowledgebase/kinsta-mu-plugin/
 * Tested versions: 3.1.1
 */
class KinstaMUPlugins extends AbstractPlugin {
	public function init(): void {

		add_action(
			'fibofilters/indexer/updater/product_update/after',
			function ( $post_id ) {
				$this->purge_single_post_cache( $post_id );
			}
		);
		add_action(
			'fibofilters/indexer/updater/product_delete/after',
			function ( $post_id ) {
				$this->purge_single_post_cache( $post_id );
			}
		);

		add_action(
			'fibofilters/indexer/status/completed',
			function () {
				$this->purge_complete_site_cache();
			}
		);
	}

	private function purge_single_post_cache( $post_id ) {
		global $kinsta_muplugin;

		if ( isset( $kinsta_muplugin->kinsta_cache_purge ) && is_callable(
			[
				$kinsta_muplugin->kinsta_cache_purge,
				'initiate_purge',
			]
		) ) {
			$kinsta_muplugin->kinsta_cache_purge->initiate_purge( $post_id );
		}
	}

	private function purge_complete_site_cache() {
		global $kinsta_muplugin;

		if ( isset( $kinsta_muplugin->kinsta_cache_purge ) && is_callable(
			[
				$kinsta_muplugin->kinsta_cache_purge,
				'purge_complete_site_cache',
			]
		) ) {
			$kinsta_muplugin->kinsta_cache_purge->purge_complete_site_cache();
		}
	}
}
