graphClient = new GraphClient(); $secureOptions = new SecureOptions(); if ($systemUserId === null) { $systemUserId = $secureOptions->get('system_user_id', ''); } if ($systemUserToken === null) { $systemUserToken = $secureOptions->get('system_user_token', ''); } $this->systemUserId = $systemUserId; $this->systemUserToken = $systemUserToken; } public static function exists(string $systemUserId, string $userAccessToken): bool { $systemUserId = trim($systemUserId); if ($systemUserId === '') { return false; } $graphClient = new GraphClient(); $response = $graphClient->get($systemUserId, array('fields' => 'id'), $userAccessToken, true); return $response->isSuccessful(); } public function tokenIsValid() { if ($this->systemUserToken === '') { return false; } $response = $this->graphClient->me($this->systemUserToken); if ( ! $response ) { return false; } $app_id = (new PluginSettings())->get_facebook_app_id(); $app_secret = (new SecureOptions())->get('facebook_app_secret', ''); $appToken = $app_id . '|' . $app_secret; $debug_token = $this->graphClient->debugToken($this->systemUserToken, $appToken); if (!isset($debug_token['data'])) { return false; } $data = $debug_token['data']; if ( (string) ($data['app_id'] ?? '') !== $app_id || empty( $data['is_valid'] ) ) { return false; } if ( (string) ($data['type'] ?? '') !== 'SYSTEM_USER' ) { return false; } return !empty( $response['id'] ) && $response['id'] === $this->systemUserId; } public function assignToWaba($wabaId, $businessId, $userAccessToken): bool { $response = $this->graphClient->post( "{$wabaId}/assigned_users", array('user' => $this->systemUserId, 'tasks' => ['MANAGE'], 'business' => $businessId), $userAccessToken, true ); if (!$response->isSuccessful()) { return false; } return true; } public function isAssignedToWaba($businessId, $wabaId, $userAccessToken): bool { $wabaId = trim($wabaId); if ($wabaId === '') { return false; } $response = $this->graphClient->get("{$wabaId}/assigned_users", array('limit' => 100, 'fields' => 'id,name,tasks', 'business' => $businessId), $userAccessToken, true); if (!$response->isSuccessful()) { return false; } $body = $response->getBody(); if (!isset($body['data']) || !is_array($body['data'])) { return false; } foreach ($body['data'] as $row) { if (!is_array($row)) { continue; } $name = $row['name'] ?? null; if (is_string($name) && $name !== ''/* && str_contains($name, self::USER_NAME)*/) { return true; } } return false; } }