<?php
/**
 * Plugin Name: WooCommerce Subscriptions
 * Plugin URI: https://www.woocommerce.com/products/woocommerce-subscriptions/
 * Description: Sell products and services with recurring payments in your WooCommerce Store.
 * Author: WooCommerce
 * Author URI: https://woocommerce.com/
 * Version: 8.8.1
 * Requires Plugins: woocommerce
 *
 * Requires at least: 6.9
 * Tested up to: 7.0
 * Requires PHP: 7.4
 *
 * WC requires at least: 10.7
 * WC tested up to: 10.8
 *
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 * Copyright: © 2025 WooCommerce
 *
 * Woo: 27147:6115e6d7e297b623a169fdcf5728b224
 *
 * @package WooCommerce Subscriptions
 * @author  WooCommerce.
 * @since   1.0
 */

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/includes/class-wc-subscriptions-dependency-manager.php';
$dependency_manager = new WC_Subscriptions_Dependency_Manager( WC_Subscriptions::$wc_minimum_supported_version );

// Check the dependencies before loading the plugin. If the dependencies are not met, display an admin notice and exit
if ( ! $dependency_manager->has_valid_dependencies() ) {
	add_action( 'admin_notices', [ $dependency_manager, 'display_dependency_admin_notice' ] );
	return;
}

/**
 * Declare plugin compatibility with WooCommerce HPOS.
 *
 * @since 4.9.0
 */
add_action(
	'before_woocommerce_init',
	function() {
		if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
		}
	}
);

add_action(
	'woocommerce_init',
	function () {
		// Overrides the WooCommerce version to the WC()->version.
		// This prevents the wrong version being stored in the transient during a WooCommerce core downgrade.
		set_transient( 'wcs_woocommerce_active_version', WC()->version, HOUR_IN_SECONDS );
	}
);

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

/**
 * The main subscriptions class.
 *
 * @since 1.0
 */
class WC_Subscriptions {
	/**
	 * Name of the option field used to store the active plugin version. This is principally
	 * used in support of plugin update logic.
	 */
	public const PLUGIN_VERSION_OPTION_NAME = 'wcs_plugin_version';

	/** @var string */
	public static $name = 'subscription';

	/** @var string */
	public static $activation_transient = 'woocommerce_subscriptions_activated';

	/** @var string */
	public static $plugin_file = __FILE__;

	/** @var string */
	public static $version = '8.8.1'; // WRCS: DEFINED_VERSION.

	/** @var string */
	public static $wc_minimum_supported_version = '7.7';

	/** @var WCS_Cache_Manager */
	public static $cache;

	/**
	 * Set up the class, including it's hooks & filters, when the file is loaded.
	 *
	 * @since 1.0
	 * @since 8.8.0 The $autoloader parameter is no longer used; classes are loaded via Composer.
	 *
	 * @param mixed $autoloader Unused. Retained for backwards compatibility.
	 */
	public static function init( $autoloader = null ) {
		$plugin      = new WC_Subscriptions_Plugin( $autoloader );
		self::$cache = $plugin->cache;
	}

	/*
	 * Plugin House Keeping
	 */

	/**
	 * Called when WooCommerce is inactive or running and out-of-date version to display an inactive notice.
	 *
	 * @deprecated 5.0.0
	 *
	 * @since 1.2
	 */
	public static function woocommerce_inactive_notice() {
		_deprecated_function( __METHOD__, '5.0.0', 'WC_Subscriptions_Dependency_Manager::display_dependency_admin_notice' );
		$dependency_manager = new WC_Subscriptions_Dependency_Manager( WC_Subscriptions::$wc_minimum_supported_version );
		$dependency_manager->display_dependency_admin_notice();
	}

	/* Deprecated Functions */

	/**
	 * Handle deprecation function calls.
	 *
	 * @since 4.0.0
	 *
	 * @param string $method    The name of the method being called.
	 * @param array  $arguments An array containing the parameters passed to the method.
	 *
	 * @return void|mixed The value returned from a deprecated function replacement or null.
	 */
	public static function __callStatic( $method, $arguments ) {
		static $deprecation_handler = null;

		// Initialise the handler if we dont have one already.
		if ( ! $deprecation_handler ) {
			$deprecation_handler = new WC_Subscriptions_Deprecation_Handler();
		}

		if ( $deprecation_handler->is_deprecated( $method ) ) {
			$deprecation_handler->trigger_notice( $method );

			return $deprecation_handler->call_replacement( $method, $arguments );
		} else {
			// Trigger an error consistant with PHP if the function called doesn't exist.
			$class = __CLASS__;
			$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 );
			$file  = $trace[0]['file'];
			$line  = $trace[0]['line'];
			throw new Error( "Call to undefined method $class::$method() in $file on line $line" ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}
	}
}

/**
 * Add woocommerce_inbox_variant for the Remote Inbox Notification.
 *
 * P2 post can be found at https://wp.me/paJDYF-1uJ.
 */
if ( ! function_exists( 'add_woocommerce_inbox_variant' ) ) {
	function add_woocommerce_inbox_variant() {
		$config_name = 'woocommerce_inbox_variant_assignment';
		if ( false === get_option( $config_name, false ) ) {
			update_option( $config_name, wp_rand( 1, 12 ) );
		}
	}
}
add_action( 'woocommerce_subscriptions_upgraded', 'add_woocommerce_inbox_variant', 10 );
register_activation_hook( __FILE__, 'add_woocommerce_inbox_variant' );

WC_Subscriptions::init();
