PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Database / Cache.php

Cache.php in Site Reviews trunk, at plugin/Database/Cache.php

115 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\SiteReviews\Database;
4
5 use GeminiLabs\SiteReviews\Helpers\Arr;
6 use GeminiLabs\SiteReviews\Helpers\Cast;
7
8 class Cache
9 {
10 public function delete(string $key, string $group): void
11 {
12 global $_wp_suspend_cache_invalidation;
13 if (empty($_wp_suspend_cache_invalidation)) {
14 $group = glsr()->prefix.$group;
15 wp_cache_delete($key, $group);
16 }
17 }
18
19 public function get(string $key, string $group, ?\Closure $callback = null, int $expire = 0)
20 {
21 $group = glsr()->prefix.$group;
22 $value = wp_cache_get($key, $group);
23 if (false === $value && $callback instanceof \Closure) {
24 if ($value = $callback()) {
25 wp_cache_add($key, $value, $group, $expire);
26 }
27 }
28 return $value;
29 }
30
31 public function getPluginVersions(): array
32 {
33 $transientKey = glsr()->prefix.'rollback_versions';
34 $cached = get_transient($transientKey);
35 if (!empty($cached)) {
36 return Cast::toArray($cached);
37 }
38 include_once \ABSPATH.'wp-admin/includes/plugin-install.php';
39 $response = plugins_api('plugin_information', [
40 'slug' => glsr()->id,
41 'fields' => [
42 'active_installs' => false,
43 'added' => false,
44 'banners' => false,
45 'contributors' => false,
46 'donate_link' => false,
47 'downloadlink' => false,
48 'homepage' => false,
49 'rating' => false,
50 'ratings' => false,
51 'screenshots' => false,
52 'sections' => false,
53 'tags' => false,
54 'versions' => true,
55 ],
56 ]);
57 if (is_wp_error($response)) {
58 glsr_log()->error($response);
59 return [];
60 }
61 $versions = Arr::consolidate($response->versions ?? []);
62 if (empty($versions)) {
63 glsr_log()->error('Unable to fetch plugin versions.');
64 return [];
65 }
66 unset($versions['trunk'], $versions[glsr()->version]);
67 krsort($versions, \SORT_NATURAL);
68 $versionKeys = array_keys($versions);
69 $versions = array_slice($versionKeys, 0, 10);
70 // Ensure the latest version from the previous major release is included
71 $prevMajor = Cast::toInt(glsr()->version('major')) - 1;
72 $prevMajorVersions = preg_grep("/^{$prevMajor}\./", $versionKeys);
73 $latestPrevMajor = reset($prevMajorVersions); // already sorted desc
74 if ($latestPrevMajor && !in_array($latestPrevMajor, $versions)) {
75 $versions[] = $latestPrevMajor;
76 }
77 set_transient($transientKey, $versions, \HOUR_IN_SECONDS);
78 return $versions;
79 }
80
81 public function getRemotePostTest(): string
82 {
83 if (false === ($test = get_transient(glsr()->prefix.'remote_post_test'))) {
84 $response = wp_safe_remote_post('https://api.wordpress.org/stats/php/1.0/');
85 $test = !is_wp_error($response) && in_array($response['response']['code'], range(200, 299))
86 ? 'Works'
87 : 'Does not work';
88 set_transient(glsr()->prefix.'remote_post_test', $test, \WEEK_IN_SECONDS);
89 }
90 return Cast::toString($test);
91 }
92
93 public function getSystemInfo(): array
94 {
95 if (false === ($data = get_transient(glsr()->prefix.'system_info'))) {
96 $callback = fn ($translation, $single) => $single;
97 add_filter('gettext_default', $callback, 10, 2);
98 try { // prevent badly made migration plugins from breaking Site Reviews...
99 $data = \WP_Debug_Data::debug_data(); // get the WordPress debug data in English
100 set_transient(glsr()->prefix.'system_info', $data, 12 * \HOUR_IN_SECONDS);
101 } catch (\TypeError $error) {
102 $data = [];
103 }
104 remove_filter('gettext_default', $callback, 10);
105 }
106 return Arr::consolidate($data);
107 }
108
109 public function store(string $key, string $group, $value, int $expire = 0): void
110 {
111 $group = glsr()->prefix.$group;
112 wp_cache_set($key, $value, $group, $expire);
113 }
114 }
115