Skip to content

Commit

Permalink
Add Filter Hook in Pixel. Remove Changes Potentially Causing 500 erro…
Browse files Browse the repository at this point in the history
…r when saving pixel.

Summary:
Title. We have an email thread with PixelYourSite open that asks for these hooks.
Reverted a part in save settings that we are seeing new 500 errors in.

Reviewed By: mengyingdu

Differential Revision: D6845837

fbshipit-source-id: f4229db
  • Loading branch information
dmitridr authored and facebook-github-bot committed Jan 30, 2018
1 parent fb01cf5 commit 886093c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
6 changes: 5 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
*** Facebook for WooCommerce Changelog ***

2018-01-22 version 1.7.8
2017-01-30 version 1.7.9
* Add Filter hook for other plugins to override pixel behavior.
* Fix 500 errors when saving settings.

2018-01-25 version 1.7.8
* Fixes WC_Facebookcommerce_Pixel reference error


Expand Down
48 changes: 43 additions & 5 deletions facebook-commerce-events-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@

class WC_Facebookcommerce_EventsTracker {
private $pixel;
private static $isEnabled = true;

public function __construct($user_info) {
$this->pixel = new WC_Facebookcommerce_Pixel($user_info);
self::apply_filters();
}

public static function apply_filters() {
self::$isEnabled = apply_filters(
"facebook_for_woocommerce_integration_pixel_enabled",
self::$isEnabled);
}

/**
Expand All @@ -26,22 +34,29 @@ public function __construct($user_info) {
* WC_Facebookcommerce_Utils::wc_enqueue_js() in this case
*/
public function inject_base_pixel() {
echo $this->pixel->pixel_base_code();
if (self::$isEnabled) {
echo $this->pixel->pixel_base_code();
}
}

/**
* Base pixel noscript to be injected on page body. This is to avoid W3
* validation error.
*/
public function inject_base_pixel_noscript() {
echo $this->pixel->pixel_base_code_noscript();
if (self::$isEnabled) {
echo $this->pixel->pixel_base_code_noscript();
}
}

/**
* Triggers ViewCategory for product category listings
*/
public function inject_view_category_event() {
global $wp_query;
if (!self::$isEnabled) {
return;
}

$products = array_values(array_map(function($item) {
return wc_get_product($item->ID);
Expand Down Expand Up @@ -82,6 +97,10 @@ public function inject_view_category_event() {
* Triggers Search for result pages (deduped)
*/
public function inject_search_event() {
if (!self::$isEnabled) {
return;
}

if (!is_admin() && is_search() && get_search_query() !== '') {
if ($this->pixel->check_last_event('Search')) {
return;
Expand All @@ -99,6 +118,10 @@ public function inject_search_event() {
* Triggers Search for result pages
*/
public function actually_inject_search_event() {
if (!self::$isEnabled) {
return;
}

$this->pixel->inject_event(
'Search',
array(
Expand All @@ -123,6 +146,10 @@ private function get_content_ids_from_cart($cart) {
* Triggers ViewContent product pages
*/
public function inject_view_content_event() {
if (!self::$isEnabled) {
return;
}

$product = wc_get_product(get_the_ID());
$content_type = 'product';
if (!$product) {
Expand Down Expand Up @@ -150,6 +177,10 @@ public function inject_view_content_event() {
* Triggers AddToCart for cart page and add_to_cart button clicks
*/
public function inject_add_to_cart_event() {
if (!self::$isEnabled) {
return;
}

$product_ids = $this->get_content_ids_from_cart(WC()->cart->get_cart());

$this->pixel->inject_event(
Expand All @@ -166,6 +197,10 @@ public function inject_add_to_cart_event() {
* Triggered by add_to_cart jquery trigger
*/
public function inject_ajax_add_to_cart_event() {
if (!self::$isEnabled) {
return;
}

ob_start();

echo '<script>';
Expand All @@ -191,7 +226,8 @@ public function inject_ajax_add_to_cart_event() {
* Triggers InitiateCheckout for checkout page
*/
public function inject_initiate_checkout_event() {
if ($this->pixel->check_last_event('InitiateCheckout')) {
if (!self::$isEnabled ||
$this->pixel->check_last_event('InitiateCheckout')) {
return;
}

Expand All @@ -213,7 +249,8 @@ public function inject_initiate_checkout_event() {
* page in cases of delayed payment.
*/
public function inject_purchase_event($order_id) {
if ($this->pixel->check_last_event('Purchase')) {
if (!self::$isEnabled ||
$this->pixel->check_last_event('Purchase')) {
return;
}

Expand Down Expand Up @@ -245,7 +282,8 @@ public function inject_purchase_event($order_id) {
* which won't invoke woocommerce_payment_complete.
*/
public function inject_gateway_purchase_event($order_id) {
if ($this->pixel->check_last_event('Purchase')) {
if (!self::$isEnabled ||
$this->pixel->check_last_event('Purchase')) {
return;
}

Expand Down
9 changes: 7 additions & 2 deletions facebook-commerce-pixel-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,18 @@ public static function build_event($event_name, $params, $method='track') {

public static function get_pixel_id() {
$fb_options = self::get_options();
return $fb_options[self::PIXEL_ID_KEY];
if (!$fb_options) {
return '';
}
return isset($fb_options[self::PIXEL_ID_KEY]) ?
$fb_options[self::PIXEL_ID_KEY] : '';
}

public static function set_pixel_id($pixel_id) {
$fb_options = self::get_options();

if ($fb_options[self::PIXEL_ID_KEY] == $pixel_id) {
if (isset($fb_options[self::PIXEL_ID_KEY])
&& $fb_options[self::PIXEL_ID_KEY] == $pixel_id) {
return;
}

Expand Down
13 changes: 9 additions & 4 deletions facebook-commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class WC_Facebookcommerce_Integration extends WC_Integration {

public function init_settings() {
parent::init_settings();
}

public function init_pixel() {
WC_Facebookcommerce_Pixel::initialize();

// Migrate WC customer pixel_id from WC settings to WP options.
Expand Down Expand Up @@ -97,6 +99,10 @@ public function __construct() {
: '';

$pixel_id = WC_Facebookcommerce_Pixel::get_pixel_id();
if (!$pixel_id) {
$pixel_id = isset($this->settings['fb_pixel_id']) ?
$this->settings['fb_pixel_id'] : '';
}
$this->pixel_id = isset($pixel_id)
? $pixel_id
: '';
Expand Down Expand Up @@ -138,6 +144,7 @@ public function __construct() {

// Hooks
if (is_admin()) {
$this->init_pixel();
$this->init_form_fields();
// Display an info banner for eligible pixel and user.
if ($this->external_merchant_settings_id
Expand Down Expand Up @@ -1019,12 +1026,10 @@ function ajax_save_fb_settings() {
// To prevent race conditions with pixel-only settings,
// only save a pixel if we already have an API key.
if ($this->settings['fb_api_key']) {
$pixel_id = WC_Facebookcommerce_Pixel::get_pixel_id();
if ($pixel_id != $_REQUEST['pixel_id']) {
$this->settings['fb_pixel_id'] = $_REQUEST['pixel_id'];
if ($this->pixel_id != $_REQUEST['pixel_id']) {
$this->settings['pixel_install_time'] = current_time('mysql');
}

WC_Facebookcommerce_Pixel::set_pixel_id($_REQUEST['pixel_id']);
} else {
WC_Facebookcommerce_Utils::log(
"Got pixel-only settings, doing nothing");
Expand Down
2 changes: 1 addition & 1 deletion facebook-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: Grow your business on Facebook! Use this official plugin to help sell more of your products using Facebook. After completing the setup, you'll be ready to create ads that promote your products and you can also create a shop section on your Page where customers can browse your products on Facebook.
* Author: Facebook
* Author URI: https://www.facebook.com/
* Version: 1.7.8
* Version: 1.7.9
* Woo: 2127297:0ea4fe4c2d7ca6338f8a322fb3e4e187
* Text Domain: facebook-for-woocommerce
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/fbutils.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class WC_Facebookcommerce_Utils {

const FB_RETAILER_ID_PREFIX = 'wc_post_id_';
const PLUGIN_VERSION = '1.7.8'; // Change it in `facebook-for-*.php` also
const PLUGIN_VERSION = '1.7.9'; // Change it in `facebook-for-*.php` also

const FB_VARIANT_IMAGE = 'fb_image';
const FB_VARIANT_SIZE = 'size';
Expand Down

0 comments on commit 886093c

Please sign in to comment.