<?php
/**
 * Plugin Name: WhatsApp Chat for WordPress / WooCommerce
 * Plugin URI: https://codecanyon.net/item/whatsapp-chat-for-wordpress-woocommerce/19935771
 * Description: Open a WhatsApp chat between reader/customer and blog/store owner.
 * Version: 1.2.1
 * Author: idnovate.com
 * Author URI: https://www.idnovate.com/
 * Developer: idnovate.com
 * Developer URI: https://www.idnovate.com/
 * Text Domain: id_whatsappchat
 * Domain Path: /languages
 *
 * Copyright: © 2017 idnovate.com
 * License: Commercial
 * License URI: 
 */

require_once dirname( __FILE__ ) . '/app/code/admin/classes/class.settings-api.php';
require_once dirname( __FILE__ ) . '/app/code/admin/classes/class.whatsappchat.php';
require_once dirname( __FILE__ ) . '/app/code/admin/settings.php';

new Idnovate_Settings_API_WhatsAppChat();
new Idnovate_WhatsAppChat();

add_action('wp_footer', 'whatsappchat_add');
add_shortcode('whatsappchat', 'whatsappchat_shortcode');
add_filter('widget_text','whatsappchat_shortcode');

load_plugin_translations();
function load_plugin_translations() {
    load_plugin_textdomain( 'id_whatsappchat', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}

$plugin = plugin_basename(__FILE__);
add_filter( "plugin_action_links_$plugin", 'add_settings_link' );

function add_settings_link($links)
{
    $settings_link = '<a href="'.admin_url('options-general.php?page=whatsappchat-settings').'">'.__('Settings', 'id_whatsappchat').'</a>';
    array_unshift($links, $settings_link);
    return $links;
}

function whatsappchat_add_js()
{
    wp_enqueue_script('whatsappchat', plugins_url('app/views/js/id_whatsappchat.js', __FILE__));
}
add_action( 'wp_enqueue_scripts', 'whatsappchat_add_js' );

function whatsappchat_add_css()
{
    wp_enqueue_style('whatsappchat', plugins_url('app/views/css/id_whatsappchat.css', __FILE__) );
    wp_enqueue_style('whatsappchat-fa', plugins_url('app/views/css/font-awesome.min.css', __FILE__) );
}
add_action( 'wp_enqueue_scripts', 'whatsappchat_add_css' );

function in_admin_header_id_whatsappchat() {
    wp_enqueue_style('whatsappchat', plugins_url('app/views/css/id_whatsappchat_admin.css', __FILE__) );
    wp_enqueue_script('whatsappchat', plugins_url('app/views/js/id_whatsappchat_admin.js', __FILE__));
}
add_action('in_admin_header', 'in_admin_header_id_whatsappchat');

function whatsappchat_add($atts = ''){
    include_once dirname( __FILE__ ) . '/app/views/front/whatsappchat-button.php';
    $whatsappchat_chats = Idnovate_WhatsAppChat::get_settings_values();
    $current_user_roles = wp_get_current_user()->roles;
    foreach ($whatsappchat_chats as $key => $whatsappchat_chat) {
        if ($whatsappchat_chat['active'] == '1') {
            $user_roles_allowed = $whatsappchat_chat['roles'];
            if ($user_roles_allowed == '') {
                continue;
            } else {
                if (in_array('everyone', $user_roles_allowed)) {
                    $current_user_roles[] = 'everyone';
                }
                if (!array_intersect($current_user_roles, $user_roles_allowed)) {
                    continue;
                }
            }
            if ($whatsappchat_chat['shortcode'] == '1') {
                if ($atts == '') {
                    continue;
                } else {
                    return WhatsappChat_Button::get_button($whatsappchat_chat, (int)$atts['id']);
                }
            } else {
                $is_wp_wc = false;
                $current_page = '';
                if ( is_home() ) {
                    $current_page = 'home';
                    $is_wp_wc = 'wp';
                } elseif ( is_front_page() ) {
                    $current_page = 'frontpage';
                    $is_wp_wc = 'wp';
                } elseif ( is_page()) {
                    $current_page = 'page';
                    $is_wp_wc = 'wp';
                } elseif (!is_page() AND is_single() AND get_post_type() == 'post') {
                    $current_page = 'post';
                    $is_wp_wc = 'wp';
                } elseif (is_category()) {
                    $current_page = 'category';
                    $is_wp_wc = 'wp';
                } elseif (is_archive()) {
                    $current_page = 'archive';
                    $is_wp_wc = 'wp';
                } elseif (is_tag()) {
                    $current_page = 'tag';
                    $is_wp_wc = 'wp';
                }
                if (class_exists( 'WooCommerce' )) {
                    if (is_shop()) {
                        $current_page = 'shop';
                        $is_wp_wc = 'wc';
                    } elseif (is_cart()) {
                        $current_page = 'cart';
                        $is_wp_wc = 'wc';
                    } elseif (is_checkout()) {
                        $current_page = 'checkout';
                        $is_wp_wc = 'wc';
                    } elseif (is_product()) {
                        $current_page = 'product';
                        $is_wp_wc = 'wc';
                    } elseif (is_product_category()) {
                        $current_page = 'product_category';
                        $is_wp_wc = 'wc';
                    } elseif (is_product_tag()) {
                        $current_page = 'product_tag';
                        $is_wp_wc = 'wc';
                    } elseif (is_wc_endpoint_url( 'view-order' )) {
                        $current_page = 'view_order';
                        $is_wp_wc     = 'wc';
                    } elseif (is_wc_endpoint_url( 'lost-password' )) {
                        $current_page = 'lost_password';
                        $is_wp_wc = 'wc';
                    } elseif (is_wc_endpoint_url( 'terms' )) {
                        $current_page = 'terms';
                        $is_wp_wc = 'wc';
                    } elseif (is_account_page()) {
                        $current_page = 'myaccount';
                        $is_wp_wc = 'wc';
                    }
                }
                if (isset($whatsappchat_chat['show_on'])) {
                    $show_on = $whatsappchat_chat['show_on'];
                } else {
                    $show_on = array('wp'); //default
                }
                if ($show_on == '') {
                    continue;
                } else {
                    if (!in_array($is_wp_wc, $show_on )) {
                        continue;
                    }
                }
                if (!in_array('everywhere', $whatsappchat_chat['filter_by_page'])) {
                    if (!in_array($current_page, $whatsappchat_chat['filter_by_page'])) {
                        continue;
                    }
                }
                echo WhatsappChat_Button::get_button($whatsappchat_chat, $key + 1);
            }
        }
    }
}

function whatsappchat_shortcode($atts = ''){
    include_once dirname( __FILE__ ) . '/app/views/front/whatsappchat-button.php';
    if (is_array($atts)) {
    	$id = $atts['id'];
    } else {
    	$atts_formatted = shortcode_parse_atts($atts);
	    if (isset($atts_formatted['role']) && $atts_formatted['role'] = 'form') { //Contact Form 7 compatibility
		    return $atts;
	    }
    	if (isset($atts_formatted['id'])) {
		    $id = str_replace(']', '', $atts_formatted['id']);
	    } else {
    		return $atts;
	    }
    }
    $whatsappchat_chat = Idnovate_WhatsAppChat::get_settings_values($id);
    if (!isset($whatsappchat_chat) || !$whatsappchat_chat || count($whatsappchat_chat) == 0) {
        return $atts;
    }
    $current_user_roles = wp_get_current_user()->roles;
    if ($whatsappchat_chat['active'] == '1') {
        $user_roles_allowed = $whatsappchat_chat['roles'];
        if ($user_roles_allowed == '') {
            return false;
        } else {
            if (in_array('everyone', $user_roles_allowed)) {
                $current_user_roles[] = 'everyone';
            }
            if (!array_intersect($current_user_roles, $user_roles_allowed)) {
                return false;
            }
        }
        return WhatsappChat_Button::get_button($whatsappchat_chat, (int)$id);
    } else {
        return $atts;
    }
}