<?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.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 ( !class_exists('Idnovate_WhatsAppChat' ) ):
class Idnovate_WhatsAppChat {

	function __construct() {
		add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
	}

	public static function get_settings_values($id = false) {
		if ($id) {
			return get_option( 'whatsappchat_settings_'.$id );
		}
		$values = array();
		$nb = get_option( 'whatsappchat_settings_nb' );
		for ($i=1; $i <= $nb; $i++) {
			if (get_option( 'whatsappchat_settings_'.$i ) != '') {
				$values[] = get_option( 'whatsappchat_settings_'.$i );
			}
		}
		return $values;
	}

	public function formatMobilePhoneForWhatsapp($phone, $id_country = false)
	{
		$phone = preg_replace('/[^0-9]+/', '', $phone);
		if (!$id_country) {
			return $phone;
		} else {
			$country = new Country((int)$id_country);
			if (substr($phone, 0, strlen($country->call_prefix)) == $country->call_prefix) {
				return $phone;
			} else {
				return $country->call_prefix.$phone;
			}
		}
	}

	public static function getWhatsappUrl($phone = false, $text = false, $chat_group = '')
	{
		include_once 'class.mobile_detect_whatsapp.php';
		$mobile = new Mobile_Detect_WhatsApp();
		if ($mobile->isMobile() || $mobile->isTablet()) {
			if (Idnovate_WhatsAppChat::isFacebookInstagramInAppBrowser()) {
				if ($mobile->is('AndroidOS')) {
					return 'intent://send/'.($phone ? '&phone='.$phone : '').'#Intent;scheme=smsto;package=com.whatsapp;action=android.intent.action.SENDTO;end';
				}
			}
			$url = 'https://api.whatsapp.com/';
		} else {
			$url = 'https://web.whatsapp.com/';
		}
		if ($chat_group != '') {
			return 'https://chat.whatsapp.com/'.$chat_group;
		}
		return $url.'send?l='.substr(get_bloginfo ( 'language' ), 0, 2).($phone ? '&phone='.$phone : '').($text ? '&text='.$text : '');
	}

	public static function plugin_row_meta( $links, $file ) {
		if ( $file == 'id_whatsappchat/id_whatsappchat.php' ) {
			$row_meta = array(
				'docs'    => '<a href="' . esc_url( plugins_url('id_whatsappchat/doc/index.html?TB_iframe=true&width=600&height=550') ) . '" class="thickbox open-plugin-details-modal" title="' . esc_attr( __( 'View WhatsApp Chat Documentation', 'id_whatsappchat' ) ) . '">' . __( 'Documentation', 'id_whatsappchat' ) . '</a>',
			);
			return array_merge( $links, $row_meta );
		}
		return (array) $links;
	}

	public static function getProtocolUrl()
	{
		if (isset($_SERVER['HTTPS'])) {
			$protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
		} else {
			$protocol = 'http';
		}

		return $protocol."://";
	}

	protected function isFacebookInstagramInAppBrowser()
	{
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		if (strpos($user_agent, 'FBAN')) {
			return true;
		} elseif (strpos($user_agent, 'Instagram')) {
			return true;
		} else {
			return false;
		}
	}
}
endif;
