Skip to content

Commit 1b745ee

Browse files
committed
feat: Implemented auto initializing
1 parent f3830e3 commit 1b745ee

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
"autoload": {
3737
"psr-4": {
3838
"Oblak\\WP\\": "src"
39-
}
39+
},
40+
"files": [
41+
"src/Utils/oblak-wppu-init.php",
42+
"src/Utils/oblak-wppu-utils.php"
43+
]
4044
},
4145
"config": {
4246
"allow-plugins": {

src/Updater/Update_Manager.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Package_Updater class file
3+
* Update_Manager class file
44
*
55
* @package Package Updater
66
*/
@@ -10,13 +10,13 @@
1010
/**
1111
* Package Updater
1212
*/
13-
class Package_Updater {
13+
class Update_Manager {
1414
/**
1515
* Class instance
1616
*
1717
* @var static
1818
*/
19-
protected static ?Package_Updater $instance = null;
19+
protected static ?Update_Manager $instance = null;
2020

2121
/**
2222
* Array holding hostnames and their handlers
@@ -40,6 +40,15 @@ public static function init(): void {
4040
static::$instance ??= new static();
4141
}
4242

43+
/**
44+
* Checks if the singleton has been initialized
45+
*
46+
* @return bool
47+
*/
48+
public static function initialized(): bool {
49+
return null !== static::$instance;
50+
}
51+
4352
/**
4453
* Loads the update handlers
4554
*/

src/Utils/oblak-wppu-init.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Package updater initializer
4+
*
5+
* @package Package Updater
6+
*/
7+
8+
use Oblak\WP\Updater\Update_Manager;
9+
10+
if ( ! function_exists( 'wp_package_updater_init' ) && function_exists( 'add_action' ) ) {
11+
12+
/**
13+
* Initializes the package updater
14+
*/
15+
function wp_package_updater_init() {
16+
Update_Manager::init();
17+
}
18+
add_action( 'plugins_loaded', 'wp_package_updater_init', 0, 0 );
19+
20+
21+
if ( did_action( 'plugins_loaded' ) && ! doing_action( 'plugins_loaded' ) && ! Update_Manager::initialized() ) {
22+
wp_package_updater_init();
23+
}
24+
}

src/Utils/oblak-wppu-utils.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Package updater utilities
4+
*
5+
* @package Package Updater
6+
*/
7+
8+
use Oblak\WP\Updater\Interfaces\Package_Handler_Interface;
9+
10+
/**
11+
* Register a new package handler
12+
*
13+
* @param string $hostname Hostname for the handler.
14+
* @param Package_Handler_Interface $handler Handler instance.
15+
*/
16+
function wppu_register_handler( string $hostname, Package_Handler_Interface $handler ): void {
17+
add_filter(
18+
'wp_package_handlers',
19+
fn( $h ) => array_merge(
20+
$h,
21+
array( $hostname => $handler )
22+
)
23+
);
24+
}

0 commit comments

Comments
 (0)