Esempio n. 1
0
 /**
  * Action to perform on accept/skip offer
  */
 function so_process_offer_action()
 {
     global $current_user, $sa_smart_offers;
     $so_offer = new SO_Offer();
     $so_offers = new SO_Offers();
     if (isset($_GET['so_action']) && ($_GET['so_action'] == "accept" || $_GET['so_action'] == "skip")) {
         $current_offer_id = $_GET['so_offer_id'];
         $source = !empty($_GET['source']) ? $_GET['source'] : null;
         list($where, $where_url) = $so_offers->get_page_details();
         $page = $where . '_page';
         list($accepted_session_variable, $accepted_ids_in_session) = $so_offers->get_accepted_offer_ids_from_session();
         list($skipped_session_variable, $skipped_ids_in_session) = $so_offers->get_skipped_offer_ids_from_session();
         $skip_offer_id_variable = $where == "any" ? str_replace(array('/', '-', '&', '=', ':'), '', $where_url) . '_skip_offer_id' : $where . '_skip_offer_id';
         list($offer_id_on_skipping, $skipped_offer_id_variable) = $so_offers->get_offer_id_on_skipping($skip_offer_id_variable);
         $parent_offer_id_variable = $where == "any" ? str_replace(array('/', '-', '&', '=', ':'), '', $where_url) . '_parent_offer_id' : $where . '_parent_offer_id';
         $check_parent_offer_id_set_or_not = SO_Session_Handler::check_session_set_or_not($parent_offer_id_variable);
         if (!$check_parent_offer_id_set_or_not) {
             SO_Session_Handler::so_set_session_variables($parent_offer_id_variable, $current_offer_id);
         }
         if ($_GET['so_action'] == "accept") {
             $variation_data = isset($_POST['variation_id']) || isset($_POST['quantity']) ? $_POST : array();
             $parent_offer_id = '';
             if ($offer_id_on_skipping != '') {
                 $check_parent_offer_id = SO_Session_Handler::check_session_set_or_not($parent_offer_id_variable);
                 $parent_offer_id = $check_parent_offer_id ? SO_Session_Handler::so_get_session_value($parent_offer_id_variable) : '';
             }
             SO_Session_Handler::so_delete_session($parent_offer_id_variable);
             SO_Session_Handler::so_delete_session($skip_offer_id_variable);
             SO_Session_Handler::so_set_session_variables('sa_smart_offers_accepted_offer_ids', $current_offer_id);
             // Update stats
             $so_offer->update_accept_skip_count($current_offer_id, 'accepted');
             // validate offer before add to cart.
             $offer_ids = array($current_offer_id);
             $is_valid = $this->is_offer_valid($page, $offer_ids);
             if (!empty($is_valid) && is_array($is_valid)) {
                 // Adds to cart
                 $so_offer->action_on_accept_offer($current_offer_id, $page, $parent_offer_id, $variation_data);
             } else {
                 // display notice
                 $sa_smart_offers->wc_add_notice(__('This is not valid offer for you.', SA_Smart_Offers::$text_domain), 'error');
             }
         } elseif ($_GET['so_action'] == "skip") {
             $so_offer->update_accept_skip_count($current_offer_id, 'skipped');
             // Update if this offer needs to be skipped permanently for this user
             $skip_permanently = get_post_meta($current_offer_id, 'sa_smart_offer_if_denied_skip_permanently', true);
             if (!empty($skip_permanently) && $skip_permanently == true && $current_user->ID != 0) {
                 $customer_skipped_offers = $this->get_skipped_offers($current_offer_id);
                 $customer_skipped_offers = array_unique($customer_skipped_offers);
                 update_user_meta($current_user->ID, 'customer_skipped_offers', $customer_skipped_offers);
             }
             // To store skipped offers in session even if they are updated in DB
             SO_Session_Handler::so_set_session_variables('sa_smart_offers_skipped_offer_ids', $current_offer_id);
             SO_Session_Handler::so_delete_session($skip_offer_id_variable);
             $redirecting_option = get_post_meta($current_offer_id, 'sa_smart_offer_if_denied', true);
             $redirect_to = get_post_meta($current_offer_id, 'url', true);
             if (strpos($where_url, 'so_action=skip')) {
                 $where_url = esc_url(remove_query_arg(array('so_action', 'so_offer_id', 'source'), $where_url));
             }
             ob_clean();
             if (empty($redirecting_option)) {
                 wp_safe_redirect($where_url);
             } elseif (!empty($source)) {
                 if ($page == "checkout_page" && $source == 'so_post_checkout') {
                     $form_values = SO_Session_Handler::check_session_set_or_not('so_checkout_form_data');
                     if ($form_values) {
                         $sa_so_form_checkout = SO_Session_Handler::so_get_session_value('so_checkout_form_data');
                     } else {
                         $sa_so_form_checkout = null;
                     }
                     if (!empty($sa_so_form_checkout)) {
                         $_POST = $sa_so_form_checkout;
                     }
                     if (wc_get_page_id('terms') > 0) {
                         $_POST['terms'] = 'yes';
                     }
                     if ($sa_smart_offers->is_wc_gte_21()) {
                         wc_clear_notices();
                     } else {
                         $sa_smart_offers->global_wc()->clear_messages();
                     }
                     $woocommerce_checkout = $sa_smart_offers->global_wc()->checkout();
                     $woocommerce_checkout->process_checkout();
                 } elseif ($page == "checkout_page" && $source == 'so_pre_checkout') {
                     wp_safe_redirect($where_url);
                 }
             } else {
                 if ($redirecting_option == 'order_page') {
                     wp_safe_redirect($where_url);
                 } elseif ($redirect_to != "") {
                     if ($redirecting_option == "offer_page") {
                         $so_offer->force_show_smart_offers($redirect_to);
                     } elseif ($redirecting_option == "url") {
                         if (!preg_match("~^(?:ht)tps?://~i", $redirect_to)) {
                             $return_url = @$_SERVER["HTTPS"] == "on" ? "https://" : "http://";
                             $return_url = "http://" . $redirect_to;
                         } else {
                             $return_url = $redirect_to;
                         }
                         wp_redirect($return_url);
                     } elseif ($redirecting_option == "particular_page") {
                         wp_safe_redirect(get_permalink($redirect_to));
                     }
                 }
             }
             exit;
         }
     }
 }