| 1 |
<?php |
| 2 |
/** |
| 3 |
* Docket Cache. |
| 4 |
* |
| 5 |
* @author Nawawi Jamili |
| 6 |
* @license MIT |
| 7 |
* |
| 8 |
* @see https://github.com/nawawi/docket-cache |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Nawawi\DocketCache; |
| 12 |
|
| 13 |
\defined('ABSPATH') || exit; |
| 14 |
|
| 15 |
final class Crawler |
| 16 |
{ |
| 17 |
private static $version = '26.04.05'; |
| 18 |
public static $send_cookie = false; |
| 19 |
|
| 20 |
private static function default_args($param = []) |
| 21 |
{ |
| 22 |
$args = [ |
| 23 |
'blocking' => false, |
| 24 |
'timeout' => 15, |
| 25 |
'httpversion' => '1.1', |
| 26 |
'user-agent' => 'Mozilla/5.0 (compatible; docket-cache/'.self::$version.'; +https://docketcache.com)', |
| 27 |
'body' => null, |
| 28 |
'compress' => false, |
| 29 |
'decompress' => false, |
| 30 |
'sslverify' => apply_filters('https_local_ssl_verify', false), |
| 31 |
'stream' => false, |
| 32 |
'headers' => [ |
| 33 |
'REFERER' => home_url(), |
| 34 |
'Cache-Control' => 'no-cache', |
| 35 |
], |
| 36 |
]; |
| 37 |
|
| 38 |
if (self::$send_cookie && !empty($_COOKIE) && class_exists('\\WP_Http_Cookie')) { |
| 39 |
$cookies = []; |
| 40 |
foreach ($_COOKIE as $name => $value) { |
| 41 |
$cookies[] = new \WP_Http_Cookie( |
| 42 |
[ |
| 43 |
'name' => $name, |
| 44 |
'value' => $value, |
| 45 |
] |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
if (!empty($cookies)) { |
| 50 |
$args['cookies'] = $cookies; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (!empty($param) && \is_array($param)) { |
| 55 |
$args = array_merge($args, $param); |
| 56 |
} |
| 57 |
|
| 58 |
return $args; |
| 59 |
} |
| 60 |
|
| 61 |
public static function fetch_admin($url, $param = []) |
| 62 |
{ |
| 63 |
if (is_user_logged_in() && current_user_can(is_multisite() ? 'manage_network_options' : 'manage_options')) { |
| 64 |
$param['timeout'] = 3; |
| 65 |
|
| 66 |
self::$send_cookie = true; |
| 67 |
|
| 68 |
return self::fetch($url, $param); |
| 69 |
} |
| 70 |
|
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
public static function fetch_home($param = []) |
| 75 |
{ |
| 76 |
self::$send_cookie = true; |
| 77 |
$param['timeout'] = 3; |
| 78 |
|
| 79 |
return self::fetch(home_url('/'), $param); |
| 80 |
} |
| 81 |
|
| 82 |
public static function fetch($url, $param = []) |
| 83 |
{ |
| 84 |
$args = self::default_args($param); |
| 85 |
|
| 86 |
return wp_remote_get($url, $args); |
| 87 |
} |
| 88 |
|
| 89 |
public static function post($url, $param = []) |
| 90 |
{ |
| 91 |
$args = self::default_args($param); |
| 92 |
|
| 93 |
return wp_remote_post($url, $args); |
| 94 |
} |
| 95 |
|
| 96 |
public static function fetch_home_nocache($param = []) |
| 97 |
{ |
| 98 |
self::$send_cookie = true; |
| 99 |
$param['timeout'] = 3; |
| 100 |
$param['headers'] = [ |
| 101 |
'REFERER' => home_url(), |
| 102 |
'Cache-Control' => 'no-cache', |
| 103 |
]; |
| 104 |
|
| 105 |
$path = '/?nocache='.time(); |
| 106 |
|
| 107 |
return self::fetch(home_url($path), $param); |
| 108 |
} |
| 109 |
} |
| 110 |
|