File: /var/www/html/wp-content/plugins/froged/froged-conf.php
<?php
/*
Plugin Name: Froged
Plugin URI: https://app.froged.com/?utm_source=wordpress_plugin
Description: Froged. Live Chat, Email Campaigns, Lead Generation, and much more.
Version: 1.0.7
Requires at least: 3.0
Tested up to: 5.5
WC requires at least: 3.0
WC tested up to: 4.3
Author: Froged Technologies
Author URI: https://froged.com/?utm_source=wordpress_plugin
License: GPLv2
*/
if(!defined('ABSPATH')) exit;
require_once('froged-admin.php');
// ACTIVAR PLUGIN
register_activation_hook( __FILE__, 'froged_woo_install' );
function froged_woo_install() {
$froged_woo_options = array(
'id' => '',
'include_snippet' => '1',
'product_ref' => '0',
'taxes' => '1'
);
if (!get_option('froged_woo_options')) {
update_option( 'froged_woo_options', $froged_woo_options );
}
}
// INIT PLUGIN
add_action('plugins_loaded', 'froged_woo_setup');
function froged_woo_setup() {
$options = get_option('froged_woo_options');
$id = esc_html($options['id']);
if (!empty($id) && (!isset($options['include_snippet']) != '1' || $options['include_snippet']) ){
add_action( 'wp_head', 'froged_woo_header', 102 );
add_action('wp_login', 'froged_login',10,2);
add_action('wp_logout', 'froged_logout');
add_action('user_register', 'froged_on_sign_up');
add_action( 'wp_enqueue_scripts', 'froged_scripts' );
if ( class_exists( 'WooCommerce' ) ) {
add_action('woocommerce_add_to_cart','froged_addtocart',10, 6);
add_action('woocommerce_remove_cart_item','froged_removefromcart',10, 6);
add_action('wp_footer', 'froged_add_to_wishlist');
add_action('wp_ajax_getDataWhislist', 'froged_getDataWhislist');
add_action('wp_ajax_nopriv_getDataWhislist', 'froged_getDataWhislist');
}
}
}
function froged_scripts() {
wp_enqueue_script( 'contactform-js', '' . plugins_url( 'js/contactform.js', __FILE__ ) . '', array(),false,false );
}
// Login Event
function froged_login($user_login) {
froged_track('on_login');
}
// Delete cookie when logout
function froged_logout($user_login) {
froged_track('on_logout');
$options = get_option('froged_woo_options');
$cookie_name = 'fgSesionId_' . esc_html($options['id']);
setcookie($cookie_name, ' ', time() - 3600,'/', COOKIE_DOMAIN);
}
// Add to cart event
function froged_addtocart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
$product = wc_get_product($product_id);
$options = get_option('froged_woo_options');
$price = $product->get_price();
$name = $product->get_title();
$content_sku = $product->get_sku();
$price_ex = wc_get_price_excluding_tax($product);
$meta = array('item_name'=>$name,
'item'=> (isset($options['product_ref']) && $options['product_ref']) ? $product_id : $content_sku,
'value'=> (isset($options['taxes']) && $options['taxes']) ? number_format($price,2,'.','') : number_format($price_ex,2,'.',''),
'currency'=>get_option('woocommerce_currency'),
'quantity'=>$quantity);
froged_track('on_add_to_cart',$meta);
}
// Remove from cart event
function froged_removefromcart($removed_cart_item_key, $cart) {
$line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];
$product = wc_get_product($line_item[ 'product_id' ] );
$options = get_option('froged_woo_options');
$product_id = $product->get_id();
$price = $product->get_price();
$name = $product->get_title();
$content_sku = $product->get_sku();
$price_ex = wc_get_price_excluding_tax($product);
$quantity = $line_item[ 'quantity' ];
$meta = array('item_name'=>$name,
'item'=> (isset($options['product_ref']) && $options['product_ref']) ? $product_id : $content_sku,
'value'=> (isset($options['taxes']) && $options['taxes']) ? number_format($price,2,'.','') : number_format($price_ex,2,'.',''),
'currency'=>get_option('woocommerce_currency'),
'quantity'=>$quantity);
froged_track('on_remove_from_cart',$meta);
}
// Wrapper function to track API
function froged_track($event_name,$meta = array())
{
$options = get_option('froged_woo_options');
$slug = sanitize_text_field($options['id']);
$cookie_name = 'fgSesionId_' . $slug;
if(isset($_COOKIE[$cookie_name]) && !empty($slug) && (!isset($options['include_snippet']) != '1' || $options['include_snippet'])) {
$session_id = sanitize_text_field($_COOKIE[$cookie_name]);
$url_base = "https://api.froged.com/sdk/";
$url = $url_base . $slug . '/' . $session_id . '/track/' . $event_name;
$referer = wp_get_referer();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$actual_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$info = array('url' => $actual_url, 'referrer' => isset($referer) ? '' : $referer, 'userAgent' => $user_agent );
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 2,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => false,
'headers' => array(),
'body' => array('meta' => $meta, 'info' => $info),
'cookies' => array()));
}
}
function froged_getDataWhislist(){
$id_product=(int)sanitize_text_field($_POST["id_product"]);
if($id_product!=""){
global $woocommerce;
$product = wc_get_product($id_product);
$productname = $product->get_title();
$array = array(
item_name => $productname,
currency => get_woocommerce_currency_symbol()
);
wp_send_json($array);
}
}
// add to wishlist
function froged_add_to_wishlist(){
?>
<script type="text/javascript">
var cont=0;
(function($){
$(document).on('click','.add_to_wishlist', function(){
jQuery.ajax({
type: 'POST',
dataType: 'json',
url: "<?php echo admin_url('admin-ajax.php'); ?>",
data: {
action: "froged_getDataWhislist",
'id_product': $(".add_to_wishlist").attr("data-product-id")
}, success: function (data) {
if(cont==0){
cont++;
Froged('track', 'on_add_to_wishlist', {
value: '1',
currency: data.currency,
item: $(".add_to_wishlist").attr("data-product-id"),
item_name: data.item_name
});
}
}
});
});
})(jQuery);
</script>
<?php
}
// Search event
function froged_on_search($param){
if($param!=""){
?>
<script>
Froged('track', 'on_search', {
search_term: '<?php echo $param; ?>'
});
</script>
<?php
}
}
// Sign up wordpress
function froged_on_sign_up(){
froged_track('on_sign_up');
}
// Include header
function froged_woo_header() {
$options = get_option('froged_woo_options');
$id = esc_html($options['id']);
if (!isset($options['include_snippet']) != '1' || $options['include_snippet'] ) {
?>
<!-- Start Froged --><script>window.frogedSettings = { appId: '<?php echo "$id";?>' }</script>
<script>(function(){var a=document,b=window;if("function"!=typeof Froged){Froged=function(a,b,c){Froged.c(a,b,c)},Froged.q=[],Froged.c=function(a,b,c){Froged.q.push([a,b,c])},froged_data={hooks:{}};var c=function(){var b=a.createElement("script");b.type="text/javascript",b.async=!0,b.src="https://sdk.froged.com";var c=a.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)};"complete"===a.readyState?c():window.attachEvent?b.attachEvent("onload",c):b.addEventListener("load",c,!1)}})()</script>
<!-- End Froged -->
<?php
// event search
if(isset($_GET["s"]))
{
$search = sanitize_text_field($_GET["s"]);
froged_on_search($search);
}
if( is_user_logged_in() )
{
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$userId = $current_user->ID;
$email = $current_user->user_email;
$name = $current_user->display_name;
$firstname = $current_user->first_name;
$lastname = $current_user->last_name;
?>
<script>
function froged_ensureValue(strValue) {
return strValue === ""? null : strValue;
}
Froged('set',
{
username:froged_ensureValue('<?php echo "$username";?>'),
userId: froged_ensureValue('<?php echo "$userId";?>'),
email:froged_ensureValue('<?php echo "$email";?>'),
name:froged_ensureValue('<?php echo "$name";?>'),
firstname:froged_ensureValue('<?php echo "$firstname";?>'),
lastname:froged_ensureValue('<?php echo "$lastname";?>')
});
</script>
<?php
if ( class_exists( 'WooCommerce' ) ) {
$countries_obj = new WC_Countries();
$countries_array = $countries_obj->get_countries();
$country_states_array = $countries_obj->get_states();
$street = $current_user->billing_address_1." ".$current_user->billing_address_2;
$city = $current_user->billing_city;
$postalcode = $current_user->billing_postcode;
if(isset($country_states_array[$current_user->billing_country][$current_user->billing_state]))
$state = html_entity_decode($country_states_array[$current_user->billing_country][$current_user->billing_state]);
else
$state = null;
if(isset($countries_array[$current_user->billing_country]))
$country = html_entity_decode($countries_array[$current_user->billing_country]);
else
$country = null;
$phone = $current_user->billing_phone;
$company=$current_user->billing_company;
?>
<script>
Froged('set',
{
street:froged_ensureValue('<?php echo "$street";?>'),
city:froged_ensureValue('<?php echo "$city";?>'),
postalcode:froged_ensureValue('<?php echo "$postalcode";?>'),
state:froged_ensureValue('<?php echo "$state";?>'),
country:froged_ensureValue('<?php echo "$country";?>'),
phone:froged_ensureValue('<?php echo "$phone";?>'),
company:froged_ensureValue('<?php echo "$company";?>')
});
</script>
<?php
}
}
}
else{return;}
if ( class_exists( 'WooCommerce' ) ) {
// View item event
add_action('wp_footer', 'froged_viewcontent');
function froged_viewcontent() {
global $product;
if(is_product()){
$options = get_option('froged_woo_options');
$content_sku = $product->get_sku();
$content_id = $product->get_id();
$name = $product->get_title();
$price = $product->get_price();
$price_ex = wc_get_price_excluding_tax($product);
$product_cats = get_the_terms($product->get_id(), 'product_cat');
if ($product_cats) {
$cat = $product_cats[0];
$product_category = $cat->name;
} else {
$product_category = 'None';
}
if($product->price!=$product->regular_price){
?>
<script type="text/javascript">
(function($){
Froged('track', 'on_view_promotion', {
promotions: '<?php echo "sku: ".$product->get_sku()." - price offer: ".$product->price." ".get_option('woocommerce_currency'); ?>',
});
})(jQuery);
</script>
<?php
}
else{
?>
<!-- Froged on_view_item event -->
<script>
Froged('track', 'on_view_item', {
item_name: '<?php echo $name; ?>',
item_id: ['<?php echo (isset($options['product_ref']) && $options['product_ref']) ? $content_id : $content_sku; ?>'],
value: <?php echo (isset($options['taxes']) && $options['taxes']) ? number_format($price,2,'.','') : number_format($price_ex,2,'.',''); ?>,
currency:'<?php echo $options['currency'];?>'
});
</script>
<?php
}
}
}
// View item list event
add_action('wp_footer', 'froged_viewcategory');
function froged_viewcategory() {
global $wp_query;
if( is_product_category() ){
$options = get_option('froged_woo_options');
$products = array_values(array_map(function($item) {return wc_get_product($item->ID);}, $wp_query->get_posts()));
foreach ($products as $product) {
$content_sku[] = "'".$product->get_sku()."'";
$content_id[] = "'".$product->get_id()."'";
}
?>
<!-- Froged on_view_item_list event -->
<script>
Froged('track', 'on_view_item_list', {
list_name: '<?php echo single_cat_title(); ?>',
items: [<?php echo (isset($options['product_ref']) && $options['product_ref']) ? implode( ', ',$content_id) : implode( ', ',$content_sku); ?>]
});
</script>
<?php
}
}
//Begin checkout event
add_action('woocommerce_after_checkout_form', 'froged_initiatecheckout');
function froged_initiatecheckout() {
if( is_checkout() && ! is_wc_endpoint_url() ){
$options = get_option('froged_woo_options');
$cart_items = WC()->cart->cart_contents;
$cart_total_ex = WC()->cart->subtotal_ex_tax;
$cart_total = WC()->cart->subtotal;
$cart_prods = WC()->cart->get_cart();
$cart_coupons = WC()->cart->applied_coupons;
if($cart_items) foreach ($cart_items as $cart_item){
$content_id[] = "'".$cart_item['product_id']."'";
$content_sku[] = "'".$cart_item['data']->get_sku()."'";
}
?>
<!-- Froged on_begin_checkout event -->
<script>
Froged('track', 'on_begin_checkout', {
coupon: '<?php echo $cart_coupons[0];?>',
items: [<?php echo (isset($options['product_ref']) && $options['product_ref']) ? implode( ', ',$content_id) : implode( ', ',$content_sku); ?>],
value: <?php echo (isset($options['taxes']) && $options['taxes']) ? number_format($cart_total,2,'.','') : number_format($cart_total_ex,2,'.',''); ?>,
currency:'<?php echo $options['currency'];?>'
});
</script>
<?php
}
}
// Purchase event
add_action('woocommerce_thankyou', 'froged_purchase');
function froged_purchase( $order_id ) {
$options = get_option('froged_woo_options');
$order = new WC_Order( $order_id );
$order_total = $order->get_total();
$order_total_ex = $order_total - $order->get_total_tax();
$order_items = $order->get_items();
$order_num_items = $order->get_item_count();
$coupon = $order->get_used_coupons();
if($order_items) foreach ($order_items as $order_item => $item){
$content_id[] = "'".$item['product_id']."'";
$product_id = wc_get_product($item['product_id']);
$variation_id = wc_get_product($item['variation_id']);
if ($variation_id) {
$content_sku[] = "'".$variation_id->get_sku()."'";
} else {
$content_sku[] = "'".$product_id->get_sku()."'";
}
}
?>
<!-- Froged on_purchase event -->
<script>
Froged('track', 'on_purchase', {
items: [<?php echo (isset($options['product_ref']) && $options['product_ref']) ? implode( ', ',$content_id) : implode( ', ',$content_sku); ?>],
value: <?php echo (isset($options['taxes']) && $options['taxes']) ? number_format($order_total,2,'.','') : number_format($order_total_ex,2,'.',''); ?>,
currency:'<?php echo $options['currency'];?>',
transaction_id:'<?php echo $order_id;?>',
coupon:'<?php echo implode( ', ',$coupon);?>',
num_items:'<?php echo $order_num_items;?>'
});
</script>
<?php
}
}
}
?>