Skip to content

Commit

Permalink
Merge pull request #125 from pluginkollektiv/issue/112-refine-user-ag…
Browse files Browse the repository at this point in the history
…ent-check

Ticket #112 - Enhanced bot detection.
  • Loading branch information
Zodiac1978 committed Mar 22, 2019
2 parents b577957 + 03e5ee5 commit c1161a4
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion inc/class-statify-frontend.php
Expand Up @@ -165,7 +165,7 @@ private static function _skip_tracking() {
);
if ( is_null( $user_agent )
|| false === $user_agent
|| ! preg_match( '/(?:Windows|Macintosh|Linux|iPhone|iPad)/', $user_agent ) ) {
|| self::is_bot( $user_agent ) ) {
return true;
}

Expand All @@ -188,6 +188,37 @@ private static function _skip_tracking() {
return self::_is_internal();
}

/**
* Checks if user agent is a bot.
*
* @since 1.7.0
*
* @param string $user_agent Server user agent string.
*
* @return boolean $is_bot TRUE if user agent is a bot, FALSE if not.
*/
private static function is_bot( $user_agent ) {
$user_agent = strtolower( $user_agent );

$identifiers = array(
'bot',
'slurp',
'crawler',
'spider',
'curl',
'facebook',
'fetch',
);

foreach ( $identifiers as $identifier ) {
if ( strpos( $user_agent, $identifier ) !== true ) {
return true;
}
}

return false;
}

/**
* Rules to detect internal calls to skip tracking and not print code snippet.
*
Expand Down

0 comments on commit c1161a4

Please sign in to comment.