<?php
/**
 * The main WCVendors Pro Dashboard class
 *
 * This is the main controller class for the dashboard, all actions are defined in this class.
 *
 * @package    WCVendors_Pro
 * @subpackage WCVendors_Pro/public
 */

/**
 * Class WCVendors_Pro_Dashboard
 */
class WCVendors_Pro_Dashboard {

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $wcvendors_pro The ID of this plugin.
     */
    private $wcvendors_pro;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string $version The current version of this plugin.
     */
    private $version;

    /**
     * Is the plugin in debug mode
     *
     * @since    1.0.0
     * @access   private
     * @var      bool $debug plugin is in debug mode
     */
    private $debug;

    /**
     * Is the plugin in debug mode
     *
     * @since    1.0.0
     * @access   private
     * @var      array $dashboard_pages an array of dashboard pages
     */
    private $dashboard_pages = array();

    /**
     * Is the plugin base directory
     *
     * @since    1.0.0
     * @access   private
     * @var      string $base_dir string path for the plugin directory
     */
    private $base_dir;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     *
     * @param      string $wcvendors_pro The name of the plugin.
     * @param      string $version       The version of this plugin.
     * @param bool   $debug Is debug enabled.
     */
    public function __construct( $wcvendors_pro, $version, $debug ) {

        $this->wcvendors_pro = $wcvendors_pro;
        $this->version       = $version;
        $this->debug         = $debug;
        $this->base_dir      = plugin_dir_path( WCV_PRO_PLUGIN_FILE );
    }

    /**
     * Load the dasboard based on the query vars loaded.
     *
     * @since    1.0.0
     */
    public function load_dashboard() {

        $dashboard_page_ids = (array) get_option( 'wcvendors_dashboard_page_id', array() );

        if ( empty( $dashboard_page_ids ) ) {
            echo '<h2>' . esc_html__( 'Please ensure you have set a page for the Pro Dashboard.', 'wcvendors-pro' ) . '</h2>';
            exit;
        }

        ob_start();

        global $wp;

        if ( isset( $wp->query_vars['object'] ) ) {

            $type     = get_query_var( 'object' );
            $action   = get_query_var( 'action' );
            $id       = get_query_var( 'object_id' );
            $template = get_query_var( 'template' );
            $custom   = get_query_var( 'custom' );

            $this->load_page( $type, $action, $id, $template, $custom );

        } else {

            $this->load_page();
        }

        return ob_get_clean();
    }

    /**
     * Output the requested page for the dashboard
     *
     * @since    1.0.0
     *
     * @param    string $object object type.
     * @param    string $action page action.
     * @param    int    $object_id the object's post id.
     * @param    string $template template file.
     * @param    string $custom custom page.
     */
    public function load_page( $object = 'dashboard', $action = '', $object_id = null, $template = null, $custom = null ) { // phpcs:ignore

        // Permission check for all dashboard pages.
        if ( ! $this->can_view_dashboard() ) {
            $this->maybe_load_application_form();
            return false;
        }

        // Has the page been disabled ?
        if ( ! $this->page_disabled() ) {
            $return_url = $this->get_dashboard_page_url();
            wc_get_template( 'permission.php', array( 'return_url' => $return_url ), 'wc-vendors/dashboard/', $this->base_dir . 'templates/dashboard/' );

            return false;
        }

        // Does the user own this object ?
        if ( ! empty( $object_id ) && ! $this->check_object_permission( $object, $object_id ) ) {
            $return_url = $this->get_dashboard_page_url();
            wc_get_template( 'permission.php', array( 'return_url' => $return_url ), 'wc-vendors/dashboard/', $this->base_dir . 'templates/dashboard/' );

            return false;
        }
        // Include the dashboard wrapper.
        include_once apply_filters( 'wcvendors_pro_dashboard_open_path', $this->base_dir . 'public/partials/wcvendors-pro-dashboard-open.php' );

        do_action_deprecated( 'wcv_pro_before_dashboard', array(), '1.7.10', 'wcvendors_pro_before_dashboard' );
        do_action( 'wcvendors_pro_before_dashboard' );

        // Create the menu.
        $this->create_nav();

        // Print woocommerce notices.
        wc_print_notices();

        // Vendor Store Notice.
        $vendor_dashboard_notice = get_option( 'wcvendors_vendor_dashboard_notice', false );

        if ( $vendor_dashboard_notice ) {

            wc_get_template(
                'dashboard-notice.php',
                array(
                    'vendor_dashboard_notice' => $vendor_dashboard_notice,
                    'notice_type'             => 'message',
                ),
                'wc-vendors/dashboard/',
                $this->base_dir . '/templates/dashboard/'
            );
        }

        do_action_deprecated( 'wcv_pro_after_dashboard_nav', array(), '1.7.10', 'wcvendors_pro_after_dashboard_nav' );
        do_action( 'wcvendors_pro_after_dashboard_nav' );

        // if action is set send to edit page with or without object_id else list type.
        if ( 'edit' === $action ) {

            $template_name = '';
            $custom_pages  = self::get_custom_pages();
            $base_dir      = ( array_key_exists( $object, $custom_pages ) ) ? $custom_pages[ $object ]['base_dir'] : $this->base_dir . 'templates/dashboard/';

            $base_dir = apply_filters( 'wcv_dashboard_template_base_dir', $base_dir, $object, $action, $template );

            // Load the form template based on options in backend.
            $template_name = apply_filters( 'wcv_dashboard_template_name', ( 'product' === $object && ! empty( $template ) ) ? $object . '-' . $template : $object . '-' . $action, $object, $template, $action );

            wc_get_template(
                $template_name . '.php',
                array(
                    'action'    => $action,
                    'template'  => $template,
                    'object_id' => $object_id,
                ),
                'wc-vendors/dashboard/',
                $base_dir
            );

        } else { // phpcs:ignore

            // Load the custom template.
            if ( ! empty( $custom ) ) {

                $custom_pages = self::get_custom_pages();
                // Output the template if it is defined.
                if ( array_key_exists( 'template_name', $custom_pages[ $custom ] ) ) {
                    wc_get_template(
                        $custom_pages[ $custom ]['template_name'] . '.php',
                        $custom_pages[ $custom ]['args'],
                        'wc-vendors/dashboard/',
                        $custom_pages[ $custom ]['base_dir']
                    );
                } else {
                    // Allow hooking into the custom template system to not require a template.
                    do_action( 'wcv_pro_dashboard_custom_template', $object, $object_id, $template, $custom );
                }
            } else { // phpcs:ignore

                // If the object is a post type then generate a table, otherwise load the custom template.
                if ( post_type_exists( $object ) ) {

                    // Use the internal table generator to create object list.
                    $object_table = new WCVendors_Pro_Table_Helper( $this->wcvendors_pro, $this->version, $object, $object, get_current_user_id() );
                    $object_table->display();

                } else {

                    switch ( $object ) {
                        case 'order':
                            $this->load_order_page();
                            break;
                        case 'commission':
                            $this->load_commission_page();
                            break;
                        case 'rating':
                            $this->load_rating_page();
                            break;
                        case 'settings':
                            $this->load_settings_page();
                            break;
                        case 'dashboard':
                            $this->dashboard_quick_links();
                            $store_report = new WCVendors_Pro_Reports_Controller( $this->wcvendors_pro, $this->version, $this->debug );
                            $store_report->report_init();
                            $store_report->display();
                            break;
                        default:
                            do_action( 'wcv_pro_dashboard_custom_page', $object, $object_id, $template, $custom );
                            break;
                    }
                }
            }
        }

        do_action( 'wcv_pro_after_dashboard' );

        include_once apply_filters( 'wcvendors_pro_dashboard_close_path', $this->base_dir . 'public/partials/wcvendors-pro-dashboard-close.php' );
    }

    /**
     * Generate the page URL based on the dashboard page id set in options
     *
     * @since   1.0.0
     * @version 1.5.4
     *
     * @param string $page page type to output.
     */
    public static function get_dashboard_page_url( $page = '' ) {

        $dashboard_page_ids = (array) get_option( 'wcvendors_dashboard_page_id', array() );
        $dashboard_page_id  = reset( $dashboard_page_ids );
        $dashboard_url      = apply_filters( 'wcv_my_account_dashboard_url', get_permalink( $dashboard_page_id ) );

        return $dashboard_url . $page;
    }

    /**
     * Provide quick links on the dashboard to reduce click through
     *
     * @since    1.1.5
     * @version  1.8.4 - Added can submit handler
     */
    public function get_dashboard_quick_links() {

        $products_disabled  = wc_string_to_bool( get_option( 'wcvendors_product_management_cap', 'no' ) );
        $coupons_disabled   = wc_string_to_bool( get_option( 'wcvendors_shop_coupon_management_cap', 'no' ) );
        $lock_edit_products = ( 'yes' === get_user_meta( get_current_user_id(), '_wcv_lock_edit_products_vendor', true ) ) ? true : false;
        $lock_new_products  = ( 'yes' === get_user_meta( get_current_user_id(), '_wcv_lock_new_products_vendor', true ) ) ? true : false;

        $quick_links      = array();
        $add_product_link = WCVendors_Pro_Product_Controller::get_default_product_template();
        $product_ids      = WCVendors_Pro_Vendor_Controller::get_products_by_id( get_current_user_id() );
        $can_submit       = wc_string_to_bool( get_option( 'wcvendors_capability_products_enabled', 'no' ) );

        if ( empty( $product_ids ) ) {
            $coupons_disabled = true;
        }
        if ( ! wc_coupons_enabled() ) {
            $coupons_disabled = true;
        }

        if ( ! $products_disabled ) {
            $quick_links['product'] = array(
                'url'   => apply_filters( 'wcv_add_product_url', self::get_dashboard_page_url( $add_product_link['url_path'] ) ),
                'label' => __( 'Add product', 'wcvendors-pro' ),
            );

            if ( ! $can_submit ) {
                unset( $quick_links['product'] );
            }
        }
        if ( ! $coupons_disabled ) {
            $quick_links['shop_coupon'] = array(
                'url'   => self::get_dashboard_page_url( 'shop_coupon/edit' ),
                'label' => __( 'Add coupon', 'wcvendors-pro' ),
            );
        }

        if ( $lock_edit_products || $lock_new_products ) {
            unset( $quick_links['product'] );
        }

        return apply_filters( 'wcv_dashboard_quick_links', $quick_links );
    }

    /**
     * Get usage stats array.
     *
     * @return array
     */
    public function get_dashboard_usage_stats() {
        $user_id = get_current_user_id();
        $limits  = WCVendors_Pro_Upload_Limits::get_instance( $user_id );

        $stats = array();
        if ( wc_string_to_bool( get_option( 'wcvendors_dashboard_show_disk_usage', 'no' ) ) ) {
            $disk_limit = $limits->format_disk_size( $limits->get_disk_usage_limit() );
            $disk_usage = $limits->format_disk_usage_size( $limits->get_disk_usage() );

            $stats['disk-limit'] = array(
                'usage'    => $disk_usage,
                'limit'    => $disk_limit,
                'over'     => $limits->disk_limit_reached(),
                'icon'     => 'wcv-icon-hdd',
                'template' => '%s / %s',
            );
        }
        if ( wc_string_to_bool( get_option( 'wcvendors_dashboard_show_files_usage', 'no' ) ) ) {
            $files_limit = $limits->get_files_count_limit();
            $files_limit = 0 === $files_limit ? __( 'Unlimited', 'wcvendors-pro' ) : $files_limit;
            $files       = $limits->get_media();
            $files_usage = ! empty( $files ) ? count( $files ) : 0;

            $stats['file-limit'] = array(
                'usage'    => $files_usage,
                'limit'    => $files_limit,
                'over'     => $limits->files_limit_reached(),
                'icon'     => 'wcv-icon-file-alt',
                /* translators: %1$s file usage, %2$s file limit */
                'template' => __( '%1$s / %2$s files', 'wcvendors-pro' ),
            );
        }

        /**
         * Hook to this filter to change the stat buttons.
         */
        return apply_filters( 'wcvendors_dashboard_usage_stats', $stats, $user_id );
    }

    /**
     * Provide quick links on the dashboard to reduce click through
     *
     * @since    1.1.5
     */
    public function dashboard_quick_links() {

        $quick_links = $this->get_dashboard_quick_links();
        $stats       = $this->get_dashboard_usage_stats();

        wc_get_template(
            'quick-links.php',
            array(
                'quick_links' => $quick_links,
                'stats'       => $stats,
            ),
            'wc-vendors/dashboard/',
            $this->base_dir . 'templates/dashboard/'
        );
    }

    /**
     * Available dashboard urls for front end functionality
     *
     * @since    1.0.0
     * @version  1.8.4 - added can submit handler.
     */
    public function get_dashboard_pages() {

        $disable_duplicate  = ! wc_string_to_bool( get_option( 'wcvendors_capability_product_duplicate', 'no' ) );
        $lock_edit_products = get_user_meta( get_current_user_id(), '_wcv_lock_edit_products_vendor', true );
        $can_submit         = wc_string_to_bool( get_option( 'wcvendors_capability_products_enabled', 'no' ) );

        $this->dashboard_pages['product'] = array(
            'slug'    => 'product',
            'id'      => 'product',
            'label'   => __( 'Products', 'wcvendors-pro' ),
            'actions' => array(
                'edit'      => __( ' Edit', 'wcvendors-pro' ),
                'duplicate' => __( ' Duplicate', 'wcvendors-pro' ),
                'delete'    => __( ' Delete', 'wcvendors-pro' ),
            ),
        );

        if ( $disable_duplicate || $lock_edit_products ) {
            unset( $this->dashboard_pages['product']['actions']['duplicate'] );
        }

        if ( ! $can_submit || $lock_edit_products ) {
            unset( $this->dashboard_pages['product']['actions']['edit'] );
            unset( $this->dashboard_pages['product']['actions']['duplicate'] );
        }

        $this->dashboard_pages['order'] = array(
            'slug'    => 'order',
            'id'      => 'order',
            'label'   => __( 'Orders', 'wcvendors-pro' ),
            'actions' => array(),
        );

        $this->dashboard_pages['settings'] = array(
            'slug'    => 'settings',
            'id'      => 'settings',
            'label'   => __( 'Settings', 'wcvendors-pro' ),
            'actions' => array(),
        );

        $this->dashboard_pages['rating'] = array(
            'slug'    => 'rating',
            'id'      => 'rating',
            'label'   => __( 'Ratings', 'wcvendors-pro' ),
            'actions' => array(),
        );

        $this->dashboard_pages['commission'] = array(
            'slug'    => 'commission',
            'id'      => 'commission',
            'label'   => __( 'Commission', 'wcvendors-pro' ),
            'actions' => array(),
        );

        if ( 'yes' === get_option( 'woocommerce_enable_coupons' ) ) {

            $this->dashboard_pages['shop_coupon'] = array(
                'slug'    => 'shop_coupon',
                'id'      => 'shop_coupon',
                'label'   => __( 'Coupons', 'wcvendors-pro' ),
                'actions' => array(
                    'edit'   => __( 'Edit', 'wcvendors-pro' ),
                    'delete' => __( 'Delete', 'wcvendors-pro' ),
                ),
            );

        }

        return apply_filters( 'wcv_pro_dashboard_urls', $this->dashboard_pages );
    }

    /**
     * Load the orders table
     *
     * @since    1.0.0
     */
    public function load_order_page() {

        $wcvendors_pro_order_controller = new WCVendors_Pro_Order_Controller( $this->wcvendors_pro, $this->version, $this->debug );
        $wcvendors_pro_order_controller->display();
    }

    /**
     * Load the commission table
     *
     * @since    1.8.4
     */
    public function load_commission_page() {
        $wcvendors_pro_commission_download_controller = new WCVendors_Pro_Commission_Download_Controller( $this->wcvendors_pro, $this->version, $this->debug );
        $wcvendors_pro_commission_download_controller->display();
    }

    /**
     * Load the ratings page
     *
     * @since    1.0.0
     */
    public function load_rating_page() {
        $wcvendors_pro_rating_controller = new WCVendors_Pro_Ratings_Controller( $this->wcvendors_pro, $this->version, $this->debug );
        $wcvendors_pro_rating_controller->display();
    }

    /**
     * Load the store settings page
     *
     * @since    1.1.0
     */
    public function load_settings_page() {

        $vendor_id = get_current_user_id();

        $store_name              = get_user_meta( $vendor_id, 'pv_shop_name', true );
        $store_description       = get_user_meta( $vendor_id, 'pv_shop_description', true );
        $shipping_disabled       = wc_string_to_bool( get_option( 'wcvendors_shipping_management_cap', 'no' ) );
        $shipping_methods        = WC()->shipping->load_shipping_methods();
        $shipping_method_enabled = ( array_key_exists( 'wcv_pro_vendor_shipping', $shipping_methods ) && 'yes' === $shipping_methods['wcv_pro_vendor_shipping']->enabled ) ? true : 0;
        $shipping_details        = get_user_meta( get_current_user_id(), '_wcv_shipping', true );

        wc_get_template(
            'store-settings.php',
            array(
                'store_name'              => $store_name,
                'store_description'       => $store_description,
                'shipping_disabled'       => $shipping_disabled,
                'shipping_method_enabled' => $shipping_method_enabled,
                'shipping_details'        => $shipping_details,
            ),
            'wc-vendors/dashboard/',
            $this->base_dir . 'legacy/templates/dashboard/'
        );
    }

    /**
     * Check object permission to see if the vendor owns the object (this is to stop people messing with URLs)
     *
     * @since  1.0.0
     * @since  1.8.2 - Added extra object check
     * @since  1.8.3 - Added filters for 3rd party plugins to hook into checks
     *
     * @param    string $object the object type to test.
     * @param    int    $object_id  object id to check.
     */
    public static function check_object_permission( $object, $object_id ) { // phpcs:ignore

        $can_edit_live = wc_string_to_bool( get_option( 'wcvendors_capability_products_edit', 'no' ) );
        $edit_status   = apply_filters( 'wcv_edit_object_status', array( 'draft', 'pending' ) );
        $post_status   = get_post_status( $object_id );
        $can_edit      = in_array( $post_status, $edit_status, true );

        $order      = null;
        $post_owner = 0;
        if ( 'order' === $object ) {
            $order = wc_get_order( $object_id );

            if ( ! $order ) {
                return false;
            }

            if ( 'shop_order_vendor' === $order->get_type() ) {
                $order_owner = (int) $order->get_meta( 'wcv_vendor_id' );

                return ( get_current_user_id() === $order_owner ) ? true : false;
            } elseif ( 'shop_order' === $order->get_type() ) {
                $vendors_ids = (array) $order->get_meta( 'wcv_vendor_ids' );

                return in_array( get_current_user_id(), $vendors_ids, true );
            }

            $vendor_products = WCV_Queries::get_products_for_order( $object_id );
                if ( empty( $vendor_products ) ) {
                    return false;
                } else {
                    return true;
                }
        } else {
            $post_owner = get_post_field( 'post_author', $object_id );
        }

        if ( ! $can_edit_live ) {
            $can_edit_live = $can_edit ? true : false;
        }

        if ( post_type_exists( $object ) ) {
            if ( absint( $post_owner ) !== absint( get_current_user_id() ) ) {
                return apply_filters( 'wcvendors_dashboard_object_owner_check', false, $object, $object_id );
            }
        }

        switch ( $object ) {
            // Product permissions.
            case 'product':
                return ( $can_edit_live && (int) WCV_Vendors::get_vendor_from_product( $object_id ) === get_current_user_id() ) ? true : false;
            case 'shop_coupon':
                return ( (int) WCVendors_Pro_Vendor_Controller::get_vendor_from_object( $object_id ) !== get_current_user_id() ) ? false : true;
            // Dashboard.
            default:
                return apply_filters( 'wcvendors_dashboard_check_object_permission', true, $object, $object_id );
        }
    }

    /**
     * Check permission before the page loads
     *
     * @since    1.0.0
     */
    public function check_permission() {

        global $wp_query;
        $current_page_id = get_the_ID();

        if ( wcv_is_dashboard_page( $current_page_id ) ) {

            if ( ! is_user_logged_in() ) {

                $my_account_page = wc_get_page_id( 'myaccount' );

                if ( ! is_string( get_post_status( $my_account_page ) ) ) {
                    /* translators: %s the label for vendors. */
                    wc_add_notice( sprintf( __( 'Please contact the website administrator and instruct them that in order for the %s Dashboard to work for logged out users, they must have their My Account page configured and set properly in their WooCommerce settings.', 'wcvendors-pro' ), wcv_get_vendor_name() ), 'error' );
                } else {
                    wp_safe_redirect( apply_filters( 'wcv_login_redirect', get_permalink( wc_get_page_id( 'myaccount' ) ) ), 302 );
                    exit;
                }
            }

            if ( isset( $wp_query->query_vars['object'] ) ) {
                $object    = get_query_var( 'object' );
                $action    = get_query_var( 'action' );
                $object_id = get_query_var( 'object_id' ) ? get_query_var( 'object_id' ) : '';
                $user_id   = get_current_user_id();

                $lock_new_products  = ( 'yes' === get_user_meta( $user_id, '_wcv_lock_new_products_vendor', true ) ) ? true : false;
                $lock_edit_products = ( 'yes' === get_user_meta( $user_id, '_wcv_lock_edit_products_vendor', true ) ) ? true : false;

                if ( 'edit' === $action && 'product' === $object ) {

                    if ( ( $lock_new_products && $lock_edit_products ) || ( empty( $object_id ) && $lock_new_products ) || ( ! empty( $object_id ) && $lock_edit_products ) ) {

                        wp_safe_redirect( self::get_dashboard_page_url( 'product' ), 302 );
                    }
                }
            }
        }
    }

    /**
     * Can the current user view the dashboard ?
     *
     * @since    1.0.0
     */
    public function can_view_dashboard() {

        $user_id  = get_current_user_id();
        $can_view = false;

        if ( ! $user_id ) {
            echo '<h3>' . esc_html__( 'Permission denied.', 'wcvendors-pro' ) . '</h3>';
            return $can_view;
        }

        $referer               = wp_get_referer();
        $user                  = get_userdata( $user_id );
        $user_roles            = $user->roles;
        $accept_terms          = isset( $_GET['terms'] ) ? wc_string_to_bool( sanitize_text_field( wp_unslash( $_GET['terms'] ) ) ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $is_vendor             = in_array( 'vendor', $user_roles, true );
        $is_admin              = in_array( 'administrator', $user_roles, true ) || in_array( 'shop_manager', $user_roles, true );
        $is_pending_vendor     = in_array( 'pending_vendor', $user_roles, true );
        $redirect_to_form      = wc_string_to_bool( get_option( 'wcvendors_vendor_registration_form_redirect', 'no' ) );
        $manual                = wc_string_to_bool( get_option( 'wcvendors_vendor_approve_registration', 'no' ) );
        $application_submitted = wc_string_to_bool( get_user_meta( $user->ID, 'wcv_vendor_application_submitted', true ) );

        /**
         * Once the user has registered they have the vendor role.
         * So we need to check if the user is coming from the My Account page and has accepted the terms
         * then show Application Form if the Registration Form option is enabled
        */
        $my_account_url = get_permalink( wc_get_page_id( 'myaccount' ) );

        if ( ( $referer && strpos( $referer, $my_account_url ) !== false ) && $accept_terms && $redirect_to_form && $manual ) {
            $is_vendor = false;
        }

        if ( $is_vendor && ! $is_pending_vendor && $application_submitted ) {
            $can_view = true;
        }

        if ( $is_vendor && ! $redirect_to_form && ! $application_submitted ) {
            $can_view = true;
        }

        if ( $is_admin ) {
            echo esc_html(
                apply_filters(
                    'wcvendors_dashboard_admin_notice',
                    __( 'Admins cannot apply to be vendors. ', 'wcvendors-pro' )
                )
            );
        }

        return $can_view;
    }

    /**
     * Maybe load the application form if the user is a pending vendor
     *
     * Will load the application form if the vendor is set to be manually approved and the redirect to form option is enabled.
     *
     * @return void
     * @version 1.8.8
     * @since   1.8.8 - Added.
     */
    public function maybe_load_application_form() {
        $user = get_userdata( get_current_user_id() );
        if ( ! $user ) {
            return;
        }
        $user_roles            = $user->roles;
        $redirect_to_form      = wc_string_to_bool( get_option( 'wcvendors_vendor_registration_form_redirect', 'no' ) );
        $is_pending_vendor     = in_array( 'pending_vendor', $user_roles, true );
        $is_admin              = in_array( 'administrator', $user_roles, true ) || in_array( 'shop_manager', $user_roles, true );
        $is_vendor             = in_array( 'vendor', $user_roles, true );
        $application_submitted = wc_string_to_bool( get_user_meta( $user->ID, 'wcv_vendor_application_submitted', true ) );
        $can_apply_to_vendor   = ! $is_vendor && ! $is_pending_vendor && ! $is_admin;

        if ( $is_admin ) {
            return;
        }

        if ( ( $application_submitted && $is_pending_vendor ) || ( $is_pending_vendor && ! $redirect_to_form ) ) {
            echo $this->show_pending_vendor_notice(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            return;
        }

        if ( ! $application_submitted && ( $redirect_to_form || $can_apply_to_vendor ) ) {
            echo $this->show_application_form(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        }
    }

    /**
     * Show get application form
     *
     * @since    1.8.7.1
     * @version 1.8.7.2
     */
    public function show_application_form() {
        ob_start();
        include_once apply_filters( 'wcvendors_pro_dashboard_open_path', $this->base_dir . 'public/partials/wcvendors-pro-dashboard-open.php' );
        $vendor_signup_notice = get_option( 'wcvendors_vendor_signup_notice', '' );

        // Load the new sign up form template.
        wc_get_template(
            'vendor-signup-form.php',
            array(
                'vendor_signup_notice' => $vendor_signup_notice,
                'shipping_details'     => array(),
            ),
            'wc-vendors/front/',
            $this->base_dir . '/templates/front/'
        );
        include_once apply_filters( 'wcvendors_pro_dashboard_close_path', $this->base_dir . 'public/partials/wcvendors-pro-dashboard-close.php' );
        return ob_get_clean();
    }

    /**
     * Show pending vendor notice
     *
     * @since 1.8.7.1
     * @version 1.8.7.2
     */
    public function show_pending_vendor_notice() {
        ob_start();
        $vendor_pending_notice = get_option(
            'wcvendors_vendor_pending_notice',
            WCVendors_Pro_Admin_Settings::get_default_pending_vendor_notice()
        );
        wc_get_template(
            'vendor-pending-notice.php',
            array( 'vendor_pending_notice' => $vendor_pending_notice ),
            'wc-vendors/front/',
            $this->base_dir . '/templates/front/'
        );
        return ob_get_clean();
    }



    /**
     * Add the query vars for the rewrirte rules add_query_vars function.
     *
     * @access        public
     * @since         1.0.0
     *
     * @param        array $vars query vars array.
     * @return       array $vars new query vars.
     */
    public function add_query_vars( $vars ) {

        $vars[] = 'object';
        $vars[] = 'object_id';
        $vars[] = 'action';
        $vars[] = 'template';
        $vars[] = 'custom';

        return $vars;
    }

    /**
     * Get any custom pages defined by integrations
     *
     * @since   1.4.0
     * @version 1.4.0
     * @return array $pages custom page routes and template information
     */
    public static function get_custom_pages() {

        return apply_filters( 'wcv_dashboard_custom_pages', array() );
    }

    /**
     * Dashboard rewrite rules
     *
     * @since    1.0.0
     *
     * @param  array $rules rules array.
     */
    public function rewrite_rules( $rules ) {

        $dashboard_page_ids = get_option( 'wcvendors_dashboard_page_id', array() );

        if ( ! is_array( $dashboard_page_ids ) && ! empty( $dashboard_page_ids ) ) {
            $dashboard_page_ids = (array) $dashboard_page_ids;
        }

        // If the dashboard page hasn't been set, don't create the re-write rules.
        if ( ! empty( $dashboard_page_ids ) ) {

            foreach ( $dashboard_page_ids as $dashboard_page_id ) {

                $_post               = get_post( $dashboard_page_id );
                $dashboard_page_slug = $_post->post_name;

                if ( $_post->post_parent ) {
                    $_parent_slug_prefix = get_post( $_post->post_parent )->post_name;
                    $dashboard_page_slug = $_parent_slug_prefix . '/' . $dashboard_page_slug;
                }

                $pages        = self::get_dashboard_pages();
                $custom_pages = self::get_custom_pages();

                foreach ( $pages as $page ) {
                    // Type Rule.
                    $type_rule = array(
                        $dashboard_page_slug . '/' . $page['slug'] . '?$'             => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $page['slug'],
                        $dashboard_page_slug . '/' . $page['slug'] . '/page/([0-9]+)' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $page['slug'] . '&paged=$matches[1]',
                    );
                    $rules     = $type_rule + $rules;

                    // Allow custom rules to be added.
                    if ( ! empty( $custom_pages ) ) {

                        foreach ( $custom_pages as $custom_page ) {

                            // check if a parent object has been defined.
                            if ( array_key_exists( 'parent', $custom_page ) ) {
                                $custom_rule = array( $dashboard_page_slug . '/' . $custom_page['parent'] . '/' . $custom_page['slug'] . '?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $custom_page['parent'] . '&custom=' . $custom_page['slug'] );
                            } else {
                                $custom_rule = array( $dashboard_page_slug . '/' . $custom_page['slug'] . '?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $custom_page['slug'] );
                            }

                            $rules = $custom_rule + $rules;

                            // Register the actions for the custom page.
                            if ( array_key_exists( 'actions', $custom_page ) ) {

                                foreach ( $custom_page['actions'] as $action => $label ) {

                                    // Actions Rule.
                                    $action_rule = array( $dashboard_page_slug . '/' . $custom_page['slug'] . '/' . $action . '?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $custom_page['slug'] . '&action=' . $action );
                                    $id_rule     = array( $dashboard_page_slug . '/' . $custom_page['slug'] . '/' . $action . '/([0-9]+)?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $custom_page['slug'] . '&action=' . $action . '&object_id=$matches[1]' );
                                    $rules       = $action_rule + $id_rule + $rules;
                                }
                            }
                        }
                    }

                    if ( is_array( $page['actions'] ) ) {

                        foreach ( $page['actions'] as $action => $label ) {
                            // Actions Rule.
                            $action_rule   = array( $dashboard_page_slug . '/' . $page['slug'] . '/' . $action . '?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $page['slug'] . '&action=' . $action );
                            $template_rule = array( $dashboard_page_slug . '/' . $page['slug'] . '/([^/]*)/' . $action . '?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $page['slug'] . '&template=$matches[1]&action=' . $action );
                            // Id parsed ?
                            $id_rule          = array( $dashboard_page_slug . '/' . $page['slug'] . '/' . $action . '/([0-9]+)?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $page['slug'] . '&action=' . $action . '&object_id=$matches[1]' );
                            $template_id_rule = array( $dashboard_page_slug . '/' . $page['slug'] . '/([^/]*)/' . $action . '/([0-9]+)?$' => 'index.php?pagename=' . $dashboard_page_slug . '&object=' . $page['slug'] . '&template=$matches[1]&action=' . $action . '&object_id=$matches[2]' );

                            $rules = $action_rule + $template_rule + $id_rule + $template_id_rule + $rules;
                        }
                    }
                }
            }
        }

        return apply_filters( 'wcv_dashboard_rewrite_rules', $rules );
    }

    /**
     * Create the dashboard navigation from available pages.
     *
     * @since    1.0.0
     * @todo     Have this menu output better
     */
    public function create_nav() {

        $pages = self::get_dashboard_pages();

        $current_page = get_query_var( 'object' );

        $products_disabled   = wc_string_to_bool( get_option( 'wcvendors_product_management_cap', 'no' ) );
        $orders_disabled     = wc_string_to_bool( get_option( 'wcvendors_order_management_cap', 'no' ) );
        $commission_disabled = wc_string_to_bool( get_option( 'wcvendors_commission_management_cap', 'no' ) );
        $coupons_disabled    = wc_string_to_bool( get_option( 'wcvendors_shop_coupon_management_cap', 'no' ) );
        $ratings_disabled    = wc_string_to_bool( get_option( 'wcvendors_ratings_management_cap', 'no' ) );
        $settings_disabled   = wc_string_to_bool( get_option( 'wcvendors_settings_management_cap', 'no' ) );
        $viewstore_disabled  = wc_string_to_bool( get_option( 'wcvendors_view_store_cap', 'no' ) );
        $show_logout         = wc_string_to_bool( get_option( 'wcvendors_dashboard_show_logout', 'no' ) );
        $vertical_menu       = wc_string_to_bool( get_option( 'wcvendors_use_vertical_menu', 'no' ) );

        if ( $products_disabled ) {
            unset( $pages['product'] );
        }
        if ( $orders_disabled ) {
            unset( $pages['order'] );
        }
        if ( $coupons_disabled ) {
            unset( $pages['shop_coupon'] );
        }
        if ( $ratings_disabled ) {
            unset( $pages['rating'] );
        }
        if ( $commission_disabled ) {
            unset( $pages['commission'] );
        }
        if ( $settings_disabled ) {
            unset( $pages['settings'] );
        }

        // Add dashboard home to the pages array.
        $dashboard_home = apply_filters(
            'wcv_dashboard_home_url',
            array(
                'label' => __( 'Dashboard', 'wcvendors-pro' ),
                'slug'  => '',
            )
        );

        if ( ! $viewstore_disabled ) {
            $store_url = apply_filters(
                'wcv_dashboard_view_store_url',
                array(
                    'label' => __( 'View store', 'wcvendors-pro' ),
                    'id'    => 'view-store',
                    'slug'  => WCVendors_Pro_Vendor_Controller::get_vendor_store_url( get_current_user_id() ),
                )
            );
            if ( wc_string_to_bool( get_option( 'wcvendors_dashboard_view_store_new_window', 'no' ) ) ) {
                $store_url['target'] = '_blank';
            }
            $pages['view_store'] = $store_url;
        }

        if ( $show_logout ) {
            $store_url = apply_filters(
                'wcv_dashboard_show_logout',
                array(
                    'label' => __( 'Logout', 'wcvendors-pro' ),
                    'id'    => 'logout',
                    'slug'  => wc_logout_url(),
                )
            );

            $pages['logout'] = $store_url;
        }

        $pages          = array_merge( array( 'dashboard_home' => $dashboard_home ), $pages );
        $pages          = apply_filters( 'wcv_dashboard_pages_nav', $pages );
        $nav_class      = apply_filters( 'wcv_dashboard_nav_class', '' );
        $menu_dir_class = ( $vertical_menu ) ? 'vertical' : 'horizontal';
        $menu_dir_size  = ( $vertical_menu ) ? 'all-20 small-100 medium-100' : 'all-100';

        // Move this into a template.
        $menu_wrapper_start = apply_filters( 'wcv_dashboard_nav_wrapper_start', '<div class="wcv-cols-group wcv-horizontal-gutters"><div class="' . $menu_dir_size . '"><nav class="wcv-navigation ' . $nav_class . ' "><ul class="menu ' . $menu_dir_class . '  black">' );

        echo wp_kses_post( $menu_wrapper_start );

        foreach ( $pages as $page ) {

            if ( filter_var( $page['slug'], FILTER_VALIDATE_URL ) === false ) {
                $page_url = $this->get_dashboard_page_url( $page['slug'] );
            } else {
                $page_url = $page['slug'];
            }

            $class      = ( $current_page === $page['slug'] ) ? 'active' : '';
            $id         = isset( $page['id'] ) ? $page['id'] : '';
            $page_label = $page['label'];
            $target     = isset( $page['target'] ) ? $page['target'] : false;

            wc_get_template(
                'nav.php',
                array(
                    'page'       => $page,
                    'page_url'   => $page_url,
                    'target'     => $target,
                    'page_label' => $page_label,
                    'class'      => $class,
                    'id'         => $id,
                ),
                'wc-vendors/dashboard/',
                $this->base_dir . 'templates/dashboard/'
            );

        }
        echo '</ul>';

        echo '</div>';

        if ( ! $vertical_menu ) {
            echo '</div>';
        }

        do_action( 'wcv_pro_after_dashboard_nav_container' );

        if ( $vertical_menu ) {
            echo '<div class="all-80 medium-100 small-100 wcv-main-content">';
        }
    }

    /**
     * Check if a page is disabled and return if it is
     *
     * @since    1.3.0
     * @version  1.5.0
     */
    public function page_disabled() {

        $current_page = get_query_var( 'object' );
        $disabled     = false;

        switch ( $current_page ) {
            case 'product':
                $disabled = wc_string_to_bool( get_option( 'wcvendors_product_management_cap', 'no' ) );
                break;
            case 'order':
                $disabled = wc_string_to_bool( get_option( 'wcvendors_order_management_cap', 'no' ) );
                break;
            case 'commission':
                $disabled = wc_string_to_bool( get_option( 'wcvendors_commission_management_cap', 'no' ) );
                break;
            case 'shop_coupon':
                $disabled = wc_string_to_bool( get_option( 'wcvendors_shop_coupon_management_cap', 'no' ) );
                break;
            case 'rating':
                $disabled = wc_string_to_bool( get_option( 'wcvendors_ratings_management_cap', 'no' ) );
                break;
            case 'settings':
                $disabled = wc_string_to_bool( get_option( 'wcvendors_settings_management_cap', 'no' ) );
                break;
        }

        return ! apply_filters( 'wcvendors_page_disabled', $disabled, $current_page );
    }

    /**
     * Shortcode for dashboard navigation
     *
     * @since    1.3.3
     */
    public function load_dashboard_nav() {

        if ( ! is_user_logged_in() ) {

            return false;

        } elseif ( WCV_Vendors::is_vendor( get_current_user_id() ) ) {

            ob_start();
            echo '<div class="wcvendors-pro-dashboard-wrapper"><div class="wcv-grid">';
            $this->create_nav();
            echo '</div></div>';

            return ob_get_clean();
        }
    }

    /**
     * Check if the current page is a dashboard page
     *
     * @since      1.4.0
     * @access     public
     * @deprecated 1.5.4
     * @param int $page_id the page id to check.
     * @return bool returns if the page id passed is a dashboard page.
     */
    public static function is_dashboard_page( $page_id ) {
        return wcv_is_dashboard_page( $page_id );
    }

    /**
     * Check if the the vendors access has been disabled
     *
     * @since  1.4.0
     * @access public
     */
    public function lock_new_products_notice() {

        $lock_new_products         = ( 'yes' === get_user_meta( get_current_user_id(), '_wcv_lock_new_products_vendor', true ) ) ? true : false;
        $lock_new_products_notice  = get_user_meta( get_current_user_id(), '_wcv_lock_new_products_vendor_msg', true );
        $lock_edit_products        = ( 'yes' === get_user_meta( get_current_user_id(), '_wcv_lock_edit_products_vendor', true ) ) ? true : false;
        $lock_edit_products_notice = get_user_meta( get_current_user_id(), '_wcv_lock_edit_products_vendor_msg', true );
        $notice                    = '';

        if ( $lock_new_products ) {
            $notice .= $lock_new_products_notice;
        }
        if ( $lock_edit_products ) {
            $notice .= ' ' . $lock_edit_products_notice;
        }

        if ( $lock_new_products || $lock_edit_products ) {

            wc_get_template(
                'dashboard-notice.php',
                array(
                    'vendor_dashboard_notice' => $notice,
                    'notice_type'             => 'error',
                ),
                'wc-vendors/dashboard/',
                $this->base_dir . '/templates/dashboard/'
            );
        }
    }

    /**
     * Update disk and file usage after deleting or adding media file
     *
     * @since 1.8.5
     * @param int $post_id the post id of the media file.
     */
    public function update_disk_usage_status( $post_id ) {
        $author = get_post_field( 'post_author', $post_id );

        if ( ! WCV_Vendors::is_vendor( $author ) ) {
            return;
        }

        delete_transient( 'wcv_user_media_' . $author );
        delete_transient( 'wcv_user_disk_usage_' . $author );
    }
}
