Skip to content

Commit 0566ea9

Browse files
committed
Special Functions
1 parent ef4a651 commit 0566ea9

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

app/Endpoint/AbstractWpEndpoint.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,48 @@ public function get($id = null, array $params = null)
4343
try {
4444

4545
$qs = new QueryString();
46-
$qs->add('_fields', array('id', 'title', 'link', 'author', 'metadata', 'excerpt'));;
46+
47+
$uri = $this->getEndpoint();
48+
$uri .= (is_null($id) ? '' : '/' . $id);
49+
$uri .= (is_null($params) ? '' : '?' . http_build_query($params));
50+
51+
52+
$request = new Request('GET', $uri, [
53+
'connect_timeout' => 10,
54+
'timeout' => 30
55+
]);
56+
57+
$response = $this->client->send($request);
58+
if (
59+
$response->hasHeader('Content-Type')
60+
&& substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json'
61+
) {
62+
$dataRespon = [
63+
'body' => json_decode($response->getBody()->getContents(), true),
64+
'total' => $response->getHeader('X-WP-Total'),
65+
'totalpages' => $response->getHeader('X-WP-TotalPages'),
66+
];
67+
68+
return $dataRespon;
69+
}
70+
} catch (Exception $e) {
71+
return GuzzleAdapter::handleException($e);
72+
}
73+
}
74+
75+
/**
76+
* @param int $id
77+
* @param array $params - parameters that can be passed to GET
78+
* e.g. for tags: https://developer.wordpress.org/rest-api/reference/tags/#arguments
79+
* @return array
80+
* @throws \RuntimeException
81+
*/
82+
public function getPaginations($id = null, array $params = null)
83+
{
84+
try {
85+
86+
$qs = new QueryString();
87+
$qs->add('_fields', array('id', 'title', 'link', 'author', 'metadata', 'excerpt', 'date'));;
4788
$uri = $this->getEndpoint();
4889
$uri .= (is_null($id) ? '' : '/' . $id);
4990
$uri .= (is_null($params) ? '' : '?' . $qs->build() . '&' . http_build_query($params));

0 commit comments

Comments
 (0)