<?php

namespace Discountify\Utils;

use Discountify;

defined( 'ABSPATH' ) || exit;

/**
 * Helper function
 */
class Helper {

	use Singleton;

	/**
	 * Html markup validation
	 */
	public static function kses( $raw ) {
		$allowed_tags = array(
			'a'                             => array(
				'class'  => array(),
				'href'   => array(),
				'rel'    => array(),
				'title'  => array(),
				'target' => array(),
			),
			'input'                         => array(
				'value'       => array(),
				'type'        => array(),
				'size'        => array(),
				'name'        => array(),
				'checked'     => array(),
				'placeholder' => array(),
				'id'          => array(),
				'class'       => array(),
				'data-name'   => array(),
			),

			'select'                        => array(
				'value'       => array(),
				'type'        => array(),
				'size'        => array(),
				'name'        => array(),
				'placeholder' => array(),
				'id'          => array(),
				'class'       => array(),
				'multiple'    => array(),
				'data-option' => array(),
				'data-name'   => array(),
				'data-id'     => array(),
			),
			'option'                        => array(
				'selected' => array(),
				'value'    => array(),
				'disabled' => array(),
			),
			'textarea'                      => array(
				'value'       => array(),
				'type'        => array(),
				'size'        => array(),
				'name'        => array(),
				'rows'        => array(),
				'cols'        => array(),
				'placeholder' => array(),
				'id'          => array(),
				'class'       => array(),
			),
			'abbr'                          => array(
				'title' => array(),
			),
			'b'                             => array(),
			'blockquote'                    => array(
				'cite' => array(),
			),
			'cite'                          => array(
				'title' => array(),
			),
			'code'                          => array(),
			'del'                           => array(
				'datetime' => array(),
				'title'    => array(),
			),
			'dd'                            => array(),
			'div'                           => array(
				'data'  => array(),
				'class' => array(),
				'id'    => array(),
				'title' => array(),
				'style' => array(),
			),
			'dl'                            => array(),
			'dt'                            => array(),
			'em'                            => array(),
			'h1'                            => array(
				'class' => array(),
			),
			'h2'                            => array(
				'class' => array(),
			),
			'h3'                            => array(
				'class' => array(),
			),
			'h4'                            => array(
				'class' => array(),
			),
			'h5'                            => array(
				'class' => array(),
			),
			'h6'                            => array(
				'class' => array(),
			),
			'i'                             => array(
				'class' => array(),
			),
			'img'                           => array(
				'alt'    => array(),
				'class'  => array(),
				'height' => array(),
				'src'    => array(),
				'width'  => array(),
			),
			'li'                            => array(
				'class' => array(),
			),
			'ol'                            => array(
				'class' => array(),
			),
			'p'                             => array(
				'class' => array(),
			),
			'q'                             => array(
				'cite'  => array(),
				'title' => array(),
			),
			'span'                          => array(
				'class' => array(),
				'title' => array(),
				'style' => array(),
			),
			'small'                         => array(
				'class' => array(),
				'title' => array(),
				'style' => array(),
			),
			'iframe'                        => array(
				'width'       => array(),
				'height'      => array(),
				'scrolling'   => array(),
				'frameborder' => array(),
				'allow'       => array(),
				'src'         => array(),
			),
			'strike'                        => array(),
			'br'                            => array(),
			'strong'                        => array(),
			'data-wow-duration'             => array(),
			'data-wow-delay'                => array(),
			'data-wallpaper-options'        => array(),
			'data-stellar-background-ratio' => array(),
			'ul'                            => array(
				'class' => array(),
			),
			'label'                         => array(
				'class' => array(),
				'for'   => array(),
			),
		);

		if ( function_exists( 'wp_kses' ) ) { // WP is here
			return wp_kses( $raw, $allowed_tags );
		} else {
			return $raw;
		}
	}

	/**
	 * Show Notices
	 */
	public static function push( $notice ) {

		$defaults = array(
			'id'               => '',
			'type'             => 'info',
			'show_if'          => true,
			'message'          => '',
			'class'            => 'active-notice',
			'dismissible'      => false,
			'btn'              => array(),
			'dismissible-meta' => 'user',
			'dismissible-time' => WEEK_IN_SECONDS,
			'data'             => '',
		);

		$notice = wp_parse_args( $notice, $defaults );

		$classes = array( 'notice', 'notice' );

		$classes[] = $notice['class'];

		if ( isset( $notice['type'] ) ) {
			$classes[] = 'notice-' . $notice['type'];
		}

		// Is notice dismissible?
		if ( true === $notice['dismissible'] ) {
			$classes[] = 'is-dismissible';

			// Dismissable time.
			$notice['data'] = ' dismissible-time=' . esc_attr( $notice['dismissible-time'] ) . ' ';
		}

		// Notice ID.
		$notice_id    = 'sites-notice-id-' . $notice['id'];
		$notice['id'] = $notice_id;

		if ( ! isset( $notice['id'] ) ) {
			$notice_id    = 'sites-notice-id-' . $notice['id'];
			$notice['id'] = $notice_id;
		} else {
			$notice_id = $notice['id'];
		}

		$notice['classes'] = implode( ' ', $classes );

		// User meta.
		$notice['data'] .= ' dismissible-meta=' . esc_attr( $notice['dismissible-meta'] ) . ' ';

		if ( 'user' === $notice['dismissible-meta'] ) {
			$expired = get_user_meta( get_current_user_id(), $notice_id, true );
		} elseif ( 'transient' === $notice['dismissible-meta'] ) {
			$expired = get_transient( $notice_id );
		}

		// Notice visible after transient expire.
		if ( isset( $notice['show_if'] ) ) {
			if ( true === $notice['show_if'] ) {
				// Is transient expired?
				if ( false === $expired || empty( $expired ) ) {
					self::markup( $notice );
				}
			}
		} else {
			self::markup( $notice );
		}
	}

	/**
	 * Markup Notice.
	 */
	public static function markup( $notice = array() ) {
		?>
		<div id="<?php echo esc_attr( $notice['id'] ); ?>" class="<?php echo esc_attr( $notice['classes'] ); ?>" <?php echo esc_html( $notice['data'] ); ?>>
			<p>
				<?php echo esc_html( $notice['message'] ); ?>
			</p>

			<?php if ( ! empty( $notice['btn'] ) ) : ?>
				<p>
					<a href="<?php echo esc_url( $notice['btn']['url'] ); ?>" class="button-primary"><?php echo esc_html( $notice['btn']['label'] ); ?></a>
				</p>
			<?php endif; ?>
		</div>
		<?php
	}

	/**
	 * Admin pages array
	 *
	 * @return array
	 */
	public static function admin_pages_id() {
		$admin_pages = array(
			'toplevel_page_discountify',
			'discountify_page_discountify_overview',
			'discountify_page_discountify-license',
			'discountify_page_discountify-rules',
		);
		return $admin_pages;
	}

	/**
	 * woo products array
	 */
	public static function get_products() {
		if ( ! class_exists( 'WooCommerce' ) || ! function_exists( 'wc_get_products' ) ) {
			return array();
		}

		$products = wc_get_products(
			array(
				'status' => 'publish',
				'limit'  => -1,
				'return' => 'objects',
			)
		);

		$products_arr = array(
			'all' => esc_html__( 'All Products', 'discountify' ),
		);
		foreach ( $products as $product ) {
			$id = $product->get_id();

			// Add parent product
			$products_arr[ $id ] = $product->get_name();

			// Check if product is variable and add variations with attributes
			if ( $product->is_type( 'variable' ) ) {
				$variation_ids = $product->get_children();
				foreach ( $variation_ids as $variation_id ) {
					$variation_obj = wc_get_product( $variation_id );

					if ( ! $variation_obj || ! $variation_obj->exists() ) {
						continue;
					}

					// Get attribute names for this variation
					$attributes           = array();
					$variation_attributes = $variation_obj->get_attributes();

					if ( ! empty( $variation_attributes ) && is_array( $variation_attributes ) ) {
						foreach ( $variation_attributes as $attr_key => $attr_value ) {
							// Get the attribute taxonomy/name
							$attribute_name = wc_attribute_label( $attr_key, $product );

							// Get the term name if it's a taxonomy attribute
							if ( taxonomy_exists( $attr_key ) ) {
								$term = get_term_by( 'slug', $attr_value, $attr_key );
								if ( $term && ! is_wp_error( $term ) ) {
									$attr_value = $term->name;
								}
							}

							// Format the attribute value for non-taxonomy attributes
							if ( ! empty( $attr_value ) ) {
								$clean_attr_value = ucfirst( str_replace( array( '-', '_' ), ' ', $attr_value ) );
								$attributes[]     = $attribute_name . ': ' . $clean_attr_value;
							}
						}
					}

					// Create variation name with attributes
					if ( ! empty( $attributes ) ) {
						$variation_name                = $product->get_name() . ' [' . implode( ', ', $attributes ) . ']';
						$products_arr[ $variation_id ] = $variation_name;
					} else {
						// Fallback to variation name if no attributes
						$products_arr[ $variation_id ] = $variation_obj->get_name();
					}
				}
			}
		}

		return $products_arr;
	}

	/**
	 * Settings key
	 *
	 * @return array
	 */
	public static function get_settings_key() {
		$settings_key = array(
			'cart_rules'           => 'no',
			'cart_total'           => array(),
			'dis_cart_total_label' => '',
			'cart_discount_in'     => 'cart_total',
			// Style settings
			'label_text_color'     => '#000',
			'label_bg_color'       => '#ffffff',
			'label_box_shadow'     => '0 4px 4px rgba(0, 0, 0, 0.15)',
			'label_border_color'   => '#ffffff',
			'label_border_width'   => '0',
			'label_border_radius'  => '20',
		);

		return $settings_key;
	}

	/**
	 * Settings key
	 *
	 * @return array
	 */
	public static function get_all_rules( $limit = -1, $meta_data = array() ) {
		$meta_query = array();
		$args       = array(
			'post_type'      => 'discountify_rules',
			'posts_per_page' => $limit,
		);

		if ( ! empty( $meta_data ) ) {
			foreach ( $meta_data as $key => $value ) {
				array_push( $meta_query, $value );
			}

			$args['meta_query'] = $meta_query;
		}

		// WPML: discount rules are not translatable content — query across all
		// languages so rules created in any admin language apply on every frontend.
		global $sitepress;
		$switched_lang = false;
		if ( isset( $sitepress ) && is_object( $sitepress ) ) {
			$current_lang  = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : 'en';
			$sitepress->switch_lang( 'all' );
			$switched_lang = true;
		}

		$all_rules = get_posts( $args );

		if ( $switched_lang ) {
			$sitepress->switch_lang( $current_lang );
		}

		return self::get_rules_arr( $all_rules );
	}

	/**
	 * Get single rule
	 *
	 * @param [type] $id
	 * @return array
	 */
	public static function get_single_rule( $id ) {
		$rule = get_post( $id );
		return self::get_rule( $rule );
	}

	public static function get_rule( $value ) {
		$rule_arr = array(
			'ID'                       => '',
			'discount_valid'           => '',
			'rules_type'               => '',
			'enable_rules'             => 'yes',
			'discount_name'            => '',
			'discount_type'            => '',
			'discount_in'              => 'product',
			'filter_by_products'       => '',
			'filter_by_category'       => '',
			'discount_number'          => '',
			'discount_label'           => '',
			'bogo_discount_type'       => '',
			'multi_discounts'          => '',
			'bogo_discount_in'         => '',
			'enable_user_discount'     => '',
			'disc_user_roles'          => array(),
			'user_discount_condition'  => 'all_roles',
			'specific_customers'       => array(),
			// Free Shipping fields
			'free_shipping_based_on'   => 'product',
			'free_shipping_products'   => array(),
			'free_shipping_categories' => array(),
			'free_shipping_min_amount' => '',
			// Cart Adjustment fields
			'cart_discount_on'         => 'cart_total',
		);

		if ( ! empty( $value ) ) {
			$rule_arr['ID']                      = $value->ID;
			$rule_arr['rules_type']              = get_post_meta( $value->ID, 'rules_type', true );
			$enable_rules                        = get_post_meta( $value->ID, 'enable_rules', true );
			$rule_arr['enable_rules']            = ! empty( $enable_rules ) ? $enable_rules : 'yes';
			$rule_arr['discount_name']           = $value->post_title;
			$rule_arr['discount_type']           = get_post_meta( $value->ID, 'discount_type', true );
			$discount_in                         = get_post_meta( $value->ID, 'discount_in', true );
			$rule_arr['discount_in']             = ! empty( $discount_in ) ? $discount_in : 'product';
			$rule_arr['filter_by_products']      = get_post_meta( $value->ID, 'filter_by_products', true );
			$rule_arr['filter_by_category']      = get_post_meta( $value->ID, 'filter_by_category', true );
			$rule_arr['discount_type']           = get_post_meta( $value->ID, 'discount_type', true );
			$rule_arr['discount_number']         = get_post_meta( $value->ID, 'discount_number', true );
			$rule_arr['discount_label']          = get_post_meta( $value->ID, 'discount_label', true );
			$rule_arr['bogo_discount_type']      = get_post_meta( $value->ID, 'bogo_discount_type', true );
			$rule_arr['multi_discounts']         = get_post_meta( $value->ID, 'multi_discounts', true );
			$rule_arr['bogo_discount_in']        = get_post_meta( $value->ID, 'bogo_discount_in', true );
			$rule_arr['enable_user_discount']    = get_post_meta( $value->ID, 'enable_user_discount', true );
			$rule_arr['disc_user_roles']         = get_post_meta( $value->ID, 'disc_user_roles', true );
			$rule_arr['user_discount_condition'] = get_post_meta( $value->ID, 'user_discount_condition', true );
			$rule_arr['specific_customers']      = get_post_meta( $value->ID, 'specific_customers', true );
			$rule_arr['discount_valid']          = get_post_meta( $value->ID, 'discount_valid', true );
			// Free Shipping fields
			$rule_arr['free_shipping_based_on']   = get_post_meta( $value->ID, 'free_shipping_based_on', true );
			$rule_arr['free_shipping_products']   = get_post_meta( $value->ID, 'free_shipping_products', true );
			$rule_arr['free_shipping_categories'] = get_post_meta( $value->ID, 'free_shipping_categories', true );
			$rule_arr['free_shipping_min_amount'] = get_post_meta( $value->ID, 'free_shipping_min_amount', true );
			// Cart Adjustment fields
			$rule_arr['cart_discount_on'] = get_post_meta( $value->ID, 'cart_discount_on', true );
		}

		return $rule_arr;
	}

	/**
	 * Get user list
	 *
	 * @return array
	 */
	public static function user_list() {
		$user_list = array();
		$args      = array( 'fields' => array( 'ID', 'display_name' ) );
		$users     = get_users( $args );

		foreach ( $users as $key => $user ) {
			$full_name              = get_user_meta( $user->ID, 'first_name', true ) . ' ' . get_user_meta( $user->ID, 'last_name', true );
			$user_list[ $user->ID ] = empty( $full_name ) || $full_name === ' ' ? $user->display_name : $full_name;
		}

		return $user_list;
	}

	/**
	 * Get rules data
	 *
	 * @param [type] $all_rules
	 * @return array
	 */
	public static function get_rules_arr( $all_rules ) {
		$rules_arr = array();
		if ( ! empty( $all_rules ) ) {
			$get_rules = self::instance()->rules_type( false );
			foreach ( $all_rules as $key => $value ) {
				$single_rule                    = self::get_rule( $value );
				$single_rule['rules_type_name'] = ! empty( $get_rules[ $single_rule['rules_type'] ] ) ? $get_rules[ $single_rule['rules_type'] ] : '';
				$single_rule['actions']         = '
					<span class="rules-action update-rules" data-id="' . $value->ID . '"><span class="union-edit"></span></span>
					<span class="rules-action delete-rules" data-id="' . $value->ID . '"><span class="union-trash"></span></span>
				';
				array_push( $rules_arr, $single_rule );
			}
		}

		return $rules_arr;
	}

	/**
	 * Admin settings array
	 *
	 * @return array
	 */
	public static function get_settings() {
		$settings     = array();
		$get_settings = get_option( 'discountify_settings', true );
		// Style settings for discount label
		$settings['label_text_color']    = ! empty( $get_settings['label_text_color'] ) ? $get_settings['label_text_color'] : '#ffffff';
		$settings['label_bg_color']      = ! empty( $get_settings['label_bg_color'] ) ? $get_settings['label_bg_color'] : '#56c596';
		$settings['label_box_shadow']    = ! empty( $get_settings['label_box_shadow'] ) ? $get_settings['label_box_shadow'] : '0 4px 4px rgba(0, 0, 0, 0.15)';
		$settings['label_border_color']  = ! empty( $get_settings['label_border_color'] ) ? $get_settings['label_border_color'] : '#56c596';
		$settings['label_border_width']  = isset( $get_settings['label_border_width'] ) ? $get_settings['label_border_width'] : '0';
		$settings['label_border_radius'] = isset( $get_settings['label_border_radius'] ) ? $get_settings['label_border_radius'] : '3';

		return $settings;
	}
	public static function currency_symbol() {
		return class_exists( 'WooCommerce' ) ? get_woocommerce_currency_symbol() : '';
	}

	/**
	 * show % or currency label
	 */
	public static function discount_symbol( $value ) {
		$percent = array( 'percent_product', 'percent', 'percentage' );
		return ( ! empty( $value['discount_type'] ) && in_array( $value['discount_type'], $percent ) ) ? '%' : get_woocommerce_currency_symbol();
	}

	/**
	 * Get condition type
	 */
	public static function get_condtion_type( $type ) {

		$all_condtion = array(
			'>'  => esc_html__( 'Greater Than', 'discountify' ),
			'>=' => esc_html__( 'Greater Than or Equal', 'discountify' ),
			'<'  => esc_html__( 'Less Than', 'discountify' ),
			'<=' => esc_html__( 'Less Than or Equal', 'discountify' ),
		);

		return $all_condtion[ $type ];
	}


	/**
	 * show % or currency label
	 */
	public static function discount_number_with_label( $value, $total = false ) {
		$discount_label = '';

		if ( '' === $value['discount_label'] ) {
			// No custom label, show the discount amount
			$discount_label = '( - ' . $value['discount_number'] . self::discount_symbol( $value ) . ')';
		} else {
			// Custom label exists, show ONLY the label (no amount)
			$discount_label = $value['discount_label'];
		}

		return $discount_label;
	}

	/**
	 * Product  with discount
	 */
	public static function discount_product( $product_id, $filter_by_products ) {
		if ( empty( $filter_by_products ) ) {
			return false;
		}

		// Check if "all" is selected - apply to all products
		if ( $filter_by_products === 'all' || ( is_array( $filter_by_products ) && in_array( 'all', $filter_by_products, true ) ) ) {
			return true;
		}

		if ( ! is_array( $filter_by_products ) ) {
			return false;
		}

		// Cast product_id to int for comparison
		$product_id = (int) $product_id;

		// WPML: resolve translated product ID to the original language ID so rules
		// defined on the default-language product apply to all translations.
		$original_id = self::wpml_original_id( $product_id, 'product' );

		// Cast all filter products to int for proper comparison
		$filter_by_products = array_map( 'intval', $filter_by_products );

		if ( in_array( $product_id, $filter_by_products, true ) ) {
			return true;
		}

		if ( $original_id !== $product_id && in_array( $original_id, $filter_by_products, true ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Return the default-language post ID for a given post when WPML is active.
	 * Returns the original $id unchanged when WPML is not present.
	 *
	 * @param int    $id        Post / term ID.
	 * @param string $post_type Post type string or 'tax/<taxonomy>' for terms.
	 * @return int
	 */
	public static function wpml_original_id( $id, $post_type = 'product' ) {
		if ( ! function_exists( 'icl_object_id' ) && ! has_filter( 'wpml_object_id' ) ) {
			return (int) $id;
		}

		$default_lang = apply_filters( 'wpml_default_language', null );
		$original_id  = apply_filters( 'wpml_object_id', $id, $post_type, false, $default_lang );

		return $original_id ? (int) $original_id : (int) $id;
	}

	/**
	 * Product  with discount
	 */
	public static function discount_category( $product_id, $filter_by_categories ) {
		if ( empty( $filter_by_categories ) ) {
			return false;
		}

		// Check if "all" is selected - apply to all categories
		if ( $filter_by_categories === 'all' || ( is_array( $filter_by_categories ) && in_array( 'all', $filter_by_categories, true ) ) ) {
			return true;
		}

		if ( ! is_array( $filter_by_categories ) ) {
			$filter_by_categories = array( $filter_by_categories );
		}

		$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );

		if ( empty( $product_cats_ids ) ) {
			return false;
		}

		// Cast all IDs to int for proper comparison
		$product_cats_ids     = array_map( 'intval', $product_cats_ids );
		$filter_by_categories = array_map( 'intval', $filter_by_categories );

		if ( count( array_intersect( $product_cats_ids, $filter_by_categories ) ) > 0 ) {
			return true;
		}

		return false;
	}

	/**
	 * Get categories
	 *
	 * @return array
	 */
	public static function get_categories() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			return array();
		}
		$categories = get_categories( array( 'taxonomy' => 'product_cat' ) );
		$cat_arr    = array(
			'all' => esc_html__( 'All Categories', 'discountify' ),
		);
		foreach ( $categories as $key => $value ) {
			if ( $value->slug !== 'uncategorized' ) {
				$cat_arr[ $value->term_id ] = $value->name;
			}
		}

		return $cat_arr;
	}

	/**
	 * Make coupon array
	 *
	 * @return array
	 */
	public static function discount_coupon( $args ) {
		$coupon =
		array(
			'id'                         => 9999999999 . wp_rand( 2, 9 ),
			'amount'                     => $args['amount'],
			'individual_use'             => false,
			'product_ids'                => $args['product_ids'],
			'exclude_product_ids'        => array(),
			'usage_limit'                => '',
			'usage_limit_per_user'       => '',
			'limit_usage_to_x_items'     => '',
			'usage_count'                => '',
			'expiry_date'                => '',
			'apply_before_tax'           => 'yes',
			'free_shipping'              => false,
			'product_categories'         => $args['product_categories'],
			'exclude_product_categories' => $args['exclude_product_categories'],
			'exclude_sale_items'         => false,
			'minimum_amount'             => '',
			'maximum_amount'             => '',
			'customer_email'             => '',
			'discount_type'              => $args['discount_type'],
		);

		return $coupon;
	}
	/**
	 * Get products by category
	 *
	 * @param [type] $id
	 * @return array
	 */
	public static function products_category( $id ) {
		$products = array();
		$term     = get_term_by( 'id', $id, 'product_cat', 'ARRAY_A' );
		$args     = array(
			'limit'    => -1,
			'status'   => 'publish',
			'return'   => 'ids',
			'category' => array( $term['slug'] ),
		);
		$products = wc_get_products( $args );

		return $products;
	}

	/**
	 * check nonce
	 */
	public function verify_nonce( $nonce_index, $nonce_value ) {
		if ( is_null( $nonce_index ) || ! wp_verify_nonce( $nonce_value, $nonce_index ) ) {
			wp_send_json_error(
				array(
					'message' => __( 'Security check failed', 'discountify' ),
					'code'    => 401,
				)
			);
		}
	}

	/**
	 * Is pro active
	 *
	 * @return boolean
	 */
	public static function is_pro_active() {
		if ( class_exists( 'DiscountifyPro' ) ) {
			return true;
		}
		return false;
	}

	/**
	 * Get all rules
	 *
	 * @param boolean $pro_label
	 * @return array
	 */
	public static function rules_type( $pro_label = false, $key = false ) {
		if ( $key ) {
			return array( 'simple', 'cart_adjustment', 'bogo', 'total_spend', 'bulk_pricing', 'free_shipping' );
		} else {
			$pro = '';
			if ( $pro_label ) {
				$pro = self::instance()->is_pro_active() === true ? '' : ' (' . esc_html__( 'Pro', 'discountify' ) . ')';
			}
			return array(
				'simple'          => esc_html__( 'Simple Discount', 'discountify' ),
				'cart_adjustment' => esc_html__( 'Cart Adjustment', 'discountify' ),
				'bogo'            => esc_html__( 'Bogo Offer', 'discountify' ) . $pro,
				'total_spend'     => esc_html__( 'Total Spend', 'discountify' ) . $pro,
				'bulk_pricing'    => esc_html__( 'Bulk Pricing', 'discountify' ) . $pro,
				'free_shipping'   => esc_html__( 'Free Shipping', 'discountify' ) . $pro,
			);
		}
	}

	/**
	 * Check if a rules_type is a BOGO type
	 *
	 * @param string $type
	 * @return bool
	 */
	public static function is_bogo_type( $type ) {
		return in_array( $type, array( 'bogo', 'bogo_free', 'bogo_percent', 'buy_n_get_one', 'buy_x_get_y', 'category_bogo', 'bogo_fixed_price' ), true );
	}

	/**
	 * Get the bogo_discount_type value from a rules_type
	 *
	 * @param string $rules_type
	 * @return string
	 */
	public static function get_bogo_discount_type_from_rules( $rules_type ) {
		$map = array(
			'bogo_free'        => 'bogo_free',
			'bogo_percent'     => 'bogo_percent',
			'buy_n_get_one'    => 'buy_n_get_one',
			'buy_x_get_y'      => 'buy_x_get_y',
			'category_bogo'    => 'category_bogo',
			'bogo_fixed_price' => 'bogo_fixed_price',
		);
		return isset( $map[ $rules_type ] ) ? $map[ $rules_type ] : '';
	}

	/**
	 * Add custom class
	 *
	 * @param [type] $data
	 * @return string
	 */
	public static function d_none_class( $data ) {
		return ( $data === 'yes' ) ? '' : 'd-none';
	}

	/**
	 * Get roles array
	 *
	 * @return array
	 */
	public static function wp_roles_arr() {
		$editable_roles = get_editable_roles();
		foreach ( $editable_roles as $role => $details ) {
			$roles[ esc_attr( $role ) ] = translate_user_role( $details['name'] );
		}

		return $roles;
	}

	/**
	 * Get roles array
	 *
	 * @return string
	 */
	public static function ordinal_no( $number ) {
		$ends = array( 'th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th' );
		if ( ( $number % 100 ) >= 11 && ( $number % 100 ) <= 13 ) {
			$abbreviation = $number . 'th';
		} else {
			$abbreviation = $number . $ends[ $number % 10 ];
		}

		return $abbreviation;
	}
}
