golden hour
/home/godaofbq/hyperbaric-revolution.com/wp-content/plugins/discountify/utils
⬆️ Go Up
Upload
File/Folder
Size
Actions
helper.php
18.51 KB
Del
OK
singleton.php
313 B
Del
OK
Edit: helper.php
<?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 = [ 'a' => [ 'class' => [], 'href' => [], 'rel' => [], 'title' => [], 'target' => [], ], 'input' => [ 'value' => [], 'type' => [], 'size' => [], 'name' => [], 'checked' => [], 'placeholder' => [], 'id' => [], 'class' => [], 'data-name' => [] ], 'select' => [ 'value' => [], 'type' => [], 'size' => [], 'name' => [], 'placeholder' => [], 'id' => [], 'class' => [], 'multiple' => [], 'data-option' => [], 'data-name' => [], 'data-id' => [] ], 'option' => [ 'selected' => [], 'value' => [], 'disabled' => [] ], 'textarea' => [ 'value' => [], 'type' => [], 'size' => [], 'name' => [], 'rows' => [], 'cols' => [], 'placeholder' => [], 'id' => [], 'class' => [], ], 'abbr' => [ 'title' => [], ], 'b' => [], 'blockquote' => [ 'cite' => [], ], 'cite' => [ 'title' => [], ], 'code' => [], 'del' => [ 'datetime' => [], 'title' => [], ], 'dd' => [], 'div' => [ 'data' => [], 'class' => [], 'id' => [], 'title' => [], 'style' => [], ], 'dl' => [], 'dt' => [], 'em' => [], 'h1' => [ 'class' => [], ], 'h2' => [ 'class' => [], ], 'h3' => [ 'class' => [], ], 'h4' => [ 'class' => [], ], 'h5' => [ 'class' => [], ], 'h6' => [ 'class' => [], ], 'i' => [ 'class' => [], ], 'img' => [ 'alt' => [], 'class' => [], 'height' => [], 'src' => [], 'width' => [], ], 'li' => [ 'class' => [], ], 'ol' => [ 'class' => [], ], 'p' => [ 'class' => [], ], 'q' => [ 'cite' => [], 'title' => [], ], 'span' => [ 'class' => [], 'title' => [], 'style' => [], ], 'small' => [ 'class' => [], 'title' => [], 'style' => [], ], 'iframe' => [ 'width' => [], 'height' => [], 'scrolling' => [], 'frameborder' => [], 'allow' => [], 'src' => [], ], 'strike' => [], 'br' => [], 'strong' => [], 'data-wow-duration' => [], 'data-wow-delay' => [], 'data-wallpaper-options' => [], 'data-stellar-background-ratio' => [], 'ul' => [ 'class' => [], ], 'label' => [ 'class' => [], 'for' => [], ], ]; 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 = [] ) { ?> <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') ) { return array(); } $products = get_posts(array('post_type' => 'product', 'posts_per_page' => -1 , 'post_status'=>'publish') ); $products_arr = array(); $is_pro_active = Discountify::is_pro_active(); foreach ($products as $key => $value) { $id = $value->ID; $product = wc_get_product( $id ); if ( 0 == $is_pro_active && ( $product->get_type() !== "variable" && $product->get_type() !== "grouped" ) ) { $products_arr[$id] = $product->get_name(); } else if( 1 == $is_pro_active ){ $products_arr[$id] = $product->get_name(); } } return $products_arr; } /** * Settings key * * @return array */ public static function get_settings_key() { $settings_key = array('cart_rules'=>'no','cart_total'=>array(), 'shipping_discount'=>'no','shipping_cart'=>'', 'dis_cart_total_label'=>'','cart_discount_in' => 'cart_total', 'shipping_discount_in' => 'cart_total', 'shipping_discount_label' => '' ); 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; } $all_rules = get_posts( $args ); 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' => '', 'rules_type' => '', 'enable_rules' => 'yes', 'discount_name' => '', 'discount_type' => '', 'discount_in' => 'product', 'filter_by_products' => '', 'filter_by_category' => '', 'discount_number' => '', 'discount_label' => '', 'discount_label_single' => '', 'bogo_discount_type' => '', 'multi_discounts' => '', 'bogo_discount_in' => '', 'enable_user_discount' => '', 'disc_user_roles' => array(), 'user_discount_condition' => 'user_roles', 'specific_customers' => array() ); if ( !empty($value) ) { $rule_arr['ID'] = $value->ID; $rule_arr['rules_type'] = get_post_meta( $value->ID , 'rules_type' , true ); $rule_arr['enable_rules'] = get_post_meta( $value->ID , 'enable_rules' , true ); $rule_arr['discount_name'] = $value->post_title; $rule_arr['discount_type'] = get_post_meta( $value->ID , 'discount_type' , true ); $rule_arr['discount_in'] = get_post_meta( $value->ID , 'discount_in' , true ); $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['discount_label_single'] = get_post_meta( $value->ID , 'discount_label_single' , 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 ); } 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 = Discountify\Utils\Helper::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); $settings['cart_rules'] = !empty($get_settings['cart_rules']) ? $get_settings['cart_rules'] : 'no'; $settings['cart_discount_in'] = !empty($get_settings['cart_discount_in']) ? $get_settings['cart_discount_in'] : 'cart_total'; $settings['cart_total'] = !empty($get_settings['cart_total']) ? $get_settings['cart_total'] : array(array()); $settings['dis_cart_total_label'] = !empty($get_settings['dis_cart_total_label']) ? $get_settings['dis_cart_total_label'] : esc_html__('Cart Discount','discountify'); $settings['shipping_discount'] = !empty($get_settings['shipping_discount']) ? $get_settings['shipping_discount'] : 'no'; $settings['shipping_cart'] = !empty($get_settings['shipping_cart']) ? $get_settings['shipping_cart'] : array(array()); $settings['shipping_discount_in'] = !empty($get_settings['shipping_discount_in']) ? $get_settings['shipping_discount_in'] : 'cart_total'; $settings['shipping_discount_label']= !empty($get_settings['shipping_discount_label']) ? $get_settings['shipping_discount_label'] : esc_html__('Shipping Discount','discountify'); 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(); } /** * show discount label only */ public static function discount_label($value) { if ( empty( $value['discount_label'] ) ) { $discount_label = ''; } if ( $value['rules_type'] == 'simple' ) { $discount_label = $value['discount_label'] == '' ? $value['discount_number'] . self::discount_symbol($value) : $value['discount_label']; } else{ $discount_label = $value['discount_label']; } return $discount_label; } /** * show % or currency label */ public static function discount_number_with_label($value,$total = false) { $discount_label = ''; if ( '' == $value['discount_label'] ) { $discount_label = '( - ' . $value['discount_number']. self::discount_symbol($value) .')'; } else { if ( $total ) { $discount_label = $value['discount_label'] . ' '.self::discount_symbol($value); } else { $discount_label = $value['discount_label'] . ' '. $value['discount_number'] . self::discount_symbol($value); } } return $discount_label; } /** * Product with discount */ public static function discount_product($product_id,$filter_by_products) { if ( is_array($filter_by_products) && in_array( $product_id , $filter_by_products )) { return true; } return false; } /** * Product with discount */ public static function discount_category($product_id,$filter_by_categories) { $product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' ); if ( is_array($filter_by_categories) && 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(); 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 . 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','bogo','total_spend','bulk_pricing'); }else{ $pro=''; if ($pro_label) { $pro = Discountify\Utils\Helper::instance()->is_pro_active() == true ? '' : ' ('.esc_html__('Pro','discountify').')'; } return array( 'simple' => esc_html__('Simple Discount','discountify') , 'bogo' => esc_html__('Bogo Offer'.$pro,'discountify'), 'total_spend' => esc_html__('Total Spend'.$pro,'discountify'), 'bulk_pricing' => esc_html__('Bulk Pricing'.$pro,'discountify') ); } } /** * 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; } }
Save