Passed
Push — master ( fa3796...57b58d )
by Paul
13:51 queued 06:19
created

Cache::getSystemInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Controllers\TranslationController;
6
7
class Cache
8
{
9
    /**
10
     * @param string $key
11
     * @param string $group
12
     * @return void
13
     */
14 6
    public function delete($key, $group)
15
    {
16 6
        global $_wp_suspend_cache_invalidation;
17 6
        if (empty($_wp_suspend_cache_invalidation)) {
18 6
            $group = glsr()->prefix.$group;
19 6
            wp_cache_delete($key, $group);
20
        }
21 6
    }
22
23
    /**
24
     * @param string $key
25
     * @param string $group
26
     * @param \Closure|null $callback
27
     * @return mixed
28
     */
29 13
    public function get($key, $group, $callback = null)
30
    {
31 13
        $group = glsr()->prefix.$group;
32 13
        $value = wp_cache_get($key, $group);
33 13
        if (false === $value && $callback instanceof \Closure) {
34
            if ($value = $callback()) {
35
                wp_cache_add($key, $value, $group);
36
            }
37
        }
38 13
        return $value;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getCloudflareIps()
45
    {
46
        if (false === ($ipAddresses = get_transient(glsr()->prefix.'cloudflare_ips'))) {
47
            $ipAddresses = array_fill_keys(['v4', 'v6'], []);
48
            foreach (array_keys($ipAddresses) as $version) {
49
                $url = 'https://www.cloudflare.com/ips-'.$version;
50
                $response = wp_remote_get($url, ['sslverify' => false]);
51
                if (is_wp_error($response)) {
52
                    glsr_log()->error($response->get_error_message());
53
                    continue;
54
                }
55
                if ('200' != ($statusCode = wp_remote_retrieve_response_code($response))) {
56
                    glsr_log()->error('Unable to connect to '.$url.' ['.$statusCode.']');
57
                    continue;
58
                }
59
                $ipAddresses[$version] = array_filter(
60
                    (array) preg_split('/\R/', wp_remote_retrieve_body($response))
61
                );
62
            }
63
            set_transient(glsr()->prefix.'cloudflare_ips', $ipAddresses, WEEK_IN_SECONDS);
64
        }
65
        return $ipAddresses;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getRemotePostTest()
72
    {
73
        if (false === ($test = get_transient(glsr()->prefix.'remote_post_test'))) {
74
            $response = wp_remote_post('https://api.wordpress.org/stats/php/1.0/');
75
            $test = !is_wp_error($response) && in_array($response['response']['code'], range(200, 299))
76
                ? 'Works'
77
                : 'Does not work';
78
            set_transient(glsr()->prefix.'remote_post_test', $test, WEEK_IN_SECONDS);
79
        }
80
        return $test;
81
    }
82
83
84
    /**
85
     * @return array
86
     */
87
    public function getSystemInfo()
88
    {
89
        if (false === ($data = get_transient(glsr()->prefix.'system_info'))) {
90
            add_filter('gettext_default', [glsr(TranslationController::class), 'filterEnglishTranslation'], 10, 2);
91
            $data = \WP_Debug_Data::debug_data(); // get the WordPress debug data in English
92
            remove_filter('gettext_default', [glsr(TranslationController::class), 'filterEnglishTranslation'], 10);
93
            set_transient(glsr()->prefix.'system_info', $data, 12 * HOUR_IN_SECONDS);
94
        }
95
        return $data;
96
    }
97
98
    /**
99
     * @param string $key
100
     * @param string $group
101
     * @return mixed
102
     */
103 13
    public function store($key, $group, $value)
104
    {
105 13
        $group = glsr()->prefix.$group;
106 13
        wp_cache_add($key, $value, $group);
107 13
        return $value;
108
    }
109
}
110