| 1 |
<?php |
| 2 |
|
| 3 |
namespace Pixelavo; |
| 4 |
|
| 5 |
/** |
| 6 |
* Exit if accessed directly |
| 7 |
* */ |
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
class AddPixel{ |
| 13 |
|
| 14 |
private static $_instance = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Instance |
| 18 |
* |
| 19 |
* Initializes a singleton instance |
| 20 |
* |
| 21 |
* @return self self class |
| 22 |
*/ |
| 23 |
static function instance() { |
| 24 |
if ( is_null( self::$_instance ) ) { |
| 25 |
self::$_instance = new self(); |
| 26 |
} |
| 27 |
return self::$_instance; |
| 28 |
} |
| 29 |
|
| 30 |
private function __construct() { |
| 31 |
add_action('wp_head', [ $this, 'add_pixels']); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Add pixel to the site. |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
function add_pixels() { |
| 40 |
$pixels = pixelavo_get_pixel_list(apply_filters('pixelavo_pixels_limit', 1)); |
| 41 |
|
| 42 |
$pixelInitCode = ''; |
| 43 |
$pixelNoScriptCode = ''; |
| 44 |
$advanced_matching_data = []; |
| 45 |
if(function_exists('pixelavo_get_advance_matching_data')) { |
| 46 |
$advanced_matching_data = pixelavo_get_advance_matching_data(); |
| 47 |
} |
| 48 |
$advanced_matching = !empty($advanced_matching_data['raw']) ? $advanced_matching_data['raw'] : []; |
| 49 |
$advanced_matching_hashed = !empty($advanced_matching_data['hashed']) ? $advanced_matching_data['hashed'] : []; |
| 50 |
|
| 51 |
if(!empty($pixels)) { |
| 52 |
|
| 53 |
/** |
| 54 |
* Print facebook pixel code to the browser. |
| 55 |
*/ |
| 56 |
if(pixelavo_pixel_status() && !pixelavo_check_exclude_roles()) { ?> |
| 57 |
<script> |
| 58 |
!function(f,b,e,v,n,t,s) |
| 59 |
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? |
| 60 |
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; |
| 61 |
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; |
| 62 |
n.queue=[];t=b.createElement(e);t.async=!0; |
| 63 |
t.src=v;s=b.getElementsByTagName(e)[0]; |
| 64 |
s.parentNode.insertBefore(t,s)}(window, document,'script', |
| 65 |
'https://connect.facebook.net/en_US/fbevents.js'); |
| 66 |
<?php |
| 67 |
foreach ($pixels as $pixel) { |
| 68 |
if($pixel['active'] == 'on' && pixelavo_check_pixel_page($pixel)) { |
| 69 |
if(!empty($advanced_matching)) { |
| 70 |
echo "fbq('init', ".esc_attr($pixel['pixel_id']).", ".wp_json_encode($advanced_matching).");" . "\n"; |
| 71 |
} else { |
| 72 |
echo "fbq('init', ".esc_attr($pixel['pixel_id']).");" . "\n"; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
?> |
| 77 |
fbq('track', 'PageView', <?php echo wp_json_encode(pixelavo_get_event_common_data()); ?>); |
| 78 |
</script> |
| 79 |
<noscript> |
| 80 |
<?php |
| 81 |
foreach ($pixels as $pixel) { |
| 82 |
if($pixel['active'] == 'on' && pixelavo_check_pixel_page($pixel)) { |
| 83 |
if(!empty($advanced_matching)) { |
| 84 |
// Build query string with only available advanced matching fields |
| 85 |
$noscript_params = ['id' => $pixel['pixel_id'], 'ev' => 'PageView', 'noscript' => 1]; |
| 86 |
$ud_fields = ['em', 'fn', 'ln', 'ph', 'ct', 'st', 'zp', 'country']; |
| 87 |
foreach ($ud_fields as $field) { |
| 88 |
if (isset($advanced_matching_hashed[$field]) && !empty($advanced_matching_hashed[$field])) { |
| 89 |
$noscript_params["ud[{$field}]"] = $advanced_matching_hashed[$field]; |
| 90 |
} |
| 91 |
} |
| 92 |
$query_string = http_build_query($noscript_params); |
| 93 |
// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage |
| 94 |
echo "<img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?" . esc_url($query_string) . "'/>" . "\n"; |
| 95 |
} else { |
| 96 |
// phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage |
| 97 |
echo "<img height='1' width='1' style='display:none' src='https://www.facebook.com/tr?id=".esc_attr($pixel['pixel_id'])."&ev=PageView&noscript=1'/>" . "\n"; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
?> |
| 102 |
</noscript> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Initialize AddPixel Class |
| 112 |
*/ |
| 113 |
AddPixel::instance(); |
| 114 |
|