<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/* 
Plugin Name: Divi Ajax Filters
Plugin URL: https://diviengine.com
Description: Create Ajax Filters for WooCommerce & ACF. Best used with one of our plugins BodyCommerce or Machine.
Version: 1.6.3
Author: Divi Engine
Author URI: https://diviengine.com
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
@author      diviengine.com
@copyright   2020 diviengine.com

I pray that you bless the people who interact and who own this website - I pray the blessing to be one that goes beyond worldly treasures but understanding the deep love you have for them. In Jesus name, Amen

John 14:6
I am the way, and the truth, and the life. No one comes to the Father except through me.
*/


defined('DE_DF_VERSION') or define('DE_DF_VERSION', '1.6.3');

defined('DE_DF_PATH') or define('DE_DF_PATH',   plugin_dir_path(__FILE__));
defined('DE_DF_PLUGIN_URL') or define('DE_DF_PLUGIN_URL',   plugin_dir_url(__FILE__));
defined('DE_DF_APP_API_URL') or define('DE_DF_APP_API_URL',      'https://diviengine.com/index.php');
defined('DE_DF_PRODUCT_ID') or define('DE_DF_PRODUCT_ID',           'WP-DE-DF');
defined('DE_DF_PRODUCT_URL') or define('DE_DF_PRODUCT_URL', 'https://diviengine.com/product/divi-bodycommerce/');
defined('DE_DF_INSTANCE') or define('DE_DF_INSTANCE',             str_replace(array ("https://" , "http://"), "", network_site_url()));
defined('DE_DF_AUTHOR') or define('DE_DF_AUTHOR', 'Divi Engine');
defined('DE_DF_URL') or define('DE_DF_URL', 'https://www.diviengine.com');



// IF BC
if (defined('DE_DB_WOO_VERSION')) {
// IF MACHINE
} else if (defined('DE_DMACH_VERSION')) {
  include(DE_DF_PATH . '/includes/classes/init.class.php');
} else {

    include(DE_DF_PATH . '/includes/classes/class.wooslt.php');
    include(DE_DF_PATH . '/includes/classes/class.licence.php');
    include(DE_DF_PATH . '/includes/classes/class.options.php');
    include(DE_DF_PATH . '/includes/classes/class.updater.php');
    include(DE_DF_PATH . '/includes/classes/init.class.php');
    
    add_action( 'admin_enqueue_scripts', 'load_divi_engine_style_divi_filter' , 20);
    function load_divi_engine_style_divi_filter() {
        $cssfile = plugins_url( 'styles/divi-engine.css', __FILE__ );
        wp_enqueue_style( 'divi_engine_style', $cssfile , false, DE_DF_VERSION );
    }
    require_once plugin_dir_path( __FILE__ ) . 'includes/daf-settings.php';
    
}


// initialise Divi Modules
if ( !function_exists( 'de_df_initialise_ext' ) ) {
    function de_df_initialise_ext()
    {
        require_once plugin_dir_path( __FILE__ ) . 'includes/DiviFilterModules.php';
        require_once plugin_dir_path( __FILE__ ) . 'includes/loader.php';
        require_once plugin_dir_path( __FILE__ ) . 'includes/ajaxcalls/post_ajax.php';

        if ( defined('DE_DB_WOO_VERSION')) {
            $db_settings = get_option( 'divi-bodyshop-woo_options' );
            $enable_variation_swatches = unserialize( $db_settings );
            if ( isset( $enable_variation_swatches['enable_variation_swatches'][0] ) ) {
                $check_enable_variation_swatches = $enable_variation_swatches['enable_variation_swatches'][0];
            }else { $check_enable_variation_swatches = "1"; }
            if ($check_enable_variation_swatches == "0") {
                require_once dirname( __FILE__ ) .'/lib/variation-swatches.php';
                require_once dirname( __FILE__ ) .'/includes/classes/class-wvs-term-meta.php';
            }
        }else{

            require_once dirname( __FILE__ ) .'/lib/variation-swatches.php';
            require_once dirname( __FILE__ ) .'/includes/classes/class-wvs-term-meta.php';
        }
    }
}

if ( !class_exists( 'DiviExtension' ) ){
    add_action( 'divi_extensions_init', 'de_df_initialise_ext' );
}else{
    de_df_initialise_ext();
}

if ( !function_exists('divi_filter_load_actions_ajax') ) {
    function divi_filter_load_actions_ajax( $actions ) {
        $filter_handler_exist = false;
        $modal_handler_exist = false;
        $loadmore_handler_exist = false;
        $filter_counter_handler_exist = false;
        $filter_get_sub_category_handler_exist = false;

        foreach ($actions as $key => $action) {
          if ( $action == 'divi_filter_ajax_handler' )
            $filter_handler_exist = true;
          if ( $action == 'divi_filter_get_post_modal_ajax_handler' )
            $modal_handler_exist = true;
          if ( $action == 'divi_filter_loadmore_ajax_handler')
            $loadmore_handler_exist = true;
          if ( $action == 'divi_filter_get_count_ajax_handler')
            $filter_counter_handler_exist = true;
          if ( $action == 'divi_filter_get_sub_category_handler')
            $filter_get_sub_category_handler_exist = true;
        }

        if ( !$filter_handler_exist )
          $actions[] = 'divi_filter_ajax_handler';

        if ( !$modal_handler_exist )
          $actions[] = 'divi_filter_get_post_modal_ajax_handler';

        if ( !$loadmore_handler_exist )
          $actions[] = 'divi_filter_loadmore_ajax_handler';

        if ( !$filter_counter_handler_exist )
          $actions[] = 'divi_filter_get_count_ajax_handler';

        if ( !$filter_get_sub_category_handler_exist )
          $actions[] = 'divi_filter_get_sub_category_handler';

        return $actions;
    }

    add_filter( 'et_builder_load_actions', 'divi_filter_load_actions_ajax' );
}

if ( !function_exists('bodycommerce_wpml_currency_ajax_actions')) {
  function bodycommerce_wpml_currency_ajax_actions( $actions ) {
    $actions[] = 'divi_filter_ajax_handler';
    $actions[] = 'divi_filter_get_post_modal_ajax_handler';
    $actions[] = 'divi_filter_loadmore_ajax_handler';
    $actions[] = 'divi_filter_get_sub_category_handler';

    return $actions;
  }

  add_filter( 'wcml_multi_currency_ajax_actions', 'bodycommerce_wpml_currency_ajax_actions', 10, 1 );
}

if ( !function_exists('divi_filter_enqueue_scripts') ) {
  function divi_filter_enqueue_scripts() {
      wp_register_script( 'divi-filter-rangeSlider-js', plugins_url('/js/ion.rangeSlider.min.js', __FILE__ ), array( 'jquery' ), DE_DF_VERSION, true );
      wp_register_style( 'divi-filter-rangeSlider-css', plugins_url( '/styles/ion.rangeSlider.min.css' , __FILE__ ), array(), DE_DF_VERSION, 'all' );
      
      wp_register_script( 'divi-filter-select2-js', plugins_url('/js/select2.min.js', __FILE__ ), array( 'jquery' ), DE_DF_VERSION );
      wp_register_style( 'divi-filter-select2-css', plugins_url( '/styles/select2.min.css' , __FILE__ ), array(), DE_DF_VERSION, 'all' );
  }
  add_action( 'wp_enqueue_scripts', 'divi_filter_enqueue_scripts' );    
}


/*

register_uninstall_hook( __FILE__, 'df_uninstall_hook' );
register_deactivation_hook( __FILE__, 'df_deactivation_hook' );

function df_uninstall_hook() {
}

function df_deactivation_hook() {
}*/

if ( !function_exists('Divi_filter_remove_get_params')) {
    add_filter( 'query_vars', 'Divi_filter_remove_get_params', 10, 1 );

    function Divi_filter_remove_get_params( $query ){
        global $divi_filter_removed_param;
        if ( !empty( $_GET['filter' ] ) && $_GET['filter' ] == 'true'){
            foreach ($_GET as $key => $param) {
                if ( $key != 'filter'){
                    $divi_filter_removed_param[$key] = $param;
                    unset($_GET[$key]);
                }
            }
        }
        return $query;
  }  
}

// Price filter

if ( !function_exists( 'bodycommerce_product_search_join' ) ) {
    function bodycommerce_product_search_join( $join, $query ) {
    
        global $price_filter_var;
        global $wpdb;

        if ( isset($price_filter_var['is_filter']) && $price_filter_var['is_filter'] ) {
            $join  .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id ";    
        }

        if ( ! $query->is_main_query() || is_admin() || ! is_search() || ( !function_exists('is_woocommerce') || (function_exists('is_woocommerce') && ! is_woocommerce() ) ) ) {
            return $join;
        }

        $join .= " LEFT JOIN {$wpdb->postmeta} iconic_post_meta ON {$wpdb->posts}.ID = iconic_post_meta.post_id ";
        return $join;
    }
    add_filter( 'posts_join', 'bodycommerce_product_search_join', 10, 2 );
}

if ( !function_exists('bodycommerce_product_search_where') ) {
    function bodycommerce_product_search_where( $where, $query ) {

        global $price_filter_var;
        global $wpdb;

        if ( isset($price_filter_var['is_filter']) && $price_filter_var['is_filter']) {
            $where .= $wpdb->prepare(
                ' AND NOT (%f>wc_product_meta_lookup.min_price OR %f<wc_product_meta_lookup.max_price ) ',
                $price_filter_var['min_price'],
                $price_filter_var['max_price']
            );
        }

        if ( ! $query->is_main_query() || is_admin() || ! is_search() || ( !function_exists('is_woocommerce') || (function_exists('is_woocommerce') && ! is_woocommerce() ) ) ) {
            return $where;
        }
        
        $where = preg_replace(
            "/\(\s*{$wpdb->posts}.post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "({$wpdb->posts}.post_title LIKE $1) OR (iconic_post_meta.meta_key = '_sku' AND iconic_post_meta.meta_value LIKE $1)", $where );

        return $where;
    }
    add_filter( 'posts_where', 'bodycommerce_product_search_where', 10, 2 );
}

if (defined('DE_DB_WOO_VERSION')) {
    // IF MACHINE
    } else if (defined('DE_DMACH_VERSION')) {
    // IF STANDALONE
    } else {
        global $DE_DF;
        $DE_DF = new DE_DF();
    }
    
?>
