From 043f1f5cd057e40415cb0ac070d2cf6833dc6a1d Mon Sep 17 00:00:00 2001 From: Sven Wagener Date: Fri, 22 Mar 2019 12:07:29 +0100 Subject: [PATCH 1/2] Ticket #112 - Enhanced bot detection. --- inc/class-statify-frontend.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index 46a3603..bf0b072 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -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; } @@ -175,6 +175,37 @@ private static function _skip_tracking() { ); } + /** + * 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. * From d7c6c1d0e8ab77d0a2ceb9a268467d8a77381eca Mon Sep 17 00:00:00 2001 From: Sven Wagener Date: Fri, 22 Mar 2019 16:25:19 +0100 Subject: [PATCH 2/2] Issue #112 - phpcbf --- inc/class-statify-frontend.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/inc/class-statify-frontend.php b/inc/class-statify-frontend.php index e380634..be8802b 100644 --- a/inc/class-statify-frontend.php +++ b/inc/class-statify-frontend.php @@ -190,12 +190,12 @@ private static function _skip_tracking() { /** * 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. + * + * @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 );