-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathclass-facebookforwordpress.php
156 lines (134 loc) · 5.87 KB
/
class-facebookforwordpress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Plugin Name: Meta pixel for WordPress
* Plugin URI: https://www.facebook.com/business/help/881403525362441
* Description: <strong><em>***ATTENTION: After upgrade the plugin may be deactivated due to a known issue, to workaround please refresh this page and activate plugin.***</em></strong> The Facebook pixel is an analytics tool that helps you measure the effectiveness of your advertising. You can use the Facebook pixel to understand the actions people are taking on your website and reach audiences you care about.
* Author: Facebook
* Author URI: https://www.facebook.com/
* Version: {*VERSION_NUMBER*}
* Text Domain: official-facebook-pixel
*
* @package FacebookPixelPlugin
*/
/*
* Copyright (C) 2017-present, Facebook, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
namespace FacebookPixelPlugin;
defined( 'ABSPATH' ) || die( 'Direct access not allowed' );
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
use FacebookPixelPlugin\Core\FacebookPixel;
use FacebookPixelPlugin\Core\FacebookPluginConfig;
use FacebookPixelPlugin\Core\FacebookPluginUtils;
use FacebookPixelPlugin\Core\FacebookWordpressOpenBridge;
use FacebookPixelPlugin\Core\FacebookWordpressOptions;
use FacebookPixelPlugin\Core\FacebookWordpressPixelInjection;
use FacebookPixelPlugin\Core\FacebookWordpressSettingsPage;
use FacebookPixelPlugin\Core\FacebookWordpressSettingsRecorder;
use FacebookPixelPlugin\Core\ServerEventAsyncTask;
/**
* FacebookForWordpress root class.
*/
class FacebookForWordpress {
/**
* Plugin constructor. Initializes the plugin options, loads the translation files,
* sets up the Facebook pixel, sets up the pixel injection, and sets up the settings
* page. Also starts the server event async task.
*/
public function __construct() {
FacebookWordpressOptions::initialize();
load_plugin_textdomain(
FacebookPluginConfig::TEXT_DOMAIN,
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
$options = FacebookWordpressOptions::get_options();
FacebookPixel::initialize( FacebookWordpressOptions::get_pixel_id() );
add_action( 'init', array( $this, 'register_pixel_injection' ), 0 );
add_action( 'parse_request', array( $this, 'handle_events_request' ), 0 );
$this->register_settings_page();
new ServerEventAsyncTask();
}
/**
* Registers the pixel injection. This method instantiates the
* FacebookWordpressPixelInjection and calls its inject method.
*
* The inject method is responsible for adding the necessary hooks to
* inject the Facebook pixel code into the footer of the WordPress page.
*/
public function register_pixel_injection() {
$injection_obj = new FacebookWordpressPixelInjection();
$injection_obj->inject();
}
/**
* Registers the settings page for the Facebook for WordPress plugin. This method
* instantiates the FacebookWordpressSettingsPage and FacebookWordpressSettingsRecorder
* objects. The settings page object is responsible for adding the necessary hooks
* and rendering the settings page. The settings recorder object is responsible for
* recording data about the user's settings and sending it to Meta.
*/
public function register_settings_page() {
if ( is_admin() ) {
$plugin_name = plugin_basename( __FILE__ );
new FacebookWordpressSettingsPage( $plugin_name );
( new FacebookWordpressSettingsRecorder() )->init();
}
}
/**
* Handles incoming events requests by checking if the request URI
* ends with the configured open bridge path and if the request
* method is POST. If both conditions are met, it decodes the JSON
* payload from the request body and forwards it to the open bridge
* request handler. Additionally, it sets CORS headers to allow
* cross-origin requests if the origin is specified in the request
* headers.
*/
public function handle_events_request() {
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$request_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
if (
FacebookPluginUtils::ends_with(
$request_uri,
FacebookPluginConfig::OPEN_BRIDGE_PATH
) &&
isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD']
) {
$data = json_decode( file_get_contents( 'php://input' ), true );
if ( ! is_null( $data ) ) {
FacebookWordpressOpenBridge::get_instance()->handle_open_bridge_req(
$data
);
}
if ( isset( $_SERVER['HTTP_ORIGIN'] ) ) {
$origin = wp_kses( wp_unslash( $_SERVER['HTTP_ORIGIN'] ), array() );
header( "Access-Control-Allow-Origin: $origin" );
header( 'Access-Control-Allow-Credentials: true' );
header( 'Access-Control-Max-Age: 86400' );
$fbc = isset(
$_COOKIE['_fbc']
)
? $_COOKIE['_fbc'] : // phpcs:ignore
( isset( $data['_fbc'] ) ? $data['_fbc'] : null );
$fbp = isset( $_COOKIE['_fbp'] )
? $_COOKIE['_fbp'] : // phpcs:ignore
( isset( $data['_fbp'] ) ? $data['_fbp'] : null );
if ( $fbc ) {
setcookie( '_fbc', $fbc, time() + ( 86400 * 30 ), '/' );
}
if ( $fbp ) {
setcookie( '_fbp', $fbp, time() + ( 86400 * 30 ), '/' );
}
}
exit();
}
}
}
}
new FacebookForWordpress();