PluginProbe
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster / trunk
SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster vtrunk
2.6.0 trunk 1.1.0 1.1.4 1.2.1 1.2.2 1.2.3 1.2.5 1.2.9 1.3.1 1.3.2 1.5.0 1.5.1 1.5.4 1.5.7 1.5.8 1.5.9 1.6.2 1.6.5 1.6.8 1.7.2 1.7.4 1.7.5 1.7.9 1.8.1 All 42 releases
sellkit / includes / admin / settings / integration / facebook-integration.php

facebook-integration.php in SellKit – Funnel builder and checkout optimizer for WooCommerce to sell more, faster trunk, at includes/admin/settings/integration/facebook-integration.php

271 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Sellkit\Admin\Settings\Integration;
4
5 defined( 'ABSPATH' ) || die();
6
7 /**
8 * Class Facebook Pixel integration.
9 *
10 * @package Sellkit\Admin\Settings\Integration\Settings_Integration
11 * @since 1.1.0
12 */
13 class Facebook extends Settings_Integration {
14 /**
15 * The class instance.
16 *
17 * @var Object Class instance.
18 * @since 1.1.0
19 */
20 public static $instance = null;
21
22 /**
23 * Class Instance.
24 *
25 * @since 1.1.0
26 * @return Sellkit_Funnel|null
27 */
28 public static function get_instance() {
29 if ( null === self::$instance ) {
30 self::$instance = new self();
31 }
32
33 return self::$instance;
34 }
35
36 /**
37 * Class constructor.
38 *
39 * @since 1.1.0
40 */
41 public function __construct() {
42 parent::__construct();
43
44 $this->analytics();
45 }
46
47 /**
48 * Get Facebook analyticts scripts.
49 *
50 * @since 1.1.0
51 */
52 public function analytics() {
53 $fb_tracking_id = esc_attr( sellkit_get_option( 'facebook_pixel_id' ) );
54
55 if ( empty( $fb_tracking_id ) || empty( sellkit_get_option( 'facebook_pixel' ) ) ) {
56 return;
57 }
58
59 // phpcs:disable
60 $facebook_script =
61 "<!-- Facebook Pixel Script By Sellkit -->
62 <script type='text/javascript'>
63 !function(f,b,e,v,n,t,s)
64 {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
65 n.callMethod.apply(n,arguments):n.queue.push(arguments)};
66 if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
67 n.queue=[];t=b.createElement(e);t.async=!0;
68 t.src=v;s=b.getElementsByTagName(e)[0];
69 s.parentNode.insertBefore(t,s)}(window, document,'script',
70 'https://connect.facebook.net/en_US/fbevents.js');
71 </script>
72
73 <noscript>
74 <img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id=" . esc_js( $fb_tracking_id ) . "&ev=PageView&noscript=1'/>
75 </noscript>
76
77 <script type='text/javascript'>
78 fbq('init', " . esc_js( $fb_tracking_id ) . ");
79 fbq('track', 'PageView', {'plugin': 'Sellkit'});
80 </script>
81 <!-- Facebook Pixel Script By Sellkit -->
82 ";
83 // phpcs:enable
84
85 $facebook_script .= $this->facebook_pixel_events( self::$post, $fb_tracking_id );
86
87 echo $facebook_script; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Inline pixel script; tracking IDs escaped with esc_js.
88 }
89
90 /**
91 * Facebook pixel events scripts.
92 *
93 * @since 1.1.0
94 * @param array $post_meta sellkit pages data.
95 * @param string $tracking_id fb pixel tracking id.
96 */
97 private function facebook_pixel_events( $post_meta, $tracking_id ) {
98 $fb_pixel_events = sellkit_get_option( 'facebook_pixel_events' );
99
100 if ( empty( $fb_pixel_events ) ) {
101 return;
102 }
103
104 $first_index = array_key_first( $post_meta );
105
106 $sellkit_page_meta = $post_meta[ $first_index ]['type']['key'];
107 $fb_events_script = '';
108
109 foreach ( $fb_pixel_events as $event ) {
110 $function_name = "fb_pixel_{$event}";
111
112 $fb_events_script .= $this->$function_name( $sellkit_page_meta, $tracking_id );
113 }
114
115 return $fb_events_script;
116 }
117
118 /**
119 * Get facebook pixel initiate_checkout data.
120 *
121 * @since 1.1.0
122 * @param string $sellkit_page_meta sellkit pages type.
123 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
124 */
125 private function fb_pixel_initiate_checkout( $sellkit_page_meta ) {
126 if ( 'checkout' !== $sellkit_page_meta ) {
127 return;
128 }
129
130 $cart_content = $this->get_cart_data( 'add_to_cart' );
131 $initiate_checkout = $this->get_cart_data( 'initiate_checkout' );
132
133 $fb_events_script =
134 "<script type='text/javascript'>
135 fbq( 'track', 'AddToCart', $cart_content );
136 fbq( 'track', 'InitiateCheckout', $initiate_checkout );
137 </script>";
138
139 return $fb_events_script;
140 }
141
142 /**
143 * Get facebook pixel add_payment_info data.
144 *
145 * @since 1.1.0
146 * @param string $sellkit_page_meta sellkit pages type.
147 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
148 */
149 private function fb_pixel_add_payment_info( $sellkit_page_meta ) {
150 if ( 'checkout' !== $sellkit_page_meta ) {
151 return;
152 }
153
154 $cart_content = $this->get_cart_data( 'add_payment_info' );
155
156 self::$localized_data['fbAddPaymentInfo'] = $cart_content;
157 }
158
159 /**
160 * Get facebook pixel purchage_complete data.
161 *
162 * @since 1.1.0
163 * @param string $sellkit_page_meta sellkit pages type.
164 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
165 */
166 private function fb_pixel_purchace_complete( $sellkit_page_meta ) {
167 if ( 'thankyou' !== $sellkit_page_meta ) {
168 return;
169 }
170
171 $order_content = $this->get_order_data();
172
173 $fb_events_script =
174 "<script type='text/javascript'>
175 fbq( 'track', 'Purchase', $order_content );
176 </script>";
177
178 return $fb_events_script;
179 }
180
181 /**
182 * Get cart data.
183 *
184 * @since 1.1.0
185 * @param string $event Filter fb pixel tracking id.
186 */
187 private function get_cart_data( $event ) {
188 $cart_data = [];
189 $products = [];
190
191 if ( ! sellkit()->has_valid_dependencies() ) {
192 return wp_json_encode( [] );
193 }
194
195 $cart_total = WC()->cart->cart_contents_total + WC()->cart->tax_total;
196
197 $products = $this->get_products_data( WC()->cart->get_cart(), 'fb' );
198 $product_name = isset( $products['products_name'] ) ? substr( $products['products_name'], 2 ) : '';
199 $category_name = isset( $products['categories_name'] ) ? substr( $products['categories_name'], 2 ) : '';
200 $content_name = isset( $products['cart_contents'] ) ? wp_json_encode( $products['cart_contents'] ) : '';
201
202 $cart_data = [
203 'content_type' => 'product',
204 'plugin' => 'Sellkit',
205 'value' => number_format( floatval( $cart_total ), wc_get_price_decimals(), '.', '' ),
206 'content_name' => $product_name,
207 'content_category' => $category_name,
208 'contents' => $content_name,
209 'currency' => get_woocommerce_currency(),
210 'user_roles' => implode( ', ', wp_get_current_user()->roles ),
211 ];
212
213 if ( 'add_to_cart' === $event ) {
214 $cart_items_count = WC()->cart->get_cart_contents_count();
215
216 $cart_data = array_merge(
217 $cart_data,
218 [
219 'num_items' => $cart_items_count,
220 'domain' => get_site_url(),
221 'language' => get_bloginfo( 'language' ),
222 'userAgent' => wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), //phpcs:ignore
223 ]
224 );
225
226 return wp_json_encode( $cart_data );
227 }
228
229 return wp_json_encode( $cart_data );
230 }
231
232 /**
233 * Get purchase data.
234 *
235 * @since 1.1.0
236 */
237 private function get_order_data() {
238 if ( empty( self::$order_key ) ) {
239 return;
240 }
241
242 $order_id = wc_get_order_id_by_order_key( self::$order_key );
243 $order = wc_get_order( $order_id );
244 $products = $this->get_products_data( $order->get_items(), 'fb' );
245
246 $purchase_data = [
247 'transaction_id' => $order_id,
248 'content_type' => 'product',
249 'currency' => $order->get_currency(),
250 'userAgent' => wp_unslash( $_SERVER['HTTP_USER_AGENT'] ), //phpcs:ignore
251 'plugin' => 'Sellkit',
252 'value' => number_format( $order->get_total(), wc_get_price_decimals(), '.', '' ),
253 ];
254
255 if ( ! empty( $products ) ) {
256 $purchase_data = array_merge(
257 $purchase_data,
258 [
259 'content_ids' => $products['content_ids'],
260 'content_names' => $products['products_name'],
261 'content_category' => $products['categories_name'],
262 ]
263 );
264 }
265
266 return wp_json_encode( $purchase_data );
267 }
268 }
269
270 Facebook::get_instance();
271