<?php

/********************************************************************
 * Copyright (C) 2025 Hassine RIANI (https://upvimeo.com)
 *
 * This file is part of Upvimeo
 *
 * Upvimeo 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 2 of the License, or
 * (at your option) any later version.
 *
 * Upvimeo 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 Upvimeo. If not, see <https://www.gnu.org/licenses/>.
 **********************************************************************/

/**
 * Class UpVimeo_Flash
 */
class UpVimeo_Flash
{

	/**
	 * Returns the current url
	 *
	 * @param  string  $signature
	 *
	 * @return string
	 */
	public function get_current_url($signature = '')
	{
		$req_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
		$uri = str_replace('/wp-admin/', '', $req_uri);
		$url = admin_url($uri);

		if (! empty($signature)) {
			$url = add_query_arg('h', $signature, $url);
		}

		return remove_query_arg(array(
			'_wpnonce',
			'_wp_http_referer',
			'action',
			'action2',
			'record_id',
			'filter_action',
		), $url);
	}

	/**
	 * Flash message
	 *
	 * @param $message
	 * @param $type
	 *
	 * @return string
	 */
	public function flash_message($message, $type)
	{
		$signature = 'upvimeo_flash_' . md5('UPVIMEO_flash_' . get_current_user_id());
		set_transient($signature, array('message' => $message, 'type' => $type), HOUR_IN_SECONDS);

		return $signature;
	}

	/**
	 * Unflash message
	 * @return array
	 */
	public function unflash_message()
	{
		$signature = $this->get_flash_signature();
		$notice    = get_transient($signature);
		delete_transient($signature);

		return $notice;
	}

	/**
	 * Returns the flash signature
	 * @return string
	 */
	public function get_flash_signature()
	{
		return 'upvimeo_flash_' . md5('UPVIMEO_flash_' . get_current_user_id());
	}
}
