db = $db; $this->client = $client; } /** * A pull into a subsite of this network ($subsite, or 0 for a new one) from the other site (a subsite of it when * $remote_subsite > 0). * * @return array{merged: array[], renamed: array[], created: int, removed: string[], total: int} */ public function pull(array $connection, int $subsite, int $remote_subsite): array { if (!is_multisite()) { throw new RelayError('subsite.required', esc_html__('Only a network merges the users it receives.', 'siterelay')); } $list = $this->client->call($connection, 'users.list', '', $remote_subsite > 0 ? ['subsite' => $remote_subsite] : [])['meta']; $users = array_values(array_filter((array) ($list['users'] ?? []), 'is_array')); return UserMerge::preview($this->db, $subsite > 0 ? Scope::subsite($subsite) : null, $users) + ['total' => (int) ($list['total'] ?? count($users))]; } /** * A push from this site (or, on a network, its subsite $subsite) into a subsite of the other network: an existing * one ($remote_subsite) or a new one ($new_subsite). * * @return array{merged: array[], renamed: array[], created: int, removed: string[], total: int} */ public function push(array $connection, int $subsite, int $remote_subsite, ?array $new_subsite): array { $list = ShapeOut::user_list($this->db, is_multisite() ? Scope::subsite($subsite) : Scope::site()); $target = $remote_subsite > 0 ? ['subsite' => $remote_subsite] : ['new_subsite' => (array) $new_subsite]; $answer = $this->client->call($connection, 'users.preview', '', $target + ['users' => $list['users']], '', 60)['meta']; return [ 'merged' => array_values(array_filter((array) ($answer['merged'] ?? []), 'is_array')), 'renamed' => array_values(array_filter((array) ($answer['renamed'] ?? []), 'is_array')), 'created' => (int) ($answer['created'] ?? 0), 'removed' => array_values(array_filter((array) ($answer['removed'] ?? []), 'is_string')), 'total' => $list['total'], ]; } }