<?php

/**
 * A template Plugin Compatibility class.
 *
 * This is used to demonstrate the various functionalities that are available
 * for third party plugins to make their plugin compatible with LiteSpeed Cache.
 *
 * The name of the class should start with LiteSpeed_Cache_ThirdParty_*.
 * You should replace the asterisk with the name of the plugin.
 * The file is named the same as the class, except with dashes, '-', instead of underscores, '_'
 * and with all lowercase letters.
 *
 */
/**
 * The Third Party integration with the _______ plugin.
 *
 * @since		1.0.x
 * @package		LiteSpeed_Cache
 * @subpackage	LiteSpeed_Cache/thirdparty
 * @author		LiteSpeed Technologies <info@litespeedtech.com>
 */
if (!defined('ABSPATH')) {
    die();
}

class LiteSpeed_Cache_ThirdParty_Plugin
{
    const CACHETAG_KIDS = 'MTPP_K.';
    const CACHETAG_GROUP = 'MTPP_G.';

    /**
     * Detect if the page requested is related to my plugin. If it is related,
     * I need to add my functions to the needed hooks.
     *
     */
    public static function detect()
    {
        if (isMyPluginEnabled()) {
            add_filter('litespeed_cache_is_cacheable', 'LiteSpeed_Cache_ThirdParty_Plugin::is_cacheable');
            add_action('litespeed_cache_on_purge_post', 'LiteSpeed_Cache_ThirdParty_Plugin::on_purge');
            add_action('litespeed_cache_add_purge_tags', 'LiteSpeed_Cache_ThirdParty_Plugin::add_purge_tags');
            add_action('litespeed_cache_add_cache_tags', 'LiteSpeed_Cache_ThirdParty_Plugin::add_cache_tags');
        }
    }

    /**
     * This filter is used to let the cache know if a page is cacheable.
     *
     * @param $cacheable true/false, whether a previous filter determined this page is cacheable or not.
     * @return true if cacheable, false if not.
     */
    public static function is_cacheable($cacheable)
    {
        if (!$cacheable) {
            return false;
        }

        global $myPluginsData;

        // An example use case is if a page should only be shown to a specific user.
        if ($myPluginsData->has_private_information()) {
            return false;
        }
        return true;
    }

    /**
     * This action is triggered when a page needs to be purged for whatever reason.
     * e.g. updating the post, deleting the post.
     * This is useful if your plugin needs to purge other, related pages.
     *
     * @param $post_id The id of the posts about to be purged.
     */
    public static function on_purge($post_id)
    {
        global $myPluginsData;

        $sister = $myPluginsData->get_sibling_post($myPluginsData->get_post($post_id));
        if ($sister) {
            LiteSpeed_Cache_Tags::add_purge_tags(self::CACHETAG_KIDS . $sister->id);
        }
    }

    /**
     * This action can be used to add last minute purge tags.
     * This example is purging all pages that are in a group when a page in that
     * group is requested.
     */
    public static function add_purge_tags()
    {
        global $myPluginsData;

        if ($myPluginsData->is_in_a_group()) {
            LiteSpeed_Cache_Tags::add_purge_tags(self::CACHETAG_GROUP . $myPluginsData->get_group_id());
        }
    }

    /**
     * This action can be used to add last minute cache tags.
     * This example is adding the plugin specific cache tags that can be used
     * to purge later if needed.
     */
    public static function add_cache_tags()
    {
        global $myPluginsData;

        if ($myPluginsData->is_a_child()) {
            LiteSpeed_Cache_Tags::add_purge_tags(self::CACHETAG_KIDS . $myPluginsData->get_id());
        }

        if ($myPluginsData->is_in_a_group()) {
            LiteSpeed_Cache_Tags::add_purge_tags(self::CACHETAG_GROUP . $myPluginsData->get_group_id());
        }
    }

}

add_action('litespeed_cache_detect_thirdparty', 'LiteSpeed_Cache_ThirdParty_Plugin::detect');



