<?php

/**
 * The Oxygen Conditions functionality of the plugin.
 *
 * @link       https://oxyninja.com
 * @since      3.4.0
 *
 * @package    Oxy_Ninja
 * @subpackage Oxy_Ninja/admin
 */

/**
 * The Oxygen Conditions functionality of the plugin.
 *
 * @package    Oxy_Ninja
 * @subpackage Oxy_Ninja/admin
 * @author     OxyNinja <rados@oxyninja.com>
 */
class Oxy_Ninja_Conditions {
	/**
	 * The ID of this plugin.
	 *
	 * @since    3.4.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    3.4.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    3.4.0
	 * @param      string    $plugin_name       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct($plugin_name, $version) {
		$this->plugin_name = $plugin_name;
		$this->version = $version;
	}

	/**
	 * Register condition - Related Products Empty
	 *
	 * Parts from the great plugin https://wordpress.org/plugins/wplit-woo-conditions-for-oxygen/
	 *
	 * @since    3.4.0
	 */
	public function woocore_are_related_products_empty() {
		if (function_exists('oxygen_vsb_register_condition') && class_exists('woocommerce')) {
			function kondice_relate_callback($value, $operator) {
				if (
					wc_get_product() &&
					!is_shop() &&
					!is_post_type_archive('product') &&
					!is_tax(get_object_taxonomies('product'))
				) {
					$product = wc_get_product();
				} else {
					return true;
				}

				$is_empty = empty(wc_get_related_products($product->get_id())) ? false : true;

				$value = (bool) $value;

				return oxy_condition_eval_string($is_empty, $value, $operator);
			}

			global $oxy_condition_operators;
			if ($oxy_condition_operators === null || !$oxy_condition_operators) {
				return false;
			}

			oxygen_vsb_register_condition(
				'Related Products Not Empty',
				['options' => [true], 'custom' => false],
				['=='],
				'kondice_relate_callback',
				'WooCore'
			);
		} else {
			return false;
		}
	}

	/**
	 * Register condition - Product in Stock
	 *
	 * @since    3.4.1
	 */
	public function woocore_product_stock() {
		if (function_exists('oxygen_vsb_register_condition') && class_exists('woocommerce')) {
			function kondice_stock_callback($value, $operator) {
				if (
					wc_get_product() &&
					!is_shop() &&
					!is_post_type_archive('product') &&
					!is_tax(get_object_taxonomies('product'))
				) {
					$product = wc_get_product();
				} else {
					return false;
				}

				$is_in_stock = $product->is_in_stock();

				$value = (bool) $value;

				return oxy_condition_eval_string($is_in_stock, $value, $operator);
			}

			oxygen_vsb_register_condition(
				'Product in Stock',
				['options' => [true, false], 'custom' => false],
				['==', '!='],
				'kondice_stock_callback',
				'WooCore'
			);
		} else {
			return false;
		}
	}
}
