<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('DbUlpOrdersMeta')) return;
class DbUlpOrdersMeta extends DbIndeedAbstract{
  	/**
  	 * @var string
  	 */
	  protected $table = '';
    /**
  	 * @param none
  	 * @reutrn none
  	 */
  	public function __construct(){
    		global $wpdb;
    		$this->table = $wpdb->prefix . 'ulp_order_meta';
  	}
  	/**
  	 * @param int
  	 * @param string
  	 * @return mixed
  	 */
  	public function get($order_id=0, $meta_key=''){
				$order_id = esc_sql($order_id);
				$meta_key = esc_sql($meta_key);
    		$data = $this->getVar('meta_value', " order_id=$order_id AND meta_key='$meta_key' ");
    		return $data;
  	}
		public function allMetaNames(){
				return [
						'amount',
						'course',
						'user_id',
						'unique_identificator',
						'course_id',
						'used'
				];
		}
		public function getAllMetasAsArray($order_id=0){
				$names = $this->allMetaNames();
				$data = array();
				foreach ($names as $name){
						$data [$name] = $this->get($order_id, $name);
				}
				return $data;
		}
		public function getAll($order_id=0){
				$order_id = esc_sql($order_id);
				return $this->getResults('meta_key, meta_value', " order_id=$order_id ");
		}
		public function getSource($order_id=0){
				$order_id = esc_sql($order_id);
				$data = $this->getVar('meta_value', " order_id=$order_id AND meta_key='unique_identificator' ");
				if ($data){
						if (strpos($data, 'woocommerce')!==FALSE){
								return 'Woocommerce';
						} else if (strpos($data, 'ump')!==FALSE){
								return 'Ultimate Membership Pro';
						} else if (strpos($data, 'edd')!==FALSE){
								return 'Easy Download Digital';
						} else if (strpos($data, 'paypal')!==FALSE){
								return 'checkout - PayPal';
						} else if (strpos($data, 'bt')!==FALSE){
								return 'checkout - Bank transfer';
						} else if (strpos($data, 'stripe')!==FALSE){
								return 'checkout - Stripe';
						} else if (strpos($data, '2checkout')!==FALSE){
								return 'checkout - 2Checkout';
						}
				}
				return 'Unknown';
		}
  	/**
  	 * @param int
  	 * @param string
  	 * @param string
  	 * @return bool
  	 */
  	public function save($order_id=0, $meta_key='', $meta_value=''){
    		global $wpdb;
				$meta_key = esc_sql($meta_key);
				$meta_value = esc_sql($meta_value);
				$order_id = esc_sql($order_id);
    		if ($this->get($order_id, $meta_key)!==null){
    			/// update
    			return $this->update("meta_value='$meta_value'", " order_id=$order_id AND meta_key='$meta_key' ");
    		} else {
    			/// create
    			return $this->insert("null, $order_id, '$meta_key', '$meta_value'");
    		}
  	}
    /**
  	 * @param int
  	 * @param string
  	 * @param string
  	 * @return bool
  	 */
  	public function delete($order_id=0, $meta_key='', $meta_value=''){
				$order_id = esc_sql($order_id);
				$meta_key = esc_sql($meta_key);
				$meta_value = esc_sql($meta_value);
  		  return parent::delete("order_id=$order_id AND meta_key='$meta_key'");
  	}
    public function deleteAllByOrderId($order_id=0){
				$order_id = esc_sql($order_id);
        return parent::delete("order_id=$order_id");
    }
}
