| @@ -1,217 +1,217 @@ | ||
| 1 | 1 | <?php |
| 2 | -/* | |
| 3 | -Plugin Name: Wp Tracking Codes | |
| 4 | -Plugin URI: https://br.wordpress.org/plugins/wp-tracking-codes/ | |
| 5 | -Description: Centralize os códigos de acompanhamento em apenas um lugar. Suporte: Google Analytics, Google Adwords Remarketing, Facebook Pixel Code, Google Tag Manager, DataLayer Google Tag Manager for Woocommerce | |
| 6 | -Version: 1.3.2 | |
| 7 | -Author: Heitor Pedroso | |
| 8 | -Author URI: https://profiles.wordpress.org/heitor_tito | |
| 9 | -License: GPL2 | |
| 10 | -License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
| 11 | -Domain Path: /languages | |
| 12 | -Text Domain: wp-tracking-codes | |
| 13 | -*/ | |
| 2 | +/** | |
| 3 | + * Plugin Name: Wp Tracking Codes | |
| 4 | + * Plugin URI: https://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.10.0 | |
| 7 | + * Requires at least: 5.2.0 | |
| 8 | + * Tested up to: 7.0 | |
| 9 | + * Requires PHP: 7.4 | |
| 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: 10.9.0 | |
| 19 | + * | |
| 20 | + * License: GPLv2 or later | |
| 21 | + * License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
| 22 | + * | |
| 23 | + */ | |
| 14 | 24 | if(!defined('ABSPATH')){ |
| 15 | - exit; | |
| 25 | + exit; | |
| 16 | 26 | } |
| 17 | 27 | |
| 18 | 28 | if(!class_exists('Wp_Tracking_Codes')): |
| 19 | - class Wp_Tracking_Codes { | |
| 29 | + class Wp_Tracking_Codes { | |
| 30 | + /** | |
| 31 | + * Plugin version. | |
| 32 | + * | |
| 33 | + * @var string | |
| 34 | + */ | |
| 35 | + const VERSION = '1.10.0'; | |
| 36 | + /** | |
| 37 | + * Instance of this class. | |
| 38 | + * | |
| 39 | + * @var object | |
| 40 | + */ | |
| 41 | + protected static $instance = null; | |
| 20 | 42 | /** |
| 21 | - * Plugin version. | |
| 43 | + * Options | |
| 22 | 44 | * |
| 23 | - * @var string | |
| 45 | + * @var array $options Options | |
| 24 | 46 | */ |
| 25 | - const VERSION = '1.3.2'; | |
| 26 | - /** | |
| 27 | - * Instance of this class. | |
| 28 | - * | |
| 29 | - * @var object | |
| 30 | - */ | |
| 31 | - protected static $instance = null; | |
| 32 | - /** | |
| 33 | - * Initialize the plugin public actions. | |
| 34 | - */ | |
| 35 | - private function __construct() { | |
| 36 | - // Load the instalation hook | |
| 37 | - register_activation_hook( __FILE__, 'wp_tracking_codes_install' ); | |
| 38 | - // Load plugin text domain. | |
| 39 | - add_action( 'init', array( $this, 'load_plugin_textdomain' ) ); | |
| 40 | - //Load includes | |
| 41 | - $this->includes(); | |
| 42 | - //Load Tracking Analytics | |
| 43 | - add_action( 'wp_head', array( $this, 'hook_analytics' ) ); | |
| 44 | - //Load Tracking Analytics Remarketing | |
| 45 | - add_action( 'wp_footer', array( $this, 'hook_analytics_remarketing' ) ); | |
| 46 | - //Load Tracking Facebook ID | |
| 47 | - add_action( 'wp_head', array( $this, 'hook_facebook_pixel_code' ) ); | |
| 48 | - //Load Tracking Google Tag Manager in Head and after Body | |
| 49 | - add_action('wp_head', array($this, 'hook_google_tag_manager_head')); | |
| 50 | - add_filter('template_include', array( $this, 'custom_include' ),0 ); | |
| 51 | - add_filter('shutdown', array( $this, 'hook_google_tag_manager_body' ),0); | |
| 52 | - } | |
| 53 | - /** | |
| 54 | - * Return an instance of this class. | |
| 55 | - * | |
| 56 | - * @return object A single instance of this class. | |
| 57 | - */ | |
| 58 | - public static function get_instance() { | |
| 59 | - // If the single instance hasn't been set, set it now. | |
| 60 | - if ( null == self::$instance ) { | |
| 61 | - self::$instance = new self; | |
| 62 | - } | |
| 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('wp_body_open', array( $this, 'hook_google_tag_manager_body' ),0); | |
| 67 | + add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility_hpos') ); | |
| 68 | + } | |
| 69 | + /** | |
| 70 | + * Return an instance of this class. | |
| 71 | + * | |
| 72 | + * @return object A single instance of this class. | |
| 73 | + */ | |
| 74 | + public static function get_instance() { | |
| 75 | + // If the single instance hasn't been set, set it now. | |
| 76 | + if ( null == self::$instance ) { | |
| 77 | + self::$instance = new self; | |
| 78 | + } | |
| 63 | 79 | |
| 64 | - return self::$instance; | |
| 65 | - } | |
| 80 | + return self::$instance; | |
| 81 | + } | |
| 66 | 82 | |
| 67 | - /** | |
| 68 | - * Get templates path. | |
| 69 | - * | |
| 70 | - * @return string | |
| 71 | - */ | |
| 72 | - public static function get_templates_path() { | |
| 73 | - return plugin_dir_path( __FILE__ ) . 'templates/'; | |
| 74 | - } | |
| 83 | + /** | |
| 84 | + * Get templates path. | |
| 85 | + * | |
| 86 | + * @return string | |
| 87 | + */ | |
| 88 | + public static function get_templates_path() { | |
| 89 | + return plugin_dir_path( __FILE__ ) . 'templates/'; | |
| 90 | + } | |
| 75 | 91 | |
| 76 | - /** | |
| 77 | - * Load the plugin text domain for translation. | |
| 78 | - */ | |
| 79 | - public function load_plugin_textdomain() { | |
| 80 | - load_plugin_textdomain( 'wp-tracking-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | |
| 81 | - } | |
| 92 | + /** | |
| 93 | + * Load the plugin text domain for translation. | |
| 94 | + */ | |
| 95 | + public function load_plugin_textdomain() { | |
| 96 | + load_plugin_textdomain( 'wp-tracking-codes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); | |
| 97 | + } | |
| 82 | 98 | |
| 83 | - /** | |
| 84 | - * Instalation Plugin | |
| 85 | - */ | |
| 86 | - public function wp_tracking_codes_install() { | |
| 87 | - // Trigger our function that registers the custom post type | |
| 88 | - pluginprefix_setup_post_types(); | |
| 89 | - // Clear the permalinks after the post type has been registered | |
| 90 | - flush_rewrite_rules(); | |
| 91 | - } | |
| 92 | - /** | |
| 93 | - * Includes | |
| 94 | - */ | |
| 95 | - private function includes() { | |
| 96 | - include_once 'includes/class-register-codes.php'; | |
| 97 | - include_once 'includes/class-render-data-layer-gtm.php'; | |
| 98 | - } | |
| 99 | - /** | |
| 100 | - * Debug | |
| 101 | - */ | |
| 102 | - public function log_me($message) { | |
| 103 | - if (WP_DEBUG === true) { | |
| 104 | - if (is_array($message) || is_object($message)) { | |
| 105 | - error_log(print_r($message, true)); | |
| 106 | - } else { | |
| 107 | - error_log($message); | |
| 108 | - } | |
| 109 | - } | |
| 110 | - } | |
| 111 | - public function hook_analytics(){ | |
| 112 | - $this->options = get_option( 'tracking_option' ); | |
| 113 | - if( empty( $this->options['tracking_option']['analytics'] ) && !empty( $this->options['analytics'] ) ) | |
| 114 | - if($analytics = $this->options['analytics']): | |
| 115 | - echo " | |
| 116 | - <!-- Google Analytics Tag --> | |
| 117 | - <script> | |
| 118 | - (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | |
| 119 | - (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | |
| 120 | - m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | |
| 121 | - })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
| 99 | + /** | |
| 100 | + * Instalation Plugin | |
| 101 | + */ | |
| 102 | + public function wp_tracking_codes_install() { | |
| 103 | + // Trigger our function that registers the custom post type | |
| 104 | + pluginprefix_setup_post_types(); | |
| 105 | + // Clear the permalinks after the post type has been registered | |
| 106 | + flush_rewrite_rules(); | |
| 107 | + } | |
| 108 | + /** | |
| 109 | + * Includes | |
| 110 | + */ | |
| 111 | + private function includes() { | |
| 112 | + include_once 'includes/class-register-codes.php'; | |
| 113 | + include_once 'includes/class-render-google-merchant.php'; | |
| 114 | + } | |
| 122 | 115 | |
| 123 | - ga('create', '$analytics', 'auto'); | |
| 124 | - ga('send', 'pageview'); | |
| 116 | + public function hook_analytics_4(){ | |
| 117 | + $this->options = get_option( 'tracking_option' ); | |
| 118 | + if( isset( $this->options['analytics_4'] ) && !empty( $this->options['analytics_4'] ) ){ | |
| 119 | + $analytics_4 = $this->options['analytics_4']; | |
| 120 | + printf(" | |
| 121 | + <!-- Global site tag (gtag.js) - Google Analytics --> | |
| 122 | + <script async src='https://www.googletagmanager.com/gtag/js?id=%s'></script> | |
| 123 | + <script> | |
| 124 | + window.dataLayer = window.dataLayer || []; | |
| 125 | + function gtag(){dataLayer.push(arguments);} | |
| 126 | + gtag('js', new Date()); | |
| 127 | + | |
| 128 | + gtag('config', '%s'); | |
| 129 | + </script> | |
| 130 | + ", esc_attr($analytics_4), esc_attr($analytics_4)); | |
| 131 | + } | |
| 125 | 132 | |
| 126 | - </script> | |
| 127 | - <!-- Google Analytics Tag --> | |
| 128 | - "; | |
| 129 | - endif; | |
| 130 | - } | |
| 131 | - public function hook_analytics_remarketing(){ | |
| 132 | - $this->options = get_option( 'tracking_option' ); | |
| 133 | - if( empty( $this->options['tracking_option']['analytics_remarketing'] ) && !empty( $this->options['analytics_remarketing'] ) ) | |
| 134 | - if($analytics_remarketing = $this->options['analytics_remarketing']): | |
| 135 | - echo ' | |
| 136 | - <!-- Google Remarketing Tag --> | |
| 137 | - <script type="text/javascript"> | |
| 138 | - /* <![CDATA[ */ | |
| 139 | - var google_conversion_id = '.$analytics_remarketing.'; | |
| 140 | - var google_custom_params = window.google_tag_params; | |
| 141 | - var google_remarketing_only = true; | |
| 142 | - /* ]]> */ | |
| 143 | - </script> | |
| 144 | - <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script> | |
| 145 | - <noscript> | |
| 146 | - <div style="display:inline;"> | |
| 147 | - <img height="1" width="1" style="border-style:none;" alt="" | |
| 148 | - src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/'.$analytics_remarketing.'/?value=0&guid=ON&script=0"/> | |
| 149 | - </div> | |
| 150 | - </noscript> | |
| 151 | - <!-- Google Remarketing Tag --> | |
| 152 | - '; | |
| 153 | - endif; | |
| 154 | - } | |
| 155 | - public function hook_facebook_pixel_code(){ | |
| 156 | - $this->options = get_option( 'tracking_option' ); | |
| 157 | - if( empty( $this->options['tracking_option']['facebook_pixel_code'] ) && !empty( $this->options['facebook_pixel_code'] ) ) | |
| 158 | - if($facebook_pixel_code = $this->options['facebook_pixel_code']): | |
| 159 | - echo " | |
| 160 | - <!-- Facebook Pixel Code --> | |
| 161 | - <script> | |
| 162 | - !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? | |
| 163 | - n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; | |
| 164 | - n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; | |
| 165 | - t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, | |
| 166 | - document,'script','https://connect.facebook.net/en_US/fbevents.js'); | |
| 133 | + } | |
| 167 | 134 | |
| 168 | - fbq('init', '$facebook_pixel_code'); | |
| 169 | - fbq('track', 'PageView');</script> | |
| 170 | - <noscript><img height='1' width='1' style='display:none' | |
| 171 | - src='https://www.facebook.com/tr?id=1556124141356092&ev=PageView&noscript=1' | |
| 172 | - /></noscript> | |
| 173 | - <!-- End Facebook Pixel Code --> | |
| 174 | - "; | |
| 175 | - endif; | |
| 176 | - } | |
| 135 | + public function hook_analytics_remarketing() { | |
| 136 | + $this->options = get_option( 'tracking_option' ); | |
| 137 | + if( isset( $this->options['analytics_remarketing'] ) && !empty( $this->options['analytics_remarketing'] ) ){ | |
| 138 | + $analytics_remarketing = $this->options['analytics_remarketing']; | |
| 139 | + printf(" | |
| 140 | + <!-- Global site tag (gtag.js) - Google Ads --> | |
| 141 | + <script async src='https://www.googletagmanager.com/gtag/js?id=%s'></script> | |
| 142 | + <script> | |
| 143 | + window.dataLayer = window.dataLayer || []; | |
| 144 | + function gtag(){dataLayer.push(arguments);} | |
| 145 | + gtag('js', new Date()); | |
| 146 | + | |
| 147 | + gtag('config', '%s'); | |
| 148 | + </script> | |
| 149 | + ", esc_attr($analytics_remarketing), esc_attr($analytics_remarketing)); | |
| 150 | + } | |
| 151 | + } | |
| 177 | 152 | |
| 178 | - public function custom_include($template) { | |
| 179 | - ob_start(); | |
| 180 | - return $template; | |
| 181 | - } | |
| 153 | + public function hook_facebook_pixel_code() { | |
| 154 | + $this->options = get_option( 'tracking_option' ); | |
| 155 | + if ( isset( $this->options['facebook_pixel_code'] ) && ! empty( $this->options['facebook_pixel_code'] ) ){ | |
| 156 | + $facebook_pixel_code = $this->options['facebook_pixel_code']; | |
| 157 | + printf( " | |
| 158 | + <!-- Facebook Pixel Code --> | |
| 159 | + <script> | |
| 160 | + !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? | |
| 161 | + n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; | |
| 162 | + n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; | |
| 163 | + t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, | |
| 164 | + document,'script','https://connect.facebook.net/en_US/fbevents.js'); | |
| 165 | + | |
| 166 | + fbq('init', '%1\$s'); | |
| 167 | + fbq('track', 'PageView');</script> | |
| 168 | + <noscript><img height='1' width='1' style='display:none' | |
| 169 | + src='https://www.facebook.com/tr?id=%1\$s&ev=PageView&noscript=1' | |
| 170 | + /></noscript> | |
| 171 | + <!-- End Facebook Pixel Code --> | |
| 172 | + ", esc_attr($facebook_pixel_code) ); | |
| 173 | + } | |
| 174 | + } | |
| 182 | 175 | |
| 183 | - public function hook_google_tag_manager_head(){ | |
| 184 | - $this->options = get_option('tracking_option'); | |
| 185 | - if (empty($this->options['tracking_option']['google_tag_manager']) && !empty($this->options['google_tag_manager'])) | |
| 186 | - if ($google_tag_manager = $this->options['google_tag_manager']) : | |
| 187 | - echo " | |
| 188 | - <!-- Google Tag Manager --> | |
| 176 | + public function hook_google_tag_manager_head(){ | |
| 177 | + $this->options = get_option('tracking_option'); | |
| 178 | + if ( isset( $this->options['google_tag_manager'] ) && !empty($this->options['google_tag_manager'])){ | |
| 179 | + $google_tag_manager = $this->options['google_tag_manager']; | |
| 180 | + printf( | |
| 181 | + "<!-- Google Tag Manager --> | |
| 189 | 182 | <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| 190 | 183 | new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| 191 | 184 | j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 192 | 185 | 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| 193 | - })(window,document,'script','dataLayer','$google_tag_manager');</script> | |
| 194 | - <!-- End Google Tag Manager --> | |
| 195 | - "; | |
| 196 | - endif; | |
| 197 | - } | |
| 186 | + })(window,document,'script','dataLayer','%s');</script> | |
| 187 | + <!-- End Google Tag Manager -->", | |
| 188 | + esc_attr( $google_tag_manager ) | |
| 189 | + ); | |
| 190 | + } | |
| 198 | 191 | |
| 199 | - public function hook_google_tag_manager_body($classes){ | |
| 200 | - $this->options = get_option( 'tracking_option' ); | |
| 201 | - if( empty( $this->options['tracking_option']['google_tag_manager'] ) && !empty( $this->options['google_tag_manager'] ) ) | |
| 202 | - if($google_tag_manager = $this->options['google_tag_manager']): | |
| 203 | - $content = ob_get_clean(); | |
| 204 | - $code_tag = " | |
| 205 | - <!-- Google Tag Manager (noscript) --> | |
| 206 | - <noscript><iframe src='https://www.googletagmanager.com/ns.html?id=$google_tag_manager' | |
| 207 | - height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript> | |
| 208 | - <!-- End Google Tag Manager (noscript) --> | |
| 209 | - "; | |
| 210 | - $content = preg_replace('#<body([^>]*)>#i',"<body$1>{$code_tag}",$content); | |
| 211 | - echo $content; | |
| 212 | - endif; | |
| 192 | + } | |
| 193 | + | |
| 194 | + public function hook_google_tag_manager_body($classes){ | |
| 195 | + $this->options = get_option( 'tracking_option' ); | |
| 196 | + if( isset( $this->options['google_tag_manager'] ) && !empty( $this->options['google_tag_manager'] ) ){ | |
| 197 | + $google_tag_manager = $this->options['google_tag_manager']; | |
| 198 | + printf("<!-- Google Tag Manager (noscript) --> | |
| 199 | +<noscript><iframe src='https://www.googletagmanager.com/ns.html?id=%s' height='0' width='0' style='display:none;visibility:hidden'></iframe></noscript> | |
| 200 | +<!-- End Google Tag Manager (noscript) -->", | |
| 201 | + esc_attr( $google_tag_manager ) | |
| 202 | + ); | |
| 203 | + } | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * Declare_compatibility_hpos | |
| 208 | + */ | |
| 209 | + public function declare_compatibility_hpos() { | |
| 210 | + if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { | |
| 211 | + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', 'wp-tracking-codes/wp-tracking-codes.php', true ); | |
| 212 | + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', 'wp-tracking-codes/wp-tracking-codes.php', true ); | |
| 213 | + } | |
| 213 | 214 | } |
| 214 | - } | |
| 215 | - add_action( 'plugins_loaded', array( 'Wp_Tracking_Codes', 'get_instance' ) ); | |
| 216 | -endif; | |
| 217 | -//register_deactivation_hook ( __FILE__ , 'pluginprefix_function_to_run' ); | |
| 215 | + } | |
| 216 | + add_action( 'plugins_loaded', array( 'Wp_Tracking_Codes', 'get_instance' ) ); | |
| 217 | +endif; | |