PluginProbe
Disk Usage Insights / 1.4
Disk Usage Insights v1.4
trunk 1.0 1.10 1.11 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
disk-usage-insights / src / Domain / Collect / DetermineWpCoreFileJob.php

DetermineWpCoreFileJob.php in Disk Usage Insights 1.4, at src/Domain/Collect/DetermineWpCoreFileJob.php

61 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Mgleis\DiskUsageInsights\Domain\Collect;
4
5 use Mgleis\DiskUsageInsights\Domain\Jobs\BaseJob;
6
7 class DetermineWpCoreFileJob extends BaseJob {
8
9 private int $skip;
10 private int $count;
11 private int $totalCount;
12
13 public function __construct(int $skip, int $count, int $totalCount) {
14 $this->skip = $skip;
15 $this->count = $count;
16 $this->totalCount = $totalCount;
17 }
18
19 public function work() {
20
21 $this->log(self::class);
22
23 $snapshot = $this->snapshotRepository->load();
24 if (sizeof($snapshot->wpcorefiles) == 0) {
25
26 // Fetch WP Core files and store them in kv store
27 $url = sprintf('https://api.wordpress.org/core/checksums/1.0/?version=%s&locale=en_US', wp_get_wp_version());
28 $this->log("Fetching from url: " . $url);
29 $str = wp_remote_get($url)['body'];
30 $arr = json_decode($str, true, JSON_THROW_ON_ERROR);
31 if ($arr['checksums'] === false) {
32 throw new \Exception("Could not fetch the checksums of wordpress api.");
33 }
34
35 $snapshot->wpcorefiles = array_keys($arr['checksums']);
36 $this->snapshotRepository->save($snapshot);
37 }
38
39 // TODO: batch it
40 $fileEntries = $this->fileEntryRepository->get($this->skip, $this->count);
41 foreach ($fileEntries as $fileEntry) {
42
43 $relativeFilename = $this->fileEntryRepository->calcFullPath($fileEntry);
44 if (in_array($relativeFilename, $snapshot->wpcorefiles)) {
45 $fileEntry->is_wp_core_file = 1;
46 $this->fileEntryRepository->createOrUpdate($fileEntry);
47 }
48 }
49
50 }
51
52 public function toArray() {
53 return ['type' => self::class, 'args' => [$this->skip, $this->count, $this->totalCount]];
54 }
55
56 public function toDescription(): string {
57 return sprintf('Determining WordPress core files... %s%%', round(100 * $this->skip / $this->totalCount));
58 }
59
60 }
61