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