facebook-for-woocommerce
Last commit date
assets
1 year ago
data
2 years ago
i18n
1 year ago
includes
1 year ago
vendor
1 year ago
LICENSE
7 years ago
changelog.txt
1 year ago
class-wc-facebookcommerce.php
1 year ago
facebook-commerce-events-tracker.php
1 year ago
facebook-commerce-pixel-event.php
2 years ago
facebook-commerce.php
1 year ago
facebook-config-warmer.php
3 years ago
facebook-for-woocommerce.php
1 year ago
readme.txt
1 year ago
facebook-commerce-pixel-event.php
697 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | /** |
| 4 | * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved |
| 5 | * |
| 6 | * This source code is licensed under the license found in the |
| 7 | * LICENSE file in the root directory of this source tree. |
| 8 | * |
| 9 | * @package FacebookCommerce |
| 10 | */ |
| 11 | |
| 12 | use WooCommerce\Facebook\Events\Event; |
| 13 | |
| 14 | class WC_Facebookcommerce_Pixel { |
| 15 | |
| 16 | |
| 17 | const SETTINGS_KEY = 'facebook_config'; |
| 18 | const PIXEL_ID_KEY = 'pixel_id'; |
| 19 | const USE_PII_KEY = 'use_pii'; |
| 20 | const USE_S2S_KEY = 'use_s2s'; |
| 21 | const ACCESS_TOKEN_KEY = 'access_token'; |
| 22 | |
| 23 | /** |
| 24 | * Cache key for pixel script block output. |
| 25 | * |
| 26 | * @var string cache key. |
| 27 | * */ |
| 28 | const PIXEL_RENDER = 'pixel_render'; |
| 29 | |
| 30 | /** |
| 31 | * Cache key for pixel noscript block output. |
| 32 | * |
| 33 | * @var string cache key. |
| 34 | * */ |
| 35 | const NO_SCRIPT_RENDER = 'no_script_render'; |
| 36 | |
| 37 | /** |
| 38 | * Script render memoization helper. |
| 39 | * |
| 40 | * @var array Cache array. |
| 41 | */ |
| 42 | public static $render_cache = []; |
| 43 | |
| 44 | /** |
| 45 | * User information. |
| 46 | * |
| 47 | * @var array Information array. |
| 48 | */ |
| 49 | private $user_info; |
| 50 | |
| 51 | /** |
| 52 | * The name of the last event. |
| 53 | * |
| 54 | * @var string Event name. |
| 55 | */ |
| 56 | private $last_event; |
| 57 | |
| 58 | /** |
| 59 | * Class constructor. |
| 60 | * |
| 61 | * @param array $user_info User information array. |
| 62 | */ |
| 63 | public function __construct( $user_info = [] ) { |
| 64 | $this->user_info = $user_info; |
| 65 | $this->last_event = ''; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Initialize pixelID. |
| 70 | */ |
| 71 | public static function initialize() { |
| 72 | if ( ! is_admin() ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Initialize PixelID in storage - this will only need to happen when the user is an admin. |
| 77 | $pixel_id = self::get_pixel_id(); |
| 78 | if ( ! WC_Facebookcommerce_Utils::is_valid_id( $pixel_id ) && |
| 79 | class_exists( 'WC_Facebookcommerce_WarmConfig' ) ) { |
| 80 | $fb_warm_pixel_id = WC_Facebookcommerce_WarmConfig::$fb_warm_pixel_id; |
| 81 | |
| 82 | if ( WC_Facebookcommerce_Utils::is_valid_id( $fb_warm_pixel_id ) && |
| 83 | (int) $fb_warm_pixel_id == $fb_warm_pixel_id ) { |
| 84 | $fb_warm_pixel_id = (string) $fb_warm_pixel_id; |
| 85 | self::set_pixel_id( $fb_warm_pixel_id ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $is_advanced_matching_enabled = self::get_use_pii_key(); |
| 90 | if ( null == $is_advanced_matching_enabled && |
| 91 | class_exists( 'WC_Facebookcommerce_WarmConfig' ) ) { |
| 92 | $fb_warm_is_advanced_matching_enabled = |
| 93 | WC_Facebookcommerce_WarmConfig::$fb_warm_is_advanced_matching_enabled; |
| 94 | if ( is_bool( $fb_warm_is_advanced_matching_enabled ) ) { |
| 95 | self::set_use_pii_key( $fb_warm_is_advanced_matching_enabled ? 1 : 0 ); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * Gets Facebook Pixel init code. |
| 103 | * |
| 104 | * Init code might contain additional information to help matching website users with facebook users. |
| 105 | * Information is hashed in JS side using SHA256 before sending to Facebook. |
| 106 | * |
| 107 | * @return string |
| 108 | */ |
| 109 | private function get_pixel_init_code() { |
| 110 | |
| 111 | $agent_string = Event::get_platform_identifier(); |
| 112 | |
| 113 | /** |
| 114 | * Filters Facebook Pixel init code. |
| 115 | * |
| 116 | * @param string $js_code |
| 117 | */ |
| 118 | return apply_filters( |
| 119 | 'facebook_woocommerce_pixel_init', |
| 120 | sprintf( |
| 121 | "fbq('init', '%s', %s, %s);\n", |
| 122 | esc_js( self::get_pixel_id() ), |
| 123 | json_encode( $this->user_info, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ), |
| 124 | json_encode( array( 'agent' => $agent_string ), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ) |
| 125 | ) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Gets the Facebook Pixel code scripts. |
| 132 | * |
| 133 | * @return string HTML scripts |
| 134 | */ |
| 135 | public function pixel_base_code() { |
| 136 | |
| 137 | $pixel_id = self::get_pixel_id(); |
| 138 | |
| 139 | // Bail if no ID or already rendered. |
| 140 | if ( empty( $pixel_id ) || ! empty( self::$render_cache[ self::PIXEL_RENDER ] ) ) { |
| 141 | return ''; |
| 142 | } |
| 143 | |
| 144 | self::$render_cache[ self::PIXEL_RENDER ] = true; |
| 145 | |
| 146 | ob_start(); |
| 147 | |
| 148 | ?> |
| 149 | <script <?php echo self::get_script_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.Output ?>> |
| 150 | !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? |
| 151 | n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; |
| 152 | n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; |
| 153 | t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, |
| 154 | document,'script','https://connect.facebook.net/en_US/fbevents.js'); |
| 155 | </script> |
| 156 | <!-- WooCommerce Facebook Integration Begin --> |
| 157 | <script <?php echo self::get_script_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.Output ?>> |
| 158 | |
| 159 | <?php echo $this->get_pixel_init_code(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 160 | |
| 161 | fbq( 'track', 'PageView', <?php echo json_encode( self::build_params( [], 'PageView' ), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ); ?> ); |
| 162 | |
| 163 | document.addEventListener( 'DOMContentLoaded', function() { |
| 164 | // Insert placeholder for events injected when a product is added to the cart through AJAX. |
| 165 | document.body.insertAdjacentHTML( 'beforeend', '<div class=\"wc-facebook-pixel-event-placeholder\"></div>' ); |
| 166 | }, false ); |
| 167 | |
| 168 | </script> |
| 169 | <!-- WooCommerce Facebook Integration End --> |
| 170 | <?php |
| 171 | |
| 172 | return ob_get_clean(); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /** |
| 177 | * Gets Facebook Pixel code noscript part to avoid W3 validation errors. |
| 178 | * |
| 179 | * @return string |
| 180 | */ |
| 181 | public function pixel_base_code_noscript() { |
| 182 | |
| 183 | $pixel_id = self::get_pixel_id(); |
| 184 | |
| 185 | if ( empty( $pixel_id ) || ! empty( self::$render_cache[ self::NO_SCRIPT_RENDER ] ) ) { |
| 186 | return ''; |
| 187 | } |
| 188 | |
| 189 | self::$render_cache[ self::NO_SCRIPT_RENDER ] = true; |
| 190 | |
| 191 | ob_start(); |
| 192 | |
| 193 | ?> |
| 194 | <!-- Facebook Pixel Code --> |
| 195 | <noscript> |
| 196 | <img |
| 197 | height="1" |
| 198 | width="1" |
| 199 | style="display:none" |
| 200 | alt="fbpx" |
| 201 | src="https://www.facebook.com/tr?id=<?php echo esc_attr( $pixel_id ); ?>&ev=PageView&noscript=1" |
| 202 | /> |
| 203 | </noscript> |
| 204 | <!-- End Facebook Pixel Code --> |
| 205 | <?php |
| 206 | |
| 207 | return ob_get_clean(); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /** |
| 212 | * Determines if the last event in the current thread matches a given event. |
| 213 | * |
| 214 | * @since 1.11.0 |
| 215 | * |
| 216 | * @param string $event_name |
| 217 | * @return bool |
| 218 | */ |
| 219 | public function is_last_event( $event_name ) { |
| 220 | |
| 221 | return $event_name === $this->last_event; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Gets the JavaScript code to track an event. |
| 227 | * |
| 228 | * Updates the last event property and returns the code. |
| 229 | * |
| 230 | * Use {@see \WC_Facebookcommerce_Pixel::inject_event()} to print or enqueue the code. |
| 231 | * |
| 232 | * @since 1.10.2 |
| 233 | * |
| 234 | * @param string $event_name The name of the event to track. |
| 235 | * @param array $params Custom event parameters. |
| 236 | * @param string $method Name of the pixel's fbq() function to call. |
| 237 | * @return string |
| 238 | */ |
| 239 | public function get_event_code( $event_name, $params, $method = 'track' ) { |
| 240 | |
| 241 | $this->last_event = $event_name; |
| 242 | |
| 243 | return self::build_event( $event_name, $params, $method ); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Gets the JavaScript code to track an event wrapped in <script> tag. |
| 249 | * |
| 250 | * @see \WC_Facebookcommerce_Pixel::get_event_code() |
| 251 | * |
| 252 | * @since 1.10.2 |
| 253 | * |
| 254 | * @param string $event_name The name of the event to track. |
| 255 | * @param array $params Custom event parameters. |
| 256 | * @param string $method Name of the pixel's fbq() function to call. |
| 257 | * @return string |
| 258 | */ |
| 259 | public function get_event_script( $event_name, $params, $method = 'track' ) { |
| 260 | |
| 261 | ob_start(); |
| 262 | |
| 263 | ?> |
| 264 | <!-- Facebook Pixel Event Code --> |
| 265 | <script <?php echo self::get_script_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.Output ?>> |
| 266 | <?php echo $this->get_event_code( $event_name, $params, $method ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 267 | </script> |
| 268 | <!-- End Facebook Pixel Event Code --> |
| 269 | <?php |
| 270 | |
| 271 | return ob_get_clean(); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * Prints or enqueues the JavaScript code to track an event. |
| 277 | * Preferred method to inject events in a page. |
| 278 | * |
| 279 | * @see \WC_Facebookcommerce_Pixel::build_event() |
| 280 | * |
| 281 | * @param string $event_name The name of the event to track. |
| 282 | * @param array $params Custom event parameters. |
| 283 | * @param string $method Name of the pixel's fbq() function to call. |
| 284 | */ |
| 285 | public function inject_event( $event_name, $params, $method = 'track' ) { |
| 286 | if ( WC_Facebookcommerce_Utils::isWoocommerceIntegration() ) { |
| 287 | $code = $this->get_event_code( $event_name, self::build_params( $params, $event_name ), $method ); |
| 288 | |
| 289 | // If we have add to cart redirect enabled, we must defer the AddToCart events to render them the next page load. |
| 290 | $is_redirect = 'yes' === get_option( 'woocommerce_cart_redirect_after_add', 'no' ); |
| 291 | $is_add_to_cart = 'AddToCart' === $event_name; |
| 292 | if ( $is_redirect && $is_add_to_cart ) { |
| 293 | WC_Facebookcommerce_Utils::add_deferred_event( $code ); |
| 294 | } else { |
| 295 | WC_Facebookcommerce_Utils::wc_enqueue_js( $code ); |
| 296 | } |
| 297 | } else { |
| 298 | printf( $this->get_event_script( $event_name, self::build_params( $params, $event_name ), $method ) ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Gets the JavaScript code to track a conditional event wrapped in <script> tag. |
| 304 | * |
| 305 | * @see \WC_Facebookcommerce_Pixel::get_event_code() |
| 306 | * |
| 307 | * @since 1.10.2 |
| 308 | * |
| 309 | * @param string $event_name The name of the event to track. |
| 310 | * @param array $params Custom event parameters. |
| 311 | * @param string $listener Name of the JavaScript event to listen for. |
| 312 | * @param string $jsonified_pii JavaScript code representing an object of data for Advanced Matching. |
| 313 | * @return string |
| 314 | */ |
| 315 | public function get_conditional_event_script( $event_name, $params, $listener, $jsonified_pii ) { |
| 316 | |
| 317 | $code = self::build_event( $event_name, $params, 'track' ); |
| 318 | $this->last_event = $event_name; |
| 319 | |
| 320 | /** |
| 321 | * TODO: use the settings stored by {@see \WC_Facebookcommerce_Integration}. |
| 322 | * The use_pii setting here is currently always disabled regardless of |
| 323 | * the value configured in the plugin settings page {WV-2020-01-02}. |
| 324 | */ |
| 325 | |
| 326 | // Prepends fbq(...) with pii information to the injected code. |
| 327 | if ( $jsonified_pii && get_option( self::SETTINGS_KEY )[ self::USE_PII_KEY ] ) { |
| 328 | $this->user_info = '%s'; |
| 329 | $code = sprintf( $this->get_pixel_init_code(), '" || ' . $jsonified_pii . ' || "' ) . $code; |
| 330 | } |
| 331 | |
| 332 | ob_start(); |
| 333 | |
| 334 | ?> |
| 335 | <!-- Facebook Pixel Event Code --> |
| 336 | <script <?php echo self::get_script_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.Output ?>> |
| 337 | document.addEventListener( '<?php echo esc_js( $listener ); ?>', function (event) { |
| 338 | <?php echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 339 | }, false ); |
| 340 | </script> |
| 341 | <!-- End Facebook Pixel Event Code --> |
| 342 | <?php |
| 343 | |
| 344 | return ob_get_clean(); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * Prints the JavaScript code to track a conditional event. |
| 350 | * |
| 351 | * The tracking code will be executed when the given JavaScript event is triggered. |
| 352 | * |
| 353 | * @param string $event_name Name of the event. |
| 354 | * @param array $params Custom event parameters. |
| 355 | * @param string $listener Name of the JavaScript event to listen for. |
| 356 | * @param string $jsonified_pii JavaScript code representing an object of data for Advanced Matching. |
| 357 | * @return string |
| 358 | */ |
| 359 | public function inject_conditional_event( $event_name, $params, $listener, $jsonified_pii = '' ) { |
| 360 | |
| 361 | // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped |
| 362 | return $this->get_conditional_event_script( $event_name, self::build_params( $params, $event_name ), $listener, $jsonified_pii ); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /** |
| 367 | * Gets the JavaScript code to track a conditional event that is only triggered one time wrapped in <script> tag. |
| 368 | * |
| 369 | * @internal |
| 370 | * |
| 371 | * @since 1.10.2 |
| 372 | * |
| 373 | * @param string $event_name The name of the event to track. |
| 374 | * @param array $params Custom event parameters. |
| 375 | * @param string $listened_event Name of the JavaScript event to listen for. |
| 376 | * @return string |
| 377 | */ |
| 378 | public function get_conditional_one_time_event_script( $event_name, $params, $listened_event ) { |
| 379 | |
| 380 | $code = $this->get_event_code( $event_name, $params ); |
| 381 | |
| 382 | ob_start(); |
| 383 | |
| 384 | ?> |
| 385 | <!-- Facebook Pixel Event Code --> |
| 386 | <script <?php echo self::get_script_attributes(); // phpcs:ignore WordPress.Security.EscapeOutput.Output ?>> |
| 387 | function handle<?php echo $event_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>Event() { |
| 388 | <?php echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 389 | // Some weird themes (hi, Basel) are running this script twice, so two listeners are added and we need to remove them after running one. |
| 390 | jQuery( document.body ).off( '<?php echo esc_js( $listened_event ); ?>', handle<?php echo $event_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>Event ); |
| 391 | } |
| 392 | |
| 393 | jQuery( document.body ).one( '<?php echo esc_js( $listened_event ); ?>', handle<?php echo $event_name; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>Event ); |
| 394 | </script> |
| 395 | <!-- End Facebook Pixel Event Code --> |
| 396 | <?php |
| 397 | |
| 398 | return ob_get_clean(); |
| 399 | } |
| 400 | |
| 401 | |
| 402 | /** |
| 403 | * Builds an event. |
| 404 | * |
| 405 | * @see \WC_Facebookcommerce_Pixel::inject_event() for the preferred method to inject an event. |
| 406 | * |
| 407 | * @param string $event_name Event name. |
| 408 | * @param array $params Event params. |
| 409 | * @param string $method Optional, defaults to 'track'. |
| 410 | * @return string |
| 411 | */ |
| 412 | public static function build_event( $event_name, $params, $method = 'track' ) { |
| 413 | |
| 414 | // Do not send the event name in the params. |
| 415 | if ( isset( $params['event_name'] ) ) { |
| 416 | |
| 417 | unset( $params['event_name'] ); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * If possible, send the event ID to avoid duplication. |
| 422 | * |
| 423 | * @see https://developers.facebook.com/docs/marketing-api/server-side-api/deduplicate-pixel-and-server-side-events#deduplication-best-practices |
| 424 | */ |
| 425 | if ( isset( $params['event_id'] ) ) { |
| 426 | |
| 427 | $event_id = $params['event_id']; |
| 428 | unset( $params['event_id'] ); |
| 429 | } |
| 430 | |
| 431 | // If custom data is set, send only the custom data. |
| 432 | if ( isset( $params['custom_data'] ) ) { |
| 433 | |
| 434 | $params = $params['custom_data']; |
| 435 | } |
| 436 | |
| 437 | if ( ! empty( $event_id ) ) { |
| 438 | $event = sprintf( |
| 439 | "/* %s Facebook Integration Event Tracking */\n" . |
| 440 | "fbq('set', 'agent', '%s', '%s');\n" . |
| 441 | "fbq('%s', '%s', %s, %s);", |
| 442 | WC_Facebookcommerce_Utils::getIntegrationName(), |
| 443 | Event::get_platform_identifier(), |
| 444 | self::get_pixel_id(), |
| 445 | esc_js( $method ), |
| 446 | esc_js( $event_name ), |
| 447 | json_encode( self::build_params( $params, $event_name ), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ), |
| 448 | json_encode( array( 'eventID' => $event_id ), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ) |
| 449 | ); |
| 450 | |
| 451 | } else { |
| 452 | |
| 453 | $event = sprintf( |
| 454 | "/* %s Facebook Integration Event Tracking */\n" . |
| 455 | "fbq('set', 'agent', '%s', '%s');\n" . |
| 456 | "fbq('%s', '%s', %s);", |
| 457 | WC_Facebookcommerce_Utils::getIntegrationName(), |
| 458 | Event::get_platform_identifier(), |
| 459 | self::get_pixel_id(), |
| 460 | esc_js( $method ), |
| 461 | esc_js( $event_name ), |
| 462 | json_encode( self::build_params( $params, $event_name ), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT ) |
| 463 | ); |
| 464 | } |
| 465 | |
| 466 | return $event; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /** |
| 471 | * Gets an array with version_info for pixel fires. |
| 472 | * |
| 473 | * Parameters provided by users should not be overwritten by this function. |
| 474 | * |
| 475 | * @since 1.10.2 |
| 476 | * |
| 477 | * @param array $params User defined parameters. |
| 478 | * @param string $event The event name the params are for. |
| 479 | * @return array |
| 480 | */ |
| 481 | private static function build_params( $params = [], $event = '' ) { |
| 482 | |
| 483 | $params = array_replace( Event::get_version_info(), $params ); |
| 484 | |
| 485 | /** |
| 486 | * Filters the parameters for the pixel code. |
| 487 | * |
| 488 | * @since 1.10.2 |
| 489 | * |
| 490 | * @param array $params User defined parameters. |
| 491 | * @param string $event The event name. |
| 492 | */ |
| 493 | return (array) apply_filters( 'wc_facebook_pixel_params', $params, $event ); |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * Gets script tag attributes. |
| 499 | * |
| 500 | * @since 1.10.2 |
| 501 | * |
| 502 | * @return string |
| 503 | */ |
| 504 | private static function get_script_attributes() { |
| 505 | |
| 506 | $script_attributes = ''; |
| 507 | |
| 508 | /** |
| 509 | * Filters Facebook Pixel script attributes. |
| 510 | * |
| 511 | * @since 1.10.2 |
| 512 | * |
| 513 | * @param array $custom_attributes |
| 514 | */ |
| 515 | $custom_attributes = (array) apply_filters( 'wc_facebook_pixel_script_attributes', array( 'type' => 'text/javascript' ) ); |
| 516 | |
| 517 | foreach ( $custom_attributes as $tag => $value ) { |
| 518 | $script_attributes .= ' ' . $tag . '="' . esc_attr( $value ) . '"'; |
| 519 | } |
| 520 | |
| 521 | return $script_attributes; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Get the PixelId. |
| 526 | */ |
| 527 | public static function get_pixel_id() { |
| 528 | $fb_options = self::get_options(); |
| 529 | if ( ! $fb_options ) { |
| 530 | return ''; |
| 531 | } |
| 532 | return isset( $fb_options[ self::PIXEL_ID_KEY ] ) ? |
| 533 | $fb_options[ self::PIXEL_ID_KEY ] : ''; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Set the PixelId. |
| 538 | * |
| 539 | * @param string $pixel_id PixelId. |
| 540 | */ |
| 541 | public static function set_pixel_id( $pixel_id ) { |
| 542 | $fb_options = self::get_options(); |
| 543 | |
| 544 | if ( isset( $fb_options[ self::PIXEL_ID_KEY ] ) |
| 545 | && $fb_options[ self::PIXEL_ID_KEY ] == $pixel_id ) { |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | $fb_options[ self::PIXEL_ID_KEY ] = $pixel_id; |
| 550 | update_option( self::SETTINGS_KEY, $fb_options ); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Check if PII key use is enabled. |
| 555 | */ |
| 556 | public static function get_use_pii_key() { |
| 557 | $fb_options = self::get_options(); |
| 558 | if ( ! $fb_options ) { |
| 559 | return null; |
| 560 | } |
| 561 | return isset( $fb_options[ self::USE_PII_KEY ] ) ? |
| 562 | $fb_options[ self::USE_PII_KEY ] : null; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Enable or disable use of PII key. |
| 567 | * |
| 568 | * @param string $use_pii PII key. |
| 569 | */ |
| 570 | public static function set_use_pii_key( $use_pii ) { |
| 571 | $fb_options = self::get_options(); |
| 572 | |
| 573 | if ( isset( $fb_options[ self::USE_PII_KEY ] ) |
| 574 | && $fb_options[ self::USE_PII_KEY ] == $use_pii ) { |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | $fb_options[ self::USE_PII_KEY ] = $use_pii; |
| 579 | update_option( self::SETTINGS_KEY, $fb_options ); |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Check if S2S is set. |
| 584 | */ |
| 585 | public static function get_use_s2s() { |
| 586 | $fb_options = self::get_options(); |
| 587 | if ( ! $fb_options ) { |
| 588 | return false; |
| 589 | } |
| 590 | return isset( $fb_options[ self::USE_S2S_KEY ] ) ? |
| 591 | $fb_options[ self::USE_S2S_KEY ] : false; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Enable or disable use of S2S key. |
| 596 | * |
| 597 | * @param string $use_s2s S2S setting. |
| 598 | */ |
| 599 | public static function set_use_s2s( $use_s2s ) { |
| 600 | $fb_options = self::get_options(); |
| 601 | |
| 602 | if ( isset( $fb_options[ self::USE_S2S_KEY ] ) |
| 603 | && $fb_options[ self::USE_S2S_KEY ] == $use_s2s ) { |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | $fb_options[ self::USE_S2S_KEY ] = $use_s2s; |
| 608 | update_option( self::SETTINGS_KEY, $fb_options ); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Get access token. |
| 613 | */ |
| 614 | public static function get_access_token() { |
| 615 | $fb_options = self::get_options(); |
| 616 | if ( ! $fb_options ) { |
| 617 | return ''; |
| 618 | } |
| 619 | return isset( $fb_options[ self::ACCESS_TOKEN_KEY ] ) ? |
| 620 | $fb_options[ self::ACCESS_TOKEN_KEY ] : ''; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Set access token. |
| 625 | * |
| 626 | * @param string $access_token Access token. |
| 627 | */ |
| 628 | public static function set_access_token( $access_token ) { |
| 629 | $fb_options = self::get_options(); |
| 630 | |
| 631 | if ( isset( $fb_options[ self::ACCESS_TOKEN_KEY ] ) |
| 632 | && $fb_options[ self::ACCESS_TOKEN_KEY ] == $access_token ) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | $fb_options[ self::ACCESS_TOKEN_KEY ] = $access_token; |
| 637 | update_option( self::SETTINGS_KEY, $fb_options ); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Get WooCommerce/Wordpress information. |
| 642 | */ |
| 643 | private static function get_version_info() { |
| 644 | global $wp_version; |
| 645 | |
| 646 | if ( WC_Facebookcommerce_Utils::isWoocommerceIntegration() ) { |
| 647 | return array( |
| 648 | 'source' => 'woocommerce', |
| 649 | 'version' => WC()->version, |
| 650 | 'pluginVersion' => WC_Facebookcommerce_Utils::PLUGIN_VERSION, |
| 651 | ); |
| 652 | } |
| 653 | |
| 654 | return array( |
| 655 | 'source' => 'wordpress', |
| 656 | 'version' => $wp_version, |
| 657 | 'pluginVersion' => WC_Facebookcommerce_Utils::PLUGIN_VERSION, |
| 658 | ); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Get PixelID related settings. |
| 663 | */ |
| 664 | public static function get_options() { |
| 665 | |
| 666 | $default_options = array( |
| 667 | self::PIXEL_ID_KEY => '0', |
| 668 | self::USE_PII_KEY => 0, |
| 669 | self::USE_S2S_KEY => false, |
| 670 | self::ACCESS_TOKEN_KEY => '', |
| 671 | ); |
| 672 | |
| 673 | $fb_options = get_option( self::SETTINGS_KEY ); |
| 674 | |
| 675 | if ( ! is_array( $fb_options ) ) { |
| 676 | $fb_options = $default_options; |
| 677 | } else { |
| 678 | foreach ( $default_options as $key => $value ) { |
| 679 | if ( ! isset( $fb_options[ $key ] ) ) { |
| 680 | $fb_options[ $key ] = $value; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | return $fb_options; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Gets the logged in user info |
| 690 | * |
| 691 | * @return string[] |
| 692 | */ |
| 693 | public function get_user_info() { |
| 694 | return $this->user_info; |
| 695 | } |
| 696 | } |
| 697 |