<?php
/*
Plugin Name: LearnDash - Integration with Aelia Currency Switcher (PROOF OF CONCEPT)
Plugin URI: https://aelia.co/
Description: This plugin tries to pass the LearnDash "course" product to the Aelia Currency Switcher for conversion.
Version: 1.0.0
Author: Aelia
Author URI: https://aelia.co
License: GPLv3
*/

/**
 * GPL DISCLAIMER
 * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
 * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
 * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
 * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
 */

/**
 * == Changelog ==
 *
 * = 1.0.0 =
 * First release, 04 April 2022.
 */

/**
 * Experimental integration with LearnDash. This plugin tries to pass the LearnDash "course" product to
 * the Aelia Currency Switcher for conversion.
 *
 * IMPORTANT
 * This plugin is to be intended as the proof of concept to show the LeadnDash developer how they could easily
 * implement a basic integration  with the Aelia Currency Switcher. It's not meant to be a guarnatee of compatibility,
 * and we cannot provide any support for it.
 */
class WC_LearnDash_Aelia_Currency_Switcher_Integration {
	/**
	 * The singleton instance of this class.
	 *
	 * @var WC_LearnDash_Aelia_Currency_Switcher_Integration
	 */
	protected static $_instance;

	/**
	 * The product type used by LearnDash.
	 *
	 * @var string
	 */
	const PRODUCT_TYPE = 'course';

	/**
	 * The text domain used by the plugin.
	 *
	 * @var string
	 */
	protected static $text_domain = 'wc-aelia-learndash-integration';

	/**
	 * The instance of the Pricing Manager used by the Aelia Currency Switcher.
	 *
	 * @var object
	 */
	protected $pricing_manager;

	/**
	 * Returns the singleton instance of this class.
	 *
	 * @return WC_LearnDash_Aelia_Currency_Switcher_Integration
	 */
	public static function instance(): WC_LearnDash_Aelia_Currency_Switcher_Integration {
		return static::$_instance ?? static::$_instance = new static();
	}

  /**
	 * Constructor.
	 */
	public function __construct() {
		$this->set_hooks();
	}

	/**
	 * Returns the instance of the Aelia Currency Switcher.
	 *
	 * @return object|false The plugin instance, or false if the plugin is not
	 * active.
	 */
	protected function cs() {
		if(!is_object($GLOBALS['woocommerce-aelia-currencyswitcher'])) {
			return false;
		}
		return $GLOBALS['woocommerce-aelia-currencyswitcher'];
	}


  /**
   * Sets all actions and filters used by the plugin.
   */
	protected function set_hooks() {
		add_action('init', array($this, 'init'), 10);
	}

	/**
	 * Performs actions when all plugins have been loaded.
	 */
	public function init() {
		// Load the plugin's text domain, to allow localisation
		load_plugin_textdomain(self::$text_domain, false, dirname(plugin_basename(__FILE__)) . '/languages/');

		// We can only run our custom code if the Currency Switcher is active
		if($this->cs() !== false) {
			$this->pricing_manager = $this->cs()->currencyprices_manager();

			add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
			add_action('woocommerce_process_product_meta_' . self::PRODUCT_TYPE, array($this, 'woocommerce_process_product_meta'));
		}
	}

	/**
	 * Event handler fired when a Product is being saved. It processes and saves
	 * the Currency Prices associated with the Product.
	 *
	 * @param int post_id The ID of the Post (product) being saved.
	 */
	public function woocommerce_process_product_meta($post_id) {
		$this->pricing_manager->process_product_meta($post_id);
	}

	/**
	 * Alter the callbacks used by the Currency Switcher, replacing the callback
	 * for a "course" type with a custom one.
	 *
	 * @param callable convert_callback The original callback.
	 * @param WC_Product product The callback being converted.
	 * @return callable The callback that will convert the product's prices.
	 */
	public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) {
		if($product->is_type(self::PRODUCT_TYPE)) {
			$convert_callback = array($this, 'convert_product_prices');
		}

		return $convert_callback;
	}

	/**
	 * Converts the prices of a "course" product.
	 *
	 * IMPORTANT
	 * This method assumes that a "course" is just another simple product. If that's not the case, the
	 * conversion may not work as expected. In that case, the best course of action would be for the
	 * authors of LearnDash to add native multi-currency support to their plugin.
	 *
	 * @param WC_Product product The product whose prices will be converted.
	 * @param string currency The target currency.
	 * @return WC_Product The product, with the prices converted to the target
	 * currency.
	 */
	public function convert_product_prices(WC_Product $product, $currency) {
		$product = $this->pricing_manager->convert_simple_product_prices($product, $currency);

		return $product;
	}
}

WC_LearnDash_Aelia_Currency_Switcher_Integration::instance();