PluginProbe
Wp Tracking Codes / 1.8.0
Wp Tracking Codes v1.8.0
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.8.0, at wp-tracking-codes.php

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