<?php

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

if (!function_exists('ahachat_hour_reminder')) {


    function ahachat_hour_reminder($date1, $date2)
    {


        $day_date1 = date('d', strtotime($date1));

        $day_date2 = date('d', strtotime($date2));


        if ($day_date2 > $day_date1) {


            $diff = abs(strtotime($date2) - strtotime($date1));

            $years = floor($diff / (365 * 60 * 60 * 24));

            $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));

            $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

            $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));


            $hours_day = $day_date2 - $day_date1;


            return $hours_day * 24 + $hours;

        } else {

            $diff = abs(strtotime($date2) - strtotime($date1));

            $years = floor($diff / (365 * 60 * 60 * 24));

            $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));

            $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));

            $hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));

            return $hours;

        }


    }

}

//

$ahachat_first_reminder = get_option('ahachat_first_reminder');

if (isset($ahachat_first_reminder['hour']) && !empty($ahachat_first_reminder['hour'])):

    add_filter('cron_schedules', 'add_custom_cron_schedule_first_reminder');

    function add_custom_cron_schedule_first_reminder($schedules)
    {

        $second_hour_reminder = 60 * 1;//60*2 + 60*1;

        $first_hour_reminder = 60 * 1;

        $third_hour_reminder = 60 * 1;

        $schedules['ahachat_first_reminder'] = array(

            'interval' => $first_hour_reminder, //60 * 1, //(60 * 60 * $first_hour)

            'display' => __('First Reminder')

        );

        $schedules['ahachat_second_reminder'] = array(

            'interval' => $second_hour_reminder,//60 *2, //((60 * 60 * $first_hour) + (60 * 60 * $second_hour))

            'display' => __('Second Reminder')

        );

        $schedules['ahachat_third_reminder'] = array(

            'interval' => $third_hour_reminder,//60 *2, //((60 * 60 * $first_hour) + (60 * 60 * $second_hour))

            'display' => __('Third Reminder')

        );

        return $schedules;

    }

    /****************************************************/

    add_action(AHACHAT_FIRST_REMINDER_CRON_ACTION_NAME, 'ahachat_do_magic_schedule_first_reminder');

    function ahachat_do_magic_schedule_first_reminder()
    {
        $fb_api = new AHACHAT_WP_API();

        $page_id = get_option('ahachat_page_id');

        global $wpdb;

        $table_user_ref = AHACHAT_USER_REF_TABLE;

        $list_user_ref = $wpdb->get_results("SELECT * FROM $table_user_ref");

        $ahachat_first_reminder = get_option('ahachat_first_reminder');

        $ahachat_second_reminder = get_option('ahachat_second_reminder');

        $ahachat_third_reminder = get_option('ahachat_third_reminder');

        $time_now = date('Y-m-d H:i:s', time());

        //statistical

        $ahachat_table_statistical = AHACHAT_ANALYTICS_TABLE;

        $ahachat_date_statistical = date('Y-m-d', time());

        $day = date('d', time());

        $month = date('m', time());

        $year = date('Y', time());

        //statistical

        if (!empty($page_id)) {

            if (isset($ahachat_first_reminder['on_of'])) {

                $page_access_token = $fb_api->AHA_Get_Access_Token_Page();

                if (isset($ahachat_first_reminder['hour']) && !empty($ahachat_first_reminder['hour'])) {

                    $first_hour = (double)$ahachat_first_reminder['hour'];

                    if (count($list_user_ref) > 0) {

                        if (gettype($page_access_token) == "array") {

                            $page_token = $page_access_token["access_token"];

                            $content_message = $ahachat_first_reminder['something'];

                            if (isset($ahachat_first_reminder['checkout']) && empty($ahachat_first_reminder['checkout']))

                                $title_button_generic = "Checkout Now";

                            else

                                $title_button_generic = $ahachat_first_reminder['checkout'];

                            foreach ($list_user_ref as $key => $user) {

                                $user_ref = $user->user_ref;

                                $row_user_ref = $wpdb->get_row("SELECT * FROM $table_user_ref WHERE user_ref='" . $user_ref . "' ");

                                $create_time = $user->create_time;

                                if (ahachat_hour_reminder($create_time, $time_now) >= $first_hour) {

                                    if ($row_user_ref->status_send_first == 0) {// require only send once

                                        $is_shortcode = $row_user_ref->is_shortcode;

                                        $array_customer_cart = unserialize($row_user_ref->your_cart);

                                        if (count($array_customer_cart) > 0 || !empty($array_customer_cart)) {

                                            $recipient = $fb_api->AHA_Send_message_text_user_ref($page_id, $page_token, $content_message, $user_ref);

                                            $recipient = substr($recipient, 0, 18);

                                            $product_id_list = array();

                                            $quantity_list = array();

                                            foreach ($array_customer_cart as $key => $list) {

                                                array_push($product_id_list, $list["product_id"]);

                                                array_push($quantity_list, $list["quantity"]);

                                            }

                                            $url_proudct_id_quantiy = implode(",", $product_id_list) . "&quantity=" . implode(",", $quantity_list);

                                            $url_button_generic = esc_url_raw(add_query_arg('multi-product-add', $url_proudct_id_quantiy, wc_get_checkout_url()));

                                            $url_button_generic = $url_button_generic . '&reminder=first&user=' . $user_ref;

                                            $url_button_generic = str_replace("http://", "https://", $url_button_generic);

                                            //when click check out

                                            $array_product2 = array();

                                            foreach ($array_customer_cart as $key => $list) {

                                                $title = html_entity_decode(get_the_title($list["product_id"]));

                                                $price = get_post_meta($list["product_id"], '_regular_price', true);

                                                $price_sale = get_post_meta($list["product_id"], '_sale_price', true);

                                                if (!empty($price_sale))

                                                    $subtitle_before = html_entity_decode(get_option('ahachat_currency')) . $price . ' - ' . html_entity_decode(get_option('ahachat_currency')) . $price_sale;

                                                else

                                                    $subtitle_before = html_entity_decode(get_option('ahachat_currency')) . $price;

                                                $subtitle = html_entity_decode($subtitle_before . " - " . get_the_title($list["product_id"]));

                                                $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($list["product_id"]), 'full', false)[0];

                                                $image_url = str_replace("http://", "https://", $image_url);

                                                $button_view_url_product = get_the_permalink($list["product_id"]);

                                                $button_view_url_product = str_replace("http://", "https://", $button_view_url_product);

                                                $array_product2[] = array(

                                                    "title" => $title . ' X ' . $list["quantity"],

                                                    "subtitle" => $subtitle,

                                                    "image_url" => $image_url,

                                                    "default_action" => array("type" => "web_url", "url" => $button_view_url_product, "messenger_extensions" => true, "webview_height_ratio" => "tall", "fallback_url" => $button_view_url_product),

                                                    "buttons" => array(array("type" => "web_url", "url" => $url_button_generic, "title" => $title_button_generic))

                                                );

                                            }

                                            $fb_api->AHA_Send_message_one_product_user_ref($page_id, $page_token, $user_ref, $array_product2);

                                            if (!isset($ahachat_second_reminder['on_of']) && !isset($ahachat_third_reminder['on_of'])) {

                                                $wpdb->delete($table_user_ref, array('user_ref' => $user_ref), array('%s'));

                                            } else {

                                                $wpdb->update(

                                                    $table_user_ref,

                                                    array(

                                                        'status_send_first' => 1,

                                                    ),

                                                    array('user_ref' => $user_ref),

                                                    array('%d'),

                                                    array('%s')

                                                );

                                            }

                                            $list_statistical = $wpdb->get_row("SELECT * FROM $ahachat_table_statistical WHERE day='" . $day . "' AND month='" . $month . "' AND year='" . $year . "' ");

                                            if (empty($list_statistical) || $list_statistical == NULL || $list_statistical == "")://

                                                $list_user_test[] = array(

                                                    'recipient_id' => $recipient,

                                                    'page_id' => $page_id

                                                );

                                                $arr_list_user = serialize($list_user_test);

                                                //

                                                if ($is_shortcode == 1) {

                                                    $list_shortcode_user = array($recipient);

                                                    $wpdb->insert($ahachat_table_statistical,

                                                        array(

                                                            'user_test' => 0,

                                                            'send_user' => 1,

                                                            'shortcode_user' => 1,

                                                            'shortcode_sent' => 1,

                                                            'list_shortcode_user' => serialize($list_shortcode_user),

                                                            'first_reminder_sent' => 1,

                                                            'second_reminder_sent' => 0,

                                                            'third_reminder_sent' => 0,

                                                            'list_user' => $arr_list_user,

                                                            'day' => $day,

                                                            'month' => $month,

                                                            'year' => $year,

                                                            'date_statis' => $ahachat_date_statistical

                                                        ), array('%d', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s'));

                                                } else {

                                                    $wpdb->insert($ahachat_table_statistical,

                                                        array(

                                                            'user_test' => 0,

                                                            'send_user' => 1,

                                                            'first_reminder_sent' => 1,

                                                            'second_reminder_sent' => 0,

                                                            'third_reminder_sent' => 0,

                                                            'list_user' => $arr_list_user,

                                                            'day' => $day,

                                                            'month' => $month,

                                                            'year' => $year,

                                                            'date_statis' => $ahachat_date_statistical

                                                        ), array('%d', '%d', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s'));

                                                }

                                            //


                                            else:

                                                $array_list_user = unserialize($list_statistical->list_user);

                                                $user_test = $list_statistical->user_test;

                                                $send_user = $list_statistical->send_user + 1;

                                                $first_reminder_sent = $list_statistical->first_reminder_sent + 1;

                                                //

                                                $shortcode_user = $list_statistical->shortcode_user;

                                                $shortcode_sent = $list_statistical->shortcode_sent;

                                                $list_shortcode_user = $list_statistical->list_shortcode_user;

                                                if ($is_shortcode == 1) {

                                                    $shortcode_sent = $shortcode_sent + 1;

                                                    $arr__list_shortcode_user = unserialize($list_shortcode_user);

                                                    if (!in_array($recipient, $arr__list_shortcode_user)) {

                                                        $shortcode_user = $shortcode_user + 1;

                                                        array_push($arr__list_shortcode_user, $recipient);

                                                    }

                                                    $list_shortcode_user = serialize($arr__list_shortcode_user);

                                                }

                                                $arr_recipient_id = array();

                                                foreach ($array_list_user as $key => $value) {

                                                    array_push($arr_recipient_id, $value["recipient_id"]);

                                                    $list_user_test[] = array(

                                                        'recipient_id' => $value["recipient_id"],

                                                        'page_id' => $value["page_id"]

                                                    );

                                                }

                                                if (!in_array($recipient, $arr_recipient_id)) {

                                                    $list_user_test[] = array(

                                                        'recipient_id' => $recipient,

                                                        'page_id' => $page_id,

                                                    );

                                                    $user_test = $user_test + 1;

                                                }

                                                $arr_list_user = serialize($list_user_test);

                                                $wpdb->update(

                                                    $ahachat_table_statistical,

                                                    array(

                                                        'user_test' => $user_test,

                                                        'send_user' => $send_user,

                                                        'shortcode_user' => $shortcode_user,

                                                        'shortcode_sent' => $shortcode_sent,

                                                        'list_shortcode_user' => $list_shortcode_user,

                                                        'first_reminder_sent' => $first_reminder_sent,

                                                        'list_user' => $arr_list_user

                                                    ),

                                                    array('day' => $day, 'month' => $month, 'year' => $year),

                                                    array('%d', '%d', '%d', '%d', '%s', '%d', '%s'),

                                                    array('%d', '%d', '%d')

                                                );

                                            endif;

                                            $post_type_user_add_cart = new AHA_POST_TYPE_USER_ADD_TO_CART();

                                            $agrs = array(

                                                'recipient_id' => $recipient,

                                                'page_id' => $page_id

                                            );

                                            if (!$post_type_user_add_cart->CheckUserExit($agrs)) {

                                                $avatar = "-";

                                                $first_name = "-";

                                                $last_name = "-";

                                                $gender = "-";

                                                $language = "-";

                                                foreach ($array_customer_cart as $key => $list) {

                                                    $product_quantity[] = array(

                                                        'product_id' => $list["product_id"],

                                                        'quantity' => $list["quantity"]

                                                    );

                                                }

                                                $product_quantity_js = serialize($product_quantity);

                                                $arr = array(

                                                    'mess' => "",

                                                    'post_date' => current_time('Y-m-d H:i:s'),

                                                    'post_type' => AHACHAT_USER_ADD_TO_CART_POST_TYPE,

                                                    'post_status' => 'publish',

                                                    'ahachat_avatar_user' => $avatar,

                                                    'ahachat_first_name_user' => $first_name,

                                                    'ahachat_last_name_user' => $last_name,

                                                    'ahachat_gender_user' => $gender,

                                                    'ahachat_locale_user' => $language,

                                                    'ahachat_recipient_id_user' => $recipient,

                                                    'ahachat_user_ref' => $user_ref,

                                                    'ahachat_page_id' => $page_id,

                                                    'ahachat_product_user' => $product_quantity_js,

                                                    'ahachat_type' => 'Add-to-cart'

                                                );

                                                $User_Add_Cart = $post_type_user_add_cart->Insert($arr);

                                            } else {

                                                $pid = $post_type_user_add_cart->Aha_Post_ID($agrs);

                                                foreach ($array_customer_cart as $key => $list) {

                                                    $product_quantity[] = array(

                                                        'product_id' => $list["product_id"],

                                                        'quantity' => $list["quantity"]

                                                    );

                                                }

                                                $product_quantity_js = serialize($product_quantity);

                                                update_post_meta($pid, 'ahachat_product_user', $product_quantity_js);

                                                $ahachat_type = "Add-to-cart";

                                                $list_type = get_post_meta($pid, 'ahachat_type', true);

                                                $array_type = explode(",", $list_type);

                                                if (!in_array($ahachat_type, $array_type)) {

                                                    array_push($array_type, $ahachat_type);

                                                }

                                                $ahachat_type = implode(",", $array_type);

                                                update_post_meta($pid, 'ahachat_type', $ahachat_type);

                                            }

                                        } else {

                                            $wpdb->delete($table_user_ref, array('user_ref' => $user_ref), array('%s'));

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

    }

    add_action(AHACHAT_SECOND_REMINDER_CRON_ACTION_NAME, 'ahachat_do_magic_schedule_second_reminder');

    function ahachat_do_magic_schedule_second_reminder()
    {

        $fb_api = new AHACHAT_WP_API();

        $page_id = get_option('ahachat_page_id');

        global $wpdb;

        $table_user_ref = AHACHAT_USER_REF_TABLE;

        $list_user_ref = $wpdb->get_results("SELECT * FROM $table_user_ref");

        $ahachat_first_reminder = get_option('ahachat_first_reminder');

        $ahachat_second_reminder = get_option('ahachat_second_reminder');

        $ahachat_third_reminder = get_option('ahachat_third_reminder');

        $time_now = date('Y-m-d H:i:s', time());

        //statistical

        $ahachat_table_statistical = AHACHAT_ANALYTICS_TABLE;

        $ahachat_date_statistical = date('Y-m-d', time());

        $day = date('d', time());

        $month = date('m', time());

        $year = date('Y', time());

        //statistical

        if (!empty($page_id)) {

            if (isset($ahachat_second_reminder['on_of'])) {

                $page_access_token = $fb_api->AHA_Get_Access_Token_Page();

                if (isset($ahachat_second_reminder['hour']) && !empty($ahachat_second_reminder['hour'])) {

                    $first_hour = (isset($ahachat_first_reminder['hour']) && !empty($ahachat_first_reminder['hour'])) ? (double)$ahachat_first_reminder['hour'] : 0;

                    $second_hour = (double)$ahachat_second_reminder['hour'];

                    $second_minder = $first_hour + $second_hour;

                    if (count($list_user_ref) > 0) {

                        if (gettype($page_access_token) == "array") {

                            $page_token = $page_access_token["access_token"];

                            $content_message = $ahachat_second_reminder['something'];

                            $url_button_generic = esc_url(wc_get_checkout_url());

                            if (!isset($ahachat_second_reminder['checkout']) && empty($ahachat_second_reminder['checkout']))

                                $title_button_generic = "Checkout Now";

                            else

                                $title_button_generic = $ahachat_second_reminder['checkout'];

                            foreach ($list_user_ref as $key => $user) {

                                $user_ref = $user->user_ref;

                                $row_user_ref = $wpdb->get_row("SELECT * FROM $table_user_ref WHERE user_ref='" . $user_ref . "' ");

                                $create_time = $user->create_time;

                                if (ahachat_hour_reminder($create_time, $time_now) >= $second_minder) {

                                    if ($row_user_ref->status_send_second == 0 && $row_user_ref->status_send_first == 1) {// require only send once

                                        $is_shortcode = $row_user_ref->is_shortcode;

                                        $array_customer_cart = unserialize($row_user_ref->your_cart);

                                        if (count($array_customer_cart) > 0 || !empty($array_customer_cart)) {

                                            $recipient = $fb_api->AHA_Send_message_text_user_ref($page_id, $page_token, $content_message, $user_ref);

                                            $recipient = substr($recipient, 0, 18);

                                            $product_id_list = array();

                                            $quantity_list = array();

                                            foreach ($array_customer_cart as $key => $list) {

                                                array_push($product_id_list, $list["product_id"]);

                                                array_push($quantity_list, $list["quantity"]);

                                            }

                                            $url_proudct_id_quantiy = implode(",", $product_id_list) . "&quantity=" . implode(",", $quantity_list);

                                            $url_button_generic = esc_url_raw(add_query_arg('multi-product-add', $url_proudct_id_quantiy, wc_get_checkout_url()));

                                            $url_button_generic = $url_button_generic . '&reminder=second&user=' . $user_ref;

                                            $url_button_generic = str_replace("http://", "https://", $url_button_generic);

                                            //when click check out

                                            $array_product2 = array();

                                            foreach ($array_customer_cart as $key => $list) {

                                                $title = html_entity_decode(get_the_title($list["product_id"]));

                                                $price = get_post_meta($list["product_id"], '_regular_price', true);

                                                $price_sale = get_post_meta($list["product_id"], '_sale_price', true);

                                                if (!empty($price_sale))

                                                    $subtitle_before = html_entity_decode(get_option('ahachat_currency')) . $price . ' - ' . html_entity_decode(get_option('ahachat_currency')) . $price_sale;

                                                else

                                                    $subtitle_before = html_entity_decode(get_option('ahachat_currency')) . $price;

                                                $subtitle = html_entity_decode($subtitle_before . " - " . get_the_title($list["product_id"]));

                                                $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($list["product_id"]), 'full', false)[0];

                                                $image_url = str_replace("http://", "https://", $image_url);

                                                $button_view_url_product = get_the_permalink($list["product_id"]);

                                                $button_view_url_product = str_replace("http://", "https://", $button_view_url_product);

                                                $array_product2[] = array(

                                                    "title" => $title . ' X ' . $list["quantity"],

                                                    "subtitle" => $subtitle,

                                                    "image_url" => $image_url,

                                                    "default_action" => array("type" => "web_url", "url" => $button_view_url_product, "messenger_extensions" => true, "webview_height_ratio" => "tall", "fallback_url" => $button_view_url_product),

                                                    "buttons" => array(array("type" => "web_url", "url" => $url_button_generic, "title" => $title_button_generic))

                                                );

                                            }

                                            $fb_api->AHA_Send_message_one_product_user_ref($page_id, $page_token, $user_ref, $array_product2);

                                            if (!isset($ahachat_third_reminder['on_of'])) {

                                                $wpdb->delete($table_user_ref, array('user_ref' => $user_ref), array('%s'));

                                            } else {

                                                $wpdb->update(

                                                    $table_user_ref,

                                                    array(

                                                        'status_send_second' => 1

                                                    ),

                                                    array('user_ref' => $user_ref),

                                                    array('%d'),

                                                    array('%s')

                                                );

                                            }

                                            //add/update table statistical

                                            $list_statistical = $wpdb->get_row("SELECT * FROM $ahachat_table_statistical WHERE day='" . $day . "' AND month='" . $month . "' AND year='" . $year . "' ");

                                            if (empty($list_statistical) || $list_statistical == NULL || $list_statistical == "")://

                                                $user_test = 1;

                                                $send_user = 1;

                                                $list_user_test[] = array(

                                                    'recipient_id' => $recipient,

                                                    'page_id' => $page_id

                                                );

                                                $arr_list_user = serialize($list_user_test);


//

                                                if ($is_shortcode == 1) {

                                                    $list_shortcode_user = array($recipient);

                                                    $wpdb->insert($ahachat_table_statistical,

                                                        array(

                                                            'user_test' => 0,

                                                            'send_user' => 1,

                                                            'shortcode_user' => 1,

                                                            'shortcode_sent' => 1,

                                                            'list_shortcode_user' => serialize($list_shortcode_user),

                                                            'first_reminder_sent' => 0,

                                                            'second_reminder_sent' => 1,

                                                            'third_reminder_sent' => 0,

                                                            'list_user' => $arr_list_user,

                                                            'day' => $day,

                                                            'month' => $month,

                                                            'year' => $year,

                                                            'date_statis' => $ahachat_date_statistical

                                                        ), array('%d', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s'));

                                                } else {

                                                    $wpdb->insert($ahachat_table_statistical,

                                                        array(

                                                            'user_test' => $user_test,

                                                            'send_user' => $send_user,

                                                            'first_reminder_sent' => 0,

                                                            'second_reminder_sent' => 1,

                                                            'third_reminder_sent' => 0,

                                                            'list_user' => $arr_list_user,

                                                            'day' => $day,

                                                            'month' => $month,

                                                            'year' => $year,

                                                            'date_statis' => $ahachat_date_statistical

                                                        ), array('%d', '%d', '%s', '%d', '%d', '%d', '%s'));

                                                }

                                            //

                                            else:

                                                $array_list_user = unserialize($list_statistical->list_user);

                                                $user_test = $list_statistical->user_test;

                                                $send_user = $list_statistical->send_user + 1;

                                                $second_reminder_sent = $list_statistical->second_reminder_sent + 1;

                                                //

                                                $shortcode_user = $list_statistical->shortcode_user;

                                                $shortcode_sent = $list_statistical->shortcode_sent;

                                                $list_shortcode_user = $list_statistical->list_shortcode_user;

                                                if ($is_shortcode == 1) {

                                                    $shortcode_sent = $shortcode_sent + 1;

                                                    $arr__list_shortcode_user = unserialize($list_shortcode_user);

                                                    if (!in_array($recipient, $arr__list_shortcode_user)) {

                                                        $shortcode_user = $shortcode_user + 1;

                                                        array_push($arr__list_shortcode_user, $recipient);

                                                    }

                                                    $list_shortcode_user = serialize($arr__list_shortcode_user);

                                                }

                                                $arr_recipient_id = array();

                                                foreach ($array_list_user as $key => $value) {

                                                    array_push($arr_recipient_id, $value["recipient_id"]);

                                                    $list_user_test[] = array(

                                                        'recipient_id' => $value["recipient_id"],

                                                        'page_id' => $value["page_id"]

                                                    );

                                                }

                                                if (!in_array($recipient, $arr_recipient_id)) {

                                                    $list_user_test[] = array(

                                                        'recipient_id' => $recipient,

                                                        'page_id' => $page_id,

                                                    );

                                                    $user_test = $user_test + 1;

                                                }

                                                $arr_list_user = serialize($list_user_test);

                                                $wpdb->update(

                                                    $ahachat_table_statistical,

                                                    array(

                                                        'user_test' => $user_test,

                                                        'send_user' => $send_user,

                                                        'shortcode_user' => $shortcode_user,

                                                        'shortcode_sent' => $shortcode_sent,

                                                        'list_shortcode_user' => $list_shortcode_user,

                                                        'second_reminder_sent' => $second_reminder_sent,

                                                        'list_user' => $arr_list_user

                                                    ),

                                                    array('day' => $day, 'month' => $month, 'year' => $year),

                                                    array('%d', '%d', '%d', '%d', '%s', '%d', '%s'),

                                                    array('%d', '%d', '%d')

                                                );

                                            endif;


                                            $post_type_user_add_cart = new AHA_POST_TYPE_USER_ADD_TO_CART();

                                            $agrs = array(

                                                'recipient_id' => $recipient,

                                                'page_id' => $page_id

                                            );

                                            if (!$post_type_user_add_cart->CheckUserExit($agrs)) {

                                                $avatar = "-";

                                                $first_name = "-";

                                                $last_name = "-";

                                                $gender = "-";

                                                $language = "-";

                                                foreach ($array_customer_cart as $key => $list) {

                                                    $product_quantity[] = array(

                                                        'product_id' => $list["product_id"],

                                                        'quantity' => $list["quantity"]

                                                    );

                                                }

                                                $product_quantity_js = serialize($product_quantity);

                                                $arr = array(

                                                    'mess' => "",

                                                    'post_date' => current_time('Y-m-d H:i:s'),

                                                    'post_type' => AHACHAT_USER_ADD_TO_CART_POST_TYPE,

                                                    'post_status' => 'publish',

                                                    'ahachat_avatar_user' => $avatar,

                                                    'ahachat_first_name_user' => $first_name,

                                                    'ahachat_last_name_user' => $last_name,

                                                    'ahachat_gender_user' => $gender,

                                                    'ahachat_locale_user' => $language,

                                                    'ahachat_recipient_id_user' => $recipient,

                                                    'ahachat_user_ref' => $user_ref,

                                                    'ahachat_page_id' => $page_id,

                                                    'ahachat_product_user' => $product_quantity_js,

                                                    'ahachat_type' => 'Add-to-cart'

                                                );

                                                $User_Add_Cart = $post_type_user_add_cart->Insert($arr);

                                            } else {

                                                $pid = $post_type_user_add_cart->Aha_Post_ID($agrs);

                                                foreach ($array_customer_cart as $key => $list) {

                                                    $product_quantity[] = array(

                                                        'product_id' => $list["product_id"],

                                                        'quantity' => $list["quantity"]

                                                    );

                                                }

                                                $product_quantity_js = serialize($product_quantity);

                                                update_post_meta($pid, 'ahachat_product_user', $product_quantity_js);

                                                $ahachat_type = "Add-to-cart";

                                                $list_type = get_post_meta($pid, 'ahachat_type', true);

                                                $array_type = explode(",", $list_type);

                                                if (!in_array($ahachat_type, $array_type)) {

                                                    array_push($array_type, $ahachat_type);

                                                }

                                                $ahachat_type = implode(",", $array_type);

                                                update_post_meta($pid, 'ahachat_type', $ahachat_type);

                                            }

                                        } else {

                                            $wpdb->delete($table_user_ref, array('user_ref' => $user_ref), array('%s'));

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

    }

    add_action(AHACHAT_THIRD_REMINDER_CRON_ACTION_NAME, 'ahachat_do_magic_schedule_third_reminder');

    function ahachat_do_magic_schedule_third_reminder()
    {

        $fb_api = new AHACHAT_WP_API();

        $page_id = get_option('ahachat_page_id');

        global $wpdb;

        $table_user_ref = AHACHAT_USER_REF_TABLE;

        $list_user_ref = $wpdb->get_results("SELECT * FROM $table_user_ref");

        $ahachat_first_reminder = get_option('ahachat_first_reminder');

        $ahachat_second_reminder = get_option('ahachat_second_reminder');

        $ahachat_third_reminder = get_option('ahachat_third_reminder');

        $time_now = date('Y-m-d H:i:s', time());

        //statistical

        $ahachat_table_statistical = AHACHAT_ANALYTICS_TABLE;

        $ahachat_date_statistical = date('Y-m-d', time());

        $day = date('d', time());

        $month = date('m', time());

        $year = date('Y', time());

        //statistical

        if (!empty($page_id)) {

            if (isset($ahachat_third_reminder['on_of'])) {

                $page_access_token = $fb_api->AHA_Get_Access_Token_Page();

                if (isset($ahachat_third_reminder['hour']) && !empty($ahachat_third_reminder['hour'])) {

                    $first_hour = (isset($ahachat_first_reminder['hour']) && !empty($ahachat_first_reminder['hour'])) ? (double)$ahachat_first_reminder['hour'] : 0;

                    $second_hour = (isset($ahachat_second_reminder['hour']) && !empty($ahachat_second_reminder['hour'])) ? (double)$ahachat_second_reminder['hour'] : 0;

                    $third_hour = (double)$ahachat_third_reminder['hour'];

                    $third_minder = $first_hour + $second_hour + $third_hour;

                    if (count($list_user_ref) > 0) {

                        if (gettype($page_access_token) == "array") {

                            $page_token = $page_access_token["access_token"];

                            //		if(empty($ahachat_second_reminder['something'])) $ahachat_second_reminder['something']="Your cart is about to expire!";

                            $content_message = $ahachat_third_reminder['something'];

                            $url_button_generic = esc_url(wc_get_checkout_url());

                            if (!isset($ahachat_third_reminder['checkout']) && empty($ahachat_third_reminder['checkout']))

                                $title_button_generic = "Checkout Now";

                            else

                                $title_button_generic = $ahachat_third_reminder['checkout'];

                            foreach ($list_user_ref as $key => $user) {

                                $user_ref = $user->user_ref;

                                $row_user_ref = $wpdb->get_row("SELECT * FROM $table_user_ref WHERE user_ref='" . $user_ref . "' ");

                                $create_time = $user->create_time;

                                if (ahachat_hour_reminder($create_time, $time_now) >= $third_minder) {

                                    if ($row_user_ref->status_send_third == 0 && $row_user_ref->status_send_first == 1) {

                                        $is_shortcode = $row_user_ref->is_shortcode;

                                        $array_customer_cart = unserialize($row_user_ref->your_cart);

                                        if (count($array_customer_cart) > 0 || !empty($array_customer_cart)) {

                                            $recipient = $fb_api->AHA_Send_message_text_user_ref($page_id, $page_token, $content_message, $user_ref);

                                            $recipient = substr($recipient, 0, 18);

                                            $product_id_list = array();

                                            $quantity_list = array();

                                            foreach ($array_customer_cart as $key => $list) {

                                                array_push($product_id_list, $list["product_id"]);

                                                array_push($quantity_list, $list["quantity"]);

                                            }

                                            $url_proudct_id_quantiy = implode(",", $product_id_list) . "&quantity=" . implode(",", $quantity_list);

                                            $url_button_generic = esc_url_raw(add_query_arg('multi-product-add', $url_proudct_id_quantiy, wc_get_checkout_url()));

                                            $url_button_generic = $url_button_generic . '&reminder=third&user=' . $user_ref;

                                            $url_button_generic = str_replace("http://", "https://", $url_button_generic);

                                            //when click check out

                                            $array_product2 = array();

                                            foreach ($array_customer_cart as $key => $list) {

                                                $title = html_entity_decode(get_the_title($list["product_id"]));

                                                $price = get_post_meta($list["product_id"], '_regular_price', true);

                                                $price_sale = get_post_meta($list["product_id"], '_sale_price', true);

                                                if (!empty($price_sale))

                                                    $subtitle_before = html_entity_decode(get_option('ahachat_currency')) . $price . ' - ' . html_entity_decode(get_option('ahachat_currency')) . $price_sale;

                                                else

                                                    $subtitle_before = html_entity_decode(get_option('ahachat_currency')) . $price;

                                                $subtitle = html_entity_decode($subtitle_before . " - " . get_the_title($list["product_id"]));

                                                $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($list["product_id"]), 'full', false)[0];

                                                $image_url = str_replace("http://", "https://", $image_url);

                                                $button_view_url_product = get_the_permalink($list["product_id"]);

                                                $button_view_url_product = str_replace("http://", "https://", $button_view_url_product);

                                                $array_product2[] = array(

                                                    "title" => $title . ' X ' . $list["quantity"],

                                                    "subtitle" => $subtitle,

                                                    "image_url" => $image_url,

                                                    "default_action" => array("type" => "web_url", "url" => $button_view_url_product, "messenger_extensions" => true, "webview_height_ratio" => "tall", "fallback_url" => $button_view_url_product),

                                                    "buttons" => array(array("type" => "web_url", "url" => $url_button_generic, "title" => $title_button_generic))

                                                );

                                            }

                                            $fb_api->AHA_Send_message_one_product_user_ref($page_id, $page_token, $user_ref, $array_product2);

                                            $wpdb->delete($table_user_ref, array('user_ref' => $user_ref), array('%s'));

                                            $list_statistical = $wpdb->get_row("SELECT * FROM $ahachat_table_statistical WHERE day='" . $day . "' AND month='" . $month . "' AND year='" . $year . "' ");

                                            if (empty($list_statistical) || $list_statistical == NULL || $list_statistical == "")://

                                                $user_test = 1;

                                                $send_user = 1;

                                                $list_user_test[] = array(

                                                    'recipient_id' => $recipient,

                                                    'page_id' => $page_id

                                                );

                                                $arr_list_user = serialize($list_user_test);

                                                if ($is_shortcode == 1) {

                                                    $list_shortcode_user = array($recipient);

                                                    $wpdb->insert($ahachat_table_statistical,

                                                        array(

                                                            'user_test' => 0,

                                                            'send_user' => 1,

                                                            'shortcode_user' => 1,

                                                            'shortcode_sent' => 1,

                                                            'list_shortcode_user' => serialize($list_shortcode_user),

                                                            'first_reminder_sent' => 0,

                                                            'second_reminder_sent' => 0,

                                                            'third_reminder_sent' => 1,

                                                            'list_user' => $arr_list_user,

                                                            'day' => $day,

                                                            'month' => $month,

                                                            'year' => $year,

                                                            'date_statis' => $ahachat_date_statistical

                                                        ), array('%d', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s', '%d', '%d', '%d', '%s'));

                                                } else {

                                                    $wpdb->insert($ahachat_table_statistical,

                                                        array(

                                                            'user_test' => $user_test,

                                                            'send_user' => $send_user,

                                                            'first_reminder_sent' => 0,

                                                            'second_reminder_sent' => 0,

                                                            'third_reminder_sent' => 1,

                                                            'list_user' => $arr_list_user,

                                                            'day' => $day,

                                                            'month' => $month,

                                                            'year' => $year,

                                                            'date_statis' => $ahachat_date_statistical

                                                        ), array('%d', '%d', '%s', '%d', '%d', '%d', '%s'));

                                                }


                                            else:

                                                $array_list_user = unserialize($list_statistical->list_user);

                                                $user_test = $list_statistical->user_test;

                                                $send_user = $list_statistical->send_user + 1;

                                                $third_reminder_sent = $list_statistical->third_reminder_sent + 1;

                                                //

                                                $shortcode_user = $list_statistical->shortcode_user;

                                                $shortcode_sent = $list_statistical->shortcode_sent;

                                                $list_shortcode_user = $list_statistical->list_shortcode_user;

                                                if ($is_shortcode == 1) {

                                                    $shortcode_sent = $shortcode_sent + 1;

                                                    $arr__list_shortcode_user = unserialize($list_shortcode_user);

                                                    if (!in_array($recipient, $arr__list_shortcode_user)) {

                                                        $shortcode_user = $shortcode_user + 1;

                                                        array_push($arr__list_shortcode_user, $recipient);

                                                    }

                                                    $list_shortcode_user = serialize($arr__list_shortcode_user);

                                                }

                                                $arr_recipient_id = array();

                                                foreach ($array_list_user as $key => $value) {

                                                    array_push($arr_recipient_id, $value["recipient_id"]);

                                                    $list_user_test[] = array(

                                                        'recipient_id' => $value["recipient_id"],

                                                        'page_id' => $value["page_id"]

                                                    );

                                                }

                                                if (!in_array($recipient, $arr_recipient_id)) {

                                                    $list_user_test[] = array(

                                                        'recipient_id' => $recipient,

                                                        'page_id' => $page_id,

                                                    );

                                                    $user_test = $user_test + 1;

                                                }

                                                $arr_list_user = serialize($list_user_test);

                                                $wpdb->update(

                                                    $ahachat_table_statistical,

                                                    array(

                                                        'user_test' => $user_test,

                                                        'send_user' => $send_user,

                                                        'shortcode_user' => $shortcode_user,

                                                        'shortcode_sent' => $shortcode_sent,

                                                        'list_shortcode_user' => $list_shortcode_user,

                                                        'third_reminder_sent' => $third_reminder_sent,

                                                        'list_user' => $arr_list_user

                                                    ),

                                                    array('day' => $day, 'month' => $month, 'year' => $year),

                                                    array('%d', '%d', '%d', '%d', '%s', '%d', '%s'),

                                                    array('%d', '%d', '%d')

                                                );

                                            endif;

                                            $post_type_user_add_cart = new AHA_POST_TYPE_USER_ADD_TO_CART();

                                            $agrs = array(

                                                'recipient_id' => $recipient,

                                                'page_id' => $page_id

                                            );

                                            if (!$post_type_user_add_cart->CheckUserExit($agrs)) {

                                                $avatar = "-";

                                                $first_name = "-";

                                                $last_name = "-";

                                                $gender = "-";

                                                $language = "-";

                                                foreach ($array_customer_cart as $key => $list) {

                                                    $product_quantity[] = array(

                                                        'product_id' => $list["product_id"],

                                                        'quantity' => $list["quantity"]

                                                    );

                                                }

                                                $product_quantity_js = serialize($product_quantity);

                                                $arr = array(

                                                    'mess' => "",

                                                    'post_date' => current_time('Y-m-d H:i:s'),

                                                    'post_type' => AHACHAT_USER_ADD_TO_CART_POST_TYPE,

                                                    'post_status' => 'publish',

                                                    'ahachat_avatar_user' => $avatar,

                                                    'ahachat_first_name_user' => $first_name,

                                                    'ahachat_last_name_user' => $last_name,

                                                    'ahachat_gender_user' => $gender,

                                                    'ahachat_locale_user' => $language,

                                                    'ahachat_recipient_id_user' => $recipient,

                                                    'ahachat_user_ref' => $user_ref,

                                                    'ahachat_page_id' => $page_id,

                                                    'ahachat_product_user' => $product_quantity_js,

                                                    'ahachat_type' => 'Add-to-cart'

                                                );

                                                $User_Add_Cart = $post_type_user_add_cart->Insert($arr);

                                            } else {

                                                $pid = $post_type_user_add_cart->Aha_Post_ID($agrs);

                                                foreach ($array_customer_cart as $key => $list) {

                                                    $product_quantity[] = array(

                                                        'product_id' => $list["product_id"],

                                                        'quantity' => $list["quantity"]

                                                    );

                                                }

                                                $product_quantity_js = serialize($product_quantity);

                                                update_post_meta($pid, 'ahachat_product_user', $product_quantity_js);

                                                $ahachat_type = "Add-to-cart";

                                                $list_type = get_post_meta($pid, 'ahachat_type', true);

                                                $array_type = explode(",", $list_type);

                                                if (!in_array($ahachat_type, $array_type)) {

                                                    array_push($array_type, $ahachat_type);

                                                }

                                                $ahachat_type = implode(",", $array_type);

                                                update_post_meta($pid, 'ahachat_type', $ahachat_type);

                                            }

                                        } else {

                                            $wpdb->delete($table_user_ref, array('user_ref' => $user_ref), array('%s'));

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

    }

endif;

