Skip to content

Commit 25bf09e

Browse files
committed
Rearchitect Updater into a registry which handles and dispatches plugin- and theme-specific logic as necessary.
Signed-off-by: John Blackbourn <john@johnblackbourn.com>
1 parent 8f3ab3c commit 25bf09e

File tree

5 files changed

+464
-109
lines changed

5 files changed

+464
-109
lines changed

inc/updater/class-package.php

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

inc/updater/class-themepackage.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Theme package data container.
4+
*
5+
* @package FAIR
6+
*/
7+
8+
namespace FAIR\Updater;
9+
10+
/**
11+
* Represents a registered FAIR theme.
12+
*/
13+
final class ThemePackage extends Package {
14+
15+
/**
16+
* Get the theme slug.
17+
*
18+
* @return string The theme stylesheet (directory name).
19+
*/
20+
public function get_slug(): string {
21+
return basename( dirname( $this->filepath ) );
22+
}
23+
24+
/**
25+
* Get the relative path used in update transients.
26+
*
27+
* @return string The relative path (theme directory name).
28+
*/
29+
public function get_relative_path(): string {
30+
return dirname( plugin_basename( $this->filepath ) );
31+
}
32+
}

0 commit comments

Comments
 (0)