PageRenderTime 57ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/plugins/google-analytics-dashboard-for-wp/includes/frontend/frontend.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 399 lines | 268 code | 51 blank | 80 comment | 31 complexity | 3e8e66ccb8512fdda7978c5bdc72c4e5 MD5 | raw file
  1. <?php
  2. /**
  3. * Frontend events tracking.
  4. *
  5. * @since 6.0.0
  6. *
  7. * @package ExactMetrics
  8. * @author Chris Christoff
  9. */
  10. // Exit if accessed directly
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. /**
  15. * Get frontend tracking options.
  16. *
  17. * This function is used to return an array of parameters
  18. * for the frontend_output() function to output. These are
  19. * generally dimensions and turned on GA features.
  20. *
  21. * @since 7.0.0
  22. * @access public
  23. *
  24. * @return array Array of the options to use.
  25. */
  26. function exactmetrics_tracking_script( ) {
  27. require_once plugin_dir_path( EXACTMETRICS_PLUGIN_FILE ) . 'includes/frontend/class-tracking-abstract.php';
  28. $mode = is_preview() ? 'preview' : 'analytics';
  29. do_action( 'exactmetrics_tracking_before_' . $mode );
  30. do_action( 'exactmetrics_tracking_before', $mode );
  31. if ( $mode === 'preview' ) {
  32. require_once plugin_dir_path( EXACTMETRICS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-preview.php';
  33. $tracking = new ExactMetrics_Tracking_Preview();
  34. echo $tracking->frontend_output();
  35. } else {
  36. require_once plugin_dir_path( EXACTMETRICS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-analytics.php';
  37. $tracking = new ExactMetrics_Tracking_Analytics();
  38. echo $tracking->frontend_output();
  39. }
  40. do_action( 'exactmetrics_tracking_after_' . $mode );
  41. do_action( 'exactmetrics_tracking_after', $mode );
  42. }
  43. add_action( 'wp_head', 'exactmetrics_tracking_script', 6 );
  44. //add_action( 'login_head', 'exactmetrics_tracking_script', 6 );
  45. /**
  46. * Get frontend tracking options.
  47. *
  48. * This function is used to return an array of parameters
  49. * for the frontend_output() function to output. These are
  50. * generally dimensions and turned on GA features.
  51. *
  52. * @since 6.0.0
  53. * @access public
  54. *
  55. * @return array Array of the options to use.
  56. */
  57. function exactmetrics_events_tracking( ) {
  58. $track_user = exactmetrics_track_user();
  59. if ( $track_user ) {
  60. require_once plugin_dir_path( EXACTMETRICS_PLUGIN_FILE ) . 'includes/frontend/events/class-analytics-events.php';
  61. new ExactMetrics_Analytics_Events();
  62. } else {
  63. // User is in the disabled group or events mode is off
  64. }
  65. }
  66. add_action( 'template_redirect', 'exactmetrics_events_tracking', 9 );
  67. /**
  68. * Add the UTM source parameters in the RSS feeds to track traffic.
  69. *
  70. * @since 6.0.0
  71. * @access public
  72. *
  73. * @param string $guid The link for the RSS feed.
  74. *
  75. * @return string The new link for the RSS feed.
  76. */
  77. function exactmetrics_rss_link_tagger( $guid ) {
  78. global $post;
  79. if ( exactmetrics_get_option( 'tag_links_in_rss', false ) ){
  80. if ( is_feed() ) {
  81. if ( exactmetrics_get_option( 'allow_anchor', false ) ) {
  82. $delimiter = '#';
  83. } else {
  84. $delimiter = '?';
  85. if ( strpos( $guid, $delimiter ) > 0 ) {
  86. $delimiter = '&amp;';
  87. }
  88. }
  89. return $guid . $delimiter . 'utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=' . urlencode( $post->post_name );
  90. }
  91. }
  92. return $guid;
  93. }
  94. add_filter( 'the_permalink_rss', 'exactmetrics_rss_link_tagger', 99 );
  95. /**
  96. * Checks used for loading the frontend scripts/admin bar button.
  97. */
  98. function exactmetrics_prevent_loading_frontend_reports() {
  99. return ! current_user_can( 'exactmetrics_view_dashboard' ) || exactmetrics_get_option( 'hide_admin_bar_reports' ) || function_exists( 'exactmetrics_is_reports_page' ) && exactmetrics_is_reports_page();
  100. }
  101. /**
  102. * Add an admin bar menu item on the frontend.
  103. *
  104. * @since 7.5.0
  105. *
  106. * @return void
  107. */
  108. function exactmetrics_add_admin_bar_menu() {
  109. if ( exactmetrics_prevent_loading_frontend_reports() ) {
  110. return;
  111. }
  112. global $wp_admin_bar;
  113. $args = array(
  114. 'id' => 'exactmetrics_frontend_button',
  115. 'title' => '<span class="ab-icon dashicons-before dashicons-chart-bar"></span> ExactMetrics', // Maybe allow translation?
  116. 'href' => '#',
  117. );
  118. if ( method_exists( $wp_admin_bar, 'add_menu' ) ) {
  119. $wp_admin_bar->add_menu( $args );
  120. }
  121. }
  122. add_action( 'admin_bar_menu', 'exactmetrics_add_admin_bar_menu', 999 );
  123. /**
  124. * Load the scripts needed for the admin bar.
  125. *
  126. * @since 7.5.0
  127. *
  128. * @return void
  129. */
  130. function exactmetrics_frontend_admin_bar_scripts() {
  131. if ( exactmetrics_prevent_loading_frontend_reports() ) {
  132. return;
  133. }
  134. $version_path = exactmetrics_is_pro_version() ? 'pro' : 'lite';
  135. $rtl = is_rtl() ? '.rtl' : '';
  136. $frontend_js_url = defined( 'EXACTMETRICS_LOCAL_FRONTEND_JS_URL' ) && EXACTMETRICS_LOCAL_FRONTEND_JS_URL ? EXACTMETRICS_LOCAL_FRONTEND_JS_URL : plugins_url( $version_path . '/assets/vue/js/frontend.js', EXACTMETRICS_PLUGIN_FILE );
  137. if ( ! defined( 'EXACTMETRICS_LOCAL_FRONTEND_JS_URL' ) ) {
  138. wp_enqueue_style( 'exactmetrics-vue-frontend-style', plugins_url( $version_path . '/assets/vue/css/frontend' . $rtl . '.css', EXACTMETRICS_PLUGIN_FILE ), array(), exactmetrics_get_asset_version() );
  139. wp_enqueue_script( 'exactmetrics-vue-vendors', plugins_url( $version_path . '/assets/vue/js/chunk-frontend-vendors.js', EXACTMETRICS_PLUGIN_FILE ), array(), exactmetrics_get_asset_version(), true );
  140. wp_enqueue_script( 'exactmetrics-vue-common', plugins_url( $version_path . '/assets/vue/js/chunk-common.js', EXACTMETRICS_PLUGIN_FILE ), array(), exactmetrics_get_asset_version(), true );
  141. } else {
  142. wp_enqueue_script( 'exactmetrics-vue-vendors', EXACTMETRICS_LOCAL_VENDORS_JS_URL, array(), exactmetrics_get_asset_version(), true );
  143. wp_enqueue_script( 'exactmetrics-vue-common', EXACTMETRICS_LOCAL_COMMON_JS_URL, array(), exactmetrics_get_asset_version(), true );
  144. }
  145. wp_register_script( 'exactmetrics-vue-frontend', $frontend_js_url, array(), exactmetrics_get_asset_version(), true );
  146. wp_enqueue_script( 'exactmetrics-vue-frontend' );
  147. $page_title = is_singular() ? get_the_title() : exactmetrics_get_page_title();
  148. // We do not have a current auth.
  149. $site_auth = ExactMetrics()->auth->get_viewname();
  150. $ms_auth = is_multisite() && ExactMetrics()->auth->get_network_viewname();
  151. // Check if any of the other admin scripts are enqueued, if so, use their object.
  152. if ( ! wp_script_is( 'exactmetrics-vue-script' ) && ! wp_script_is( 'exactmetrics-vue-reports' ) && ! wp_script_is( 'exactmetrics-vue-widget' ) ) {
  153. wp_localize_script(
  154. 'exactmetrics-vue-frontend',
  155. 'exactmetrics',
  156. array(
  157. 'ajax' => admin_url( 'admin-ajax.php' ),
  158. 'nonce' => wp_create_nonce( 'mi-admin-nonce' ),
  159. 'network' => is_network_admin(),
  160. 'translations' => wp_get_jed_locale_data( exactmetrics_is_pro_version() ? 'exactmetrics-premium' : 'google-analytics-dashboard-for-wp' ),
  161. 'assets' => plugins_url( $version_path . '/assets/vue', EXACTMETRICS_PLUGIN_FILE ),
  162. 'addons_url' => is_multisite() ? network_admin_url( 'admin.php?page=exactmetrics_network#/addons' ) : admin_url( 'admin.php?page=exactmetrics_settings#/addons' ),
  163. 'page_id' => is_singular() ? get_the_ID() : false,
  164. 'page_title' => $page_title,
  165. 'plugin_version' => EXACTMETRICS_VERSION,
  166. 'shareasale_id' => exactmetrics_get_shareasale_id(),
  167. 'shareasale_url' => exactmetrics_get_shareasale_url( exactmetrics_get_shareasale_id(), '' ),
  168. 'is_admin' => is_admin(),
  169. 'reports_url' => add_query_arg( 'page', 'exactmetrics_reports', admin_url( 'admin.php' ) ),
  170. 'authed' => $site_auth || $ms_auth,
  171. 'getting_started_url' => is_multisite() ? network_admin_url( 'admin.php?page=exactmetrics_network#/about/getting-started' ) : admin_url( 'admin.php?page=exactmetrics_settings#/about/getting-started' ),
  172. 'wizard_url' => admin_url( 'index.php?page=exactmetrics-onboarding' ),
  173. )
  174. );
  175. }
  176. }
  177. add_action( 'wp_enqueue_scripts', 'exactmetrics_frontend_admin_bar_scripts' );
  178. add_action( 'admin_enqueue_scripts', 'exactmetrics_frontend_admin_bar_scripts', 1005 );
  179. /**
  180. * Load the tracking notice for logged in users.
  181. */
  182. function exactmetrics_administrator_tracking_notice() {
  183. // Don't do anything for guests.
  184. if ( ! is_user_logged_in() ) {
  185. return;
  186. }
  187. // Only show this to users who are not tracked.
  188. if ( exactmetrics_track_user() ) {
  189. return;
  190. }
  191. // Only show when tracking.
  192. $ua = exactmetrics_get_ua();
  193. if ( empty( $ua ) ) {
  194. return;
  195. }
  196. // Don't show if already dismissed.
  197. if ( get_option( 'exactmetrics_frontend_tracking_notice_viewed', false ) ) {
  198. return;
  199. }
  200. // Automatically dismiss when loaded.
  201. update_option( 'exactmetrics_frontend_tracking_notice_viewed', 1 );
  202. ?>
  203. <div class="exactmetrics-tracking-notice exactmetrics-tracking-notice-hide">
  204. <div class="exactmetrics-tracking-notice-icon">
  205. <img src="<?php echo esc_url( plugins_url( 'assets/images/em-mascot.png', EXACTMETRICS_PLUGIN_FILE ) ); ?>" width="40" alt="ExactMetrics Mascot" />
  206. </div>
  207. <div class="exactmetrics-tracking-notice-text">
  208. <h3><?php esc_html_e( 'Tracking is Disabled for Administrators', 'exactmetrics-premium' ); ?></h3>
  209. <p>
  210. <?php
  211. $doc_url = 'https://exactmetrics.com/docs/tracking-disabled-administrators-editors';
  212. $doc_url = add_query_arg( array(
  213. 'utm_source' => exactmetrics_is_pro_version() ? 'proplugin' : 'liteplugin',
  214. 'utm_medium' => 'frontend-notice',
  215. 'utm_campaign' => 'admin-tracking-doc',
  216. ), $doc_url );
  217. // Translators: %s is the link to the article where more details about tracking are listed.
  218. printf( esc_html__( 'To keep stats accurate, we do not load Google Analytics scripts for admin users. %1$sLearn More &raquo;%2$s', 'exactmetrics-premium' ), '<a href="' . esc_url( $doc_url ) . '" target="_blank">', '</a>' );
  219. ?>
  220. </p>
  221. </div>
  222. <div class="exactmetrics-tracking-notice-close">&times;</div>
  223. </div>
  224. <style type="text/css">
  225. .exactmetrics-tracking-notice {
  226. position: fixed;
  227. bottom: 20px;
  228. right: 15px;
  229. font-family: Arial, Helvetica, "Trebuchet MS", sans-serif;
  230. background: #fff;
  231. box-shadow: 0 0 10px 0 #dedede;
  232. padding: 6px 5px;
  233. display: flex;
  234. align-items: center;
  235. justify-content: center;
  236. width: 380px;
  237. max-width: calc( 100% - 30px );
  238. border-radius: 6px;
  239. transition: bottom 700ms ease;
  240. z-index: 10000;
  241. }
  242. .exactmetrics-tracking-notice h3 {
  243. font-size: 13px;
  244. color: #222;
  245. font-weight: 700;
  246. margin: 0 0 8px;
  247. padding: 0;
  248. line-height: 1;
  249. border: none;
  250. }
  251. .exactmetrics-tracking-notice p {
  252. font-size: 13px;
  253. color: #7f7f7f;
  254. font-weight: 400;
  255. margin: 0;
  256. padding: 0;
  257. line-height: 1.2;
  258. border: none;
  259. }
  260. .exactmetrics-tracking-notice p a {
  261. color: #7f7f7f;
  262. font-size: 13px;
  263. line-height: 1.2;
  264. margin: 0;
  265. padding: 0;
  266. text-decoration: underline;
  267. font-weight: 400;
  268. }
  269. .exactmetrics-tracking-notice p a:hover {
  270. color: #7f7f7f;
  271. text-decoration: none;
  272. }
  273. .exactmetrics-tracking-notice-icon img {
  274. height: auto;
  275. display: block;
  276. margin: 0;
  277. }
  278. .exactmetrics-tracking-notice-icon {
  279. padding: 14px;
  280. background-color: #f4f3f7;
  281. border-radius: 6px;
  282. flex-grow: 0;
  283. flex-shrink: 0;
  284. margin-right: 12px;
  285. }
  286. .exactmetrics-tracking-notice-close {
  287. padding: 0;
  288. margin: 0 3px 0 0;
  289. border: none;
  290. box-shadow: none;
  291. border-radius: 0;
  292. color: #7f7f7f;
  293. background: transparent;
  294. line-height: 1;
  295. align-self: flex-start;
  296. cursor: pointer;
  297. font-weight: 400;
  298. }
  299. .exactmetrics-tracking-notice.exactmetrics-tracking-notice-hide {
  300. bottom: -200px;
  301. }
  302. </style>
  303. <?php
  304. if ( ! wp_script_is( 'jquery', 'queue' ) ) {
  305. wp_enqueue_script( 'jquery' );
  306. }
  307. ?>
  308. <script>
  309. if ( 'undefined' !== typeof jQuery ) {
  310. jQuery( document ).ready( function ( $ ) {
  311. /* Don't show the notice if we don't have a way to hide it (no js, no jQuery). */
  312. $( document.querySelector( '.exactmetrics-tracking-notice' ) ).removeClass( 'exactmetrics-tracking-notice-hide' );
  313. $( document.querySelector( '.exactmetrics-tracking-notice-close' ) ).on( 'click', function ( e ) {
  314. e.preventDefault();
  315. $( this ).closest( '.exactmetrics-tracking-notice' ).addClass( 'exactmetrics-tracking-notice-hide' );
  316. $.ajax( {
  317. url: '<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>',
  318. method: 'POST',
  319. data: {
  320. action: 'exactmetrics_dismiss_tracking_notice',
  321. nonce: '<?php echo esc_js( wp_create_nonce( 'exactmetrics-tracking-notice' ) ); ?>',
  322. }
  323. } );
  324. } );
  325. } );
  326. }
  327. </script>
  328. <?php
  329. }
  330. add_action( 'wp_footer', 'exactmetrics_administrator_tracking_notice', 300 );
  331. /**
  332. * Ajax handler to hide the tracking notice.
  333. */
  334. function exactmetrics_dismiss_tracking_notice() {
  335. check_ajax_referer( 'exactmetrics-tracking-notice', 'nonce' );
  336. update_option( 'exactmetrics_frontend_tracking_notice_viewed', 1 );
  337. wp_die();
  338. }
  339. add_action( 'wp_ajax_exactmetrics_dismiss_tracking_notice', 'exactmetrics_dismiss_tracking_notice' );
  340. /**
  341. * If the legacy shortcodes are not registered, make sure they don't output.
  342. */
  343. function exactmetrics_maybe_handle_legacy_shortcodes() {
  344. if ( ! shortcode_exists( 'gadwp_useroptout' ) ) {
  345. add_shortcode( 'gadwp_useroptout', '__return_empty_string' );
  346. }
  347. }
  348. add_action( 'init', 'exactmetrics_maybe_handle_legacy_shortcodes', 1000 );