Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket #112 - Enhanced bot detection. #125

Merged
merged 4 commits into from
Mar 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion inc/class-statify-frontend.php
Original file line number Diff line number Diff line change
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