Skip to content

Commit a3a9a6c

Browse files
authored
Multisite Support (#37)
* Add multisite connection configuration handling in fetch_option_connection_objs method * Add methods for managing default storage connection ID with multisite support * Enhance default connection ID retrieval with validation check
1 parent 35c6d15 commit a3a9a6c

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

disciple-tools-storage-api.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ public static function validate_url( $url ): string {
118118
public static function fetch_option_connection_objs(): object {
119119
$option = get_option( self::$option_dt_storage_connection_objects );
120120

121+
// If multisite, then ensure specified connection configuration is always included.
122+
if ( is_multisite() && !empty( get_site_option( 'dt_storage_multisite_connection_object', [] ) ) ) {
123+
$multisite_connection_objects = get_site_option( 'dt_storage_multisite_connection_object', [] );
124+
125+
// Check if there is a valid, specified multisite storage connection.
126+
if ( isset( $multisite_connection_objects['id'], $multisite_connection_objects['enabled'], $multisite_connection_objects['name'], $multisite_connection_objects['type'] ) && boolval( $multisite_connection_objects['enabled'] ) ) {
127+
$type = $multisite_connection_objects['type'];
128+
129+
// Ensure identified connection contains a valid type configuration.
130+
if ( isset( $multisite_connection_objects[ $type ]['access_key'], $multisite_connection_objects[ $type ]['secret_access_key'], $multisite_connection_objects[ $type ]['region'], $multisite_connection_objects[ $type ]['endpoint'], $multisite_connection_objects[ $type ]['bucket'] ) ) {
131+
132+
// Finally, update option, with identified multisite connection.
133+
$decoded_option = json_decode( !empty( $option ) ? $option : '{}' );
134+
$decoded_option->{$multisite_connection_objects['id']} = (object) $multisite_connection_objects;
135+
$option = json_encode( $decoded_option );
136+
}
137+
}
138+
}
139+
121140
if ( ! empty( $option ) ) {
122141

123142
$connection_objs = [];

disciple-tools-storage-filters.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,48 @@
66

77
class DT_Storage {
88

9+
/**
10+
* @param string $connection_id
11+
* @return bool
12+
*/
13+
public static function update_default_connection_id( string $connection_id ): bool {
14+
return update_option( 'dt_storage_connection_id', $connection_id );
15+
}
16+
17+
/**
18+
* @return string
19+
*/
20+
public static function get_default_connection_id(): string {
21+
$storage_connection_id = dt_get_option( 'dt_storage_connection_id' );
22+
23+
// Return if a valid storage connection id is found.
24+
if ( !empty( $storage_connection_id ) ) {
25+
return $storage_connection_id;
26+
}
27+
28+
// Default to multisite global setting, if available; otherwise revert to local settings.
29+
if ( is_multisite() && !empty( get_site_option( 'dt_storage_multisite_connection_object', [] ) ) ) {
30+
$multisite_connection_objects = get_site_option( 'dt_storage_multisite_connection_object', [] );
31+
32+
// Check if there is a valid, specified multisite storage connection.
33+
if ( isset( $multisite_connection_objects['id'], $multisite_connection_objects['enabled'] ) && boolval( $multisite_connection_objects['enabled'] ) ) {
34+
35+
// Switch storage connection id to multisite global setting, if currently empty.
36+
if ( empty( $storage_connection_id ) ) {
37+
$storage_connection_id = $multisite_connection_objects['id'];
38+
self::update_default_connection_id( $storage_connection_id );
39+
}
40+
}
41+
}
42+
43+
return $storage_connection_id;
44+
}
45+
946
/**
1047
* @return object|null
1148
*/
1249
private static function get_connection(){
13-
$storage_connection_id = dt_get_option( 'dt_storage_connection_id' );
50+
$storage_connection_id = self::get_default_connection_id();
1451
if ( empty( $storage_connection_id ) ) {
1552
return null;
1653
}

0 commit comments

Comments
 (0)