|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Database; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\SqlQueries; |
|
7
|
|
|
|
|
8
|
|
|
class Cache |
|
9
|
|
|
{ |
|
10
|
|
|
const EXPIRY_TIME = WEEK_IN_SECONDS; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @return array |
|
14
|
|
|
*/ |
|
15
|
|
|
public function getCloudflareIps() |
|
16
|
|
|
{ |
|
17
|
|
|
$ipAddresses = get_transient( Application::ID.'_cloudflare_ips' ); |
|
18
|
|
|
if( $ipAddresses === false ) { |
|
19
|
|
|
$ipAddresses = array_fill_keys( ['v4', 'v6'], [] ); |
|
20
|
|
|
foreach( array_keys( $ipAddresses ) as $version ) { |
|
21
|
|
|
$response = wp_remote_get( 'https://www.cloudflare.com/ips-'.$version ); |
|
22
|
|
|
if( is_wp_error( $response )) { |
|
23
|
|
|
glsr_log()->error( $response->get_error_message() ); |
|
24
|
|
|
continue; |
|
25
|
|
|
} |
|
26
|
|
|
$ipAddresses[$version] = array_filter( explode( PHP_EOL, wp_remote_retrieve_body( $response ))); |
|
27
|
|
|
} |
|
28
|
|
|
set_transient( Application::ID.'_cloudflare_ips', $ipAddresses, static::EXPIRY_TIME ); |
|
29
|
|
|
} |
|
30
|
|
|
return $ipAddresses; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $metaKey |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getReviewCountsFor( $metaKey ) |
|
38
|
|
|
{ |
|
39
|
|
|
$counts = wp_cache_get( Application::ID, $metaKey.'_count' ); |
|
40
|
|
|
if( $counts === false ) { |
|
41
|
|
|
$counts = []; |
|
42
|
|
|
$results = glsr( SqlQueries::class )->getReviewCounts( $metaKey ); |
|
43
|
|
|
foreach( $results as $result ) { |
|
44
|
|
|
$counts[$result->name] = $result->num_posts; |
|
45
|
|
|
} |
|
46
|
|
|
wp_cache_set( Application::ID, $counts, $metaKey.'_count' ); |
|
47
|
|
|
} |
|
48
|
|
|
return $counts; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getRemotePostTest() |
|
55
|
|
|
{ |
|
56
|
|
|
$test = get_transient( Application::ID.'_remote_post_test' ); |
|
57
|
|
|
if( $test === false ) { |
|
58
|
|
|
$response = wp_remote_post( 'https://api.wordpress.org/stats/php/1.0/' ); |
|
59
|
|
|
$test = !is_wp_error( $response ) |
|
60
|
|
|
&& in_array( $response['response']['code'], range( 200, 299 )) |
|
61
|
|
|
? 'Works' |
|
62
|
|
|
: 'Does not work'; |
|
63
|
|
|
set_transient( Application::ID.'_remote_post_test', $test, static::EXPIRY_TIME ); |
|
64
|
|
|
} |
|
65
|
|
|
return $test; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|