WC_Rejoiner

Rejoiner Integration.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-rejoiner.php  
  1. class WC_Rejoiner extends WC_Integration { 
  2.  
  3. public function __construct() { 
  4.  
  5. session_start(); 
  6. $this->sess = session_id(); 
  7.  
  8. $this->id = 'wc_rejoiner'; 
  9. $this->method_title = __( 'Rejoiner', 'woocommerce' ); 
  10. $this->method_description = __( 'Find these details on the Implementation page inside your Rejoiner dashboard.', 'woocommerce' ); 
  11.  
  12. // Load the settings. 
  13. $this->init_form_fields(); 
  14. $this->init_settings(); 
  15.  
  16. // Define user set variables 
  17. $this->rejoiner_id = $this->get_option( 'rejoiner_id' ); 
  18. $this->rejoiner_domain_name = $this->get_option( 'rejoiner_domain_name' ); 
  19. $this->rejoiner_api_key = $this->get_option( 'rejoiner_api_key' ); 
  20. $this->rejoiner_api_secret = $this->get_option( 'rejoiner_api_secret' ); 
  21.  
  22. // Actions 
  23. add_action( 'woocommerce_update_options_integration_wc_rejoiner', array( $this, 'process_admin_options') ); 
  24. add_action( 'wp_loaded', array( $this, 'refill_cart' ) ); 
  25.  
  26. // Tracking code 
  27. add_action( 'wp_footer', array( $this, 'rejoiner_tracking_code' ) ); 
  28.  
  29. // REST conversion 
  30. add_action( 'woocommerce_payment_complete', array( $this, 'rejoiner_rest_convert' ), 1, 1 ); 
  31.  
  32. // JS conversion 
  33. add_action( 'woocommerce_thankyou', array( $this, 'rejoiner_conversion_code' ), 2, 1 ); 
  34.  
  35. // AJAX callback 
  36. add_action( 'wp_ajax_rejoiner_sync', array( $this, 'rejoiner_sync' ) ); 
  37.  
  38.  
  39. function init_form_fields() { 
  40.  
  41. $this->form_fields = array( 
  42.  
  43. 'rejoiner_id' => array( 
  44. 'title' => __( 'Rejoiner Account', 'woocommerce' ),  
  45. 'description' => __( 'Enter your unique Site ID', 'woocommerce' ),  
  46. 'type' => 'text',  
  47. 'default' => '' 
  48. ),  
  49. 'rejoiner_domain_name' => array( 
  50. 'title' => __( 'Set Domain Name', 'woocommerce' ),  
  51. 'description' => __( 'Enter your domain for the tracking code. Example: .domain.com or .www.domain.com, be sure to include the leading period.', 'woocommerce' ),  
  52. 'type' => 'text',  
  53. 'default' => '' 
  54. ),  
  55. 'rejoiner_api_key' => array( 
  56. 'title' => __( 'Rejoiner API Key', 'woocommerce' ),  
  57. 'description' => __( 'Enter your API Key', 'woocommerce' ),  
  58. 'type' => 'text',  
  59. 'default' => '' 
  60. ),  
  61. 'rejoiner_api_secret' => array( 
  62. 'title' => __( 'Rejoiner API Secret', 'woocommerce' ),  
  63. 'description' => __( 'Enter your API Secret', 'woocommerce' ),  
  64. 'type' => 'text',  
  65. 'default' => '' 
  66.  
  67. ); 
  68.  
  69. }  
  70.  
  71. function rejoiner_tracking_code() { 
  72.  
  73. global $rjconverted; 
  74.  
  75. $current_user = wp_get_current_user(); 
  76.  
  77. if( $current_user instanceof WP_User && !empty( $current_user->user_email ) ) 
  78. $current_user_email = $current_user->user_email; 
  79. else 
  80. $current_user_email = false; 
  81.  
  82. if( ( is_cart() || is_checkout() ) && $rjconverted != true ) { 
  83.  
  84. global $woocommerce; 
  85.  
  86. $items = array(); 
  87. $savecart = array(); 
  88.  
  89. foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { 
  90.  
  91. $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); 
  92.  
  93. if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) { 
  94.  
  95. if( $_product->variation_id > 0 && $_thumb_id = get_post_thumbnail_id( $_product->variation_id ) ) { 
  96.  
  97. $thumb_id = $_thumb_id; 
  98.  
  99. } else { 
  100.  
  101. $thumb_id = get_post_thumbnail_id( $_product->post->ID ); 
  102.  
  103.  
  104. $thumb_size = apply_filters( 'wc_rejoiner_thumb_size', 'shop_thumbnail' ); 
  105.  
  106. $thumb_url = wp_get_attachment_image_src( $thumb_id, $thumb_size, true ); 
  107.  
  108. $product_cats = get_the_terms( $_product->post->ID, 'product_cat'); 
  109.  
  110. if( is_array( $product_cats ) ) { 
  111.  
  112. foreach( $product_cats as $cat ) { 
  113.  
  114. $cats[] = $cat->slug; 
  115.  
  116.  
  117. $product_cats_json = json_encode( $cats ); 
  118.  
  119. } else { 
  120.  
  121. $product_cats_json = null; 
  122.  
  123. }  
  124.  
  125. if( !empty($thumb_url[0]) ) { 
  126.  
  127. $image = $thumb_url[0]; 
  128.  
  129. } else { 
  130.  
  131. $image = wc_placeholder_img( 'shop_thumbnail' ); 
  132.  
  133.  
  134. if( $_product->variation_id > 0 ) {  
  135.  
  136. $variantname = ''; 
  137.  
  138. foreach ( $cart_item['variation'] as $name => $value ) { 
  139.  
  140. if ( '' === $value ) 
  141. continue; 
  142.  
  143. $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) ); 
  144.  
  145. if ( taxonomy_exists( $taxonomy ) ) { 
  146. $term = get_term_by( 'slug', $value, $taxonomy ); 
  147. if ( ! is_wp_error( $term ) && $term && $term->name ) { 
  148. $value = $term->name; 
  149. $label = wc_attribute_label( $taxonomy ); 
  150.  
  151. } else { 
  152. $value = apply_filters( 'woocommerce_variation_option_name', $value ); 
  153. $product_attributes = $cart_item['data']->get_attributes(); 
  154. if ( isset( $product_attributes[ str_replace( 'attribute_', '', $name ) ] ) ) { 
  155. $label = wc_attribute_label( $product_attributes[ str_replace( 'attribute_', '', $name ) ]['name'] ); 
  156. } else { 
  157. $label = $name; 
  158.  
  159. $variantname.= ', ' . $label . ': ' . $value; 
  160. $item_data[$name] = $value; 
  161.  
  162.  
  163. $variant = apply_filters( 'wc_rejoiner_cart_item_variant', $variantname ); 
  164. $item_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . $variant;  
  165.  
  166. $_item = array( 
  167. 'product_id' => $_product->post->ID,  
  168. 'name' => $this->escape_for_json( apply_filters( 'wc_rejoiner_cart_item_name', $item_name ) ),  
  169. 'item_qty' => $cart_item['quantity'],  
  170. 'price' => $this->format_money( $_product->get_price() ),  
  171. 'qty_price' => $this->format_money( $cart_item['line_total'] ),  
  172. 'image_url' => $this->format_image_url( $image ),  
  173. 'product_url' => get_permalink( $_product->post->ID ),  
  174. 'category' => $product_cats_json,  
  175. 'variation_id' => $_product->variation_id  
  176. ); 
  177.  
  178. $attributes = apply_filters( 'wc_rejoiner_cart_item_attributes', null, $item_data ); 
  179.  
  180. if( is_array( $attributes ) ) { 
  181. $_item['attribute_name'] = $attributes['attribute_name']; 
  182. $_item['attribute_value'] = $this->escape_for_json( $attributes['attribute_value'] ); 
  183.  
  184. $items[] = $_item; 
  185.  
  186. $savecart[] = array( 
  187. 'product_id' => $_product->post->ID,  
  188. 'item_qty' => $cart_item['quantity'],  
  189. 'variation_data' => $item_data,  
  190. 'variation_id' => $_product->variation_id 
  191. ); 
  192.  
  193. } else { 
  194.  
  195. $items[] = array( 
  196. 'product_id' => $_product->post->ID,  
  197. 'name' => $this->escape_for_json( apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) ),  
  198. 'item_qty' => $cart_item['quantity'],  
  199. 'price' => $this->format_money( $_product->get_price() ),  
  200. 'qty_price' => $this->format_money( $cart_item['line_total'] ),  
  201. 'image_url' => $this->format_image_url( $image ),  
  202. 'product_url' => get_permalink( $_product->post->ID ),  
  203. 'category' => $product_cats_json  
  204. ); 
  205.  
  206. $savecart[] = array( 
  207. 'product_id' => $_product->post->ID,  
  208. 'item_qty' => $cart_item['quantity'] 
  209. ); 
  210.  
  211.  
  212.  
  213.  
  214. set_transient( 'rjcart_' . $this->sess, $savecart, 168 * HOUR_IN_SECONDS); 
  215.  
  216. $cartdata = array( 
  217. 'cart_value' => $this->format_money( $woocommerce->cart->total ),  
  218. 'cart_item_count' => $woocommerce->cart->cart_contents_count,  
  219. ); 
  220.  
  221. $js = $this->build_rejoiner_push( $items, $cartdata, $current_user_email ); 
  222.  
  223. } elseif( $rjconverted != true ) { 
  224.  
  225. $rejoiner_id = $this->rejoiner_id; 
  226. $rejoiner_domain_name = $this->rejoiner_domain_name; 
  227.  
  228. if( !empty( $rejoiner_id ) && !empty( $rejoiner_domain_name ) ) { 
  229.  
  230. $js = <<<EOF 
  231. <!-- Rejoiner Tracking - added by WooCommerceRejoiner --> 
  232.  
  233. <script type='text/javascript'> 
  234. var _rejoiner = _rejoiner || []; 
  235. _rejoiner.push(['setAccount', '{$rejoiner_id}']); 
  236. _rejoiner.push(['setDomain', '{$rejoiner_domain_name}']); 
  237.  
  238. (function() { 
  239. var s = document.createElement('script'); s.type = 'text/javascript'; 
  240. s.async = true; 
  241. s.src = 'https://cdn.rejoiner.com/js/v4/rejoiner.lib.js'; 
  242. var x = document.getElementsByTagName('script')[0]; 
  243. x.parentNode.insertBefore(s, x); 
  244. })(); 
  245.  
  246. EOF; 
  247.  
  248. if( $current_user_email != false ) { 
  249. $js.= "_rejoiner.push(['setCustomerEmail', { 'email' : '$current_user_email' } ]);"; 
  250.  
  251. if( is_product() ) { 
  252.  
  253. $_product = wc_get_product( get_the_ID() ); 
  254. $product_id = $_product->post->ID; 
  255.  
  256. $name = $this->escape_for_json( apply_filters( 'wc_rejoiner_cart_item_name', $_product->post->post_title ) ); 
  257. $product_url = get_permalink( $product_id ); 
  258.  
  259. $thumb_id = get_post_thumbnail_id( $product_id ); 
  260. $thumb_size = apply_filters( 'wc_rejoiner_thumb_size', 'shop_thumbnail' ); 
  261. $thumb_url = wp_get_attachment_image_src( $thumb_id, $thumb_size, true ); 
  262. $thumb_url = $thumb_url[0]; 
  263.  
  264. $product_cats = get_the_terms( $product_id, 'product_cat'); 
  265. $product_cats_json = ''; 
  266.  
  267. if( is_array( $product_cats ) ) { 
  268.  
  269. foreach( $product_cats as $cat ) { 
  270.  
  271. $cats[] = $cat->slug; 
  272.  
  273.  
  274. $product_cats_json = "'category':" . json_encode( $cats ) .', '; 
  275.  
  276.  
  277. $price = $this->format_money( $_product->get_price() ); 
  278.  
  279. $js.= " 
  280. _rejoiner.push(['trackProductView', { 
  281. 'product_id': '$product_id',  
  282. 'name': '$name',  
  283. $product_cats_json 
  284. 'price': $price,  
  285. 'product_url': '$product_url',  
  286. 'image_url': '$thumb_url' 
  287. }]);  
  288. "; 
  289.  
  290.  
  291.  
  292. $js.= '</script> 
  293. <!-- End Rejoiner Tracking -->'; 
  294.  
  295.  
  296. if( isset( $js ) ) 
  297. echo $js; 
  298.  
  299.  
  300. function rejoiner_sync() { 
  301.  
  302. global $woocommerce; 
  303.  
  304. $cart = array( 
  305. 'cart_value' => $this->format_money( $woocommerce->cart->total ),  
  306. 'cart_item_count' => $woocommerce->cart->cart_contents_count,  
  307. ); 
  308.  
  309. $returnUrl = $woocommerce->cart->get_cart_url() . '?rjcart=' . $this->sess; 
  310. $cart['return_url'] = apply_filters( 'rejoiner_returnurl', $returnUrl, $this->sess, $cart ); 
  311.  
  312. wp_send_json( $cart );  
  313.  
  314.  
  315. function format_money( $number ) { 
  316.  
  317. $incents = $number * 100; 
  318. $incents = intval( $incents ); 
  319. return $incents; 
  320.  
  321.  
  322. function format_description( $text ) { 
  323.  
  324. $text = str_replace( "'", "\'", strip_tags( $text ) ); 
  325. $text = str_replace( array("\r", "\n"), "", $text ); 
  326.  
  327. return $text; 
  328.  
  329.  
  330. function format_image_url( $url ) { 
  331.  
  332. if( stripos( $url, 'http' ) === false ) { 
  333.  
  334. $url = get_site_url() . $url; 
  335.  
  336.  
  337. return $url; 
  338.  
  339.  
  340. function escape_for_json( $str ) { 
  341.  
  342. return str_ireplace( "'", "\'", $str ); 
  343.  
  344.  
  345. function build_rejoiner_push( $items, $cart, $current_user_email ) { 
  346.  
  347. global $woocommerce, $rjremoved; 
  348.  
  349. $rejoiner_id = $this->rejoiner_id; 
  350. $rejoiner_domain_name = $this->rejoiner_domain_name; 
  351. $ajaxurl = admin_url( 'admin-ajax.php' ); 
  352.  
  353. $returnUrl = $woocommerce->cart->get_cart_url() . '?rjcart=' . $this->sess; 
  354.  
  355. $cart['return_url'] = apply_filters( 'rejoiner_returnurl', $returnUrl, $this->sess, $cart ); 
  356.  
  357. $cartdata = $this->rejoiner_encode( $cart ); 
  358. $cartjs = "_rejoiner.push(['setCartData', $cartdata]);"; 
  359. $itemjs = ''; 
  360. $emailjs = ''; 
  361.  
  362. if( $current_user_email != false ) 
  363. $emailjs = "_rejoiner.push(['setCustomerEmail', { 'email' : '$current_user_email' } ]);"; 
  364.  
  365. foreach( $items as $item ) { 
  366.  
  367. $data = $this->rejoiner_encode( $item ); 
  368. $itemjs.= "_rejoiner.push(['setCartItem', $data]);\r\n"; 
  369.  
  370.  
  371. if( !empty( $rejoiner_id ) && !empty( $rejoiner_domain_name ) ) { 
  372.  
  373. $js = <<<EOF 
  374. <!-- Rejoiner Tracking - added by WooCommerceRejoiner --> 
  375.  
  376. <script type='text/javascript'> 
  377. var _rejoiner = _rejoiner || []; 
  378. _rejoiner.push(['setAccount', '{$rejoiner_id}']); 
  379. _rejoiner.push(['setDomain', '{$rejoiner_domain_name}']); 
  380.  
  381. (function() { 
  382. var s = document.createElement('script'); s.type = 'text/javascript'; 
  383. s.async = true; 
  384. s.src = 'https://cdn.rejoiner.com/js/v4/rejoiner.lib.js'; 
  385. var x = document.getElementsByTagName('script')[0]; 
  386. x.parentNode.insertBefore(s, x); 
  387. })(); 
  388.  
  389. $cartjs 
  390. $itemjs 
  391. $emailjs 
  392.  
  393. (function ($, r) { 
  394. var Rejoiner = { 
  395. removeInProgress: false,  
  396. init: function() { 
  397. console.log('initialized Rejoiner'); 
  398. $(document).ready(function() { 
  399. $( document ).on( 
  400. 'click',  
  401. 'td.product-remove > a',  
  402. Rejoiner.beginItemRemove  
  403. ); 
  404. $( document ).on( 
  405. 'added_to_cart updated_wc_div updated_shipping_method updated_cart_totals',  
  406. Rejoiner.requestUpdates 
  407. ); 
  408. }); 
  409. },  
  410. beginItemRemove: function(e) { 
  411. Rejoiner.removeInProgress = $(this).data('product_id'); 
  412. },  
  413. requestUpdates: function(e) { 
  414. if (Rejoiner.removeInProgress !== false) { 
  415. console.log('removing item '+Rejoiner.removeInProgress+' from cart'); 
  416. r.push(['removeCartItem', {product_id: Rejoiner.removeInProgress}]); 
  417. Rejoiner.removeInProgress = false; 
  418. console.log('requesting update setCartData'); 
  419. $.post( 
  420. '$ajaxurl',  
  421. {action: 'rejoiner_sync'},  
  422. Rejoiner.updateCartData 
  423. );  
  424. }  
  425. },  
  426. updateCartData: function(data) { 
  427. r.push(['setCartData', data]); 
  428. console.log( 'updated cart data with:'); 
  429. console.log( data ); 
  430. }; 
  431.  
  432. Rejoiner.init(); 
  433.  
  434. })(jQuery, _rejoiner); 
  435.  
  436. </script> 
  437. <!-- End Rejoiner Tracking --> 
  438. EOF; 
  439.  
  440. } else { 
  441.  
  442. $js = "\r\n<!-- WooCommerce Rejoiner ERROR: You must enter your details on the integrations settings tab. -->\r\n";  
  443.  
  444.  
  445. return $js;  
  446.  
  447.  
  448. function rejoiner_encode( $array ) { 
  449.  
  450. $json = '{'; 
  451.  
  452. foreach( $array as $key => $val ) { 
  453.  
  454. if( is_array( json_decode( $val, true ) ) )  
  455. $items[]= "'$key' : $val"; 
  456. else 
  457. $items[]= "'$key' : '$val'";  
  458.  
  459.  
  460. $json.= implode( ', ', $items ) . '}'; 
  461.  
  462. return $json;  
  463.  
  464.  
  465. function rejoiner_conversion_code( $order_id ) { 
  466.  
  467. global $rjconverted; 
  468.  
  469. $rjconverted = true; 
  470.  
  471. $rejoiner_id = $this->rejoiner_id; 
  472. $rejoiner_domain_name = $this->rejoiner_domain_name; 
  473.  
  474. if ( !$rejoiner_id ) { 
  475. return; 
  476.  
  477. $order = wc_get_order( $order_id ); 
  478.  
  479. $total = $this->format_money( $order->get_total() ); 
  480. $item_count = $order->get_item_count(); 
  481.  
  482. $promos = $order->get_used_coupons(); 
  483. if( is_array( $promos ) && ( count( $promos ) > 1 ) ) 
  484. $promo = implode( ', ', $promos ); 
  485. elseif( is_array( $promos ) ) 
  486. $promo = array_pop( $promos ); 
  487.  
  488. if( isset( $promo ) ) 
  489. $promojs = "'promo': '{$promo}', "; 
  490. else  
  491. $promojs = ''; 
  492.  
  493. $returnurl = $order->get_view_order_url(); 
  494.  
  495. $line_items = $order->get_items(); 
  496. $items = array(); 
  497.  
  498. foreach ( $line_items as $item ) { 
  499.  
  500. $_product = $order->get_product_from_item( $item ); 
  501.  
  502. $qty = $item['qty']; 
  503.  
  504. $linetotal = $order->get_line_total( $item, true, true ); 
  505.  
  506. $thumb_id = get_post_thumbnail_id( $_product->post->ID ); 
  507.  
  508. $thumb_size = apply_filters( 'wc_rejoiner_thumb_size', 'shop_thumbnail' ); 
  509.  
  510. $thumb_url = wp_get_attachment_image_src( $thumb_id, $thumb_size, true );  
  511.  
  512. if( !empty($thumb_url[0]) ) { 
  513.  
  514. $image = $thumb_url[0]; 
  515.  
  516. } else { 
  517.  
  518. $image = wc_placeholder_img( 'shop_thumbnail' ); 
  519.  
  520.  
  521. $product_cats = get_the_terms( $_product->post->ID, 'product_cat'); 
  522.  
  523. if( is_array( $product_cats ) ) { 
  524.  
  525. foreach( $product_cats as $cat ) { 
  526.  
  527. $cats[] = $cat->slug; 
  528.  
  529.  
  530. $product_cats_json = json_encode( $cats ); 
  531.  
  532. } else { 
  533.  
  534. $product_cats_json = null; 
  535.  
  536.  
  537. $_item = array( 
  538. 'product_id' => $_product->post->ID,  
  539. 'name' => $this->escape_for_json( apply_filters( 'woocommerce_cart_item_name', $_product->get_title() ) ),  
  540. 'item_qty' => $qty,  
  541. 'price' => $this->format_money( $_product->get_price() ),  
  542. 'qty_price' => $this->format_money( $linetotal ),  
  543. 'image_url' => $this->format_image_url( $image ),  
  544. 'product_url' => get_permalink( $_product->post->ID ),  
  545. 'category' => $product_cats_json  
  546. ); 
  547.  
  548. $items[] = $this->rejoiner_encode( $_item ); 
  549.  
  550.  
  551. $itemsjs = implode( ', ', $items );  
  552.  
  553. $js = <<<EOF 
  554. <!-- Rejoiner JavaScript API Conversion - added by WooCommerce Rejoiner --> 
  555.  
  556. <script type='text/javascript'> 
  557. var _rejoiner = _rejoiner || []; 
  558. _rejoiner.push(['setAccount', '{$rejoiner_id}']); 
  559. _rejoiner.push(['setDomain', '{$rejoiner_domain_name}']); 
  560. _rejoiner.push(['sendConversion', { 
  561. cart_data: { 
  562. 'cart_value': {$total},  
  563. 'cart_item_count': {$item_count},  
  564. 'customer_order_number': '{$order_id}',  
  565. {$promojs} 
  566. 'return_url': '{$returnurl}' 
  567. },  
  568. cart_items: [ 
  569. $itemsjs  
  570. ]} 
  571. ]); 
  572.  
  573. (function() { 
  574. var s = document.createElement('script'); 
  575. s.type = 'text/javascript'; 
  576. s.async = true; 
  577. s.src = 'https://cdn.rejoiner.com/js/v4/rejoiner.lib.js'; 
  578. var x = document.getElementsByTagName('script')[0]; 
  579. x.parentNode.insertBefore(s, x); 
  580. })(); 
  581. </script> 
  582.  
  583. <!-- End Rejoiner JavaScript API Conversion -->  
  584. EOF; 
  585.  
  586. echo $js; 
  587.  
  588.  
  589. function rejoiner_hash( $api_secret, $http_verb, $request_path, $request_body ) { 
  590.  
  591. $content = "{$http_verb}\n{$request_path}\n{$request_body}"; 
  592. $hash = hash_hmac( 'sha1', $content, $api_secret, true ); 
  593. return base64_encode( $hash ); 
  594.  
  595.  
  596. function rejoiner_rest_convert( $order_id ) { 
  597.  
  598. global $rjconverted; 
  599.  
  600. $rejoiner_id = $this->rejoiner_id; 
  601. $rejoiner_domain_name = $this->rejoiner_domain_name; 
  602. $rejoiner_api_key = $this->rejoiner_api_key; 
  603. $rejoiner_api_secret = $this->rejoiner_api_secret; 
  604.  
  605. if( !$rejoiner_id || !$rejoiner_api_key || !$rejoiner_api_secret ) { 
  606. return; 
  607.  
  608. $order = new WC_Order( $order_id ); 
  609. $email = $order->billing_email; 
  610. $body = '{"email": "' . $email . '"}'; 
  611.  
  612. $rejoiner_path = '/api/1.0/site/' . $rejoiner_id . '/lead/convert';  
  613. $hash = $this->rejoiner_hash( $rejoiner_api_secret, 'POST', $rejoiner_path, $body ); 
  614. $auth = 'Rejoiner ' . $rejoiner_api_key . ':' . $hash; 
  615.  
  616. $baseurl = 'https://app.rejoiner.com'; 
  617. $posturl = $baseurl . $rejoiner_path; 
  618.  
  619. $args = array( 
  620. 'body' => $body,  
  621. 'timeout' => '5',  
  622. 'redirection' => '5',  
  623. 'httpversion' => '1.0',  
  624. 'blocking' => true,  
  625. 'headers' => array( 
  626. 'Authorization' => $auth,  
  627. 'Content-Type' => 'application/json' 
  628. ),  
  629. ); 
  630.  
  631. $response = wp_remote_post( $posturl, $args ); 
  632.  
  633. if( !is_wp_error( $response ) ) 
  634. $code = $response['response']['code']; 
  635.  
  636. if ( $code == 200 ) { 
  637.  
  638. $rjconverted = true; 
  639.  
  640. if( WP_DEBUG_LOG ) 
  641. error_log( "Rejoiner REST Conversion Success / HTTP Response: $code" ); 
  642.  
  643. } else { 
  644.  
  645. $rjconverted = false; 
  646.  
  647. if( is_wp_error( $response ) ) { 
  648.  
  649. $error_message = $response->get_error_message(); 
  650.  
  651. if( WP_DEBUG_LOG ) 
  652. error_log( "Rejoiner REST Conversion Error : $error_message" ); 
  653.  
  654. } else { 
  655.  
  656. if( WP_DEBUG_LOG ) 
  657. error_log( "Rejoiner REST Conversion Error / HTTP Response: $code" );  
  658.  
  659. }  
  660.  
  661.  
  662.  
  663.  
  664. function refill_cart() { 
  665.  
  666. if ( isset( $_GET['rjcart'] ) ) { 
  667.  
  668. global $woocommerce; 
  669.  
  670. $this_sess = $_GET['rjcart'];  
  671.  
  672. $carturl = $woocommerce->cart->get_cart_url(); 
  673.  
  674. $rjcart = get_transient( 'rjcart_' . $this_sess ); 
  675.  
  676. if( !empty( $rjcart ) ) { 
  677.  
  678. $woocommerce->cart->empty_cart(); 
  679.  
  680. foreach( $rjcart as $product ) { 
  681.  
  682. if( !empty( $product['variation_id'] ) && $product['variation_id'] > 0 ) { 
  683.  
  684. $woocommerce->cart->add_to_cart(  
  685. $product['product_id'],  
  686. $product['item_qty'],  
  687. $product['variation_id'],  
  688. $product['variation_data'] 
  689. ); 
  690.  
  691. } else { 
  692.  
  693. $woocommerce->cart->add_to_cart( 
  694. $product['product_id'],  
  695. $product['item_qty'] 
  696. );  
  697.  
  698.  
  699.  
  700. $utm_source = ( isset( $_GET['utm_source'] ) ) ? $_GET['utm_source'] : 'rejoiner' ; 
  701. $utm_medium = ( isset( $_GET['utm_medium'] ) ) ? $_GET['utm_medium'] : 'email' ; 
  702. $utm_campaign = ( isset( $_GET['utm_campaign'] ) ) ? $_GET['utm_campaign'] : 'email' ; 
  703. $utm_content = ( isset( $_GET['utm_content'] ) ) ? $_GET['utm_content'] : 'default' ; 
  704.  
  705. header( "location:$carturl?utm_source=$utm_source&utm_medium=$utm_medium&utm_campaign=$utm_campaign&utm_content=$utm_content" );  
  706. exit;  
  707.  
  708. }  
  709.  
  710.  
  711.