<?php

/*
 * Plugin Name:       WPSAAD Addons for Dokan and Elementor
 * Plugin URI:        https://wpsaad.com/product/addons-for-dokan-and-elementor/
 * Description: Awesome Elementor widgets and Addons for Dokan.
 * Requires at least: 5.2
 * Requires PHP:      7.2
 * Author:            WPSAAD
 * Author URI:        https://wpsaad.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       wpsaad-addons-for-dokan-and-elementor
 * Domain Path:       /languages
 * Requires Plugins:   woocommerce, dokan-lite, elementor
 * Version:           1.3.0
 * 
 * 
 */
// Exit if accessed directly.
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'wafdae_fs' ) ) {
    wafdae_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'wafdae_fs' ) ) {
        // Create a helper function for easy SDK access.
        function wafdae_fs() {
            global $wafdae_fs;
            if ( !isset( $wafdae_fs ) ) {
                // Manually include the Freemius SDK (not needed if using Composer).
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $wafdae_fs = fs_dynamic_init( array(
                    'id'             => '17251',
                    'slug'           => 'wpsaad-addons-for-dokan-and-elementor',
                    'type'           => 'plugin',
                    'navigation'     => 'tabs',
                    'public_key'     => 'pk_78bf1ddc2df518b1ddaa45509b1fd',
                    'is_premium'     => false,
                    'has_addons'     => false,
                    'has_paid_plans' => true,
                    'menu'           => array(
                        'slug'   => 'wafdae-admin-menu',
                        'parent' => array(
                            'slug' => 'wpsaad-menu',
                        ),
                    ),
                    'is_live'        => true,
                ) );
            }
            return $wafdae_fs;
        }

        // Init Freemius.
        wafdae_fs();
        // Signal that SDK was initiated.
        do_action( 'wafdae_fs_loaded' );
    }
    //Include admin settings
    if ( !class_exists( 'WPSAAD_Addons_Admin' ) ) {
        require_once plugin_dir_path( __FILE__ ) . 'admin/settings.php';
    }
    add_action( 'admin_enqueue_scripts', 'wafdae_style_script' );
    function wafdae_style_script() {
        wp_enqueue_script( 'wafdae-switcher-script', plugins_url( '/assets/js/jquery.switcher.min.js', __FILE__ ) );
        wp_enqueue_style( 'wafdae-switcher-style', plugins_url( '/assets/css/switcher.css', __FILE__ ) );
        wp_enqueue_script( 'wafdae-admin', plugins_url( '/admin/js/admin.js', __FILE__ ) );
        wp_enqueue_style( 'wafdae-admin', plugins_url( '/admin/css/admin.css', __FILE__ ) );
    }

    // Include the Dokan widget class.
    if ( !function_exists( 'wafdae_include_dokan_widget' ) ) {
        function wafdae_include_dokan_widget(  $widgets_manager  ) {
            if ( get_option( 'wpsaad_enable_vendors_listing' ) ) {
                if ( !class_exists( 'WAFDAE_Dokan_Stores' ) ) {
                    require_once plugin_dir_path( __FILE__ ) . 'widgets/dokan-stores.php';
                }
                $widgets_manager->register( new \WAFDAE_Dokan_Stores() );
            }
        }

        add_action( 'elementor/widgets/widgets_registered', 'wafdae_include_dokan_widget' );
    }
    // Register the plugin as Elementor extension.
    // function wpsaad_register_dokan_widget()
    // {
    // \Elementor\Plugin::instance()->widgets_manager->register_widget_type(new \WAFDAE_Dokan_Vendors_Slider());
    // \Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \Dokan_Stores() );
    // }
    // add_action('elementor/widgets/register', 'wpsaad_register_dokan_widget');
    // Enqueue custom styles for the widget
    // function edw_enqueue_custom_styles()
    // {
    //     wp_enqueue_style('dokan-widgets-css', plugin_dir_url(__FILE__) . 'assets/css/dokan-slider.css');
    // }
    // add_action('wp_enqueue_scripts', 'edw_enqueue_custom_styles');
    if ( !function_exists( 'wafdae_add_elementor_widget_categories' ) ) {
        function wafdae_add_elementor_widget_categories(  $elements_manager  ) {
            $elements_manager->add_category( 'wpsaad', [
                'title' => esc_html__( 'Dokan ♚ [WPSAAD] ', 'wpsaad-addons-for-dokan-and-elementor' ),
                'icon'  => 'fa fa-plug',
            ] );
        }

        add_action( 'elementor/elements/categories_registered', 'wafdae_add_elementor_widget_categories' );
    }
    if ( !function_exists( 'wafdae_widgets_functions' ) ) {
        add_action( 'elementor/loaded', 'wafdae_widgets_functions' );
        function wafdae_widgets_functions() {
            // Force Dokan assets to load when widget is present
            add_filter( 'dokan_is_store_listing', function ( $found ) {
                // $content_data = \Elementor\Plugin::$instance->frontend->get_builder_content(get_the_ID(), true);
                // if (strpos($content_data, 'dokan_stores') !== false) {
                $found = true;
                return $found;
                // }
            } );
        }

    }
}