| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package GoAffPro |
| 4 |
* @version 2.7.12 |
| 5 |
* @copyright Goaffpro |
| 6 |
* @licence GPL-2.0 |
| 7 |
*/ |
| 8 |
/* |
| 9 |
Plugin Name: Goaffpro Affiliate Marketing |
| 10 |
Plugin URI: https://goaffpro.com/#merchants |
| 11 |
Description: This plugin connects your goaffpro account to your store. Log in to your <a target="_blank" href="https://goaffpro.com">goaffpro account</a> to add this site to your profile |
| 12 |
Author: Goaffpro |
| 13 |
Version: 2.7.12 |
| 14 |
Author URI: https://goaffpro.com/ |
| 15 |
*/ |
| 16 |
|
| 17 |
$goaffpro_plugin_version = "2.7.10"; |
| 18 |
$goaffpro_plugin_version_code = 34; |
| 19 |
|
| 20 |
$goaffpro_token_key = 'goaffpro_public_token'; |
| 21 |
if(is_multisite()){ |
| 22 |
$goaffpro_token_key .= "_".get_current_blog_id(); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Register activation hook and save a 12 character public token in database |
| 28 |
* */ |
| 29 |
register_activation_hook( __FILE__, 'goaffpro_on_activated' ); |
| 30 |
function goaffpro_on_activated(){ |
| 31 |
global $goaffpro_token_key; |
| 32 |
$token = wp_generate_password(12, false); |
| 33 |
add_option($goaffpro_token_key, $token); |
| 34 |
return $token; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Helper function to return the generated public token |
| 39 |
*/ |
| 40 |
function get_goaffpro_public_token(){ |
| 41 |
global $goaffpro_token_key; |
| 42 |
$token = get_option($goaffpro_token_key); |
| 43 |
/** |
| 44 |
* on multi-sites, plugin activation hook is not fired on individual sites |
| 45 |
* so create the option at load time |
| 46 |
*/ |
| 47 |
if($token) { |
| 48 |
return $token; |
| 49 |
}else{ |
| 50 |
return goaffpro_on_activated(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Add goaffpro client sdk to the website's footer |
| 58 |
*/ |
| 59 |
add_action('wp_footer', 'goaffpro_client_footer'); |
| 60 |
|
| 61 |
function goaffpro_client_footer(){ |
| 62 |
$javascript_url = 'https://api.goaffpro.com/loader.js?shop='.get_goaffpro_public_token(); |
| 63 |
wp_enqueue_script("goaffpro_ref_tracking", $javascript_url); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Add goaffpro client sdk `thank you` script in woocommerce thank you page |
| 68 |
*/ |
| 69 |
add_action('woocommerce_thankyou', 'goaffpro_checkout_footer', 10, 1); |
| 70 |
|
| 71 |
function goaffpro_checkout_footer($order_id){ |
| 72 |
wp_enqueue_script( 'goaffpro_order_callback' ); |
| 73 |
try{ |
| 74 |
// retrieve the order representation for order_id |
| 75 |
$order = goaffpro_get_transformed_order($order_id); |
| 76 |
}catch(Exception $e){ |
| 77 |
$order = array('id'=>$order_id); |
| 78 |
} |
| 79 |
// The script must be added to the footer to ensure that the order data is present before the call is made |
| 80 |
$javascript_url = 'https://api.goaffpro.com/loader.js?shop='.get_goaffpro_public_token(); |
| 81 |
wp_enqueue_script("goaffpro_ref_tracking", $javascript_url); |
| 82 |
wp_add_inline_script("goaffpro_ref_tracking", "window.goaffpro_order = ".json_encode($order),'before'); |
| 83 |
$checkout_widget_url = 'https://api.goaffpro.com/checkout_widget.js?shop='.get_goaffpro_public_token().'&woo_order_id='.$order_id; |
| 84 |
wp_enqueue_script("goaffpro_postcheckout_popup", $checkout_widget_url, array(), false, true); |
| 85 |
try{ |
| 86 |
$attr = array( |
| 87 |
'publicToken' => get_goaffpro_public_token(), |
| 88 |
'order_id'=>$order_id |
| 89 |
); |
| 90 |
wp_remote_post("https://api.goaffpro.com/woocommerce/internal_hook", $attr); |
| 91 |
}catch(Exception $e){ |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Add goaffpro referral cookie in woocommerce order metadata if referral cookie is available. |
| 99 |
* This helps in tagging the orders received with the unique ID of the affiliate who sent the customer |
| 100 |
*/ |
| 101 |
add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) { |
| 102 |
if(!isset($_COOKIE['ref'])) { |
| 103 |
return; |
| 104 |
} |
| 105 |
$referral_code = sanitize_text_field($_COOKIE['ref']); |
| 106 |
if($referral_code){ |
| 107 |
update_post_meta($order_id, 'ref', $referral_code); |
| 108 |
} |
| 109 |
$visit_id = sanitize_text_field($_COOKIE['gfp_v_id']); |
| 110 |
if($visit_id){ |
| 111 |
update_post_meta($order_id,'gfp_v_id', $visit_id); |
| 112 |
} |
| 113 |
} , 10, 2); |
| 114 |
|
| 115 |
/** |
| 116 |
* Adds a goaffpro endpoint to the REST API to verify the plugin integration with the goaffpro service |
| 117 |
*/ |
| 118 |
add_action( 'rest_api_init', 'add_goaffpro_api_route'); |
| 119 |
|
| 120 |
function add_goaffpro_api_route(){ |
| 121 |
register_rest_route( 'goaffpro', '/config', array( |
| 122 |
'methods' => 'GET', |
| 123 |
'callback' => 'get_goaffpro_config', |
| 124 |
'permission_callback' => '__return_true' |
| 125 |
)); |
| 126 |
register_rest_route('goaffpro','/public_token', array( |
| 127 |
'methods' => 'GET', |
| 128 |
'callback' => 'get_goaffpro_public_token', |
| 129 |
'permission_callback'=>'__return_true' |
| 130 |
)); |
| 131 |
/* |
| 132 |
register_rest_route('goaffpro','/test', array( |
| 133 |
'methods' => 'GET', |
| 134 |
'callback' => 'get_wc_order', |
| 135 |
'permission_callback'=>'__return_true' |
| 136 |
)); |
| 137 |
*/ |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns a goaffpro_order representation of the woocommerce_order object |
| 142 |
*/ |
| 143 |
function goaffpro_get_transformed_order($order_id){ |
| 144 |
$order = wc_get_order($order_id); |
| 145 |
$line_items = array(); |
| 146 |
foreach ($order->get_items() as $item_id => $item ) { |
| 147 |
$price = $order->get_item_subtotal($item); |
| 148 |
$quantity = $item->get_quantity(); |
| 149 |
$total = $order->get_line_total($item); |
| 150 |
$tax = $order->get_line_tax($item); |
| 151 |
$subtotal = $order->get_line_subtotal($item); |
| 152 |
$discount = $subtotal - $total + $tax; |
| 153 |
$data = $item->get_data(); |
| 154 |
array_push($line_items, array( |
| 155 |
'id'=>$item_id, |
| 156 |
'name'=>$item->get_name(), |
| 157 |
'quantity'=>$quantity, |
| 158 |
'discount'=>$discount, |
| 159 |
'total'=>$total, |
| 160 |
'tax'=>$tax, |
| 161 |
'price'=>$price, |
| 162 |
'product_id'=>$data['product_id'], |
| 163 |
'variation_id'=>$data['variation_id'], |
| 164 |
// 'data'=>$data, |
| 165 |
)); |
| 166 |
} |
| 167 |
return array( |
| 168 |
'id'=>$order->get_id(), |
| 169 |
'number'=>$order->get_order_number(), |
| 170 |
// 'data'=>$order->get_data(), |
| 171 |
'total'=>$order->get_total(), |
| 172 |
'subtotal'=>$order->get_subtotal(), |
| 173 |
'discount'=> $order->get_discount_total(), |
| 174 |
'tax'=>$order->get_total_tax(), |
| 175 |
'shipping'=> $order->get_shipping_total(), |
| 176 |
'currency'=> $order->get_currency(), |
| 177 |
'customer'=>$order->get_address(), |
| 178 |
'coupons'=>$order->get_coupon_codes(), |
| 179 |
'order_status_url'=>$order->get_checkout_order_received_url(), |
| 180 |
'status'=> $order->get_status(), |
| 181 |
'line_items'=>$line_items, |
| 182 |
); |
| 183 |
} |
| 184 |
/* |
| 185 |
function get_wc_order(){ |
| 186 |
return goaffpro_get_transformed_order('66'); |
| 187 |
} |
| 188 |
*/ |
| 189 |
|
| 190 |
function get_goaffpro_config(){ |
| 191 |
global $goaffpro_plugin_version; |
| 192 |
global $goaffpro_plugin_version_code; |
| 193 |
$data = array( |
| 194 |
'goaffpro_public_token'=> get_goaffpro_public_token(), |
| 195 |
'store_name'=> get_option('blogname'), |
| 196 |
'plugin_version' => $goaffpro_plugin_version, |
| 197 |
'plugin_version_code' => $goaffpro_plugin_version_code, |
| 198 |
); |
| 199 |
return $data; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
function goaffpro_apply_discount_to_cart() { |
| 205 |
try{ |
| 206 |
if(isset($_COOKIE['discount_code'])){ |
| 207 |
$coupon_code = sanitize_text_field($_COOKIE['discount_code']); |
| 208 |
} |
| 209 |
if(empty($coupon_code)){ |
| 210 |
$coupon_code = WC()->session->get( 'coupon_code'); |
| 211 |
} |
| 212 |
if ( ! empty( $coupon_code ) && ! WC()->cart->has_discount( $coupon_code ) ){ |
| 213 |
WC()->cart->add_discount( $coupon_code ); // apply the coupon discount |
| 214 |
WC()->session->__unset( 'coupon_code' ); // remove coupon code from session |
| 215 |
} |
| 216 |
}catch(Exception $e){ |
| 217 |
|
| 218 |
} |
| 219 |
} |
| 220 |
add_action( 'woocommerce_before_cart_table', 'goaffpro_apply_discount_to_cart'); |
| 221 |
|
| 222 |
function goaffpro_get_custom_coupon_code_to_session() { |
| 223 |
try{ |
| 224 |
// retrieve coupon code from URL |
| 225 |
if( isset( $_GET[ 'coupon_code' ] ) ) { |
| 226 |
// Ensure that customer session is started |
| 227 |
if( !WC()->session->has_session() ) |
| 228 |
WC()->session->set_customer_session_cookie(true); |
| 229 |
// Check and register coupon code in a custom session variable |
| 230 |
$coupon_code = WC()->session->get( 'coupon_code' ); |
| 231 |
if( empty( $coupon_code ) && isset( $_GET[ 'coupon_code' ] ) ) { |
| 232 |
$coupon_code = esc_attr( $_GET[ 'coupon_code' ] ); |
| 233 |
WC()->session->set( 'coupon_code', $coupon_code ); // Set the coupon code in session |
| 234 |
} |
| 235 |
} |
| 236 |
}catch(Exception $e){ |
| 237 |
|
| 238 |
} |
| 239 |
} |
| 240 |
add_action( 'init', 'goaffpro_get_custom_coupon_code_to_session' ); |
| 241 |
|
| 242 |
function goaffpro_get_product_variation_attributes($variation_id){ |
| 243 |
$variation = new WC_Product_Variation($variation_id); |
| 244 |
$attributes = array(); |
| 245 |
foreach ( $variation->get_variation_attributes() as $attribute_name => $attribute ) { |
| 246 |
$attributes[wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $variation )] = $attribute; |
| 247 |
} |
| 248 |
return $attributes; |
| 249 |
} |
| 250 |
/* |
| 251 |
This function adds support for query parameters to build a checkout dynamically |
| 252 |
Supported parameters |
| 253 |
gfp_checkout = product_id.quantity.variation_id|product_id.quantity.variation_id |
| 254 |
coupon_code = test |
| 255 |
gfp_new_cart = 1 |
| 256 |
*/ |
| 257 |
function goaffpro_set_checkout_from_url() { |
| 258 |
if(!isset( $_GET['gfp_checkout'] ) ) { |
| 259 |
return; |
| 260 |
} |
| 261 |
$cart_type = $_GET['gfp_new_cart']; |
| 262 |
if(isset($cart_type)){ |
| 263 |
WC()->cart->empty_cart(); |
| 264 |
} |
| 265 |
$products = $_GET['gfp_checkout']; |
| 266 |
$parts = explode("|",$products); |
| 267 |
foreach($parts as $product){ |
| 268 |
$x = explode(".", trim($product)); |
| 269 |
$product_id = trim($x[0]); |
| 270 |
$quantity = isset($x[1]) ? trim($x[1]) : 1; |
| 271 |
$variation_id = isset($x[2]) ? trim($x[2]) : null; |
| 272 |
$variation_attributes = isset($variation_id) ? goaffpro_get_product_variation_attributes($variation_id) : null; |
| 273 |
if(!empty($product_id)){ |
| 274 |
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation_attributes ); |
| 275 |
} |
| 276 |
} |
| 277 |
$coupon = $_GET['coupon_code']; |
| 278 |
if(isset($coupon)){ |
| 279 |
WC()->cart-> apply_coupon( $coupon ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
add_action( 'template_redirect', 'goaffpro_set_checkout_from_url'); |
| 284 |
|
| 285 |
|
| 286 |
add_filter('woocommerce_rest_prepare_product_object', 'goaffpro_add_variation_data_to_rest_api', 20, 3); |
| 287 |
add_filter('woocommerce_rest_prepare_product_variation_object', 'goaffpro_add_variation_data_to_rest_api', 20, 3); |
| 288 |
|
| 289 |
function goaffpro_add_variation_data_to_rest_api($response, $object, $request) { |
| 290 |
$variations = isset($response->data['variations']) ? $response->data['variations'] : null; |
| 291 |
$variations_res = array(); |
| 292 |
$variations_array = array(); |
| 293 |
if (!empty($variations) && is_array($variations)) { |
| 294 |
foreach ($variations as $variation) { |
| 295 |
$variation_id = $variation; |
| 296 |
$variation = new WC_Product_Variation($variation_id); |
| 297 |
$variations_res['id'] = $variation_id; |
| 298 |
$variations_res['on_sale'] = $variation->is_on_sale(); |
| 299 |
$variations_res['regular_price'] = (float)$variation->get_regular_price(); |
| 300 |
$variations_res['sale_price'] = (float)$variation->get_sale_price(); |
| 301 |
$variations_res['sku'] = $variation->get_sku(); |
| 302 |
$variations_res['quantity'] = $variation->get_stock_quantity(); |
| 303 |
if ($variations_res['quantity'] == null) { |
| 304 |
$variations_res['quantity'] = ''; |
| 305 |
} |
| 306 |
$variations_res['stock'] = $variation->get_stock_quantity(); |
| 307 |
|
| 308 |
$attributes = array(); |
| 309 |
// variation attributes |
| 310 |
foreach ( $variation->get_variation_attributes() as $attribute_name => $attribute ) { |
| 311 |
// taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_` |
| 312 |
$attributes[] = array( |
| 313 |
'name' => wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $variation ), |
| 314 |
'slug' => str_replace( 'attribute_', '', wc_attribute_taxonomy_slug( $attribute_name ) ), |
| 315 |
'option' => $attribute, |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
$variations_res['attributes'] = $attributes; |
| 320 |
$variations_array[] = $variations_res; |
| 321 |
} |
| 322 |
} |
| 323 |
$response->data['product_variations'] = $variations_array; |
| 324 |
|
| 325 |
return $response; |
| 326 |
} |
| 327 |
|
| 328 |
?> |
| 329 |
|