| 1 |
<?php |
| 2 |
|
| 3 |
namespace CryptX\Admin; |
| 4 |
|
| 5 |
/** |
| 6 |
* What a newly created site in a network starts with. |
| 7 |
* |
| 8 |
* On a network every site keeps its own settings, and since 4.1.1 that is |
| 9 |
* enforced rather than assumed -- network activation used to copy the first |
| 10 |
* site's values to every other one, exclusion list and encryption secret |
| 11 |
* included, which left addresses in the open on sites whose administrator had |
| 12 |
* excluded nothing. The fix was to give each site its own. |
| 13 |
* |
| 14 |
* That fix left a gap this closes: a network administrator who wants every new |
| 15 |
* site to start with, say, the picture variant and feeds protected had to open |
| 16 |
* each site and set it again. So there is now one list of defaults for the |
| 17 |
* network, and it applies at exactly one moment -- when a site is set up. |
| 18 |
* |
| 19 |
* Two properties keep this from becoming the bug it grew out of: |
| 20 |
* |
| 21 |
* It never touches a site that already exists. Not on save, not on activation, |
| 22 |
* not ever. A network default is a starting point, not an instruction. |
| 23 |
* |
| 24 |
* And two settings are deliberately not shareable, because they mean different |
| 25 |
* things on different sites. The list of excluded post IDs is the one that |
| 26 |
* caused the original bug: post 17 on one site has nothing to do with post 17 |
| 27 |
* on another, so copying the list excludes the wrong posts -- and an excluded |
| 28 |
* post is an unprotected post. The uploaded image is an attachment ID and has |
| 29 |
* the same problem, in the harmless direction: it would simply not exist. |
| 30 |
* |
| 31 |
* The secrets are not here at all, because they are not settings -- they never |
| 32 |
* reach SettingsSchema, so there is no way for them to arrive. |
| 33 |
* |
| 34 |
* @package CryptX |
| 35 |
* @since 4.2.0 |
| 36 |
*/ |
| 37 |
final class NetworkDefaults |
| 38 |
{ |
| 39 |
/** Where the list lives. A site option, so it is one per network. */ |
| 40 |
public const OPTION = 'cryptx_network_defaults'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Settings that mean something different on every site. |
| 44 |
* |
| 45 |
* @return array<int, string> The keys that are never shared. |
| 46 |
*/ |
| 47 |
public static function notShareable(): array |
| 48 |
{ |
| 49 |
return ['excludedIDs', 'alt_uploadedimage']; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* The fields a network administrator may set. |
| 54 |
* |
| 55 |
* @return array<string, array<string, mixed>> The schema, minus the two. |
| 56 |
*/ |
| 57 |
public static function fields(): array |
| 58 |
{ |
| 59 |
return array_diff_key( |
| 60 |
SettingsSchema::fields(), |
| 61 |
array_flip(self::notShareable()) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* The stored defaults, cleaned. |
| 67 |
* |
| 68 |
* @return array<string, mixed> Only keys a network default may carry. |
| 69 |
*/ |
| 70 |
public static function get(): array |
| 71 |
{ |
| 72 |
if (!is_multisite()) { |
| 73 |
return []; |
| 74 |
} |
| 75 |
|
| 76 |
$stored = get_site_option(self::OPTION, []); |
| 77 |
|
| 78 |
if (!is_array($stored)) { |
| 79 |
return []; |
| 80 |
} |
| 81 |
|
| 82 |
// Through the same sanitiser as everything else, then filtered again: |
| 83 |
// a key could have been written before it joined the excluded list, or |
| 84 |
// by something other than this screen. |
| 85 |
return array_diff_key( |
| 86 |
SettingsSchema::sanitize($stored), |
| 87 |
array_flip(self::notShareable()) |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Stores a new set of defaults. |
| 93 |
* |
| 94 |
* @param array<string, mixed> $values The submitted values. |
| 95 |
* |
| 96 |
* @return array<string, mixed> What was actually stored. |
| 97 |
*/ |
| 98 |
public static function save(array $values): array |
| 99 |
{ |
| 100 |
$clean = array_diff_key( |
| 101 |
SettingsSchema::sanitize($values), |
| 102 |
array_flip(self::notShareable()) |
| 103 |
); |
| 104 |
|
| 105 |
update_site_option(self::OPTION, $clean); |
| 106 |
|
| 107 |
return $clean; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* The values a site should start with. |
| 112 |
* |
| 113 |
* @return array<string, mixed> Plugin defaults with the network's on top. |
| 114 |
*/ |
| 115 |
public static function forNewSite(): array |
| 116 |
{ |
| 117 |
return array_merge( |
| 118 |
SettingsSchema::sanitize(SettingsSchema::defaults()), |
| 119 |
self::get() |
| 120 |
); |
| 121 |
} |
| 122 |
} |
| 123 |
|