PluginProbe
Metricool – Social media and site statistics / 2.1.1
Metricool – Social media and site statistics v2.1.1
2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Http / Metricool / Entities / UserSettings.php

UserSettings.php in Metricool – Social media and site statistics 2.1.1, at app/Http/Metricool/Entities/UserSettings.php

71 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Http\Metricool\Entities;
6
7 use Metricool\Http\Metricool\Exceptions\ApiException;
8 use Metricool\Http\Metricool\MetricoolClient;
9
10 class UserSettings
11 {
12 protected MetricoolClient $client;
13 private string $endpoint = 'v2/settings/users/';
14
15 public function __construct(MetricoolClient $client)
16 {
17 $this->client = $client;
18 $this->endpoint = $this->endpoint . $client->getUserId();
19 }
20
21 /**
22 * @throws ApiException
23 */
24 public function get(): array
25 {
26 $response = $this->client->get($this->endpoint);
27 return ($response['data'] ?? []);
28 }
29
30 /**
31 * @throws ApiException
32 */
33 public function patch(array $data): array
34 {
35 $response = $this->client->patch(
36 $this->getFieldPatchEndpoint($data),
37 $data
38 );
39
40 return ($response['data'] ?? []);
41 }
42
43 /**
44 * This method will return the endpoint to be used for the patch request.
45 * It will append the fields as query parameters to the endpoint.
46 *
47 * @example /v2/settings/users/123?fields=name&fields=language
48 *
49 * @internal We cannot use {@see add_query_arg()} here because it does not
50 * support multiple query parameters with the same name:
51 * {@see https://core.trac.wordpress.org/ticket/51552}
52 */
53 private function getFieldPatchEndpoint(array $payload): string
54 {
55 if (empty($this->endpoint)) {
56 throw new \InvalidArgumentException('Endpoint cannot be empty');
57 }
58
59 $filters = array_keys($payload);
60 $queryString = implode('&', array_map(function ($value) {
61 return 'fields=' . urlencode($value);
62 }, $filters));
63
64 if (empty($queryString)) {
65 return $this->endpoint;
66 }
67
68 return $this->endpoint . '?' . $queryString;
69 }
70 }
71