/includes/frontend/frontend.php

  1. <?php 
  2. /** 
  3. * Frontend events tracking. 
  4. * 
  5. * @since 6.0.0 
  6. * 
  7. * @package MonsterInsights 
  8. * @author Chris Christoff 
  9. */ 
  10.  
  11. // Exit if accessed directly 
  12. if ( ! defined( 'ABSPATH' ) ) { 
  13. exit; 
  14.  
  15.  
  16. /** 
  17. * Get frontend tracking options. 
  18. * 
  19. * This function is used to return an array of parameters 
  20. * for the frontend_output() function to output. These are  
  21. * generally dimensions and turned on GA features. 
  22. * 
  23. * @since 6.0.0 
  24. * @access public 
  25. * 
  26. * @return array Array of the options to use. 
  27. */ 
  28. function monsterinsights_tracking_script( ) { 
  29.  
  30. $tracking_mode = monsterinsights_get_option( 'tracking_mode', 'analytics' ); 
  31. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/class-tracking-abstract.php'; 
  32. $mode = ''; 
  33.  
  34. if ( is_preview() ) { 
  35. $mode = 'preview'; 
  36. } else if ( monsterinsights_disabled_user_group() ) { 
  37. $mode = 'disabled'; 
  38. } else if ( $tracking_mode === 'analytics' ) { 
  39. $mode = 'analytics'; 
  40. } else if ( $tracking_mode === 'ga' ) { 
  41. $mode = 'ga'; 
  42. } else { 
  43. //$mode = apply_filters( 'monsterinsights_custom_tracking_name', 'name-of-method' ); 
  44.  
  45. do_action( 'monsterinsights_tracking_before_' . $mode ); 
  46. do_action( 'monsterinsights_tracking_before', $mode ); 
  47.  
  48. switch ( $mode ) { 
  49. case 'preview': 
  50. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-preview.php'; 
  51. $tracking = new MonsterInsights_Tracking_Preview(); 
  52. echo $tracking->frontend_output(); 
  53. break; 
  54.  
  55. case 'disabled': 
  56. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-disabled.php'; 
  57. $tracking = new MonsterInsights_Tracking_Disabled(); 
  58. echo $tracking->frontend_output(); 
  59. break; 
  60.  
  61. case 'analytics': 
  62. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-analytics.php'; 
  63. $tracking = new MonsterInsights_Tracking_Analytics(); 
  64. echo $tracking->frontend_output(); 
  65. break; 
  66.  
  67. case 'ga': 
  68. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/tracking/class-tracking-ga.php'; 
  69. $tracking = new MonsterInsights_Tracking_GA(); 
  70. echo $tracking->frontend_output(); 
  71. break; 
  72.  
  73. default: 
  74. //do_action( 'monsterinsights_custom_tracking' ); 
  75. break; 
  76.  
  77. do_action( 'monsterinsights_tracking_after_' . $mode ); 
  78. do_action( 'monsterinsights_tracking_after', $mode ); 
  79. add_action( 'wp_head', 'monsterinsights_tracking_script', 8 ); 
  80. //add_action( 'login_head', 'monsterinsights_tracking_script', 8 ); 
  81.  
  82. /** 
  83. * Get frontend tracking options. 
  84. * 
  85. * This function is used to return an array of parameters 
  86. * for the frontend_output() function to output. These are  
  87. * generally dimensions and turned on GA features. 
  88. * 
  89. * @since 6.0.0 
  90. * @access public 
  91. * 
  92. * @return array Array of the options to use. 
  93. */ 
  94. function monsterinsights_events_tracking( ) { 
  95. $events_mode = monsterinsights_get_option( 'events_mode', false ); 
  96. $tracking_mode = monsterinsights_get_option( 'tracking_mode', false ); 
  97. $disabled_user = monsterinsights_disabled_user_group(); 
  98.  
  99. if ( ! $disabled_user && $events_mode === 'php' && ( $tracking_mode === 'ga' || $tracking_mode === 'analytics' ) ) { 
  100. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/events/class-events-php.php'; 
  101. new MonsterInsights_Events_PHP(); 
  102. } else if ( ! $disabled_user && $events_mode === 'js' && $tracking_mode === 'analytics' ) { 
  103. require_once plugin_dir_path( MONSTERINSIGHTS_PLUGIN_FILE ) . 'includes/frontend/events/class-events-js.php'; 
  104. new MonsterInsights_Events_JS(); 
  105. } else { 
  106. // User is in the disabled group or events mode is off 
  107. add_action( 'template_redirect', 'monsterinsights_events_tracking', 9 ); 
  108. //add_action( 'login_head', 'monsterinsights_events_tracking', 8 ); 
  109.  
  110. function monsterinsights_disabled_user_group( ) { 
  111. $user = wp_get_current_user(); 
  112. $disabled = false; 
  113.  
  114. $roles = monsterinsights_get_option( 'ignore_users', array() ); 
  115.  
  116. if ( ! empty( $roles ) && is_array( $roles ) ) { 
  117. foreach ( $roles as $role ) { 
  118. if ( is_string( $role ) ) { 
  119. if ( current_user_can( $role ) ) { 
  120. $disabled = true; 
  121. break; 
  122.  
  123. $track_super_admin = apply_filters( 'monsterinsights_track_super_admins', false ); 
  124. if ( $track_super_admin === false && is_super_admin() ) { 
  125. $disabled = true; 
  126.  
  127. // or if UA code is not entered 
  128. $ua_code = monsterinsights_get_ua(); 
  129. if ( empty( $ua_code ) ) { 
  130. $disabled = true; 
  131.  
  132. return apply_filters( 'monsterinsights_disabled_user_group', $disabled, $user ); 
  133.  
  134. /** 
  135. * Add the UTM source parameters in the RSS feeds to track traffic. 
  136. * 
  137. * @since 6.0.0 
  138. * @access public 
  139. * 
  140. * @param string $guid The link for the RSS feed. 
  141. * 
  142. * @return string The new link for the RSS feed. 
  143. */ 
  144. function monsterinsights_rss_link_tagger( $guid ) { 
  145. global $post; 
  146.  
  147. if ( monsterinsights_get_option( 'tag_links_in_rss', false ) ) { 
  148. if ( is_feed() ) { 
  149. if ( monsterinsights_get_option( 'allow_anchor', false ) ) { 
  150. $delimiter = '#'; 
  151. } else { 
  152. $delimiter = '?'; 
  153. if ( strpos( $guid, $delimiter ) > 0 ) { 
  154. $delimiter = '&'; 
  155. return $guid . $delimiter . 'utm_source=rss&utm_medium=rss&utm_campaign=' . urlencode( $post->post_name ); 
  156. return $guid; 
  157. add_filter( 'the_permalink_rss', 'monsterinsights_rss_link_tagger', 99 ); 
.