Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

Cache   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 21.74%

Importance

Changes 0
Metric Value
wmc 22
eloc 76
dl 0
loc 146
ccs 15
cts 69
cp 0.2174
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getSystemInfo() 0 9 2
A delete() 0 6 2
A store() 0 5 1
A getPluginVersions() 0 40 4
A getRemotePostTest() 0 10 4
A get() 0 10 4
A getCloudflareIps() 0 23 5
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database;
4
5
use GeminiLabs\SiteReviews\Controllers\TranslationController;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
8
class Cache
9
{
10
    /**
11
     * @param string $key
12
     * @param string $group
13
     * @return void
14
     */
15 6
    public function delete($key, $group)
16
    {
17 6
        global $_wp_suspend_cache_invalidation;
18 6
        if (empty($_wp_suspend_cache_invalidation)) {
19 6
            $group = glsr()->prefix.$group;
20 6
            wp_cache_delete($key, $group);
21
        }
22 6
    }
23
24
    /**
25
     * @param string $key
26
     * @param string $group
27
     * @param \Closure|null $callback
28
     * @return mixed
29
     */
30 14
    public function get($key, $group, $callback = null)
31
    {
32 14
        $group = glsr()->prefix.$group;
33 14
        $value = wp_cache_get($key, $group);
34 14
        if (false === $value && $callback instanceof \Closure) {
35
            if ($value = $callback()) {
36
                wp_cache_add($key, $value, $group);
37
            }
38
        }
39 14
        return $value;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getCloudflareIps()
46
    {
47
        if (false !== ($ipAddresses = get_transient(glsr()->prefix.'cloudflare_ips'))) {
48
            return $ipAddresses;
49
        }
50
        $ipAddresses = array_fill_keys(['v4', 'v6'], []);
51
        foreach (array_keys($ipAddresses) as $version) {
52
            $url = 'https://www.cloudflare.com/ips-'.$version;
53
            $response = wp_remote_get($url, ['sslverify' => false]);
54
            if (is_wp_error($response)) {
55
                glsr_log()->error($response->get_error_message());
56
                continue;
57
            }
58
            if ('200' != ($statusCode = wp_remote_retrieve_response_code($response))) {
59
                glsr_log()->error(sprintf('Unable to connect to %s [%s]', $url, $statusCode));
60
                continue;
61
            }
62
            $ipAddresses[$version] = array_filter(
63
                (array) preg_split('/\R/', wp_remote_retrieve_body($response))
64
            );
65
        }
66
        set_transient(glsr()->prefix.'cloudflare_ips', $ipAddresses, WEEK_IN_SECONDS);
67
        return $ipAddresses;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getPluginVersions()
74
    {
75
        $versions = get_transient(glsr()->prefix.'rollback_versions');
76
        if (!empty($versions)) {
77
            return $versions;
78
        }
79
        include_once ABSPATH.'wp-admin/includes/plugin-install.php';
80
        $response = plugins_api('plugin_information', [
81
            'slug' => glsr()->id,
82
            'fields' => [
83
                'active_installs' => false,
84
                'added' => false,
85
                'banners' => false,
86
                'contributors' => false,
87
                'donate_link' => false,
88
                'downloadlink' => false,
89
                'homepage' => false,
90
                'rating' => false,
91
                'ratings' => false,
92
                'screenshots' => false,
93
                'sections' => false,
94
                'tags' => false,
95
                'versions' => true,
96
            ],
97
        ]);
98
        if (is_wp_error($response)) {
99
            return [];
100
        }
101
        $versions = array_keys(Arr::consolidate(Arr::get($response, 'versions')));
102
        $versions = array_filter($versions, function ($version) {
103
            $minorVersion = (float) glsr()->version('minor');
104
            $versionLimit = sprintf('%.2f', ($minorVersion - (3 / 100)));
105
            $maxLimit = version_compare($version, glsr()->version, '<');
106
            $minLimit = version_compare($version, $versionLimit, '>=');
107
            return $maxLimit && $minLimit;
108
        });
109
        natsort($versions);
110
        $versions = array_reverse($versions);
111
        set_transient(glsr()->prefix.'rollback_versions', $versions, HOUR_IN_SECONDS);
112
        return $versions;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getRemotePostTest()
119
    {
120
        if (false === ($test = get_transient(glsr()->prefix.'remote_post_test'))) {
121
            $response = wp_remote_post('https://api.wordpress.org/stats/php/1.0/');
122
            $test = !is_wp_error($response) && in_array($response['response']['code'], range(200, 299))
123
                ? 'Works'
124
                : 'Does not work';
125
            set_transient(glsr()->prefix.'remote_post_test', $test, WEEK_IN_SECONDS);
126
        }
127
        return $test;
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function getSystemInfo()
134
    {
135
        if (false === ($data = get_transient(glsr()->prefix.'system_info'))) {
136
            add_filter('gettext_default', [glsr(TranslationController::class), 'filterEnglishTranslation'], 10, 2);
137
            $data = \WP_Debug_Data::debug_data(); // get the WordPress debug data in English
138
            remove_filter('gettext_default', [glsr(TranslationController::class), 'filterEnglishTranslation'], 10);
139
            set_transient(glsr()->prefix.'system_info', $data, 12 * HOUR_IN_SECONDS);
140
        }
141
        return $data;
142
    }
143
144
    /**
145
     * @param string $key
146
     * @param string $group
147
     * @return mixed
148
     */
149 14
    public function store($key, $group, $value)
150
    {
151 14
        $group = glsr()->prefix.$group;
152 14
        wp_cache_add($key, $value, $group);
153 14
        return $value;
154
    }
155
}
156