PluginProbe
Goaffpro Affiliate Marketing / trunk
Goaffpro Affiliate Marketing vtrunk
trunk 2.7.10
goaffpro / goaffpro.php

goaffpro.php in Goaffpro Affiliate Marketing trunk, at goaffpro.php

335 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.11";
18 $goaffpro_plugin_version_code = 35;
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 * Add goaffpro client sdk to the website's footer
56 */
57 add_action('wp_footer', 'goaffpro_client_footer');
58
59 function goaffpro_client_footer(){
60 $javascript_url = 'https://api.goaffpro.com/loader.js?shop='.get_goaffpro_public_token();
61 wp_enqueue_script("goaffpro_ref_tracking", $javascript_url);
62 }
63
64 /**
65 * Add goaffpro client sdk `thank you` script in woocommerce thank you page
66 */
67 add_action('woocommerce_thankyou', 'goaffpro_checkout_footer', 10, 1);
68
69 function goaffpro_checkout_footer($order_id){
70 try{
71 // retrieve the order representation for order_id
72 $order = goaffpro_get_transformed_order($order_id);
73 }catch(\Throwable $e){
74 $order = array('id'=>$order_id);
75 }
76 // The script must be added to the footer to ensure that the order data is present before the call is made
77 $javascript_url = 'https://api.goaffpro.com/loader.js?shop='.get_goaffpro_public_token();
78 wp_enqueue_script("goaffpro_ref_tracking", $javascript_url);
79 wp_add_inline_script("goaffpro_ref_tracking", "window.goaffpro_order = ".json_encode($order),'before');
80 $checkout_widget_url = 'https://api.goaffpro.com/checkout_widget.js?shop='.get_goaffpro_public_token().'&woo_order_id='.$order_id;
81 wp_enqueue_script("goaffpro_postcheckout_popup", $checkout_widget_url, array(), false, true);
82 try{
83 $attr = array(
84 'publicToken' => get_goaffpro_public_token(),
85 'order_id'=>$order_id
86 );
87 wp_remote_post("https://api.goaffpro.com/woocommerce/internal_hook", array('body' => $attr));
88 }catch(\Throwable $e){
89
90 }
91 }
92
93
94 /**
95 * Add goaffpro referral cookie in woocommerce order metadata if referral cookie is available.
96 * This helps in tagging the orders received with the unique ID of the affiliate who sent the customer
97 */
98 add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {
99 if(!isset($_COOKIE['ref'])) {
100 return;
101 }
102 $order = wc_get_order($order_id);
103 if(!$order){
104 return;
105 }
106 $referral_code = sanitize_text_field($_COOKIE['ref']);
107 if($referral_code){
108 $order->update_meta_data('ref', $referral_code);
109 }
110 $visit_id = isset($_COOKIE['gfp_v_id']) ? sanitize_text_field($_COOKIE['gfp_v_id']) : '';
111 if($visit_id){
112 $order->update_meta_data('gfp_v_id', $visit_id);
113 }
114 $order->save();
115 } , 10, 2);
116
117 /**
118 * Adds a goaffpro endpoint to the REST API to verify the plugin integration with the goaffpro service
119 */
120 add_action( 'rest_api_init', 'add_goaffpro_api_route');
121
122 function add_goaffpro_api_route(){
123 register_rest_route( 'goaffpro', '/config', array(
124 'methods' => 'GET',
125 'callback' => 'get_goaffpro_config',
126 'permission_callback' => '__return_true'
127 ));
128 register_rest_route('goaffpro','/public_token', array(
129 'methods' => 'GET',
130 'callback' => 'get_goaffpro_public_token',
131 'permission_callback'=>'__return_true'
132 ));
133 /*
134 register_rest_route('goaffpro','/test', array(
135 'methods' => 'GET',
136 'callback' => 'get_wc_order',
137 'permission_callback'=>'__return_true'
138 ));
139 */
140 }
141
142 /**
143 * Returns a goaffpro_order representation of the woocommerce_order object
144 */
145 function goaffpro_get_transformed_order($order_id){
146 $order = wc_get_order($order_id);
147 $line_items = array();
148 foreach ($order->get_items() as $item_id => $item ) {
149 $price = $order->get_item_subtotal($item);
150 $quantity = $item->get_quantity();
151 $total = $order->get_line_total($item);
152 $tax = $order->get_line_tax($item);
153 $subtotal = $order->get_line_subtotal($item);
154 $discount = $subtotal - $total + $tax;
155 $data = $item->get_data();
156 array_push($line_items, array(
157 'id'=>$item_id,
158 'name'=>$item->get_name(),
159 'quantity'=>$quantity,
160 'discount'=>$discount,
161 'total'=>$total,
162 'tax'=>$tax,
163 'price'=>$price,
164 'product_id'=>$data['product_id'],
165 'variation_id'=>$data['variation_id'],
166 // 'data'=>$data,
167 ));
168 }
169 return array(
170 'id'=>$order->get_id(),
171 'number'=>$order->get_order_number(),
172 // 'data'=>$order->get_data(),
173 'total'=>$order->get_total(),
174 'subtotal'=>$order->get_subtotal(),
175 'discount'=> $order->get_discount_total(),
176 'tax'=>$order->get_total_tax(),
177 'shipping'=> $order->get_shipping_total(),
178 'currency'=> $order->get_currency(),
179 'customer'=>$order->get_address(),
180 'coupons'=>$order->get_coupon_codes(),
181 'order_status_url'=>$order->get_checkout_order_received_url(),
182 'status'=> $order->get_status(),
183 'line_items'=>$line_items,
184 );
185 }
186 /*
187 function get_wc_order(){
188 return goaffpro_get_transformed_order('66');
189 }
190 */
191
192 function get_goaffpro_config(){
193 global $goaffpro_plugin_version;
194 global $goaffpro_plugin_version_code;
195 $data = array(
196 'goaffpro_public_token'=> get_goaffpro_public_token(),
197 'store_name'=> get_option('blogname'),
198 'plugin_version' => $goaffpro_plugin_version,
199 'plugin_version_code' => $goaffpro_plugin_version_code,
200 );
201 return $data;
202 }
203
204
205
206 function goaffpro_apply_discount_to_cart() {
207 try{
208 $coupon_code = '';
209 $discount_cookie = isset( $_COOKIE['discount_code'] ) ? $_COOKIE['discount_code']
210 : ( isset( $_COOKIE['dcode'] ) ? $_COOKIE['dcode'] : null );
211 if ( $discount_cookie !== null && is_scalar( $discount_cookie ) ) {
212 $coupon_code = wc_format_coupon_code( wp_unslash( $discount_cookie ) );
213 }
214 if ( '' === $coupon_code ) {
215 $session_code = WC()->session->get( 'coupon_code' );
216 $coupon_code = ( $session_code && is_scalar( $session_code ) )
217 ? wc_format_coupon_code( $session_code ) : '';
218 }
219 if ( '' !== $coupon_code && ! WC()->cart->has_discount( $coupon_code ) ) {
220 WC()->cart->add_discount( $coupon_code );
221 WC()->session->__unset( 'coupon_code' );
222 }
223 }catch(\Throwable $e){
224
225 }
226 }
227 add_action( 'woocommerce_before_cart_table', 'goaffpro_apply_discount_to_cart');
228
229 function goaffpro_get_custom_coupon_code_to_session() {
230 try{
231 // retrieve coupon code from URL
232 if ( isset( $_GET['coupon_code'] ) && is_scalar( $_GET['coupon_code'] ) ) {
233 $incoming = wc_format_coupon_code( wp_unslash( $_GET['coupon_code'] ) );
234 if ( '' !== $incoming ) {
235 if ( ! WC()->session->has_session() )
236 WC()->session->set_customer_session_cookie( true );
237 if ( '' === (string) WC()->session->get( 'coupon_code' ) ) {
238 WC()->session->set( 'coupon_code', $incoming );
239 }
240 }
241 }
242 }catch(\Throwable $e){
243
244 }
245 }
246 add_action( 'init', 'goaffpro_get_custom_coupon_code_to_session' );
247
248 function goaffpro_get_product_variation_attributes($variation_id){
249 $variation = new WC_Product_Variation($variation_id);
250 $attributes = array();
251 foreach ( $variation->get_variation_attributes() as $attribute_name => $attribute ) {
252 $attributes[wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $variation )] = $attribute;
253 }
254 return $attributes;
255 }
256 /*
257 This function adds support for query parameters to build a checkout dynamically
258 Supported parameters
259 gfp_checkout = product_id.quantity.variation_id|product_id.quantity.variation_id
260 coupon_code = test
261 gfp_new_cart = 1
262 */
263 function goaffpro_set_checkout_from_url() {
264 if(!isset( $_GET['gfp_checkout'] ) ) {
265 return;
266 }
267 if ( ! empty( $_GET['gfp_new_cart'] ) ) {
268 WC()->cart->empty_cart();
269 }
270 $products = $_GET['gfp_checkout'];
271 $parts = explode("|",$products);
272 foreach($parts as $product){
273 $x = explode(".", trim($product));
274 $product_id = absint($x[0]);
275 $quantity = isset($x[1]) ? absint($x[1]) : 1;
276 $variation_id = isset($x[2]) ? absint($x[2]) : null;
277 $variation_attributes = !empty($variation_id) ? goaffpro_get_product_variation_attributes($variation_id) : null;
278 if(!empty($product_id)){
279 WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation_attributes );
280 }
281 }
282 $coupon = isset( $_GET['coupon_code'] ) && is_scalar( $_GET['coupon_code'] )
283 ? wc_format_coupon_code( wp_unslash( $_GET['coupon_code'] ) ) : '';
284 if ( '' !== $coupon ) {
285 WC()->cart->apply_coupon( $coupon );
286 }
287 }
288
289 add_action( 'template_redirect', 'goaffpro_set_checkout_from_url');
290
291
292 add_filter('woocommerce_rest_prepare_product_object', 'goaffpro_add_variation_data_to_rest_api', 20, 3);
293 add_filter('woocommerce_rest_prepare_product_variation_object', 'goaffpro_add_variation_data_to_rest_api', 20, 3);
294
295 function goaffpro_add_variation_data_to_rest_api($response, $object, $request) {
296 $variations = isset($response->data['variations']) ? $response->data['variations'] : null;
297 $variations_res = array();
298 $variations_array = array();
299 if (!empty($variations) && is_array($variations)) {
300 foreach ($variations as $variation) {
301 $variation_id = $variation;
302 $variation = new WC_Product_Variation($variation_id);
303 $variations_res['id'] = $variation_id;
304 $variations_res['on_sale'] = $variation->is_on_sale();
305 $variations_res['regular_price'] = (float)$variation->get_regular_price();
306 $variations_res['sale_price'] = (float)$variation->get_sale_price();
307 $variations_res['sku'] = $variation->get_sku();
308 $variations_res['quantity'] = $variation->get_stock_quantity();
309 if ($variations_res['quantity'] == null) {
310 $variations_res['quantity'] = '';
311 }
312 $variations_res['stock'] = $variation->get_stock_quantity();
313
314 $attributes = array();
315 // variation attributes
316 foreach ( $variation->get_variation_attributes() as $attribute_name => $attribute ) {
317 // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
318 $attributes[] = array(
319 'name' => wc_attribute_label( str_replace( 'attribute_', '', $attribute_name ), $variation ),
320 'slug' => str_replace( 'attribute_', '', wc_attribute_taxonomy_slug( $attribute_name ) ),
321 'option' => $attribute,
322 );
323 }
324
325 $variations_res['attributes'] = $attributes;
326 $variations_array[] = $variations_res;
327 }
328 }
329 $response->data['product_variations'] = $variations_array;
330
331 return $response;
332 }
333
334 ?>
335