Skip to content

Commit 5a4abb9

Browse files
committed
Update README.md to enhance documentation on dataflow and timestamp handling in super-admin-all-sites-menu plugin
1 parent 9841672 commit 5a4abb9

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

README.md

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,27 +111,59 @@ A demo is available in [WordPress Playground](https://playground.wordpress.net/?
111111

112112
- If you activate the Restricted Site Access plugin (included), you'll see a red icon next to the site name. ATM, this only works on the main site due to [issues with WordPress Playground](https://github.com/WordPress/wordpress-playground/issues/2054).
113113

114-
## Flow
115-
116-
```mermaid
117-
sequenceDiagram
118-
Actor User
119-
participant Menu
120-
participant IndexedDB
121-
participant WordPress
122-
User->>Menu: Open the menu
123-
Menu->>IndexedDB: Check if IndexedDB in sync
124-
IndexedDB->>Menu: Update sites menu
125-
Note over Menu,IndexedDB: If in sync.
126-
IndexedDB->>WordPress: Request sites
127-
Note over IndexedDB,WordPress: If not in sync.
128-
WordPress->>IndexedDB: Get sites
129-
IndexedDB->>IndexedDB: Update IndexedDB
130-
IndexedDB->>Menu: Update sites menu
131-
Menu->>User: Read Menu
132-
133-
134-
```
114+
## Behide the scenes
115+
116+
Let me break down the dataflow related to the timestamp in the super-admin-all-sites-menu plugin:
117+
118+
1. Initial Timestamp Creation:
119+
- In super-admin-all-sites-menu.php, a timestamp is generated when the plugin initializes
120+
- This is exposed via the `get_timestamp()` method of the main plugin class
121+
2. PHP to JavaScript Transfer:
122+
- The timestamp is passed to JavaScript via `wp_add_inline_script()` in super-admin-all-sites-menu.php around line 409:
123+
```php
124+
wp_add_inline_script( 'super-admin-all-sites-menu', 'var allSitesMenuTimestamp = ' . $this->get_timestamp() . ';', 'before' );
125+
```
126+
- Where `$data` includes the timestamp:
127+
```php
128+
$data = wp_json_encode([
129+
'timestamp' => $this->get_timestamp(),
130+
// other data...
131+
]);
132+
```
133+
3. JavaScript Storage Check:
134+
- In index.js, the `populateDB()` function compares timestamps:
135+
```javascript
136+
if (
137+
typeof data !== 'undefined' &&
138+
typeof data.timestamp !== 'undefined' &&
139+
pluginAllSitesMenu.timestamp > data.timestamp
140+
) {
141+
await db.delete();
142+
}
143+
```
144+
4. Database Population:
145+
- If the IndexedDB is empty or was deleted due to timestamp mismatch:
146+
```javascript
147+
if ((await db.count()) === 0) {
148+
loadSites(db, {
149+
offset: 0,
150+
delayMs: 200,
151+
});
152+
}
153+
```
154+
5. Timestamp Update Triggers:
155+
- The timestamp is updated when:
156+
- A site is added/deleted from the network
157+
- A plugin that affects the menu is activated/deactivated
158+
- A blog name is changed
159+
- The Restricted Site Access plugin status changes
160+
161+
This creates a caching mechanism where:
162+
163+
- The PHP timestamp acts as a "version" of the site data
164+
- The JS code compares this against the stored timestamp
165+
- Mismatches trigger a refresh of the cached data
166+
- Matches allow using the existing cached data
135167

136168
## Changelog
137169

0 commit comments

Comments
 (0)