PluginProbe
Wp Tracking Codes / 1.9.1
Wp Tracking Codes v1.9.1
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.0, 1.10.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 1.9.1 1.9.2 1.9.3
wp-tracking-codes / wp-tracking-codes.php

wp-tracking-codes.php in Wp Tracking Codes 1.9.1, at wp-tracking-codes.php

236 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Wp Tracking Codes
4 * Plugin URI: https://br.wordpress.org/plugins/wp-tracking-codes/
5 * Description: The tracking codes in one place. Support: GTM, GA4 Global Tag, ADS Remarketing Global Tag, Google Merchant Customer Reviews for WooCommerce, Facebook.
6 * Version: 1.9.1
7 * Requires at least: 5.2.0
8 * Tested up to: 6.4.3
9 * Requires PHP: 7.2
10 * Author: Array.codes
11 * Author URI: https://array.codes
12 * Developer: Heitor Sousa
13 * Developer URI: https://array.codes/
14 * Domain Path: /languages
15 * Text Domain: wp-tracking-codes
16 *
17 * WC requires at least: 4.8.0
18 * WC tested up to: 8.7.0
19 *
20 * License: GPLv2 or later
21 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
22 *
23 */
24 if(!defined('ABSPATH')){
25 exit;
26 }
27
28 if(!class_exists('Wp_Tracking_Codes')):
29 class Wp_Tracking_Codes {
30 /**
31 * Plugin version.
32 *
33 * @var string
34 */
35 const VERSION = '1.9.1';
36 /**
37 * Instance of this class.
38 *
39 * @var object
40 */
41 protected static $instance = null;
42 /**
43 * Options
44 *
45 * @var array $options Options
46 */
47 private $options = array();
48 /**
49 * Initialize the plugin public actions.
50 */
51 private function __construct() {
52 // Load the instalation hook
53 register_activation_hook( __FILE__, 'wp_tracking_codes_install' );
54 // Load plugin text domain.
55 add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
56 //Load includes
57 $this->includes();
58 //Load Tracking Analytics
59 add_action( 'wp_head', array( $this, 'hook_analytics_4' ) );
60 //Load Tracking Analytics Remarketing
61 add_action( 'wp_footer', array( $this, 'hook_analytics_remarketing' ) );
62 //Load Tracking Facebook ID
63 add_action( 'wp_head', array( $this, 'hook_facebook_pixel_code' ) );
64 //Load Tracking Google Tag Manager in Head and after Body
65 add_action('wp_head', array($this, 'hook_google_tag_manager_head'));
66 add_filter('template_include', array( $this, 'custom_include' ),0 );
67 add_filter('wp_body_open', array( $this, 'hook_google_tag_manager_body' ),0);
68 add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility_hpos') );
69 }
70 /**
71 * Return an instance of this class.
72 *
73 * @return object A single instance of this class.
74 */
75 public static function get_instance() {
76 // If the single instance hasn't been set, set it now.
77 if ( null == self::$instance ) {
78 self::$instance = new self;
79 }
80
81 return self::$instance;
82 }
83
84 /**
85 * Get templates path.
86 *
87 * @return string
88 */
89 public static function get_templates_path() {
90 return plugin_dir_path( __FILE__ ) . 'templates/';
91 }
92
93 /**
94 * Load the plugin text domain for translation.
95 */
96 public function load_plugin_textdomain() {
97 load_plugin_textdomain( 'wp-tracking-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
98 }
99
100 /**
101 * Instalation Plugin
102 */
103 public function wp_tracking_codes_install() {
104 // Trigger our function that registers the custom post type
105 pluginprefix_setup_post_types();
106 // Clear the permalinks after the post type has been registered
107 flush_rewrite_rules();
108 }
109 /**
110 * Includes
111 */
112 private function includes() {
113 include_once 'includes/class-register-codes.php';
114 include_once 'includes/class-render-data-layer-gtm.php';
115 include_once 'includes/class-render-google-merchant.php';
116 }
117 /**
118 * Debug
119 */
120 public function log_me($message) {
121 if (WP_DEBUG === true) {
122 if (is_array($message) || is_object($message)) {
123 error_log(print_r($message, true));
124 } else {
125 error_log($message);
126 }
127 }
128 }
129
130 public function hook_analytics_4(){
131 $this->options = get_option( 'tracking_option' );
132 if( isset( $this->options['analytics_4'] ) && !empty( $this->options['analytics_4'] ) ){
133 $analytics_4 = $this->options['analytics_4'];
134 printf("
135 <!-- Global site tag (gtag.js) - Google Analytics -->
136 <script async src='https://www.googletagmanager.com/gtag/js?id=%s'></script>
137 <script>
138 window.dataLayer = window.dataLayer || [];
139 function gtag(){dataLayer.push(arguments);}
140 gtag('js', new Date());
141
142 gtag('config', '%s');
143 </script>
144 ", esc_attr($analytics_4), esc_attr($analytics_4));
145 }
146
147 }
148
149 public function hook_analytics_remarketing() {
150 $this->options = get_option( 'tracking_option' );
151 if( isset( $this->options['analytics_remarketing'] ) && !empty( $this->options['analytics_remarketing'] ) ){
152 $analytics_remarketing = $this->options['analytics_remarketing'];
153 printf("
154 <!-- Global site tag (gtag.js) - Google Ads -->
155 <script async src='https://www.googletagmanager.com/gtag/js?id=%s'></script>
156 <script>
157 window.dataLayer = window.dataLayer || [];
158 function gtag(){dataLayer.push(arguments);}
159 gtag('js', new Date());
160
161 gtag('config', '%s');
162 </script>
163 ", esc_attr($analytics_remarketing), esc_attr($analytics_remarketing));
164 }
165 }
166
167 public function hook_facebook_pixel_code() {
168 $this->options = get_option( 'tracking_option' );
169 if ( isset( $this->options['facebook_pixel_code'] ) && ! empty( $this->options['facebook_pixel_code'] ) ){
170 $facebook_pixel_code = $this->options['facebook_pixel_code'];
171 printf( "
172 <!-- Facebook Pixel Code -->
173 <script>
174 !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
175 n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
176 n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
177 t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
178 document,'script','https://connect.facebook.net/en_US/fbevents.js');
179
180 fbq('init', '%s');
181 fbq('track', 'PageView');</script>
182 <noscript><img height='1' width='1' style='display:none'
183 src='https://www.facebook.com/tr?id=1556124141356092&ev=PageView&noscript=1'
184 /></noscript>
185 <!-- End Facebook Pixel Code -->
186 ", esc_attr($facebook_pixel_code) );
187 }
188 }
189
190 public function custom_include($template) {
191 ob_start();
192 return $template;
193 }
194
195 public function hook_google_tag_manager_head(){
196 $this->options = get_option('tracking_option');
197 if ( isset( $this->options['google_tag_manager'] ) && !empty($this->options['google_tag_manager'])){
198 $google_tag_manager = $this->options['google_tag_manager'];
199 printf(
200 "<!-- Google Tag Manager -->
201 <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
202 new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
203 j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
204 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
205 })(window,document,'script','dataLayer','%s');</script>
206 <!-- End Google Tag Manager -->",
207 esc_attr( $google_tag_manager )
208 );
209 }
210
211 }
212
213 public function hook_google_tag_manager_body($classes){
214 $this->options = get_option( 'tracking_option' );
215 if( isset( $this->options['google_tag_manager'] ) && !empty( $this->options['google_tag_manager'] ) ){
216 $google_tag_manager = $this->options['google_tag_manager'];
217 printf("<!-- Google Tag Manager (noscript) -->
218 <noscript><iframe src='https://www.googletagmanager.com/ns.html?id=%s' height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript>
219 <!-- End Google Tag Manager (noscript) -->",
220 esc_attr( $google_tag_manager )
221 );
222 }
223 }
224
225 /**
226 * Declare_compatibility_hpos
227 */
228 public function declare_compatibility_hpos() {
229 if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
230 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', 'wp-tracking-codes/wp-tracking-codes.php', true );
231 \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', 'wp-tracking-codes/wp-tracking-codes.php', true );
232 }
233 }
234 }
235 add_action( 'plugins_loaded', array( 'Wp_Tracking_Codes', 'get_instance' ) );
236 endif;