<?php

/*
 Plugin Name: TIP Countdown Generator
 Plugin URI: https://www.themeinprogress.com/tip-countdown-generator
 Description: Create Evergreen and fixed-date countdown timers for offers, webinars, launches, and promotional campaigns with clear deadlines.
 Version: 1.0.1
 Text Domain: tip-countdown-generator
 Author: ThemeinProgress
 Author URI: https://www.themeinprogress.com
 License: GPL3
 Domain Path: /languages/

 Copyright 2026 ThemeinProgress  (email : support@wpinprogress.com)

 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.

 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, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

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

define( 'TIPCG_FOLDER', dirname(__FILE__) );
define( 'TIPCG_PLUGIN_FOLDER', plugins_url(false, __FILE__ ) );
define( 'TIPCG_OPTION_KEY', 'tipcg_settings' );
define( 'TIPCG_NAME', 'TIP Countdown Generator' );
define( 'TIPCG_VERSION', '1.0.1' );
define( 'TIPCG_TRACKING_OPTION_KEY', 'tipcg_countdown_tracking' );
define( 'TIPCG_TRACKING_CLEANUP_HOOK', 'tipcg_cleanup_tracking_data' );
define( 'TIPCG_UPGRADE_LINK', 'https://www.themeinprogress.com/tip-countdown-generator-pro/' );
define( 'TIPCG_SALE_PAGE', 'https://www.themeinprogress.com/tip-countdown-generator-pro/?ref=2&campaign=');

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Prevent direct access.
}
if( !class_exists( 'tipcg_init' ) ) {

	/**
	 * Bootstraps plugin lifecycle hooks, dependencies, front-end runtime and admin panel.
	 */
	class tipcg_init {

		/**
		 * Front-end runtime instance.
		 *
		 * @var tipcg_frontend|null
		 */
		protected $frontend = null;

		/**
		 * Constructor.
		 */
		public function __construct() {

			add_action( 'plugins_loaded', array( $this, 'plugin_setup' ) );
			add_action( 'init', array( $this, 'set_tracking_cookie' ) );
			add_action( 'init', array( $this, 'maybe_schedule_tracking_cleanup' ) );
			add_action( 'wp_enqueue_scripts', array( $this, 'site_scripts' ) );
			add_shortcode( 'tipcg_countdown', array( $this, 'add_shortcode' ) );
			add_action( 'wp_loaded', array( $this, 'plugin_panel' ) );
			add_action( TIPCG_TRACKING_CLEANUP_HOOK, array( $this, 'cleanup_tracking_data' ) );
			add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ), 10, 2 );

		}

		/**
		 * Schedule tracking cleanup on plugin activation.
		 */
		public static function activate() {
			if ( ! wp_next_scheduled( TIPCG_TRACKING_CLEANUP_HOOK ) ) {
				wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', TIPCG_TRACKING_CLEANUP_HOOK );
			}
		}

		/**
		 * Clear tracking cleanup on plugin deactivation.
		 */
		public static function deactivate() {
			wp_clear_scheduled_hook( TIPCG_TRACKING_CLEANUP_HOOK );
		}

		/**
		 * Loads plugin dependencies.
		 */
		public function plugin_setup() {

			require_once TIPCG_FOLDER . '/core/functions/tipcg_functions.php';
			require_once TIPCG_FOLDER . '/core/includes/class-form.php';
			require_once TIPCG_FOLDER . '/core/includes/class-panel.php';
			require_once TIPCG_FOLDER . '/core/includes/class-tracking.php';
			require_once TIPCG_FOLDER . '/core/includes/class-frontend.php';
			require_once TIPCG_FOLDER . '/core/includes/class-notice.php';

			$this->frontend = new tipcg_frontend();

		}

		/**
		 * Returns the front-end runtime instance.
		 *
		 * @return tipcg_frontend
		 */
		protected function frontend() {

			if ( $this->frontend instanceof tipcg_frontend ) {
				return $this->frontend;
			}

			require_once TIPCG_FOLDER . '/core/includes/class-tracking.php';
			require_once TIPCG_FOLDER . '/core/includes/class-frontend.php';

			$this->frontend = new tipcg_frontend();

			return $this->frontend;

		}

		/**
		 * Ensures the tracking cookie exists.
		 */
		public function set_tracking_cookie() {
			$this->frontend()->set_tracking_cookie();
		}

		/**
		 * Ensures the cleanup cron is scheduled.
		 */
		public function maybe_schedule_tracking_cleanup() {
			$this->frontend()->maybe_schedule_tracking_cleanup();
		}

		/**
		 * Cleans stale tracking records.
		 */
		public function cleanup_tracking_data() {
			$this->frontend()->cleanup_tracking_data();
		}

		/**
		 * Renders the countdown shortcode.
		 *
		 * @param array $atts Shortcode attributes.
		 *
		 * @return string Rendered shortcode output.
		 */
		public function add_shortcode( $atts ) {
			return $this->frontend()->add_shortcode( $atts );
		}

		/**
		 * Enqueues front-end styles and scripts.
		 */
		public function site_scripts() {
			$this->frontend()->site_scripts();
		}

		/**
		 * Adds plugin action links.
		 *
		 * @param array $links Existing plugin action links.
		 *
		 * @return array Updated plugin action links.
		 */
		public function plugin_action_links( $links ) {

			$links[] = '<a href="' . esc_url( get_admin_url( null, 'admin.php?page=tipcg_panel' ) ) . '">' . esc_html__( 'Settings', 'tip-countdown-generator' ) . '</a>';

			return $links;

		}

		/**
		 * Loads the plugin admin panel.
		 */
		public function plugin_panel() {

			if ( is_admin() ) {
				require_once TIPCG_FOLDER . '/core/admin/panel.php';
			}

		}

	}

	register_activation_hook( __FILE__, array( 'tipcg_init', 'activate' ) );
	register_deactivation_hook( __FILE__, array( 'tipcg_init', 'deactivate' ) );

	new tipcg_init();

}

?>
