checkPluginState();
$purchase = new FlycartWooDiscountRulesPurchase();
$suffix = $purchase->getSuffix();
ob_start();
$config = $data;
$pro = $this->isPro;
$category = $this->getCategoryList();
$coupons = array();
if(in_array($tab, array('pricing-rules-new', 'pricing-rules-view', 'cart-rules-new', 'cart-rules-view'))){
$has_large_no_of_coupon = FlycartWooDiscountBase::hasLargeNumberOfCoupon();
if($pro && (!$has_large_no_of_coupon)) $coupons = $this->getCouponsList();
}
//$users = $this->getUserList();
FlycartWoocommerceVersion::wcVersion('3.0')? $flycart_wdr_woocommerce_version = 3: $flycart_wdr_woocommerce_version = 2;
$userRoles = $this->getUserRoles();
$userRoles['woo_discount_rules_guest'] = esc_html__('Guest', 'woo-discount-rules');
$countries = $this->getAllCountries();
if (!isset($config)) return false;
if (!isset($path) or is_null($config)) return false;
include($path);
$html = ob_get_contents();
ob_end_clean();
return $html;
}
public function checkPluginState()
{
$purchase = new FlycartWooDiscountRulesPurchase();
$this->isPro = $purchase->isPro();
return $this->isPro;
}
/**
* To Retrieve the list of Users.
*
* @return array
*/
public function getUserList()
{
$result = array();
foreach (get_users() as $user) {
$result[$user->ID] = '#' . $user->ID . ' ' . $user->user_email;
}
return $result;
}
/**
* Get all coupons
* */
public function getCouponsList(){
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);
$coupon_list = array();
$coupons = get_posts( $args );
if(!empty($coupons)){
foreach ($coupons as $coupon){
//$coupon_list[$coupon->post_name] = $coupon->post_title;
if(function_exists('wc_strtolower')){
$coupon_name = wc_strtolower($coupon->post_title);
} else {
$coupon_name = strtolower($coupon->post_title);
}
$coupon_list[$coupon_name] = $coupon->post_title;
}
}
return $coupon_list;
}
/**
* To Retrieve the active tab.
*
* @return string
*/
public function getCurrentTab()
{
$postData = \FlycartInput\FInput::getInstance();
$tab = $this->default_page;
$empty_tab = $postData->get('tab', null);
if (!empty($empty_tab) && $postData->get('tab', '') != '') {
$tab = sanitize_text_field($postData->get('tab', ''));
}
return $tab;
}
/**
* To Get All Countries.
*
* @return array
*/
public function getAllCountries()
{
$countries = new WC_Countries();
if ($countries && is_array($countries->countries)) {
return array_merge(array(), $countries->countries);
} else {
return array();
}
}
/**
* To Get All Capabilities list.
*
* @return array
*/
public function getCapabilitiesList()
{
$capabilities = array();
if (class_exists('Groups_User') && class_exists('Groups_Wordpress') && function_exists('_groups_get_tablename')) {
global $wpdb;
$capability_table = _groups_get_tablename('capability');
$all_capabilities = $wpdb->get_results('SELECT capability FROM ' . $capability_table);
if ($all_capabilities) {
foreach ($all_capabilities as $capability) {
$capabilities[$capability->capability] = $capability->capability;
}
}
} else {
global $wp_roles;
if (!isset($wp_roles)) {
get_role('administrator');
}
$roles = $wp_roles->roles;
if (is_array($roles)) {
foreach ($roles as $rolename => $atts) {
if (isset($atts['capabilities']) && is_array($atts['capabilities'])) {
foreach ($atts['capabilities'] as $capability => $value) {
if (!in_array($capability, $capabilities)) {
$capabilities[$capability] = $capability;
}
}
}
}
}
}
return array_merge(array(), $capabilities);
}
/**
* @return array
*/
public function getUserRoles()
{
global $wp_roles;
if (!isset($wp_roles)) {
$wp_roles = new WP_Roles();
}
$roles = array();
foreach ($wp_roles->get_names() as $key => $wp_role ){
if(function_exists('translate_user_role')) $roles[$key] = translate_user_role($wp_role);
else $roles[$key] = $wp_role;
}
return $roles;
}
/**
* Get list of roles assigned to current user
*
* @access public
* @return array
*/
public static function getCurrentUserRoles()
{
$current_user = wp_get_current_user();
$userRoles = $current_user->roles;
if(get_current_user_id() == 0){
$userRoles[] = 'woo_discount_rules_guest';
}
return $userRoles;
}
/**
* @return array
*/
public function getCategoryList()
{
$result = array();
$taxonomies = apply_filters('woo_discount_rules_accepted_taxonomy_for_category', array('product_cat'));
$post_categories_raw = get_terms($taxonomies, array('hide_empty' => 0));
if(is_array($post_categories_raw)){
$post_categories_raw_count = count($post_categories_raw);
foreach ($post_categories_raw as $post_cat_key => $post_cat) {
if(isset($post_cat->name)){
$category_name = $post_cat->name;
if ($post_cat->parent) {
$parent_id = $post_cat->parent;
$has_parent = true;
// Make sure we don't have an infinite loop here (happens with some kind of "ghost" categories)
$found = false;
$i = 0;
while ($has_parent && ($i < $post_categories_raw_count || $found)) {
// Reset each time
$found = false;
$i = 0;
foreach ($post_categories_raw as $parent_post_cat_key => $parent_post_cat) {
$i++;
if ($parent_post_cat->term_id == $parent_id) {
$category_name = $parent_post_cat->name . ' → ' . $category_name;
$found = true;
if ($parent_post_cat->parent) {
$parent_id = $parent_post_cat->parent;
} else {
$has_parent = false;
}
break;
}
}
}
}
$result[$post_cat->term_id] = $category_name;
}
}
}
return $result;
}
/**
* get taxonomy list
*/
public static function getTaxonomyList()
{
$skip_default_taxonomies = array('category', 'post_tag', 'nav_menu', 'link_category', 'post_format', 'action-group', 'product_type', 'product_visibility', 'product_shipping_class', 'product_cat');
$args = array();
$output = 'objects';
$taxonomies = get_taxonomies($args, $output);
$additional_taxonomies = array();
foreach ($taxonomies as $taxonomy){
if(!in_array($taxonomy->name, $skip_default_taxonomies)){
$load_attributes_in_categories = apply_filters('woo_discount_rules_load_attributes_in_categories', false);
if(!(substr( $taxonomy->name, 0, 3 ) === "pa_") || $load_attributes_in_categories){
$additional_taxonomies[$taxonomy->name] = $taxonomy->label;
}
}
}
return $additional_taxonomies;
}
/**
* Get Category by passing product ID or Product.
*
* @param $item
* @param bool $is_id
* @return array
*/
public static function getCategoryByPost($item, $is_id = false)
{
if ($is_id) {
$id = $item;
} else {
$id = FlycartWoocommerceProduct::get_id($item['data']);
}
$product = FlycartWoocommerceProduct::wc_get_product($id);
$categories = FlycartWoocommerceProduct::get_category_ids($product);
return $categories;
}
/**
* To Parsing the Array from String to Int.
*
* @param array $array
*/
public static function toInt(array &$array)
{
foreach ($array as $index => $item) {
$array[$index] = intval($item);
}
}
/**
* @param $html
* @return bool|mixed
*/
static function makeString($html)
{
if (is_null($html) || empty($html) || !isset($html)) return false;
$out = $html;
// This Process only helps, single level array.
if (is_array($html)) {
foreach ($html as $id => $value) {
self::escapeCode($value);
$html[$id] = $value;
}
return $out;
} else {
self::escapeCode($html);
return $html;
}
}
/**
* Re-Arrange the Index of Array to Make Usable.[2-D Array Only]
* @param $rules
*/
public static function reArrangeArray(&$rules)
{
$result = array();
foreach ($rules as $index => $item) {
foreach ($item as $id => $value) {
if(!in_array($id, array('product_variants'))){
$result[$id] = $value;
}
}
}
$rules = $result;
}
/**
* @param $value
*/
static function escapeCode(&$value)
{
if(is_string($value)){
// Four Possible tags for PHP to Init.
$value = preg_replace(array('/^<\?php.*\?\>/', '/^<\%.*\%\>/', '/^<\?.*\?\>/', '/^<\?=.*\?\>/'), '', $value);
$value = self::delete_all_between('', $value);
$value = self::delete_all_between('', '?>', $value);
$value = self::delete_all_between('=', '?>', $value);
$value = self::delete_all_between('<%', '%>', $value);
$value = str_replace(array(''), '', $value);
}
}
/**
* @param $beginning
* @param $end
* @param $string
* @return mixed
*/
static function delete_all_between($beginning, $end, $string)
{
if (!is_string($string)) return false;
$beginningPos = strpos($string, $beginning);
$endPos = strpos($string, $end);
if ($beginningPos === false || $endPos === false) {
return $string;
}
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
return str_replace($textToDelete, '', $string);
}
/**
* To get slider content through curl
* */
public static function getSideBarContent(){
$html = '';
if(is_callable('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.flycart.org/updates/woo-discount-rules.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
$contents_decode = json_decode($contents);
if(isset($contents_decode['0']->promo_html)){
$html = $contents_decode['0']->promo_html;
}
}
return $html;
}
/**
* Check do the discount rules need to execute
* */
public static function doIHaveToRun(){
$status = true;
if(is_admin()){
$status = false;
}
if(defined('DOING_AJAX') && DOING_AJAX){
$status = true;
$postData = \FlycartInput\FInput::getInstance();
$action = $postData->get('action', '');
$form = $postData->get('from', '');
if($action == 'saveCartRule' || $action == 'savePriceRule'){
$status = false;
} else if(($action == 'UpdateStatus' || $action == 'RemoveRule') && ($form == 'cart-rules' || $form == 'pricing-rules')){
$status = false;
} else if($action == 'saveConfig' && $form == 'settings'){
$status = false;
}
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
$status = false;
}
return apply_filters('woo_discount_rules_apply_rules', $status);
}
public static function haveToApplyTheRules(){
$status = true;
$config = new FlycartWooDiscountBase();
$do_not_run_while_have_third_party_coupon = $config->getConfigData('do_not_run_while_have_third_party_coupon', 0);
if($do_not_run_while_have_third_party_coupon == '1'){
$hasCoupon = self::hasCouponInCart();
if($hasCoupon){
self::resetTheCartValues();
$status = false;
}
}
return apply_filters('woo_discount_rules_apply_rules', $status);
}
/**
* Check has coupon - not related to woo discount rules
* */
public static function hasCouponInCart(){
$result = false;
$cartRules = new FlycartWooDiscountRulesCartRules();
$coupon_code = $cartRules->getCouponCode();
$coupon_code = strtolower($coupon_code);
global $woocommerce;
if (!empty($woocommerce->cart->applied_coupons)) {
$appliedCoupons = $woocommerce->cart->applied_coupons;
if(is_array($appliedCoupons) && count($appliedCoupons) > 0){
$used_coupons = self::getUsedCouponsInRules();
$used_coupons[] = $coupon_code;
$used_coupons = apply_filters('woo_discount_rules_coupons_to_skip_while_apply_third_party_coupon_and_disable_rules', $used_coupons);
if(!empty($used_coupons)){
$used_coupons = array_map('strtolower', $used_coupons);
}
foreach ($appliedCoupons as $appliedCoupon){
if(!in_array($appliedCoupon, $used_coupons)){
$result = true;
break;
}
}
}
}
return $result;
}
protected static function resetTheCartValues(){
global $woocommerce;
// Make sure item exists in cart
if (!isset($woocommerce->cart)) return;
if (!isset($woocommerce->cart->cart_contents)) return;
if(!empty($woocommerce->cart->cart_contents) && count($woocommerce->cart->cart_contents) > 0){
foreach ($woocommerce->cart->cart_contents as $key => $cart){
if(isset($woocommerce->cart->cart_contents[$key]['woo_discount'])){
unset($woocommerce->cart->cart_contents[$key]['woo_discount']);
}
}
}
}
/**
* get Used Coupons
* */
public static function getUsedCouponsInRules(){
$coupons = array();
$post_args = array('post_type' => array('woo_discount', 'woo_discount_cart'), 'numberposts' => '-1', 'post_status' => 'publish');
$post_args['meta_key'] = 'used_coupon';
$post_args['meta_query'] = array(
array(
'key' => 'status',
'value' => 'publish',
)
);
$posts = get_posts($post_args);
if(!empty($posts) && count($posts) > 0){
foreach ($posts as $post){
$used_coupons = get_post_meta($post->ID, 'used_coupon');
if(!empty($used_coupons)){
$coupons = array_merge($coupons, $used_coupons);
}
}
}
return $coupons;
}
/**
* get Used Coupons
* */
public static function getDynamicUsedCouponsInRules(){
$coupons = array();
$post_args = array('post_type' => array('woo_discount', 'woo_discount_cart'), 'numberposts' => '-1', 'post_status' => 'publish');
$post_args['meta_key'] = 'dynamic_coupons_to_apply';
$post_args['meta_query'] = array(
array(
'key' => 'status',
'value' => 'publish',
)
);
$posts = get_posts($post_args);
if(!empty($posts) && count($posts) > 0){
foreach ($posts as $post){
$used_coupons = get_post_meta($post->ID, 'dynamic_coupons_to_apply');
if(!empty($used_coupons)){
$coupons = array_merge($coupons, $used_coupons);
}
}
}
return $coupons;
}
/**
* Get Current date and time based on Wordpress time zone
*
* @param string $date
* @return string
* */
public static function getCurrentDateAndTimeBasedOnTimeZone($date = ''){
if(empty($date)){
$current_date = new DateTime('now', new DateTimeZone('UTC'));
$date = $current_date->format('Y-m-d H:i:s');
}
$offset = get_option('gmt_offset');
if(empty($offset)){
$offset = 0;
}
//$time_zone = get_option('timezone_string');
return date("Y-m-d H:i:s", strtotime($date) + (3600 * $offset) );
}
/**
* Validate the start and end date
*
* @param string $date_from
* @param string $date_to
* @return boolean
* */
public static function validateDateAndTime($date_from, $date_to){
$valid = true;
$current_date = self::getCurrentDateAndTimeBasedOnTimeZone();
if($date_from != ''){
if(!(strtotime($date_from) <= strtotime($current_date))) $valid = false;
}
if($date_to != ''){
if(!(strtotime($date_to) >= strtotime($current_date))) $valid = false;
}
return $valid;
}
/**
* Validate the start and end date
*
* @param string $date_from
* @param string $date_to
* @return boolean
* */
public static function validateDateAndTimeWarningText($date_from, $date_to){
$string = '';
$current_date = self::getCurrentDateAndTimeBasedOnTimeZone();
if($date_from != ''){
if(!(strtotime($date_from) <= strtotime($current_date))) $string = esc_html__('Start date and time is set in the future date', 'woo-discount-rules');
}
if($date_to != ''){
if(!(strtotime($date_to) >= strtotime($current_date))) $string = esc_html__('Validity expired', 'woo-discount-rules');;
}
return $string;
}
/**
* Validate the start and end date
*
* @param string $date_from
* @param string $date_to
* @return boolean
* */
public static function validateDateAndTimeWarningTextForListing($date_from, $date_to){
$string = '';
$current_date = self::getCurrentDateAndTimeBasedOnTimeZone();
if($date_from != ''){
if(!(strtotime($date_from) <= strtotime($current_date))) $string = esc_html__('Will run in future', 'woo-discount-rules');
}
if($date_to != ''){
if(!(strtotime($date_to) >= strtotime($current_date))) $string = esc_html__('Not running - validity expired', 'woo-discount-rules');;
}
return $string;
}
/**
* Validate the start and end date
*
* @param string $date_from
* @param string $date_to
* @return boolean
* */
public static function validateDateAndTimeWarningTextForListingHTML($date_from, $date_to){
$string = '';
$validate_date_string = self::validateDateAndTimeWarningTextForListing($date_from, $date_to);
if(empty($validate_date_string)){
$string .= " - (".esc_html__('Running', 'woo-discount-rules').")";
} else {
$current_date_and_time = self::getCurrentDateAndTimeBasedOnTimeZone();
$current_date_and_time = date('m/d/Y H:i', strtotime($current_date_and_time));
$string .= " - (".$validate_date_string.")";
$string .= "
".esc_html__('Your server current date and time: ', 'woo-discount-rules')." ".$current_date_and_time."";
}
return $string;
}
/**
* Reorder the rule if order id already exists
*
* @param int $id
* @param int $order_id
* @param string $post_type
* @return int
* */
public static function reOrderRuleIfExists($id, $order_id, $post_type){
$posts = get_posts(array('post_type' => $post_type, 'numberposts' => '-1', 'exclude' => array($id)));
$greaterId = $alreadyExists = 0;
if (!empty($posts) && count($posts) > 0) {
foreach ($posts as $index => $item) {
$orderId = get_post_meta($item->ID, 'rule_order', true);
if(!empty($orderId)){
if((int)$order_id == (int)$orderId){
$alreadyExists = 1;
}
if($orderId > $greaterId){
$greaterId = $orderId;
}
}
}
}
if($alreadyExists){
$greaterId++;
return $greaterId;
}
return $order_id;
}
/**
* Get all sub categories
* */
public static function getAllSubCategories($cat){
$taxonomies = apply_filters('woo_discount_rules_accepted_taxonomy_for_category', array('product_cat'));
$category_with_sub_cat = $cat;
foreach ($taxonomies as $taxonomy){
$category_with_sub = self::getAllSubCategoriesRecursive($cat, $taxonomy);
$category_with_sub_cat = array_merge($category_with_sub_cat, $category_with_sub);
}
return $category_with_sub_cat;
// $category_with_sub_cat = $cat;
// foreach($cat as $c) {
// $args = array('hierarchical' => 1,
// 'show_option_none' => '',
// 'hide_empty' => 0,
// 'parent' => $c,
// 'taxonomy' => 'product_cat');
// $categories = get_categories( $args );
// foreach($categories as $category) {
// //$category_with_sub_cat[] = $category->term_id;
// $category_with_sub_cat = array_merge($category_with_sub_cat, self::getAllSubCategories(array($category->term_id)));
// }
// }
// $category_with_sub_cat = array_unique($category_with_sub_cat);
//
// return $category_with_sub_cat;
}
/**
* Get all sub categories
* */
protected static function getAllSubCategoriesRecursive($cat, $taxonomy = 'product_cat'){
$category_with_sub_cat = $cat;
foreach($cat as $c) {
if(isset(self::$sub_categories[$c])){
$category_with_sub_cat = array_merge($category_with_sub_cat, self::$sub_categories[$c]);
} else {
$args = array('hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $c,
'taxonomy' => $taxonomy);
$categories = get_categories( $args );
foreach($categories as $category) {
//$category_with_sub_cat[] = $category->term_id;
$category_with_sub_cat = array_merge($category_with_sub_cat, self::getAllSubCategoriesRecursive(array($category->term_id), $taxonomy));
}
self::$sub_categories[$c] = $category_with_sub_cat;
}
}
$category_with_sub_cat = array_unique($category_with_sub_cat);
return $category_with_sub_cat;
}
/**
* Docs HTML
* @param string $url
* @param string $utm_term
* @return string
* */
public static function docsURL($url, $utm_term){
$url_prefix = 'https://docs.flycart.org/woocommerce-discount-rules/';
$utm = '?utm_source=woo-discount-rules&utm_campaign=doc&utm_medium=text-click&utm_content=';
$url = trim($url, '/');
return $url_prefix.$url.$utm.$utm_term;
}
/**
* Docs HTML
* @param string $url
* @param string $utm_term
* @return string
* */
public static function docsDirectURL($url, $utm_term){
$utm = '?utm_source=woo-discount-rules&utm_campaign=doc&utm_medium=text-click&utm_content=';
$url = trim($url, '/');
return $url.$utm.$utm_term;
}
/**
* Docs HTML
* @param string $url
* @param string $utm_term
* @return string
* */
public static function docsURLHTML($url, $utm_term, $additional_class = '', $text = ''){
if(!empty($additional_class)){
$additional_class = 'wdr_read_doc '.$additional_class;
} else {
$additional_class = 'wdr_read_doc';
}
if(empty($text)) $text = esc_html__('Read Docs', 'woo-discount-rules');
$html = ''.$text.'';
return $html;
}
/**
* Docs HTML
* @param string $url
* @param string $utm_term
* @return string
* */
public static function docsURLHTMLForDocumentation($url, $utm_term, $text, $description_text = '', $additional_class = ''){
if(!empty($additional_class)){
$additional_class = 'wdr_read_documentation '.$additional_class;
} else {
$additional_class = 'wdr_read_documentation';
}
$html = '
'.$description_text.'
'; $html .= '