forked from nawawi/cache-http-response
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache-http-response.php
More file actions
104 lines (82 loc) · 3.3 KB
/
Copy pathcache-http-response.php
File metadata and controls
104 lines (82 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Plugin Name: Cache HTTP Response.
* Plugin URI: https://github.com/nawawi/cache-http-response
* Version: 1.0.0
* Description: Performance tweaks for WordPress to cache HTTP API Call response.
* GitHub Plugin URI: https://github.com/nawawi/cache-http-response
* Author: Nawawi Jamili
* Author URI: https://github.com/nawawi
* Requires at least: 5.6
* Requires PHP: 7.4
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
namespace Nawawi\CacheHttpResponse;
\defined('ABSPATH') || exit;
final class cache_http_response
{
private $cache_prefix = 'cachehttpresponse_';
private $cache_ttl = 300;
private $cache_include = [];
private $cache_exclude = [];
public function register()
{
add_action('init', function () {
add_filter('http_response', [$this, 'http_response'], \PHP_INT_MIN, 3);
add_filter('pre_http_request', [$this, 'pre_http_request'], \PHP_INT_MIN, 3);
}, \PHP_INT_MAX);
}
private function cache_key($url)
{
return $this->cache_prefix.md5($url);
}
public function http_response($response, $args, $url)
{
if (200 !== $response['response']['code']) {
return $response;
}
$site_host = wp_parse_url(site_url(), \PHP_URL_HOST);
$hostname = wp_parse_url($url, \PHP_URL_HOST);
// Exclude wp.org.
if (apply_filters('cache_http_reponse/exclude_wporg', true) && 'wordpress.org' === $hostname || '.wordpress.org' === substr($hostname, -\strlen('.wordpress.org'))) {
return $response;
}
// Exclude Localhost.
if (apply_filters('cache_http_reponse/exclude_localhost', true) && false !== strpos($hostname, $site_host)) {
return $response;
}
$cache_key = $this->cache_key($url);
$cache_ttl = (int) apply_filters('cache_http_reponse/ttl', $this->cache_ttl);
if (empty($cache_ttl)) {
$cache_ttl = $this->cache_ttl;
}
$include_list = array_unique(apply_filters('cache_http_reponse/include', $this->cache_include));
$exclude_list = array_unique(apply_filters('cache_http_reponse/exclude', $this->cache_exclude));
if (empty($include_list) && empty($exclude_list)) {
set_transient($cache_key, $response, $cache_ttl);
return $response;
}
if (!empty($include_list) && \is_array($include_list) && \in_array($url, $include_list)) {
if (!empty($exclude_list) && \is_array($exclude_list) && !\in_array($url, $exclude_list)) {
set_transient($cache_key, $response, $cache_ttl);
}
return $response;
}
if (!empty($exclude_list) && \is_array($exclude_list) && !\in_array($url, $exclude_list)) {
set_transient($cache_key, $response, $cache_ttl);
return $response;
}
return $response;
}
public function pre_http_request($preempt, $parsed_args, $url)
{
$cache_key = $this->cache_key($url);
$data = get_transient($cache_key);
if (!empty($data) && \is_array($data)) {
return $data;
}
return $preempt;
}
}
(new cache_http_response())->register();