<?php

if ( ! defined( 'ABSPATH' ) ) {
	// Exit if accessed directly.
	exit;
}

use QodeBadgesForWooCommerce\Database\Qode_Product_Badge_Relation_Table_Setup;

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_html' ) ) {
	function qode_badges_for_woocommerce_badge_html( $badge_id, $params = array() ) {
		echo wp_kses_post( qode_badges_for_woocommerce_get_badge_html( $badge_id, $params ) );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_get_badge_html' ) ) {
	function qode_badges_for_woocommerce_get_badge_html( $badge_id, $params = array() ) {
		$type     = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_type', true );
		$position = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_position', true );

		$classes_params             = array();
		$classes_params['badge_id'] = ! empty( $badge_id ) ? $badge_id : '';
		$classes_params['type']     = ! empty( $type ) ? $type : '';
		$classes_params['position'] = ! empty( $position ) ? $position : '';

		$final_params = array(
			'badge_id' => $badge_id,
			'type'     => $type,
			'classes'  => qode_badges_for_woocommerce_badge_classes( $classes_params ),
		);

		if ( is_array( $params ) && count( $params ) ) {
			$final_params = array_merge( $final_params, $params );
		}

		$final_params = apply_filters( 'qode_badges_for_woocommerce_filter_badge_final_params_for_html', $final_params );

		$badge_html = qode_badges_for_woocommerce_get_template_part( 'post-types', 'badge/layouts/' . esc_attr( $final_params['type'] ) . '/templates/badge', '', $final_params );

		return wp_kses_post( apply_filters( 'qode_badges_for_woocommerce_filter_badge_html', $badge_html, $badge_id, $final_params ) );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_classes' ) ) {
	function qode_badges_for_woocommerce_badge_classes( $params = array(), $additional_classes = array() ) {
		$classes = array( 'qbfw-badge' );

		if ( is_array( $params ) && count( $params ) ) {
			$classes[] = isset( $params['badge_id'] ) && ! empty( $params['badge_id'] ) ? 'qbfw-badge-id-' . $params['badge_id'] : '';
			$classes[] = isset( $params['type'] ) && ! empty( $params['type'] ) ? 'qbfw-badge-type-' . $params['type'] : '';
			$classes[] = isset( $params['position'] ) && ! empty( $params['position'] ) ? 'qbfw-badge-position-' . str_replace( '_', '-', $params['position'] ) : '';

			$colors_show_types = apply_filters( 'qode_badges_for_woocommerce_filter_colors_badge_types', array() );
			$classes[]         = isset( $params['type'] ) && in_array( $params['type'], $colors_show_types, true ) ? 'qbfw-background-style' : '';

			$border_show_types = apply_filters( 'qode_badges_for_woocommerce_filter_border_badge_types', array() );
			$classes[]         = isset( $params['type'] ) && in_array( $params['type'], $border_show_types, true ) ? 'qbfw-border-style' : '';
		}

		if ( is_array( $additional_classes ) && count( $additional_classes ) ) {
			$classes = array_merge( $classes, $additional_classes );
		}
		$classes = apply_filters( 'qode_badges_for_woocommerce_filter_badge_classes_array', $classes, $params );

		return esc_attr( implode( ' ', $classes ) );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_single_image_html' ) ) {
	/**
	 * Function adds badge on single product page through filter
	 *
	 * @param string $html
	 * @param string $post_thumbnail_id
	 *
	 * @return string $new_html
	 */
	function qode_badges_for_woocommerce_single_image_html( $html ) {
		$new_html   = $html;
		$product_id = get_the_ID();
		$flexslider = (bool) apply_filters( 'woocommerce_single_product_flexslider_enabled', get_theme_support( 'wc-product-gallery-slider' ) );

		// if we are on main image in post single - it contains wp-post-image class.
		if ( str_contains( $html, 'wp-post-image' ) ) {
			$badge = qode_badges_for_woocommerce_get_badge_from_id( $product_id );

			if ( ! empty( $badge ) ) {
				// if flexslider is enabled there is no need for our holder for positioning, it interferes with slider and zoom options.
				if ( $flexslider ) {
					$position = strpos( $html, '</div>' );
					if ( false === $position ) {
						$new_html = $badge . $html;
					} else {
						$new_html = substr( $html, 0, $position ) . $badge . substr( $html, $position );
					}
				} else {
					$new_html = preg_replace( '/<\/div>(?!.*<\/div>)/', $badge . '</div>', $html );
				}
			}
		}

		return $new_html;
	}

	add_filter( 'woocommerce_single_product_image_thumbnail_html', 'qode_badges_for_woocommerce_single_image_html', 999, 1 );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_before_title_html' ) ) {
	function qode_badges_for_woocommerce_before_title_html() {
		$product_id = get_the_ID();

		qode_badges_for_woocommerce_badge_from_id( $product_id, 'below_image' );
	}

	// 2 position is to make sure it is before 5 - which is title action order.
	add_action( 'woocommerce_single_product_summary', 'qode_badges_for_woocommerce_before_title_html', 2 );

	// 1000 position is to make sure it is on last place in before_shop_loop_item_title.
	add_action( 'woocommerce_before_shop_loop_item_title', 'qode_badges_for_woocommerce_before_title_html', 1000 );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_list_image_html' ) ) {
	/**
	 * Function adds badge on product list through filter
	 *
	 * @param string $image
	 * @param object $product
	 *
	 * @return string $new_html
	 */
	function qode_badges_for_woocommerce_list_image_html( $image, $product ) {
		$new_html = $image;
		$badge    = qode_badges_for_woocommerce_get_badge_from_id( $product->get_id() );

		// check conditions for badge showing and badge exists.
		if ( qode_badges_for_woocommerce_show_badge() && ! empty( $badge ) ) {
			$new_html  = '<div class="qbfw-image-holder">' . $image;
			$new_html .= $badge;
			$new_html .= '</div>';
		}

		return $new_html;
	}

	add_filter( 'woocommerce_product_get_image', 'qode_badges_for_woocommerce_list_image_html', 999, 2 );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_show_badge' ) ) {
	function qode_badges_for_woocommerce_show_badge() {
		$show = true;

		// hide in dashboard.
		if ( is_admin() ) {
			$show = false;
		}

		// hide in cart and checkout pages.
		if ( is_cart() || is_checkout() ) {
			$show = false;
		}

		return $show;
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_body_badges_widget_class' ) ) {
	function qode_badges_for_woocommerce_body_badges_widget_class( $classes ) {

		$show_in_widgets = qode_badges_for_woocommerce_get_option_value( 'admin', 'qode_badges_for_woocommerce_show_badges_in_widgets' );

		if ( 'no' === $show_in_widgets ) {
			$classes[] = 'qbfw-badge-in-widget--hide';
		}

		return $classes;
	}
	add_filter( 'body_class', 'qode_badges_for_woocommerce_body_badges_widget_class' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_get_cpt_items' ) ) {
	/**
	 * Returns array of badge items
	 *
	 * @param array $args
	 * @param bool $enable_default - add first element empty for default value.
	 *
	 * @return array
	 */
	function qode_badges_for_woocommerce_get_cpt_items( $args = array(), $enable_default = false ) {
		$options    = array();
		$query_args = array(
			'post_status'    => 'publish',
			'post_type'      => 'qbfw_badge',
			'posts_per_page' => '-1',
			'fields'         => 'ids',
		);

		if ( ! empty( $args ) ) {
			foreach ( $args as $key => $value ) {
				if ( ! empty( $value ) ) {
					$query_args[ $key ] = $value;
				}
			}
		}

		$cpt_items = new \WP_Query( $query_args );

		if ( $cpt_items->have_posts() ) {

			if ( $enable_default ) {
				$options[''] = esc_html__( 'Default', 'qode-badges-for-woocommerce' );
			}

			foreach ( $cpt_items->posts as $id ) :
				$options[ $id ] = get_the_title( $id );
			endforeach;
		}

		wp_reset_postdata();

		return $options;
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_regenerate_product_in_database' ) ) {
	function qode_badges_for_woocommerce_badge_regenerate_product_in_database( $product_id, $product_params = array() ) {
		$product_table = Qode_Product_Badge_Relation_Table_Setup::get_instance();

		$product_table->delete_from_database_by_field( $product_id, 'product_id' );

		$rows       = array();
		$start_date = null;
		$end_date   = null;

		if ( is_array( $product_params ) && count( $product_params ) ) {
			$badge    = isset( $product_params['qode_badges_for_woocommerce_badge'] ) ? sanitize_text_field( wp_unslash( $product_params['qode_badges_for_woocommerce_badge'] ) ) : '';
			$schedule = isset( $product_params['qode_badges_for_woocommerce_schedule_badge'] ) ? sanitize_text_field( wp_unslash( $product_params['qode_badges_for_woocommerce_schedule_badge'] ) ) : '';

			if ( 'yes' === $schedule ) {
				$start_date = isset( $product_params['qode_badges_for_woocommerce_schedule_badge_start_date'] ) ? sanitize_text_field( wp_unslash( $product_params['qode_badges_for_woocommerce_schedule_badge_start_date'] ) ) : '';
				$end_date   = isset( $product_params['qode_badges_for_woocommerce_schedule_badge_end_date'] ) ? sanitize_text_field( wp_unslash( $product_params['qode_badges_for_woocommerce_schedule_badge_end_date'] ) ) : '';
			}
		} else {
			$badge    = get_post_meta( $product_id, 'qode_badges_for_woocommerce_badge', true );
			$schedule = get_post_meta( $product_id, 'qode_badges_for_woocommerce_schedule_badge', true );

			if ( 'yes' === $schedule ) {
				$start_date = get_post_meta( $product_id, 'qode_badges_for_woocommerce_schedule_badge_start_date', true );
				$end_date   = get_post_meta( $product_id, 'qode_badges_for_woocommerce_schedule_badge_end_date', true );
			}
		}

		if ( ! empty( $badge ) && 'qbfw_badge' === get_post_type( $badge ) && 'publish' === get_post_status( $badge ) ) {
			$row    = array(
				'product_id' => $product_id,
				'badge_id'   => $badge,
				'rule_id'    => - 1,
				'start_date' => $start_date,
				'end_date'   => $end_date,
				'user_roles' => '',
				'user_ids'   => '',
				'group_id'   => - 1,
			);
			$rows[] = $row;
		}

		$product_table->insert_into_database( $rows );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_from_id' ) ) {
	function qode_badges_for_woocommerce_badge_from_id( $product_id, $badge_position = 'on_image' ) {
		echo wp_kses_post( qode_badges_for_woocommerce_get_badge_from_id( $product_id, $badge_position ) );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_get_badge_from_id' ) ) {
	function qode_badges_for_woocommerce_get_badge_from_id( $product_id, $badge_position = 'on_image' ) {
		$badge_html        = '';
		$badge_ids         = array();
		$formatted_results = array();
		$results           = QodeBadgesForWooCommerce\Database\Qode_Product_Badge_Relation_Table_Setup::get_instance()->get_badge_from_product_id( $product_id );

		if ( is_array( $results ) && count( $results ) ) {
			foreach ( $results as $result ) {
				$group_id = $result[3];

				$formatted_results[ $group_id ][] = $result;
			}

			foreach ( $formatted_results as $key => $group_result ) {
				if ( is_array( $group_result ) && count( $group_result ) ) {
					$group_meta_position = '';
					// If $key which is group_id is different then -1 then it should be wrapped in group holder which will determine position.
					if ( -1 !== $key ) {
						$group_meta_position = get_post_meta( $key, 'qode_badges_for_woocommerce_premium_group_position', true );

						// check if group position is compatible with $badge_position.
						if ( $badge_position === $group_meta_position ) {
							$badge_html .= '<div class="qbwf-badge-group-holder qbfw-rule-' . $key . ' qbfw-rule-position-' . str_replace( '_', '-', $group_meta_position ) . '">';
						}
					}
					foreach ( $group_result as $result ) {
						$badge_id   = $result[0];
						$user_roles = $result[1];
						$user_ids   = $result[2];

						// check if id is already in badge ids so not to duplicate html.
						$exists = in_array( $badge_id, $badge_ids, true );

						$badge_meta_position = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_position', true );

						// if $key is different then -1, then it is grouped adn grouped meta determines position.
						// if it is not grouped, then badge meta determines position.
						if ( ( -1 !== $key && $badge_position === $group_meta_position ) || ( -1 === $key && $badge_position === $badge_meta_position ) ) {
							if ( ! $exists ) {
								$badge_ids[] = $badge_id;

								$show_for_user = qode_badges_for_woocommerce_show_for_user( $user_roles, $user_ids );

								if ( $show_for_user ) {
									$badge_html .= qode_badges_for_woocommerce_get_badge_html( $badge_id );
								}
							}
						}
					}
					if ( -1 !== $key ) {
						if ( $badge_position === $group_meta_position ) {
							$badge_html .= '</div>';
						}
					}
				}
			}
		}

		return $badge_html;
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badges_get_badges_style' ) ) {
	function qode_badges_for_woocommerce_badges_get_badges_style( $css ) {
		$badges_css = get_transient( QODE_BADGES_FOR_WOOCOMMERCE_CSS_TRANSIENT_NAME );

		if ( empty( $badges_css ) ) {
			$badges = array_keys( qode_badges_for_woocommerce_get_cpt_items() );

			foreach ( $badges as $badge ) {
				$main_style_array = array();

				$position = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_position', true );

				if ( 'on_image' === $position ) {
					// get position style - returns an array.
					$main_style_array = qode_badges_for_woocommerce_badge_css_position( $badge );
				}

				// get default sizing style.
				$width       = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_width', true );
				$height      = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_height', true );
				$padding     = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_padding', true );
				$margin      = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_margin', true );
				$width_880   = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_width_880', true );
				$height_880  = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_height_880', true );
				$padding_880 = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_padding_880', true );

				if ( ! empty( $width ) ) {
					$width              = qode_badges_for_woocommerce_string_ends_with_allowed_units( $width ) ? $width : intval( $width ) . 'px';
					$main_style_array[] = '--qbfw-width:' . $width;

					if ( ! empty( $width_880 ) ) {
						$width_880          = qode_badges_for_woocommerce_string_ends_with_allowed_units( $width_880 ) ? $width_880 : intval( $width_880 ) . 'px';
						$main_style_array[] = '--qbfw-width-880:' . $width_880;
					} else {
						$main_style_array[] = '--qbfw-width-880:' . $width;
					}
				}
				if ( ! empty( $height ) ) {
					$height             = qode_badges_for_woocommerce_string_ends_with_allowed_units( $height ) ? $height : intval( $height ) . 'px';
					$main_style_array[] = '--qbfw-height:' . $height;

					if ( ! empty( $height_880 ) ) {
						$height_880         = qode_badges_for_woocommerce_string_ends_with_allowed_units( $height_880 ) ? $height_880 : intval( $height_880 ) . 'px';
						$main_style_array[] = '--qbfw-height-880:' . $height_880;
					} else {
						$main_style_array[] = '--qbfw-height-880:' . $height;
					}
				}
				if ( ! empty( $padding ) ) {
					$main_style_array[] = '--qbfw-padding:' . $padding;

					if ( ! empty( $padding_880 ) ) {
						$main_style_array[] = '--qbfw-padding-880:' . $padding_880;
					} else {
						$main_style_array[] = '--qbfw-padding-880:' . $padding;
					}
				}
				if ( ! empty( $margin ) ) {
					$main_style_array[] = '--qbfw-margin:' . $margin;
				}

				// get default colors style.
				$text_color       = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_text_color', true );
				$text_align       = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_text_alignment', true );
				$background_color = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_background_color', true );

				if ( ! empty( $text_color ) ) {
					$main_style_array[] = 'color:' . $text_color;
				}
				if ( ! empty( $text_align ) ) {
					$main_style_array[] = '--qbfw-text-align:' . $text_align;
				}
				if ( ! empty( $background_color ) ) {
					$main_style_array[] = '--qbfw-background-color:' . $background_color;
				}

				// get default border style.
				$border_color  = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_border_color', true );
				$border_width  = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_border_width', true );
				$border_style  = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_border_style', true );
				$border_radius = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_border_radius', true );

				$border_width = ! empty( $border_width ) ? $border_width : '1px';
				$border_width = qode_badges_for_woocommerce_string_ends_with_allowed_units( $border_width ) ? $border_width : intval( $border_width ) . 'px';

				if ( ! empty( $border_color ) ) {
					$main_style_array[] = '--qbfw-border-color:' . $border_color;
				}
				if ( ! empty( $border_width ) ) {
					$main_style_array[] = '--qbfw-border-width:' . $border_width;
				}
				if ( ! empty( $border_style ) ) {
					$main_style_array[] = '--qbfw-border-style:' . $border_style;
				}
				if ( ! empty( $border_radius ) ) {
					$main_style_array[] = '--qbfw-border-radius:' . $border_radius;
				}

				$z_index = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_z_index', true );

				if ( ! empty( $z_index ) ) {
					$main_style_array[] = 'z-index:' . intval( $z_index );
				}

				$main_style = esc_attr( implode( '; ', $main_style_array ) );

				// attach additional style if needed.
				$additional_style = implode( '; ', apply_filters( 'qode_badges_for_woocommerce_filter_additional_badge_style', array(), $badge ) );

				$type = get_post_meta( $badge, 'qode_badges_for_woocommerce_badge_type', true );

				$style = '.qbfw-badge.qbfw-badge-type-' . $type . '.qbfw-badge-id-' . $badge . " {
					{$main_style};
					{$additional_style};
				}";

				$css .= $style;

				// attach general additional style if needed.
				$general_additional_badge_style = apply_filters( 'qode_badges_for_woocommerce_filter_general_additional_badge_style', '', $badge );

				$css .= $general_additional_badge_style;

			}
			// attach general additional style if needed.
			$general_additional_style = apply_filters( 'qode_badges_for_woocommerce_filter_general_additional_style', '' );

			$css .= $general_additional_style;

			set_transient( QODE_BADGES_FOR_WOOCOMMERCE_CSS_TRANSIENT_NAME, wp_strip_all_tags( $css ) );
		} else {
			$css .= $badges_css;
		}

		return $css;
	}

	add_filter( 'qode_badges_for_woocommerce_badges_filter_badges_style', 'qode_badges_for_woocommerce_badges_get_badges_style' );
	add_filter( 'qode_badges_for_woocommerce_badges_filter_preview_style', 'qode_badges_for_woocommerce_badges_get_badges_style' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_show_for_user' ) ) {
	function qode_badges_for_woocommerce_show_for_user( $user_roles, $user_ids ) {
		if ( empty( $user_roles ) && empty( $user_ids ) ) {
			return true;
		}

		$current_user_id = get_current_user_id();
		if ( 0 === $current_user_id ) {
			return false;
		}
		$current_user_roles = get_user_by( 'id', $current_user_id )->roles;

		if ( ! empty( $user_roles ) ) {
			$user_roles_array = explode( ',', $user_roles );

			foreach ( $user_roles_array as $user_role ) {
				if ( in_array( $user_role, $current_user_roles, true ) ) {
					return true;
				}
			}
		}

		if ( ! empty( $user_ids ) ) {
			$user_ids_array = explode( ',', $user_ids );

			if ( in_array( $current_user_id, $user_ids_array, true ) ) {
				return true;
			}
		}

		return false;
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_on_badge_save' ) ) {
	function qode_badges_for_woocommerce_on_badge_save() {
		delete_transient( QODE_BADGES_FOR_WOOCOMMERCE_CSS_TRANSIENT_NAME );

		$product_query_args = array(
			'post_type' => 'product',
		);

		$products = qode_badges_for_woocommerce_get_cpt_items( $product_query_args );

		if ( is_array( $products ) && count( $products ) ) {
			foreach ( $products as $product_id => $product ) {
				qode_badges_for_woocommerce_badge_regenerate_product_in_database( $product_id );
			}
		}

		do_action( 'qode_badges_for_woocommerce_action_after_save_qbfw_badge' );
	}

	add_action( 'save_post_qbfw_badge', 'qode_badges_for_woocommerce_on_badge_save' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_css_position' ) ) {
	function qode_badges_for_woocommerce_badge_css_position( $badge_id ) {
		$position_style = array();

		$translate_horizontal = 0;
		$translate_vertical   = 0;

		$position_horizontal = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_horizontal_position', true );
		$position_vertical   = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_vertical_position', true );
		$horizontal_offset   = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_horizontal_offset', true );
		$vertical_offset     = get_post_meta( $badge_id, 'qode_badges_for_woocommerce_badge_vertical_offset', true );

		if ( ! empty( $horizontal_offset ) ) {
			$translate_horizontal = qode_badges_for_woocommerce_string_ends_with_allowed_units( $horizontal_offset ) ? $horizontal_offset : intval( $horizontal_offset ) . 'px';
		}
		if ( ! empty( $vertical_offset ) ) {
			$translate_vertical = qode_badges_for_woocommerce_string_ends_with_allowed_units( $vertical_offset ) ? $vertical_offset : intval( $vertical_offset ) . 'px';
		}

		switch ( $position_horizontal ) {
			case 'left':
				$position_style[] = 'left: 0';
				break;
			case 'center':
				$position_style[]     = 'left: 50%';
				$translate_horizontal = '-50%';
				break;
			case 'right':
				$position_style[] = 'right: 0';
				break;
		}

		switch ( $position_vertical ) {
			case 'top':
				$position_style[] = 'top: 0';
				break;
			case 'middle':
				$position_style[]   = 'top: 50%';
				$translate_vertical = '-50%';
				break;
			case 'bottom':
				$position_style[] = 'bottom: 0';
				break;
		}

		$position_style[] = '--qbfw-translate-x: ' . $translate_horizontal;
		$position_style[] = '--qbfw-translate-y: ' . $translate_vertical;

		return $position_style;
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_prepare_badges_text_print' ) ) {
	function qode_badges_for_woocommerce_prepare_badges_text_print( $text, $params = array() ) {
		echo wp_kses_post( qode_badges_for_woocommerce_prepare_badges_text( $text, $params ) );
	}
}

if ( ! function_exists( 'qode_badges_for_woocommerce_prepare_badges_text' ) ) {
	function qode_badges_for_woocommerce_prepare_badges_text( $text, $params = array() ) {
		$text = apply_filters( 'qode_badges_for_woocommerce_filter_prepare_badges_text', $text, $params );

		return wp_kses_post( qode_badges_for_woocommerce_framework_wp_kses_html( 'content', $text ) );
	}
}
if ( ! function_exists( 'qode_badges_for_woocommerce_badge_posts_columns' ) ) {
	function qode_badges_for_woocommerce_badge_posts_columns( $columns ) {
		$columns_date_title = $columns['date'];
		unset( $columns['date'] );

		// add badge type and add date to the position after it.
		$columns['qode_badge_type']    = esc_html__( 'Type', 'qode-badges-for-woocommerce' );
		$columns['qode_badge_preview'] = esc_html__( 'Preview', 'qode-badges-for-woocommerce' );
		$columns['date']               = $columns_date_title;

		return $columns;
	}

	add_filter( 'manage_qbfw_badge_posts_columns', 'qode_badges_for_woocommerce_badge_posts_columns' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_badge_posts_custom_columns' ) ) {
	function qode_badges_for_woocommerce_badge_posts_custom_columns( $column, $post_id ) {
		$badge_types = apply_filters(
			'qode_badges_for_woocommerce_filter_badge_types',
			array(
				'textual' => esc_html__( 'Textual', 'qode-badges-for-woocommerce' ),
				'image'   => esc_html__( 'Image', 'qode-badges-for-woocommerce' ),
			)
		);

		switch ( $column ) {
			case 'qode_badge_type':
				$type = get_post_meta( $post_id, 'qode_badges_for_woocommerce_badge_type', true );
				echo ! empty( $type ) && isset( $badge_types[ $type ] ) ? esc_html( $badge_types[ $type ] ) : '';
				break;
			case 'qode_badge_preview':
				qode_badges_for_woocommerce_badge_html( $post_id, array( 'table_preview' => true ) );
				break;
		}
	}

	add_action( 'manage_qbfw_badge_posts_custom_column', 'qode_badges_for_woocommerce_badge_posts_custom_columns', 10, 2 );
}
