<?php
/**
 * Name:    Dev4Press\v55\Core\Cache\UserTransient
 * Version: v5.5
 * Author:  Milan Petrovic
 * Email:   support@dev4press.com
 * Website: https://www.dev4press.com/
 *
 * @package Dev4PressLibrary
 *
 * == Copyright ==
 * Copyright 2008 - 2026 Milan Petrovic (email: support@dev4press.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 */

namespace Dev4Press\v55\Core\Cache;

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

class UserTransient {
	protected function __construct() {
	}

	/** @deprecated 5.5.0 Use self::i() instead. */
	public static function instance() : static {
		return static::i();
	}

	public static function i() : static {
		static $instance = null;

		if ( ! isset( $instance ) ) {
			$instance = new static();
		}

		return $instance;
	}

	public function delete( $user_id, $transient ) : void {
		$transient_option  = '_transient_' . $transient;
		$transient_timeout = '_transient_timeout_' . $transient;

		delete_user_meta( $user_id, $transient_option );
		delete_user_meta( $user_id, $transient_timeout );
	}

	public function get( $user_id, $transient ) {
		$transient_option  = '_transient_' . $transient;
		$transient_timeout = '_transient_timeout_' . $transient;

		if ( get_user_meta( $user_id, $transient_timeout, true ) < time() ) {
			delete_user_meta( $user_id, $transient_option );
			delete_user_meta( $user_id, $transient_timeout );

			return false;
		}

		return get_user_meta( $user_id, $transient_option, true );
	}

	public function set( $user_id, $transient, $value, $expiration = 86400 ) : void {
		$transient_option  = '_transient_' . $transient;
		$transient_timeout = '_transient_timeout_' . $transient;

		if ( get_user_meta( $user_id, $transient_option, true ) != '' ) {
			delete_user_meta( $user_id, $transient_option );
			delete_user_meta( $user_id, $transient_timeout );
		}

		add_user_meta( $user_id, $transient_timeout, time() + $expiration, true );
		add_user_meta( $user_id, $transient_option, $value, true );
	}
}
