<?php
/*
 * Developed By Devozon
 *
 * Check Out at https://devozon.com/
 * Email: support@devozon.com
 * Message: https://www.facebook.com/devozon
 *
 * Please feel free to contact me at any time if you need a custom script or any development work.
 *
 * Devozon Team:
 * Coded by Sanad Nabil
 * Theme by Ahmed Reda
 * Tested By Smith Carter
 *
 * Devozon All rights reserved
 * Copyright (c) 2023-2025.
 * Last Update 22 \ 6 \ 2025
 */

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

class FENSE_BPVT_STATS
{
	public string $dbKey = 'bpvt_stats_';
	public string $Key;
	public object $post;
	public object $DB;
	public bool $ran = false;
	public string $url = '';
	
	public function __construct()
	{
		$this->DB = new stdClass();
		$this->post = new stdClass();
		
		/**  Start STATS CLASS */
		$this->init();
	}
	
	
	public function init()
	{
		/**  Load Once */
		if ( $this->ran ) return null;
		
		/**  If Not Enabled Escape */
		if ( !$this->__isEnabled() ) return null;
		
		/**  Set Data */
		$this->post = $GLOBALS['post'] ?? $this->post;
		$this->url = get_permalink() ?? '';
		
		/**  Unique */
		if ( $this->__getCookie() ) return;
		
		/**  Choose Type */
		$this->Type();
	}
	
	public function Type(): bool
	{
		/**  Generate Data */
		$this->__generate();
		
		/**  Assign Key */
		$this->__Key();
		
		return $this->Save();
	}
	
	public function Save(): bool
	{
		// Get current transient
		$transient = get_transient( $this->Key );
		
		$visits = $transient->visits ?? 0;
		
		$this->DB->visits = $visits + 1;
		
		set_transient( $this->Key, $this->DB, DAY_IN_SECONDS );
		
		$this->__setCookie();
		
		$this->ran = true;
		return true;
	}
	
	public function __Key(): void
	{
		$key = md5( $this->url );
		$this->Key = $this->dbKey . $key;
	}
	
	public function __setCookie(): void
	{
		// set for 24 hours
		setcookie(
			'bpvt_st_visit',          // name
			true,                // value
			time() + DAY_IN_SECONDS,
			COOKIEPATH,            // path (WP constant)
			COOKIE_DOMAIN,         // domain (WP constant)
			is_ssl(),              // secure
			true                   // httpOnly
		);
	}
	
	public function __getCookie()
	{
		return $_COOKIE['bpvt_st_visit'] ?? null;
	}
	
	public function __generate()
	{
		$this->DB->id = $this->post->ID ?? null;
		$this->DB->title = $this->post->post_title ?? null;
		$this->DB->url = $this->url ?? null;
	}
	
	public function __DIE( $data ): bool
	{
		wp_die( '<pre>' . htmlspecialchars( print_r( $data, true ) ) . '</pre>' );
	}
	
	public function __isEnabled(): bool
	{
		if ( $this->__isLoginPage() ) return true;
		if ( !doing_action( 'wp' ) || $this->__isAjaxCall() || $this->__isAdminOrLoggedIn() ) return false;
		
		$stats = esc_attr( get_option(  'FENSE_BPVT_DEVOZON_enable_stats' ) );
		if (empty( $stats )) return false;
		
		return true;
	}
	
	public function __isLoginPage(): bool
	{
		if ( isset( $GLOBALS['pagenow'] ) && $GLOBALS['pagenow'] === 'wp-login.php' && doing_action( 'login_init' ) ) {
			return true;
		}
		
		return false;
	}
	
	public function __isAjaxCall(): bool
	{
		if ( function_exists( 'DOING_AJAX' ) && defined( 'DOING_AJAX' ) ) {
			return true;
		}
		
		return false;
	}
	
	public function __isAdminOrLoggedIn(): bool
	{
		if ( is_admin() || is_user_logged_in() ) {
			return true;
		}
		
		return false;
	}
}
