<?php

namespace GemBoards\hooks;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // if accessed directly, exit


class Board {

	public static function init() {
		$self    = new self();
		$objects = [
			'card',
			'assignee',
		];
		foreach ( $objects as $object_type ) {
			add_action( "gemboards/$object_type/after/object_save", [ $self, 'clear_cache' ] );
			add_action( "gemboards/$object_type/deleted", [ $self, 'clear_cache' ] );
		}

		add_action( 'gemboards/sprint/after/object_save', [ $self, 'clear_cache_sprints' ] );
		add_action( 'gemboards/sprint/deleted', [ $self, 'clear_cache_sprints' ] );

		add_action( 'gemboards/task_checklist_group/after/object_save', [ $self, 'clear_cache_checklist_group' ] );
		add_action( 'gemboards/task_checklist_group/deleted', [ $self, 'clear_cache_checklist_group' ] );

		add_action( 'gemboards/task_checklist/after/object_save', [ $self, 'clear_cache_checklist' ] );
		add_action( 'gemboards/task_checklist/deleted', [ $self, 'clear_cache_checklist' ] );
	}

	public function clear_cache() {
		wp_cache_flush_group( 'gemboards_single_board_api' );
	}

	public function clear_cache_sprints() {
		wp_cache_flush_group( 'gemboards_pro_sprints' );
	}

	public function clear_cache_checklist_group() {
		wp_cache_flush_group( 'gemboards_pro_task_checklist_groups' );
	}

	public function clear_cache_checklist() {
		wp_cache_flush_group( 'gemboards_pro_task_checklists' );
	}

}
