| 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 = apply_filters('pixelavo_advanced_matching', []); |
| 45 |
$advanced_matching_hashed = apply_filters('pixelavo_advanced_matching_hashed', []); |
| 46 |
|
| 47 |
if(!empty($pixels)) { |
| 48 |
|
| 49 |
/** |
| 50 |
* Print facebook pixel code to the browser. |
| 51 |
*/ |
| 52 |
if(pixelavo_pixel_status() && !pixelavo_check_exclude_roles()) { ?> |
| 53 |
<script> |
| 54 |
!function(f,b,e,v,n,t,s) |
| 55 |
{if(f.fbq)return;n=f.fbq=function(){n.callMethod? |
| 56 |
n.callMethod.apply(n,arguments):n.queue.push(arguments)}; |
| 57 |
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; |
| 58 |
n.queue=[];t=b.createElement(e);t.async=!0; |
| 59 |
t.src=v;s=b.getElementsByTagName(e)[0]; |
| 60 |
s.parentNode.insertBefore(t,s)}(window, document,'script', |
| 61 |
'https://connect.facebook.net/en_US/fbevents.js'); |
| 62 |
<?php |
| 63 |
foreach ($pixels as $pixel) { |
| 64 |
if($pixel['active'] == 'on' && pixelavo_check_pixel_page($pixel)) { |
| 65 |
if(!empty($advanced_matching)) { |
| 66 |
echo "fbq('init', ".esc_attr($pixel['pixel_id']).", JSON.parse(".wp_json_encode($advanced_matching)."));" . "\n"; |
| 67 |
} else { |
| 68 |
echo "fbq('init', ".esc_attr($pixel['pixel_id']).");" . "\n"; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
?> |
| 73 |
fbq('track', 'PageView', <?php echo wp_json_encode(pixelavo_get_event_common_data()); ?>); |
| 74 |
</script> |
| 75 |
<noscript> |
| 76 |
<?php |
| 77 |
foreach ($pixels as $pixel) { |
| 78 |
if($pixel['active'] == 'on' && pixelavo_check_pixel_page($pixel)) { |
| 79 |
if(!empty($advanced_matching)) { |
| 80 |
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&ud[em]=".esc_attr($advanced_matching_hashed['em'])."&ud[fn]=".esc_attr($advanced_matching_hashed['fn'])."&ud[ln]=".esc_attr($advanced_matching_hashed['ln'])."&ud[ph]=".esc_attr($advanced_matching_hashed['ph'])."&ud[ct]=".esc_attr($advanced_matching_hashed['ct'])."&ud[st]=".esc_attr($advanced_matching_hashed['st'])."&ud[zp]=".esc_attr($advanced_matching_hashed['zp'])."&ud[country]=".esc_attr($advanced_matching_hashed['country'])."'/>" . "\n"; |
| 81 |
} else { |
| 82 |
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"; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
?> |
| 87 |
</noscript> |
| 88 |
<?php |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Initialize AddPixel Class |
| 97 |
*/ |
| 98 |
AddPixel::instance(); |
| 99 |
|