PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / trunk
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin vtrunk
1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 0.8.6 All 33 releases
desktop-mode / includes / network / hub.php

hub.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin trunk, at includes/network/hub.php

186 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * OpenStation — The hub: the network as one list, served to its
4 * members.
5 *
6 * `GET /desktop-mode/v1/network` is what a member fetches to show the
7 * same switcher the hub's own sites show: the network's name and key,
8 * its Network Admin, every local site and every member. A member asks
9 * with a signed request (`openstation_network_signed_headers()`), and
10 * only a pinned key is answered; a logged-in network administrator may
11 * read it too.
12 *
13 * @package OpenStation
14 */
15
16 defined( 'ABSPATH' ) || exit;
17
18 /**
19 * The switcher entries for the registry's members.
20 *
21 * @return array<int,array<string,mixed>> Each `id` (`member:<id>`), `name`, `shellUrl`, `url`, `publicKey`, `kind`, `status`.
22 */
23 function openstation_network_member_entries() {
24 $entries = array();
25 foreach ( openstation_network_members() as $member ) {
26 $entries[] = array(
27 'id' => 'member:' . $member['id'],
28 'name' => $member['name'],
29 'shellUrl' => $member['shellUrl'],
30 'url' => $member['url'],
31 'publicKey' => $member['publicKey'],
32 'kind' => 'member',
33 'status' => $member['status'],
34 );
35 }
36 return $entries;
37 }
38
39 /**
40 * The hub's own sites as switcher entries: every site of the network
41 * on a multisite (by path, capped as the super admin's row is), or the
42 * one site of a single-site hub.
43 *
44 * @return array<int,array<string,mixed>>
45 */
46 function openstation_network_local_entries() {
47 $screen = 'admin.php?page=' . OPENSTATION_SHELL_PAGE_SLUG;
48 if ( ! is_multisite() ) {
49 return array(
50 array(
51 'id' => 'hub',
52 'name' => (string) get_bloginfo( 'name' ),
53 'shellUrl' => esc_url_raw( admin_url( $screen ) ),
54 'url' => esc_url_raw( home_url( '/' ) ),
55 'kind' => 'local',
56 'status' => 'paired',
57 ),
58 );
59 }
60 $entries = array();
61 foreach ( openstation_multisite_network_sites() as $blog_id => $name ) {
62 $entries[] = array(
63 'id' => (string) $blog_id,
64 'name' => $name,
65 'shellUrl' => esc_url_raw( get_admin_url( $blog_id, $screen ) ),
66 'url' => esc_url_raw( get_home_url( $blog_id, '/' ) ),
67 'kind' => 'local',
68 'status' => 'paired',
69 );
70 }
71 return $entries;
72 }
73
74 /**
75 * The network as one list: identity, Network Admin, every site.
76 *
77 * @return array<string,mixed>
78 */
79 function openstation_network_hub_list() {
80 $identity = openstation_network_identity();
81 $admin = null;
82 if ( is_multisite() ) {
83 $admin = array(
84 'url' => esc_url_raw( network_admin_url() ),
85 'shellUrl' => $identity['shellUrl'],
86 // Every row: the hub gates them by capability on arrival,
87 // and a member cannot know a user's network role.
88 'rows' => openstation_multisite_network_admin_rows( true ),
89 );
90 }
91 return array(
92 'name' => $identity['name'],
93 'url' => $identity['url'],
94 'shellUrl' => $identity['shellUrl'],
95 'publicKey' => $identity['publicKey'],
96 'networkAdmin' => $admin,
97 'sites' => array_merge( openstation_network_local_entries(), openstation_network_member_entries() ),
98 );
99 }
100
101 /**
102 * Whether this install is a hub: it has members.
103 *
104 * @return bool
105 */
106 function openstation_network_is_hub() {
107 return array() !== openstation_network_members();
108 }
109
110 /**
111 * Register the list route.
112 */
113 function openstation_network_register_hub_route() {
114 register_rest_route(
115 'desktop-mode/v1',
116 '/network',
117 array(
118 'methods' => WP_REST_Server::READABLE,
119 'callback' => 'openstation_rest_network_list',
120 'permission_callback' => 'openstation_rest_network_list_permission',
121 )
122 );
123 }
124 add_action( 'rest_api_init', 'openstation_network_register_hub_route' );
125
126 /**
127 * Who may read the list: a member signing with its pinned key, or a
128 * logged-in administrator of the hub.
129 *
130 * @param WP_REST_Request $request Request.
131 * @return true|WP_Error
132 */
133 function openstation_rest_network_list_permission( WP_REST_Request $request ) {
134 if ( current_user_can( is_multisite() ? 'manage_network' : 'manage_options' ) ) {
135 return true;
136 }
137 $signer = openstation_network_request_signer( $request );
138 if ( '' !== $signer && null !== openstation_network_member_by_key( $signer ) ) {
139 return true;
140 }
141 return new WP_Error(
142 'openstation_network_not_member',
143 __( 'This site is not a member of the network. Add it on the network first.', 'desktop-mode' ),
144 array( 'status' => 403 )
145 );
146 }
147
148 /**
149 * GET /desktop-mode/v1/network
150 *
151 * @return WP_REST_Response
152 */
153 function openstation_rest_network_list() {
154 return rest_ensure_response( openstation_network_hub_list() );
155 }
156
157 /**
158 * The multisite block a single-site hub's shell boots with: itself
159 * first, then its members. A multisite hub builds its own block in
160 * `openstation_multisite_payload()`, where the members are appended
161 * to the network's sites.
162 *
163 * @return array<string,mixed>|null Null when this install has no members.
164 */
165 function openstation_network_hub_payload() {
166 if ( is_multisite() || ! openstation_network_is_hub() ) {
167 return null;
168 }
169 $sites = array();
170 foreach ( array_merge( openstation_network_local_entries(), openstation_network_member_entries() ) as $entry ) {
171 $sites[] = array(
172 'id' => $entry['id'],
173 'name' => $entry['name'],
174 'shellUrl' => $entry['shellUrl'],
175 'kind' => $entry['kind'],
176 'foreign' => 'member' === $entry['kind'],
177 );
178 }
179 return array(
180 'isNetworkAdmin' => false,
181 'networkAdmin' => null,
182 'current' => 'hub',
183 'sites' => $sites,
184 );
185 }
186