PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / trunk
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript vtrunk
4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 All 169 releases
searchpro / inc / crawler / berqDetectCrawler.php

berqDetectCrawler.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript trunk, at inc/crawler/berqDetectCrawler.php

36 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit;
3
4 class berqDetectCrawler {
5 public static function is_crawler() {
6 // Load the JSON data
7 $json_file = optifer_PATH . '/inc/crawler/crawler-user-agents.json';
8 if (empty($_SERVER['HTTP_USER_AGENT'])) {
9 return false;
10 }
11 $user_agent = $_SERVER['HTTP_USER_AGENT'];
12
13
14 $crawler_data = json_decode(file_get_contents($json_file), true);
15
16 if (!$crawler_data) {
17 error_log('Failed to load or decode the JSON file.');
18 return false; // Return false if the JSON file couldn't be loaded or decoded
19 }
20
21 // Loop through each crawler pattern in the JSON file
22 foreach ($crawler_data as $crawler) {
23 if (isset($crawler['pattern'])) {
24 // Escape backslashes and forward slashes for accurate pattern matching
25 $pattern = str_replace(['\\/', '/'], ['/', '\/'], $crawler['pattern']);
26
27 // Check if the user agent matches the pattern
28 if (preg_match('/' . $pattern . '/i', $user_agent)) {
29 return true; // User agent matches a crawler pattern
30 }
31 }
32 }
33
34 return false; // No match found, user agent is not a crawler
35 }
36 }