<?php
/**
 * WC Vendors Template functions
 *
 * Functions for templates
 *
 * @version 2.6.5 - Fix security issues.
 *
 * @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
 *
 * @package WCVendors/Functions
 * @version 2.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}


if ( ! function_exists( 'wcv_get_vendor_order_items' ) ) {
    /**
     * Get HTML for the order items to be shown in emails.
     *
     * @param WC_Order $order Order object.
     * @param array    $args  Arguments.
     *
     * @since 2.0.0
     * @return string
     */
    function wcv_get_vendor_order_items( $order, $args = array() ) {

        ob_start();

        $defaults = array(
            'show_sku'       => false,
            'show_image'     => false,
            'image_size'     => array( 32, 32 ),
            'plain_text'     => false,
            'sent_to_admin'  => false,
            'sent_to_vendor' => false,
            'totals_display' => 'both',
            'vendor_items'   => array(),
            'vendor_id'      => 0,
        );

        $args     = wp_parse_args( $args, $defaults );
        $template = $args['plain_text'] ? 'emails/plain/vendor-order-items.php' : 'emails/vendor-order-items.php';

        wc_get_template(
            $template,
            apply_filters(
                'wcvendors_vendor_order_items_args',
                array(
                    'order'          => $order,
                    'items'          => $args['vendor_items'],
                    'show_sku'       => $args['show_sku'],
                    'show_image'     => $args['show_image'],
                    'image_size'     => $args['image_size'],
                    'plain_text'     => $args['plain_text'],
                    'sent_to_admin'  => $args['sent_to_admin'],
                    'sent_to_vendor' => $args['sent_to_vendor'],
                    'totals_display' => $args['totals_display'],
                    'vendor_id'      => $args['vendor_id'],
                )
            ),
            'woocommerce',
            WCV_TEMPLATE_BASE
        );

        return apply_filters( 'wcvendors_vendor_order_items_table', ob_get_clean(), $order );
    }
}


if ( ! function_exists( 'wcv_get_vendor_item_totals' ) ) {
    /**
     * Get the Vendor order item total rows
     *
     * @param WC_Order $order Order object.
     * @param array    $items Order items.
     * @param int      $vendor_id Vendor ID.
     * @param object   $email The email object.
     * @param string   $totals_display The totals display.
     *
     * @since   2.0.0
     * @version 2.2.3
     * @return array
     */
    function wcv_get_vendor_item_totals( $order, $items, $vendor_id, $email, $totals_display = 'both' ) {

        $product_subtotal    = 0;
        $commission_subtotal = 0;
        $commission_total    = 0;
        $tax                 = 0;
        $shipping            = 0;
        $total_rows          = array();
        $discount            = 0;

        // Calculate vendor specific discount.
        foreach ( $items as $item ) {
            // Get the product discount.
            $discount += ( $item->get_subtotal() - $item->get_total() );
        }

        $vendor_commissions = WCV_Vendors::get_vendor_dues_from_order( $order );

        // Get vendor commission information.
        foreach ( $vendor_commissions as $commission ) {

            if ( absint( $vendor_id ) === absint( $commission['vendor_id'] ) ) {

                $commission_subtotal += $commission['commission'];
                $shipping             = $commission['shipping'];
                $tax                  = $commission['tax'];
                $commission_total    += $commission['total'];
            }
        }

        // Commission subtotal.
        if ( 'commission' === $totals_display || 'both' === $totals_display ) {
            // Commission Subtotal.
            $total_rows['commission_subtotal'] = array(
                'label' => __( 'Commission subtotal:', 'wc-vendors' ),
                'value' => wc_price( $commission_subtotal, array( 'currency' => $order->get_currency() ) ),
            );
        }

        if ( 0 < $discount ) {
            $total_rows['discount'] = array(
                'label' => __( 'Discount:', 'wc-vendors' ),
                'value' => wc_price( -1 * $discount, array( 'currency' => $order->get_currency() ) ),
            );
        }

        // Product subtotals.
        if ( 'product' === $totals_display || 'both' === $totals_display ) {

            foreach ( $items as $item ) {
                $product_subtotal += $item->get_subtotal();
            }

            $total_rows['product_subtotal'] = array(
                'label' => __( 'Product subtotal:', 'wc-vendors' ),
                'value' => wc_price( $product_subtotal, array( 'currency' => $order->get_currency() ) ),
            );
        }

        // Shipping.
        if ( wc_string_to_bool( get_option( 'wcvendors_vendor_give_shipping', 'no' ) ) ) {
            $total_rows['shipping'] = array(
                'label' => __( 'Shipping:', 'wc-vendors' ),
                'value' => wc_price( $shipping, array( 'currency' => $order->get_currency() ) ),
            );
        }

        // Tax.
        if ( wc_string_to_bool( get_option( 'wcvendors_vendor_give_taxes', 'no' ) ) ) {
            $total_rows['tax'] = array(
                'label' => __( 'Tax:', 'wc-vendors' ),
                'value' => wc_price( $tax, array( 'currency' => $order->get_currency() ) ),
            );
        }

        // Payment Method.
        if ( 'yes' === $email->get_option( 'payment_method' ) ) {
            $total_rows['payment_method'] = array(
                'label' => __( 'Payment method:', 'wc-vendors' ),
                'value' => $order->get_payment_method_title(),
            );
        }

        // Commission total.
        if ( 'both' === $totals_display || 'commission' === $totals_display ) {
            // Commission Subtotal.
            $total_rows['commission_total'] = array(
                'label' => __( 'Commission total:', 'wc-vendors' ),
                'value' => wc_price( $commission_total, array( 'currency' => $order->get_currency() ) ),
            );
        }

        // Product totals.
        if ( 'both' === $totals_display || 'product' === $totals_display ) {
            $product_total = $product_subtotal + $shipping + $tax - $discount;
            if ( 0 > $product_total ) {
                $product_total = 0;
            }
            $total_rows['product_total'] = array(
                'label' => __( 'Product total:', 'wc-vendors' ),
                'value' => wc_price( $product_total, array( 'currency' => $order->get_currency() ) ),
            );
        }

        if ( 'none' === $totals_display ) {
            $total_rows = array();
        }

        return apply_filters( 'wcvendors_get_vendor_item_totals', $total_rows, $order, $items, $vendor_id, $totals_display );
    }
}

if ( ! function_exists( 'is_wcv_pro_active' ) ) {
    /**
     * Check if WC Vendors Pro is active
     *
     * @since 2.1.4
     * @return bool True if active false otherwise
     */
    function is_wcv_pro_active() {
        if ( defined( 'WCV_PRO_PLUGIN_FILE' ) ) {
            return true;
        }

        return false;
    }
}

if ( ! function_exists( 'wcv_get_pro_option' ) ) {
    /**
     * Get pro option
     *
     * @since 2.6.2 - Check for pro feature before retrieving option
     * @param string $option_name The option name to retrieve.
     * @param mixed  $default_value Default value if option doesn't exist or Pro is not active.
     * @return mixed The option value or default value.
     */
    function wcv_get_pro_option( $option_name, $default_value = '' ) {

        // If Pro is not active, return default.
        if ( ! is_wcv_pro_active() ) {
            return $default_value;
        }

        return get_option( $option_name, $default_value );
    }
}

if ( ! function_exists( 'wcv_get_sold_by_link' ) ) {
    /**
     * Get the vendor sold by URL
     *
     * @param int    $vendor_id - vendor's id.
     * @param string $css_class - optional css class.
     */
    function wcv_get_sold_by_link( $vendor_id, $css_class = '' ) {
        $class   = isset( $css_class ) ? 'class="' . $css_class . '"' : '';
        $sold_by = WCV_Vendors::is_vendor( $vendor_id )
            ? sprintf( '<a href="%s" %s>%s</a>', WCV_Vendors::get_vendor_shop_page( $vendor_id ), $class, WCV_Vendors::get_vendor_sold_by( $vendor_id ) )
            : get_bloginfo( 'name' );

        $sold_by = apply_filters_deprecated( 'wcv_sold_by_link', array( $sold_by, $vendor_id ), '2.3.0', 'wcvendors_sold_by_link' );
        return apply_filters( 'wcvendors_sold_by_link', $sold_by, $vendor_id );
    }
}


if ( ! function_exists( 'wcv_get_vendor_sold_by' ) ) {
    /**
     * Get vendor sold by
     *
     * @param int $vendor_id The vendor ID.
     * @return string
     */
    function wcv_get_vendor_sold_by( $vendor_id ) {

        $sold_by_label     = __( get_option( 'wcvendors_label_sold_by' ), 'wc-vendors' ); // phpcs:ignore
        $sold_by_separator = __( get_option( 'wcvendors_label_sold_by_separator' ), 'wc-vendors' ); // phpcs:ignore
        $sold_by           = wcv_get_sold_by_link( $vendor_id, 'wcvendors_cart_sold_by_meta' );

        $vendor_sold_by = sprintf(
            apply_filters( 'wcvendors_cart_sold_by_meta_template', '%1$s%2$s %3$s', get_the_ID(), $vendor_id ),
            apply_filters( 'wcvendors_cart_sold_by_meta', $sold_by_label, get_the_ID(), $vendor_id ),
            apply_filters( 'wcvendors_cart_sold_by_meta_separator', $sold_by_separator, get_the_ID(), $vendor_id ),
            $sold_by
        );

        return $vendor_sold_by;
    }
}

/**
 * Generate a dropdown of the PayPal Masspay wallet options.
 *
 * @version 2.4.3 - added
 */
if ( ! function_exists( 'wcv_paypal_masspay_walet_select' ) ) {
    /**
     * Show PayPal Masspay wallet select
     *
     * @param string $option The selected option.
     * @return string
     */
    function wcv_paypal_masspay_walet_select( $option ) {
        $select = '<select name="wcv_paypal_masspay_wallet" id="wcv_paypal_masspay_wallet" class="" style="width: 25em;">';
        foreach ( wcv_paypal_wallet() as $option_key => $option_value ) :
            $select .= '<option value="' . esc_attr( $option_key ) . '"' . selected( $option, $option_key, false ) . '>' . esc_attr( $option_value ) . '</option>';
        endforeach;
        $select .= '</select>';

        return $select;
    }
}
if ( ! function_exists( 'wcv_before_vendor_list' ) ) {
    /**
     * Before vendor list
     *
     * @param string $display_mode - display mode.
     */
    function wcv_before_vendor_list( $display_mode ) {
        $css_class = array( $display_mode );
        $css_class = apply_filters( 'wcvendors_vendor_list_open_class', $css_class );
        $css_class = array_map( 'strtolower', $css_class );
        $css_class = implode( ' ', $css_class );
        echo wp_kses(
            sprintf(
                apply_filters(
                    'wcvendors_vendor_list_open',
                    '<ul class="wcv_vendorslist %s">'
                ),
                esc_attr( $css_class )
            ),
            wcv_allowed_html_tags()
        );
    }
}

if ( ! function_exists( 'wcv_after_vendor_list' ) ) {
    /**
     * Before vendor list
     */
    function wcv_after_vendor_list() {
        echo wp_kses(
            apply_filters( 'wcvendors_vendor_list_close', '</ul>' ),
            wcv_allowed_html_tags()
        );
    }
}

if ( ! function_exists( 'wcv_vendor_list_loop' ) ) {
    /**
     * Vendor list loop
     *
     * @param array $vendors array of vendors.
     */
    function wcv_vendor_list_loop( $vendors ) {
        if ( is_object( $vendors ) ) {
            $vendors = array( $vendors );
        }

        ob_start();

        foreach ( $vendors as $vendor ) {

            if ( is_int( $vendor ) ) {
                $vendor = get_user_by( 'id', $vendor );
            }

            if ( ! $vendor ) {
                continue;
            }

            if ( ! WCV_Vendors::is_vendor( $vendor->ID ) ) {
                continue;
            }

            $vendor_avatar = wcv_get_vendor_avatar( $vendor->ID );
            $store_phone   = isset( $vendor->_wcv_store_phone ) ? $vendor->_wcv_store_phone : '';
            $store_address = isset( $vendor->_wcv_store_address1 ) ? $vendor->_wcv_store_address1 : '';
            $shop_name     = isset( $vendor->pv_shop_name ) ? $vendor->pv_shop_name : '';
            $shop_desc     = isset( $vendor->pv_shop_description ) ? $vendor->pv_shop_description : '';
            wc_get_template(
                'vendor-list-loop.php',
                array(
                    'shop_link'        => WCV_Vendors::get_vendor_shop_page( $vendor->ID ),
                    'shop_name'        => $shop_name ? $shop_name : get_userdata( $vendor->ID )->display_name,
                    'vendor_id'        => $vendor->ID,
                    'shop_description' => $shop_desc ? $shop_desc : '',
                    'avatar'           => $vendor_avatar,
                    'phone'            => $store_phone ? $store_phone : __( 'Not available', 'wc-vendors' ),
                    'address'          => $store_address ? $store_address : __( 'Not available', 'wc-vendors' ),
                ),
                'wc-vendors/front/',
                WCV_PLUGIN_DIR . 'templates/front/'
            );
        }
        $output = ob_get_clean();
        echo wp_kses(
            $output,
            wcv_allowed_html_tags()
        );
    }
}

if ( ! function_exists( 'wcv_vendor_list_filter' ) ) {
    /**
     * Vendor list filter
     *
     * @param  string $display_mode List display mode.
     * @param  string $search_term search term.
     * @param  string $vendor_count vendor count.
     */
    function wcv_vendor_list_filter( $display_mode, $search_term, $vendor_count ) {

        ob_start();
        wc_get_template(
            'vendor-list-filter.php',
            array(
                'display_mode'  => $display_mode,
                'search_term'   => $search_term,
                'vendors_count' => $vendor_count,
            ),
            'wc-vendors/front/',
            WCV_PLUGIN_DIR . 'templates/front/'
        );
        $output = ob_get_clean();

        echo wp_kses(
            $output,
            wcv_allowed_html_tags()
        );
    }
}

if ( ! function_exists( 'wcv_get_icon' ) ) {
    /**
     * Get the svg icon
     *
     * @param string $wrapper_class - wrapper class.
     * @param string $id - id.
     * @return string
     */
    function wcv_get_icon( $wrapper_class = '', $id = '' ) {
        $time          = filemtime( WCV_PLUGIN_DIR . 'assets/svg/wcv-icons.svg' );
        $icon_file_url = WCV_ASSETS_URL . 'svg/wcv-icons.svg?t=' . $time;
        $icon          = sprintf(
            '<svg class="%s"><use xlink:href="%s#%s"></use></svg>',
            esc_attr( $wrapper_class ),
            esc_url( $icon_file_url ),
            esc_attr( $id )
        );

        return $icon;
    }
}

if ( ! function_exists( 'wcv_svg_icon_allowed_tags' ) ) {
    /**
     * Allowed HTML tags for escaping an inline SVG icon produced by wcv_get_icon().
     *
     * Centralises the wp_kses() allow-list so the icon-escaping ruleset lives in one place.
     *
     * @since 2.7.0
     *
     * @return array Allowed tags map for wp_kses().
     */
    function wcv_svg_icon_allowed_tags() {
        return array(
            'svg' => array(
                'class' => array(),
            ),
            'use' => array(
                'xlink:href' => array(),
            ),
        );
    }
}
