Log in or Register to your Affiracle account to add this site to your profile Version: 1.1.6 Author: Affiracle Technologies */ if ( ! defined( 'ABSPATH' ) ) exit; $affiracle_plugin_version = "1.1.6"; $affiracle_plugin_version_code = 6; $affiracle_token_key = 'affiracle_public_token'; if(is_multisite()){ $affiracle_token_key .= "_".get_current_blog_id(); } register_activation_hook( __FILE__, 'affiracle_on_activated' ); add_action( 'rest_api_init', 'affiracle_add_api_route'); add_filter('woocommerce_get_checkout_order_received_url', 'affiracle_add_order_params_to_return_url', 10, 2); function affiracle_add_api_route(){ register_rest_route( 'affiracle', '/config', array( 'methods' => 'GET', 'callback' => 'affiracle_get_config', 'permission_callback' => '__return_true' )); register_rest_route('affiracle','/public_token', array( 'methods' => 'GET', 'callback' => 'affiracle_get_public_token', 'permission_callback'=>'__return_true' )); } function affiracle_get_config(){ global $affiracle_plugin_version; global $affiracle_plugin_version_code; $main_plugin_file = plugin_basename(__FILE__); $data = array( 'affiracle_public_token'=> affiracle_get_public_token(), 'store_name'=> get_option('blogname'), 'plugin_version' => $affiracle_plugin_version, 'plugin_version_code' => $affiracle_plugin_version_code ); if ( ! function_exists( 'is_plugin_active' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if (is_plugin_active($main_plugin_file)) { $data['plugin_status'] = 'Activated'; } else { $data['plugin_status'] = 'Not Activated'; } return $data; } function affiracle_generateValidShortUUID() { $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $charLength = strlen($characters); $shortUUID = ''; for ($i = 0; $i < 22; $i++) { $randomIndex = mt_rand(0, $charLength - 1); $shortUUID .= $characters[$randomIndex]; } return $shortUUID; } function affiracle_on_activated(){ global $affiracle_token_key; $affiracle_unique_token = affiracle_generateValidShortUUID(); add_option($affiracle_token_key, $affiracle_unique_token); return $affiracle_unique_token; } function affiracle_get_public_token(){ global $affiracle_token_key; $affiracle_unique_token = get_option($affiracle_token_key); if($affiracle_unique_token) { return $affiracle_unique_token; }else{ return affiracle_on_activated(); } } function affiracle_client_footer(){ $public_token = affiracle_get_public_token(); $version = date('dmY'); $javascript_url = 'https://miracle.affiracle.com/' . $public_token . '.js?v=' . $version; wp_enqueue_script("affiracle_ref_tracking", $javascript_url); } add_action('wp_head', 'affiracle_client_footer'); add_action( 'woocommerce_order_status_processing', 'affiracle_set_order_cookie', 10, 2 ); add_action( 'woocommerce_order_status_completed', 'affiracle_set_order_cookie', 10, 2 ); function affiracle_set_order_cookie( $order_id, $order ) { if ( ! is_admin() ) { $order_id = absint( $order_id ); setcookie( 'affiracle__last_order_id', $order_id, time() + DAY_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); $_COOKIE['affiracle__last_order_id'] = $order_id; } } function affiracle_is_order_context() { if ( ! empty( $_GET['key'] ) && strpos( $_GET['key'], 'wc_order_' ) === 0 ) { return true; } if ( ! empty( $_SERVER['REQUEST_URI'] ) && preg_match( '#/order-(?:received|pay)/#', $_SERVER['REQUEST_URI'] ) ) { return true; } if ( function_exists( 'is_order_received_page' ) && is_order_received_page() ) { return true; } if ( function_exists( 'is_checkout' ) && is_checkout() ) { return true; } return false; } function affiracle_add_script_to_thankyou_page($order_id = '') { static $affiracle_done = false; if ( $affiracle_done ) return false; if ( ! function_exists( 'wc_get_order' ) ) return false; if ( ! $order_id && ! affiracle_is_order_context() ) return false; if ( isset($_GET['key']) && substr( $_GET['key'], 0, 9 ) === "wc_order_" ) { $order_id = wc_get_order_id_by_order_key($_GET['key']); } if ( ! empty( $_SERVER['REQUEST_URI'] ) && preg_match( '#/order-(?:received|pay)/(\d+)#', $_SERVER['REQUEST_URI'], $m ) ) { $order_id = absint( $m[1] ); } if ( ! $order_id && ! empty( $_GET['order_id'] ) ) { $order_id = absint( $_GET['order_id'] ); } if ( ! $order_id && function_exists('get_query_var') ) { foreach ( ['order-pay','order-received'] as $endpoint ) { if ( $id = absint( get_query_var( $endpoint ) ) ) { $order_id = $id; break; } } } if ( ! $order_id && ! empty( $_COOKIE['affiracle__last_order_id'] ) ) { $order_id = absint( $_COOKIE['affiracle__last_order_id'] ); } if(empty($order_id)) return false; $order = wc_get_order($order_id); if ( ! $order instanceof WC_Order ) return false; $affiracle_done = true; $order_data = array( 'code' => $order_id, 'total' => $order->get_total(), 'line_items' => array(), ); foreach ($order->get_items() as $item) { $product = $item->get_product(); if ( ! $product ) continue; $line_item = array( 'name' => $item->get_name(), 'quantity' => $item->get_quantity(), 'price' => $item->get_subtotal(), 'sku' => $product->get_sku(), 'product_id' => $product->get_id(), 'tax' => $item->get_total_tax(), 'discount' => $item->get_subtotal() - $item->get_total(), 'categories' => array(), ); $categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'id=>name')); foreach ($categories as $category_id => $category_name) { $category_data = array( 'term_id' => $category_id, 'name' => $category_name, 'parent' => wp_get_term_taxonomy_parent_id($category_id, 'product_cat'), ); $line_item['categories'][] = $category_data; } $order_data['line_items'][] = $line_item; } $affiracle_script = 'window.affiracle_order_details=' . json_encode($order_data) . ';'; affiracle_client_footer(); wp_add_inline_script("affiracle_ref_tracking",$affiracle_script,'before'); try { $webhook_url = 'https://affiracle.com/api/woocommerce/'.affiracle_get_public_token().'/'.$order_id; wp_add_inline_script("affiracle_ref_tracking",$affiracle_script,'after'); if ( ! $order->get_meta( '_affiracle_reported' ) ) { $response = wp_safe_remote_post($webhook_url, array( 'headers' => array('Content-Type' => 'application/json'), )); if ( ! is_wp_error( $response ) ) { $order->update_meta_data( '_affiracle_reported', 1 ); $order->save(); } } } catch (Exception $e) { error_log('Error processing new order: ' . $e->getMessage()); } } add_action('woocommerce_thankyou', 'affiracle_add_script_to_thankyou_page',10,1); add_action('wp_footer', 'affiracle_add_script_to_thankyou_page'); function affiracle_add_order_params_to_return_url( $url, $order ) { return add_query_arg( [ 'order_id' => $order->get_id(), 'order_key' => $order->get_order_key(), ], $url ); }