forked from fairpm/fair-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-package.php
More file actions
109 lines (96 loc) · 2.18 KB
/
class-package.php
File metadata and controls
109 lines (96 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Package data container.
*
* @package FAIR
*/
namespace FAIR\Updater;
use FAIR\Packages;
/**
* Represents a registered FAIR package (plugin or theme).
*/
abstract class Package {
/**
* The DID of the package.
*
* @var string
*/
public string $did;
/**
* Absolute path to the main file.
*
* @var string
*/
public string $filepath;
/**
* Current installed version.
*
* @var string|null
*/
public ?string $local_version;
/**
* Cached metadata document.
*
* @var \FAIR\Packages\MetadataDocument|null
*/
private $metadata = null;
/**
* Cached release document.
*
* @var \FAIR\Packages\ReleaseDocument|null
*/
private $release = null;
/**
* Constructor.
*
* @param string $did The DID of the package.
* @param string $filepath Absolute path to the main file.
*/
public function __construct( string $did, string $filepath ) {
$this->did = $did;
$this->filepath = $filepath;
$this->local_version = $filepath ? get_file_data( $filepath, [ 'Version' => 'Version' ] )['Version'] : null;
}
/**
* Get the package slug.
*
* @return string The slug (directory name for plugins, stylesheet for themes).
*/
abstract public function get_slug(): string;
/**
* Get the relative path used in update transients.
*
* @return string The relative path.
*/
abstract public function get_relative_path(): string;
/**
* Get the metadata document, fetching and caching if needed.
*
* @return \FAIR\Packages\MetadataDocument|\WP_Error|null
*/
final public function get_metadata() {
if ( $this->metadata === null ) {
$metadata = Packages\fetch_package_metadata( $this->did );
if ( ! is_wp_error( $metadata ) ) {
$this->metadata = $metadata;
}
return $metadata;
}
return $this->metadata;
}
/**
* Get the release document, fetching and caching if needed.
*
* @return \FAIR\Packages\ReleaseDocument|\WP_Error|null
*/
final public function get_release() {
if ( $this->release === null ) {
$release = Packages\get_latest_release_from_did( $this->did );
if ( ! is_wp_error( $release ) ) {
$this->release = $release;
}
return $release;
}
return $this->release;
}
}