MonsterInsights_Link

The Google Analytics for WordPress by MonsterInsights MonsterInsights Link class.

Defined (1)

The class is defined in the following location(s).

/includes/frontend/events/class-link.php  
  1. class MonsterInsights_Link { 
  2. /** 
  3. * Holds the base class object. 
  4. * @since 6.0.0 
  5. * @access public 
  6. * @var object $base Base class object. 
  7. */ 
  8. public $base; 
  9.  
  10. /** 
  11. * The category of the link. 
  12. * @since 6.0.0 
  13. * @access public 
  14. *  
  15. * @var string $category Category of the link. 
  16. */ 
  17. public $category; 
  18.  
  19. /** 
  20. * Get the domain and host. False if empty. 
  21. * @since 6.0.0 
  22. * @access public 
  23. *  
  24. * @var array|bool $domain Domain of link. 
  25. */ 
  26. public $domain; 
  27.  
  28. /** 
  29. * Extension of the url. 
  30. * @since 6.0.0 
  31. * @access public 
  32. *  
  33. * @var string $extension File extension in given url. 
  34. */ 
  35. public $extension; 
  36.  
  37. /** 
  38. * Host of the URL. 
  39. * @since 6.0.0 
  40. * @access public 
  41. * @var string $host Host of given url. 
  42. */ 
  43. public $host; 
  44.  
  45. /** 
  46. * The link attributes of the URL. 
  47. * @since 6.0.0 
  48. * @access public 
  49. * @var string $link_attributes Link attributes of given hyperlink. 
  50. */ 
  51. public $link_attributes; 
  52.  
  53. /** 
  54. * The text of the link. 
  55. * @since 6.0.0 
  56. * @access public 
  57. * @var string $link_text Text of given hyperlink. 
  58. */ 
  59. public $link_text; 
  60.  
  61. /** 
  62. * The full url without the protocol. 
  63. * @since 6.0.0 
  64. * @access public 
  65. * @var string $original_url The full url without the protocol. 
  66. */ 
  67. public $original_url; 
  68.  
  69. /** 
  70. * The protocol of the url. 
  71. * @since 6.0.0 
  72. * @access public 
  73. * @var string $protocol The protocol of the link. 
  74. */ 
  75. public $protocol; 
  76.  
  77. /** 
  78. * The type of the URL - for example: internal as outbound, outbound, internal. 
  79. * @since 6.0.0 
  80. * @access public 
  81. * @var string $type The type of the link. 
  82. */ 
  83. public $type; 
  84.  
  85. /** 
  86. * The full hyperlink. 
  87. * @since 6.0.0 
  88. * @access public 
  89. * @var string $hyperlink The hyperlink. 
  90. */ 
  91. public $hyperlink; 
  92.  
  93. /** 
  94. * Constructor of the class. 
  95. * @since 6.0.0 
  96. * @access public 
  97. * @param MonsterInsights $base The base plugin object. 
  98. * @param string $category The category of the url (ex: outbound-link). 
  99. * @param array $matches Matches found for the hyperlink. 
  100. */ 
  101. public function __construct( $base, $category, $matches ) { 
  102. $this->base = $base; 
  103. $this->category = $category; 
  104. $this->original_url = $matches[5]; 
  105. $this->domain = $this->get_domain(); 
  106. $this->extension = substr( strrchr( $this->original_url, '.' ), 1 ); 
  107. $this->host = $this->domain['host']; 
  108. $this->link_attributes = trim( $matches[1] . ' ' . $matches[6] ); 
  109. $this->link_text = $matches[7]; 
  110. $this->hyperlink = $matches[0]; 
  111. $this->protocol = $matches[4]; 
  112. $this->type = $this->get_target_type(); 
  113. /** 
  114. * Parse the domain. 
  115. * @since 6.0.0 
  116. * @access public 
  117. *  
  118. * @return array|bool The domain/host of the link. 
  119. */ 
  120. public function get_domain() { 
  121. $hostPattern = '/^(https?:\/\/)?([^\/]+)/i'; 
  122. $domainPatternUS = '/[^\.\/]+\.[^\.\/]+$/'; 
  123. $domainPatternUK = '/[^\.\/]+\.[^\.\/]+\.[^\.\/]+$/'; 
  124. $matching = preg_match( $hostPattern, $this->original_url, $matches ); 
  125. if ( $matching ) { 
  126. $host = $matches[2]; 
  127. if ( preg_match( '/.*\..*\..*\..*$/', $host ) ) { 
  128. preg_match( $domainPatternUK, $host, $matches ); 
  129. } else { 
  130. preg_match( $domainPatternUS, $host, $matches ); 
  131.  
  132. if ( isset( $matches[0] ) ) { 
  133. return array( 'domain' => $matches[0], 'host' => $host ); 
  134. return false; 
  135.  
  136. /** 
  137. * Getting the type for current target. 
  138. * @since 6.0.0 
  139. * @access protected 
  140. *  
  141. * @return string The type of link. 
  142. */ 
  143. protected function get_target_type() { 
  144. $download_extensions = explode( ', ', str_replace( '.', '', monsterinsights_get_option( 'extensions_of_files', '' ) ) ); 
  145. $download_extensions = array_map( 'trim', $download_extensions ); 
  146. $full_url = $this->protocol . '://' . $this->domain['domain']; 
  147. if ( $this->protocol == 'mailto' ) { 
  148. $type = 'email'; 
  149. } else if ( in_array( $this->extension, $download_extensions ) ) { 
  150. $type = 'download'; 
  151. } else if ( in_array( $this->protocol, array( 'http', 'https') ) && $full_url !== rtrim( home_url(), '\/' ) ) { 
  152. $type = 'outbound'; 
  153. } else { 
  154. $type = $this->parse_internal_link_type(); 
  155. return $type; 
  156.  
  157. /** 
  158. * Parse the type for outbound links. 
  159. * @since 6.0.0 
  160. * @access protected 
  161. * @return string The type of link. 
  162. */ 
  163. protected function parse_internal_link_type() { 
  164. $out_links = explode( ', ', monsterinsights_get_option( 'track_internal_as_outbound', '' ) ); 
  165. $out_links = array_unique( array_map( 'trim', $out_links ) ); 
  166. if ( ! empty( $this->original_url ) && count( $out_links ) >= 1 ) { 
  167. foreach ( $out_links as $out ) { 
  168. if ( ! empty( $out ) && strpos( $this->original_url, $out ) !== false ) { 
  169. return 'internal-as-outbound'; 
  170. return 'internal';