bals.NonPrefixedHooknameFound -- applying third-party filter for compatibility if (Woocommerce::checkCouponAlreadyExistsInWooCommerce($coupon_name)) { return array( 'rule_id' => $rule_id, 'coupon_exists' => 'coupon already exists in woocommerce' ); } else { $rule_conditions[$coupon_key]['options']['custom_value'] = $coupon_name; } $awdr_coupon_names = array_merge($awdr_coupon_names,array($coupon_name)); }else if($type == 'cart_coupon'){ if(!empty($awdr_woo_coupon_name)){ $awdr_coupon_names = array_merge($awdr_coupon_names,$awdr_woo_coupon_name); } }else if(!empty($subtotal_promotion_message) && $subtotal_promotion_message != ''){ $rule_conditions[$coupon_key]['options']['subtotal_promotion_message'] = self::validateHtmlBeforeSave($subtotal_promotion_message); } } } $rule_additional = $this->getFromArray($post, 'additional', array()); $rule_additional = apply_filters('advanced_woo_discount_rules_update_additional_data_before_save_rule', $rule_additional, $post, $this, $rule_id, $rule_filters, $rule_conditions); $product_adjustments = $this->getFromArray($post, 'product_adjustments', array()); if(isset( $product_adjustments['cart_label']) && !empty( $product_adjustments['cart_label'])){ $product_adjustments['cart_label'] = self::validateHtmlBeforeSave( $product_adjustments['cart_label']); } $cart_adjustments = $this->getFromArray($post, 'cart_adjustments', array()); if(isset($cart_adjustments['label']) && !empty($cart_adjustments['label'])){ $cart_adjustments['label'] = self::validateHtmlBeforeSave($cart_adjustments['label']); } $bulk_adjustments = $this->getFromArray($post, 'bulk_adjustments', array()); if(isset( $bulk_adjustments['cart_label']) && !empty( $bulk_adjustments['cart_label'])){ $bulk_adjustments['cart_label'] = self::validateHtmlBeforeSave( $bulk_adjustments['cart_label']); } $set_adjustments = $this->getFromArray($post, 'set_adjustments', array()); if(isset($set_adjustments['cart_label']) && !empty($set_adjustments['cart_label'])){ $set_adjustments['cart_label'] = self::validateHtmlBeforeSave($set_adjustments['cart_label']); } $buyx_getx_adjustments = $this->getFromArray($post, 'buyx_getx_adjustments', array()); $buy_x_get_y_adjustments = $this->getFromArray($post, 'buyx_gety_adjustments', array()); if(!empty($buy_x_get_y_adjustments)){ foreach ($buy_x_get_y_adjustments['ranges'] as $key => $range){ $buy_x_get_y_adjustments['ranges'][$key]['product_varients'] = array(); $buy_x_get_y_adjustments['ranges'][$key]['product_variants_for_sale_badge'] = array(); if(isset($range['products']) && !empty($range['products'])){ foreach ($range['products'] as $product_id){ $variants = $this->getVariantsOfProducts(array($product_id)); if(!empty($variants)){ $buy_x_get_y_adjustments['ranges'][$key]['products_variants'][$product_id] = $variants; } $parent_id = $this->getParentOfVariant(array($product_id)); if(!empty($parent_id)){ $buy_x_get_y_adjustments['ranges'][$key]['product_variants_for_sale_badge'][] = $parent_id; } } } } } $rule_language = $this->getFromArray($post, 'rule_language', array()); $discount_badge = $this->getFromArray($post, 'discount_badge', array()); $discount_type = $this->getFromArray($post, 'discount_type', NULL); $discount_badge_text = isset($_POST['discount_badge']['badge_text']) ? stripslashes($_POST['discount_badge']['badge_text']): '';//phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if (!empty($discount_badge_text)) { $discount_badge['badge_text'] = self::validateHtmlBeforeSave($discount_badge_text); } if(!empty($awdr_coupon_names)){ $awdr_coupon_names = array_unique($awdr_coupon_names); } if($date_from !== null){ $date_from = intval($date_from); } if($date_to !== null){ $date_to = intval($date_to); } $current_time = current_time('mysql', true); $rule_title = (empty($title)) ? esc_html__('Untitled Rule', 'woo-discount-rules') : $title; $arg = array( 'title' => sanitize_text_field($rule_title), 'enabled' => intval($enabled), 'exclusive' => intval($exclusive), 'usage_limits' => intval($usage_limits), 'date_from' => $date_from, 'date_to' => $date_to, 'filters' => json_encode($rule_filters), 'conditions' => json_encode($rule_conditions), 'additional' => json_encode($rule_additional), 'product_adjustments' => json_encode($product_adjustments), 'cart_adjustments' => json_encode($cart_adjustments), 'buy_x_get_x_adjustments' => json_encode($buyx_getx_adjustments), 'buy_x_get_y_adjustments' => json_encode($buy_x_get_y_adjustments), 'bulk_adjustments' => json_encode($bulk_adjustments), 'rule_language' => json_encode($rule_language), 'set_adjustments' => json_encode($set_adjustments), 'advanced_discount_message' => json_encode($discount_badge), 'discount_type' => esc_sql($discount_type), 'used_coupons' => json_encode($awdr_coupon_names), ); if (!is_null($rule_id) && !empty($rule_id)) { $arg['modified_by'] = intval($current_user); $arg['modified_on'] = esc_sql($current_date_time); $column_format = array('%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s'); }else{ $arg['created_by'] = intval($current_user); $arg['created_on'] = esc_sql($current_date_time); $arg['modified_by'] = intval($current_user); $arg['modified_on'] = esc_sql($current_date_time); $column_format = array('%s', '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%d', '%s'); } $arg = apply_filters( 'advanced_woo_discount_rules_before_save_rule_column', $arg, $rule_id, $post); $rule_id = DBTable::saveRule($column_format, $arg, $rule_id); if($rule_id){ OnSaleShortCode::updateOnsaleRebuildPageStatus($rule_id); do_action('advanced_woo_discount_rules_after_save_rule', $rule_id, $post, $arg, $rule_additional); } return $rule_id; } /** * Remove some Html tags before save * @param $value * @return mixed */ static function validateHtmlBeforeSave($value){ if (!empty($value)) { $html = html_entity_decode($value); $html = preg_replace('/(<(script|style|iframe)\b[^>]*>).*?(<\/\2>)/is', "$1$3", $html); $allowed_html = array( 'br' => array(), 'strong' => array(), 'span' => array('class' => array(), 'style' => array()), 'div' => array('class' => array(), 'style' => array()), 'p' => array('class' => array(), 'style' => array()), 'table' => array('class' => array(), 'style' => array(), 'border' => array(), 'cellpadding' => array(), 'cellspacing' => array()), 'tr' => array('class' => array()), 'td' => array('class' => array()), 'th' => array('class' => array()), 'h4' => array('class' => array()), 'h3' => array('class' => array()), 'h1' => array('class' => array()), 'h2' => array('class' => array()), ); // Since v2.4.1 $allowed_html = apply_filters( 'advanced_woo_discount_rules_allowed_html_elements_and_attributes', $allowed_html); return wp_kses($html, $allowed_html); } return $value; } /** * Get data from array * @param $array * @param $key * @param $default * @return array|mixed */ function getFromArray($array, $key, $default) { if (!is_array($array)) { return $default; } if (isset($array[$key])) { if ($key == 'filters') { return $this->addAdditionalDataForFilters($array[$key]); } else if ($key == 'conditions') { return $this->addAdditionalDataForConditions($array[$key]); } return $array[$key]; } return $default; } /** * Format filters * * @param $array_filters array * @return array * */ function addAdditionalDataForFilters($array_filters) { if (!empty($array_filters)) { foreach ($array_filters as $key => $array_filter) { if (isset($array_filter['type']) && isset($array_filter['value'])) { $array_filters[$key]['product_variants'] = array(); if ($array_filter['type'] == 'products' && !empty($array_filter['value'])) { if (is_array($array_filter['value'])) { $array_filters[$key]['product_variants'] = $this->getVariantsOfProducts($array_filter['value']); $array_filters[$key]['product_variants_for_sale_badge'] = $this->getParentOfVariant($array_filter['value']); } } } } } return $array_filters; } /** * Format filters * * @param $array_filters array * @return array * */ function addAdditionalDataForConditions($array_filters) { if (!empty($array_filters)) { foreach ($array_filters as $key => $array_filter) { if (isset($array_filter['type']) && isset($array_filter['options'])) { if (in_array($array_filter['type'], array('cart_item_product_combination', 'cart_item_products', 'purchase_previous_orders_for_specific_product', 'purchase_quantities_for_specific_product')) && !empty($array_filter['options'])) { $product_field_key = 'product'; if ($array_filter['type'] == 'cart_item_products') { $product_field_key = 'value'; } elseif (in_array($array_filter['type'], array('purchase_previous_orders_for_specific_product', 'purchase_quantities_for_specific_product'))) { $product_field_key = 'products'; } if (is_array($array_filter['options']) && isset($array_filter['options'][$product_field_key])) { $array_filters[$key]['options']['product_variants'] = array(); if (is_array($array_filter['options'][$product_field_key]) && !empty($array_filter['options'][$product_field_key])) { $variants = $this->getVariantsOfProducts($array_filter['options'][$product_field_key]); $array_filters[$key]['options']['product_variants'] = $variants; } } } } } } return $array_filters; } /** * Get variants of the products * * @param $product_ids array * @return array */ function getVariantsOfProducts($product_ids) { $variants = array(); if (!empty($product_ids)) { foreach ($product_ids as $product_id) { $product = Woocommerce::getProduct($product_id); if (!empty($product) && is_object($product) && method_exists($product, 'is_type')) { if ($product->is_type(array('variable', 'variable-subscription'))) { $additional_variants = Woocommerce::getProductChildren($product); if (!empty($additional_variants) && is_array($additional_variants)) { $variants = array_merge($variants, $additional_variants); } } } } } return $variants; } /** * Get siblings of the variants * * @param $product_ids array * @return array */ function getParentOfVariant($product_ids) { $variants = array(); if (!empty($product_ids)) { foreach ($product_ids as $product_id) { $parent_id = Woocommerce::getProductParentId((int)$product_id); if(!empty($parent_id) && !in_array($parent_id ,$variants)) { $variants[] = $parent_id; } } } return $variants; } /** * search rule by rulename * @param $name * @param $available_conditions * @return array|\stdClass */ function searchRuleByName($name, $available_conditions) { $rule = DBTable::getRules(null, $name); if (empty($rule)) { echo ""; return $this->getRuleObject(DBTable::getRules(), $available_conditions); } return $this->getRuleObject($rule, $available_conditions); } /** * Export all rules * @return array|\stdClass */ function exportRuleByName($names) { return DBTable::getRules(null, null, $names); } /** * Hide Discount blocks if values get empty * @param $discount_obj * @return int */ function showHideDiscount($discount_obj) { $show_discount_block = 0; if (!empty($discount_obj)) { foreach ($discount_obj as $discount_object) { $show_discount_block = $discount_object->value; if (!empty($show_discount_block)) { return $show_discount_block; } } } return $show_discount_block; } /** * get all custom coupons * @return array */ function getAllDynamicCoupons() { $available_rules = DBTable::getRules(); $custom_coupons = array(); foreach ($available_rules as $rule) { if (isset($rule->conditions) && !empty($rule->conditions) && $rule->conditions != '{}' && $rule->conditions != '[]') { $conditions = json_decode($rule->conditions); foreach ($conditions as $condition) { $option_obj = (isset($condition->options) && !empty($condition->options) ? $condition->options : ''); $type = (isset($condition->type) && !empty($condition->type) ? $condition->type : ''); $operator = (isset($option_obj->operator) && !empty($option_obj->operator) ? $option_obj->operator : ''); $custom_value = (isset($option_obj->custom_value) && !empty($option_obj->custom_value) ? $option_obj->custom_value : ''); if ($type == 'cart_coupon' && $operator == 'custom_coupon' && $custom_value != '') { $custom_coupons[] = $custom_value; } } } } return $custom_coupons; } /** * get all url coupons * @return array */ function getAllUrlCoupons() { $available_rules = DBTable::getRules(); $url_coupons = array(); foreach ($available_rules as $rule) { if (isset($rule->enabled) && $rule->enabled == 1 && isset($rule->conditions) && !empty($rule->conditions) && $rule->conditions != '{}' && $rule->conditions != '[]') { $conditions = json_decode($rule->conditions); foreach ($conditions as $condition) { $option_obj = (isset($condition->options) && !empty($condition->options) ? $condition->options : ''); $type = (isset($condition->type) && !empty($condition->type) ? $condition->type : ''); $operator = (isset($option_obj->operator) && !empty($option_obj->operator) ? $option_obj->operator : ''); $enable_url = (isset($option_obj->enable_url)) ? true : false; $values = (isset($option_obj->value) && is_array($option_obj->value)) ? $option_obj->value : []; $custom_value = (isset($option_obj->custom_value) && !empty($option_obj->custom_value) ? $option_obj->custom_value : ''); if ($type == 'cart_coupon' && $enable_url) { if ($operator == 'custom_coupon' && $custom_value != '') { $url_coupons[] = $custom_value; } elseif (in_array($operator, ['all', 'at_least_one'])) { foreach ($values as $value) { $url_coupons[] = $value; } } } } } } return array_unique($url_coupons); } /** * get all custom coupons * @return array */ function getCouponsFromDiscountRules() { $available_rules = DBTable::getRules(); $custom_coupons = array(); $woo_coupons = array(); if (!empty($available_rules)) { foreach ($available_rules as $rule) { if (isset($rule->conditions) && !empty($rule->conditions) && $rule->conditions != '{}' && $rule->conditions != '[]') { $conditions = json_decode($rule->conditions); foreach ($conditions as $condition) { $option_obj = (isset($condition->options) && !empty($condition->options) ? $condition->options : ''); $type = (isset($condition->type) && !empty($condition->type) ? $condition->type : ''); $operator = (isset($option_obj->operator) && !empty($option_obj->operator) ? $option_obj->operator : ''); $custom_value = (isset($option_obj->custom_value) && !empty($option_obj->custom_value) ? $option_obj->custom_value : ''); $value = (isset($option_obj->value) && !empty($option_obj->value) ? $option_obj->value : ''); if ($type == 'cart_coupon' && $operator == 'custom_coupon' && !empty($custom_value)) { $custom_coupons[] = $custom_value; } elseif ($type == 'cart_coupon' && $operator != 'custom_coupon' && !empty($value)) { $woo_coupons[] = $value; } } } } } return array('custom_coupons' => $custom_coupons, 'woo_coupons' => $woo_coupons); } /** * The rule valid status * @return bool|string */ function getRuleVaildStatus() { $valid_rule = false; $current_time = current_time('timestamp'); $rule_start_date = $this->getStartDate(true); $rule_end_date = $this->getEndDate(true); if (!is_null($rule_start_date) && $current_time < $rule_start_date) { $valid_rule = "in_future"; } else if (!is_null($rule_end_date) && $current_time > $rule_end_date) { $valid_rule = "expired"; } return $valid_rule; } /** * check the rule has product discount * @return bool */ function hasUsedCoupons() { if (isset($this->rule->used_coupons)) { if (!empty($this->rule->used_coupons) && $this->rule->used_coupons != '{}' && $this->rule->used_coupons != '[]') { return json_decode($this->rule->used_coupons); } } return false; } }