<?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.
 * Version: 1.0.5
 * 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: 
 */

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

class WhatsappChat_Button extends Idnovate_WhatsAppChat
{
    public static function get_button($options, $id = '')
    {
        include_once WP_PLUGIN_DIR.'/id_whatsappchat/app/code/admin/classes/class.mobile_detect_whatsapp.php';
        $mobile = new Mobile_Detect_WhatsApp();
	    if (isset($options['only_mobile']) && $options['only_mobile'] == '0' && $mobile->isMobile()) {
		    return false;
	    }
	    if (isset($options['only_tablet']) && $options['only_tablet'] == '0' && $mobile->isTablet()) {
		    return false;
	    }
	    if (isset($options['only_desktop']) && $options['only_desktop'] == '0' && $mobile->isDesktop()) {
		    return false;
	    }
	    $mobile_phone = '';
        if (isset($options['mobile_phone'.'_'.get_locale()]) && $options['mobile_phone'.'_'.get_locale()] != '') {
            $mobile_phone = $options['mobile_phone'.'_'.get_locale()];
        } else {
            $mobile_phone = $options['mobile_phone'];
        }
        $chat_group_id = '';
        if (isset($options['chat_group'.'_'.get_locale()]) && $options['chat_group'.'_'.get_locale()] != '') {
            $chat_group_id = $options['chat_group'.'_'.get_locale()];
        } else {
            $chat_group_id = $options['chat_group'];
        }
        $text = '';
        if (isset($options['text'.'_'.get_locale()]) && $options['text'.'_'.get_locale()] != '') {
            $text = $options['text'.'_'.get_locale()];
        } else {
            $text = $options['text'];
        }
	    $message = '';
	    if (isset($options['share_option']) && $options['share_option'] == '1') {
		    $message = parent::getProtocolUrl().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	    } elseif (isset($options['message']) && $options['message'] != '') {
		    $message = $options['message'];
	    }
        ?>
        <?php if ($options['custom_js'] != ''): ?>
            <script>
                <?php echo $options['custom_js']; ?>
            </script>
        <?php endif; ?>
        <?php if ($options['custom_css'] != ''): ?>
            <style type="text/css">
                <?php echo $options['custom_css']; ?>
            </style>
        <?php endif; ?>
        <?php if ($options['shortcode'] == '1'): ?>
            <?php if ($options['hook'] == 'floatingButton' || $options['hook'] == 'floatingButtonTop' || $options['hook'] == 'floatingButtonCenter'): ?>
                <a href="<?php echo parent::getWhatsappUrl($mobile_phone, $message, $chat_group_id); ?>" class="<?php echo 'whatsapp_'.$id; ?> float float-<?php echo $options['position']; ?> float-<?php echo $options['hook']; ?>" style="background-color: <?php echo $options['color'] ?>;" target="_blank">
                    <i class="<?php echo 'whatsapp_'.$id; ?> fa fa-whatsapp whatsapp " <?php if ($text != ''): ?>style="padding-right:0px!important;"<?php endif; ?>></i>
                </a>
                <?php if ($text != ''): ?>
                    <div class="<?php echo 'whatsapp_'.$id; ?> label-container label-container-<?php echo $options['position']; ?> float-<?php echo $options['hook']; ?>">
                        <?php if ($options['position'] == 'left' || $options['position'] == 'center'): ?>
                            <i class="<?php echo 'whatsapp_'.$id; ?> fa fa-caret-left label-arrow" style="font-size: x-large;"></i>
                            <div class="<?php echo 'whatsapp_'.$id; ?> label-text"><?php echo $text ?></div>
                        <?php else: ?>
                            <div class="<?php echo 'whatsapp_'.$id; ?> label-text"><?php echo $text ?></div>
                            <i class="<?php echo 'whatsapp_'.$id; ?> fa fa-play label-arrow"></i>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <?php if ($id > 0): ?>
                    <?php $_html = '' ?>
                    <?php
                        $_html .= '<a class="whatsapp_'.$id.'" target="_blank" href="'.parent::getWhatsappUrl($mobile_phone, $message, $chat_group_id).'">';
                        $_html .= '<div class="whatsapp_'.$id.' whatsapp shortcode '.$options['position'].'" style="background-color: '.$options['color'].'">';
                        $_html .= '<span class="whatsapp_'.$id.'"';
                        if ($options['color'] != '') {
                            $_html .= 'style="background-color: '.$options['color'].'"';
                        }
                        $_html .= '>';
                        $_html .= '<i class="whatsapp_'.$id.' whatsapp-icon" ';
                        if ($text != '') {
                            $_html .= 'style="padding-right:0px!important;"';
                        }
                        $_html .= '></i> ';
                        $_html .= $text;
                        $_html .= '</span></div></a>';
                        return $_html;
                    ?>
                <?php endif; ?>
            <?php endif; ?>
        <?php else: ?>
            <?php if ($options['hook'] == 'floatingButton' || $options['hook'] == 'floatingButtonTop' || $options['hook'] == 'floatingButtonCenter'): ?>
                <a href="<?php echo parent::getWhatsappUrl($mobile_phone, $message, $chat_group_id); ?>" class="<?php echo 'whatsapp_'.$id; ?> float float-<?php echo $options['position']; ?> float-<?php echo $options['hook']; ?>" style="background-color: <?php echo $options['color'] ?>;" target="_blank">
                    <i class="<?php echo 'whatsapp_'.$id; ?> fa fa-whatsapp whatsapp " <?php if ($text != ''): ?>style="padding-right:0px!important;"<?php endif; ?>></i>
                </a>
                <?php if ($text != ''): ?>
                    <div class="<?php echo 'whatsapp_'.$id; ?> label-container label-container-<?php echo $options['position']; ?> float-<?php echo $options['hook']; ?>">
                        <?php if ($options['position'] == 'left' || $options['position'] == 'center'): ?>
                            <i class="<?php echo 'whatsapp_'.$id; ?> fa fa-caret-left label-arrow" style="font-size: x-large;"></i>
                            <div class="<?php echo 'whatsapp_'.$id; ?> label-text"><?php echo $text ?></div>
                        <?php else: ?>
                            <div class="<?php echo 'whatsapp_'.$id; ?> label-text"><?php echo $text ?></div>
                            <i class="<?php echo 'whatsapp_'.$id; ?> fa fa-play label-arrow"></i>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <a target="_blank" class="<?php echo 'whatsapp_'.$id; ?>" href="<?php echo parent::getWhatsappUrl($mobile_phone, $message, $chat_group_id); ?>">
                    <div class="<?php echo 'whatsapp_'.$id; ?> whatsapp whatsapp-<?php echo $options['hook'].' '.$options['position']; ?>" style="background-color: <?php echo $options['color'] ?>;">
                        <span class="<?php echo 'whatsapp_'.$id; ?>" <?php if ($options['color'] != ''): ?> style="background-color: <?php echo $options['color'] ?>" <?php endif; ?>>
                            <i class="<?php echo 'whatsapp_'.$id; ?> whatsapp-icon" <?php if ($text != ''): ?>style="padding-right:0px!important;"<?php endif; ?>></i>
                            <?php echo $text ?>
                        </span>
                    </div>
                </a>
            <?php endif; ?>
        <?php endif; ?>
        <?php
    }
}
