<?php
/**
 * Plugin Name: WooCommerce Bookings
 * Requires Plugins: woocommerce
 * Plugin URI: https://woocommerce.com/products/woocommerce-bookings/
 * Description: Setup bookable products such as for reservations, services and hires.
 * Version: 3.7.0
 * Author: WooCommerce
 * Author URI: https://woocommerce.com
 * Text Domain: woocommerce-bookings
 * Domain Path: /languages
 * Tested up to: 7.0
 * Requires at least: 6.6
 * WC tested up to: 10.9
 * WC requires at least: 9.8
 * PHP tested up to: 8.4
 * Requires PHP: 7.4
 *
 * Copyright: © 2026 WooCommerce
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Woo: 390890:911c438934af094c2b38d5560b9f50f3
 *
 * @package WooCommerce Bookings
 */

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

use WooCommerce\Bookings\Blocks\WC_Bookings_Blocks_Integration;

// phpcs:disable WordPress.Files.FileName

/**
 * WooCommerce fallback notice.
 *
 * @since 1.13.0
 */
function woocommerce_bookings_missing_wc_notice() {
	/* translators: %s WC download URL link. */
	echo '<div class="error"><p><strong>' . sprintf( esc_html__( 'Bookings requires WooCommerce to be installed and active. You can download %s here.', 'woocommerce-bookings' ), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>' ) . '</strong></p></div>';
}

if ( ! defined( 'WC_BOOKINGS_ABSPATH' ) ) {
	define( 'WC_BOOKINGS_ABSPATH', dirname( __FILE__ ) . '/' );
}

/**
 * Option key name for triggering activation notices.
 *
 * @since 1.14.4
 */
if ( ! defined( 'WC_BOOKINGS_ACTIVATION_NOTICE_KEY' ) ) {
	define( 'WC_BOOKINGS_ACTIVATION_NOTICE_KEY', 'woocommerce_bookings_show_activation_notice' );
}

if ( ! class_exists( 'WC_Bookings' ) ) :

	define( 'WC_BOOKINGS_VERSION', '3.7.0' ); // WRCS: DEFINED_VERSION.
	define( 'WC_BOOKINGS_DB_VERSION', '1.14.6' );
	define( 'WC_BOOKINGS_TEMPLATE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
	define( 'WC_BOOKINGS_PLUGIN_URL', untrailingslashit( plugins_url( '/', __FILE__ ) ) );
	define( 'WC_BOOKINGS_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
	define( 'WC_BOOKINGS_MAIN_FILE', __FILE__ );
	define( 'WC_BOOKINGS_GUTENBERG_EXISTS', function_exists( 'register_block_type' ) ? true : false );
	if ( ! defined( 'WC_BOOKINGS_CONNECT_WOOCOMMERCE_URL' ) ) {
		define( 'WC_BOOKINGS_CONNECT_WOOCOMMERCE_URL', 'https://api.woocommerce.com/integrations' );
	}

	if ( ! defined( 'WC_BOOKINGS_DEBUG' ) ) {
		define( 'WC_BOOKINGS_DEBUG', false );
	}

	/**
	 * WC Bookings class
	 */
	class WC_Bookings {
		/**
		 * The single instance of the class.
		 *
		 * @var $_instance
		 * @since 1.13.0
		 */
		protected static $_instance = null; // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore

		/**
		 * Constructor.
		 *
		 * @since 1.13.0
		 */
		public function __construct() {
			$this->includes();
			WC_Bookings_Install::init();
			$this->init_hooks();

			add_action( 'plugins_loaded', array( $this, 'woocommerce_bookings_init' ), 10 );
		}

		/**
		 * Initialize activation and deactivation hooks.
		 */
		public function init_hooks() {
			register_activation_hook( __FILE__, array( $this, 'activate' ) );
			register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
		}

		/**
		 * Init WooCommerce Bookings when plugins are loaded.
		 *
		 * @since 1.13.1
		 */
		public function woocommerce_bookings_init() {
			if ( ! class_exists( 'WooCommerce' ) ) {
				add_action( 'admin_notices', 'woocommerce_bookings_missing_wc_notice' );
				return;
			}

			$this->wpdb_table_fix();
			$this->init();

			// Declare compatibility with High-Performance Order Storage.
			add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
		}

		/**
		 * Declare compatibility with High-Performance Order Storage.
		 *
		 * @since 1.15.66
		 */
		public function declare_hpos_compatibility() {
			if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
			}
		}

		/**
		 * Main Bookings Instance.
		 *
		 * Ensures only one instance of Bookings is loaded or can be loaded.
		 *
		 * @since 1.13.0
		 * @return WC_Bookings
		 */
		public static function instance() {
			if ( is_null( self::$_instance ) ) {
				self::$_instance = new self();
			}

			return self::$_instance;
		}

		/**
		 * Cloning is forbidden.
		 *
		 * @since 1.13.0
		 */
		public function __clone() {
			wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'woocommerce-bookings' ), WC_BOOKINGS_VERSION );
		}

		/**
		 * Unserializing instances of this class is forbidden.
		 *
		 * @since 1.13.0
		 */
		public function __wakeup() {
			wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce-bookings' ), WC_BOOKINGS_VERSION );
		}

		/**
		 * Cleanup on plugin deactivation.
		 *
		 * @since 1.11
		 */
		public function deactivate() {
			if ( class_exists( 'WC_Admin_Notes' ) ) {
				WC_Bookings_Inbox_Notice::remove_activity_panel_inbox_notes();
			} else {
				WC_Admin_Notices::remove_notice( 'woocommerce_bookings_activation' );
			}
		}

		/**
		 * Activation hook.
		 */
		public function activate() {
			if ( ! class_exists( 'WooCommerce' ) ) {
				add_action( 'admin_notices', 'woocommerce_bookings_missing_wc_notice' );
				return;
			}

			// Flag to trigger activation notice after includes have been loaded.
			update_option( WC_BOOKINGS_ACTIVATION_NOTICE_KEY, true );

			// Register the rewrite endpoint before permalinks are flushed.
			$endpoint = get_option( 'woocommerce_myaccount_bookings_endpoint', 'bookings' );
			add_rewrite_endpoint(
			/**
			 * Filter the rewrite endpoint for the bookings page.
			 *
			 * @since 1.13.0
			 */
				apply_filters( 'woocommerce_bookings_account_endpoint', $endpoint ),
				EP_PAGES
			);

			WC_Bookings_Install::maybe_install();

			// Flush Permalinks.
			flush_rewrite_rules();
		}

		/**
		 * Load Classes.
		 *
		 * @throws Exception When composer install hasn't been ran.
		 */
		public function includes() {
			$loader = require WC_BOOKINGS_ABSPATH . 'lib/packages/autoload.php';

			if ( ! $loader ) {
				throw new Exception( 'vendor/autoload.php missing please run `composer install`' );
			}

			require_once WC_BOOKINGS_ABSPATH . 'includes/wc-bookings-functions.php';
		}

		/**
		 * Init all the classes.
		 */
		private function init() {
			// Cache.
			new WC_Bookings_Cache();

			// Initialize.
			new WC_Bookings_Core_Compatibility();
			new WC_Bookings_Init();
			WC_Bookings_Timezone_Settings::instance();

			// Order compatibility class.
			new WC_Booking_Order_Compat();

			// WC AJAX.
			new WC_Bookings_WC_Ajax();

			WC_Booking_Form_Handler::init();
			WC_Booking_Order_Manager::instance();
			new WC_Product_Booking_Manager();
			new WC_Booking_Cron_Manager();
			WC_Bookings_Google_Calendar_Connection::instance();
			new WC_Booking_Coupon();

			if ( class_exists( 'WC_Product_Addons' ) ) {
				new WC_Bookings_Addons();
			}
			if ( class_exists( 'WC_Deposits' ) ) {
				new WC_Bookings_Deposits();
			}
			if ( class_exists( '\WooCommerce\PayPalCommerce\PluginModule' ) ) {
				new WC_Bookings_PayPal_Payments();
			}

			if ( class_exists( 'WC_Abstract_Privacy' ) ) {
				new WC_Booking_Privacy();
			}

			new WC_Booking_Email_Manager();
			WC_Booking_Cart_Manager::get_instance();
			new WC_Booking_Checkout_Manager();
			new WC_Bookings_REST_API();
			new WC_Booking_Store_API();
			new WC_Bookings_Product_Rest_API();

			if ( is_admin() ) {
				new WC_Bookings_Menus();
				new WC_Bookings_Templates();
				new WC_Bookings_Report_Dashboard();
				new WC_Bookings_WC_Status_Dashboard();
				new WC_Bookings_Admin();
				new WC_Bookings_Ajax();
				new WC_Bookings_Admin_Add_Ons();
				new WC_Booking_Products_Export();
				new WC_Booking_Products_Import();
				new WC_Bookings_Tracks();
				new WC_Bookings_Single_Export();
				new WC_Bookings_Admin_Exporters();
				new WC_Bookings_Admin_Importers();
			}

			WC_Bookings_Encryption::instance();

			add_filter( 'product_type_selector', array( $this, 'product_type_selector' ) );
			add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
			add_action( 'init', array( $this, 'show_activation_notice' ), 10 );

			if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
				new WC_Bookings_Blocks_Controller();
			}
		}

		/**
		 * Add the booking product type
		 *
		 * @param array $types - Array of product types.
		 * @return array
		 */
		public function product_type_selector( $types ) {
			$types['booking'] = __( 'Bookable product', 'woocommerce-bookings' );
			return $types;
		}

		/**
		 * Need to correct table names for meta to work.
		 */
		public function wpdb_table_fix() {
			global $wpdb;
			$wpdb->bookings_availabilitymeta = $wpdb->prefix . 'wc_bookings_availabilitymeta';
			$wpdb->tables[]                  = 'wc_bookings_availabilitymeta';
		}

		/**
		 * Shows admin notice after activation.
		 *
		 * Notices are triggered by a flag in options so they can be triggered once on activation
		 * and then actually shown once all necessary resources have been loaded.
		 *
		 * @since 1.14.4
		 */
		public function show_activation_notice() {
			if ( false !== get_option( WC_BOOKINGS_ACTIVATION_NOTICE_KEY ) ) {
				delete_option( WC_BOOKINGS_ACTIVATION_NOTICE_KEY );
				if ( class_exists( 'WC_Admin_Notes' ) ) {
					WC_Bookings_Inbox_Notice::add_activity_panel_inbox_welcome_note();
				} else {
					$notice_html = '<strong>' . esc_html__( 'WooCommerce Bookings has been activated!', 'woocommerce-bookings' ) . '</strong><br><br>';
					/* translators: 1: href link to list of bookings, 2: booking post type link */
					$notice_html .= sprintf( __( '<a href="%1$s">Add or edit a product</a> to manage bookings in the Product Data section for individual products and then go to the <a href="%2$s" target="_blank">Bookings page</a> to manage them individually.', 'woocommerce-bookings' ), admin_url( 'post-new.php?post_type=product&bookable_product=1' ), admin_url( 'edit.php?post_type=wc_booking' ) );
					WC_Admin_Notices::add_custom_notice( 'woocommerce_bookings_activation', $notice_html );
				}
			}
		}

		/**
		 * Load the plugin text domain for translation.
		 */
		public function load_plugin_textdomain() {
			load_plugin_textdomain( 'woocommerce-bookings', false, plugin_basename( __DIR__ ) . '/languages' );
		}
	}
endif;

$GLOBALS['wc_bookings'] = WC_Bookings::instance();

// Subscribe to automated translations.
add_filter( 'woocommerce_translations_updates_for_woocommerce-bookings', '__return_true' );

/**
 * Gets block-based features initialized.
 */
add_action(
	'woocommerce_blocks_loaded',
	function() {
		if ( WC_BOOKINGS_GUTENBERG_EXISTS ) {
			require_once WC_BOOKINGS_ABSPATH . 'includes/blocks/class-wc-bookings-blocks.php';
			new WC_Bookings_Blocks_Integration();
		};
	}
);
