<?php

namespace Fab\Plugin\Helper;

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

/**
 * Transient
 *
 * @package    Fab
 * @subpackage Fab/Plugin/Helper
 */
trait Transient {

    /**
     * Transient Name.
     *
     * @var string
     */
    protected $transient_name;

    /**
     * Transient Expiration.
     *
     * @var int
     */
    protected $transient_expiration;

    /**
     * Get transient.
     *
     * @return int
     */
    public function get_transient() {
        return get_transient( $this->transient_name );
    }

    /**
     * Set transient.
     *
     * @param string $data Data to save.
     * @return void
     */
    public function set_transient( $data ) {
        set_transient( $this->transient_name, $data, $this->transient_expiration );
    }

    /**
     * Delete transient.
     *
     * @return void
     */
    public function delete_transient() {
        delete_transient( $this->transient_name );
    }

    /**
     * Reset all FAB visit-related cookies immediately.
     *
     * @param string $reason Reason to include in debug log.
     * @return void
     */
    public function reset_visit_cookies( $reason = '' ) {
        foreach ( array( 'fab_visited_posts', 'fab_visit_page_counts', 'fab_visit_page_last' ) as $cookie_name ) {
            setcookie( $cookie_name, '', time() - 3600, '/' );
            unset( $_COOKIE[ $cookie_name ] );
        }
        if ( ! empty( $reason ) ) {
            debug_log( sprintf( '[FAB] reset_visit_cookies: done. reason=%s', $reason ? $reason : 'n/a' ) );
        }
    }
}
