Skip to content

Commit 62a226b

Browse files
authored
Merge branch 'main' into add-exceptions
Signed-off-by: Lyrisbee <Lyrisbee@users.noreply.github.com>
2 parents 7dcca4e + f5f73bf commit 62a226b

File tree

7 files changed

+70
-10
lines changed

7 files changed

+70
-10
lines changed

src/Facades/WordPress.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
* @method static \Storipress\WordPress\WordPress setPassword(string $password)
2727
* @method static \Storipress\WordPress\WordPress userAgent()
2828
* @method static \Storipress\WordPress\WordPress withUserAgent(string $userAgent)
29+
* @method static \Storipress\WordPress\WordPress prefix()
30+
* @method static \Storipress\WordPress\WordPress setPrefix(string $prefix)
31+
* @method static \Storipress\WordPress\WordPress prettyUrl()
32+
* @method static \Storipress\WordPress\WordPress isPrettyUrl()
2933
*/
3034
class WordPress extends Facade
3135
{

src/Requests/Category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function list(): array
2626

2727
return array_map(
2828
fn ($data) => CategoryObject::from($data),
29-
$data
29+
$data,
3030
);
3131
}
3232

src/Requests/GeneralRequest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,21 @@ public function delete(string $path, array $arguments): bool
5656
return $this->request('delete', $path, $arguments);
5757
}
5858

59-
public function getUrl(string $path): string
59+
public function getUrl(string $path, string $prefix, bool $pretty): string
6060
{
61-
return sprintf('%s/wp-json/%s',
61+
if ($pretty) {
62+
return sprintf(
63+
'%s/%s/%s',
64+
rtrim($this->app->site(), '/'),
65+
$prefix,
66+
ltrim($path, '/'),
67+
);
68+
}
69+
70+
return sprintf(
71+
'%s?rest_route=/%s',
6272
rtrim($this->app->site(), '/'),
63-
ltrim($path, '/')
73+
ltrim($path, '/'),
6474
);
6575
}
6676
}

src/Requests/Request.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ protected function request(
5454
$http->withUserAgent($this->app->userAgent());
5555
}
5656

57-
$response = $http->{$method}($this->getUrl($path), $options);
57+
$response = $http->{$method}(
58+
$this->getUrl(
59+
$path,
60+
$this->app->prefix(),
61+
$this->app->isPrettyUrl(),
62+
),
63+
$options,
64+
);
5865

5966
if (!($response instanceof Response)) {
6067
throw $this->unexpectedValueException();
@@ -82,12 +89,23 @@ protected function request(
8289
return $payload;
8390
}
8491

85-
public function getUrl(string $path): string
92+
public function getUrl(string $path, string $prefix, bool $pretty): string
8693
{
87-
return sprintf('%s/wp-json/wp/%s/%s',
94+
if ($pretty) {
95+
return sprintf(
96+
'%s/%s/wp/%s/%s',
97+
rtrim($this->app->site(), '/'),
98+
$prefix,
99+
self::VERSION,
100+
ltrim($path, '/'),
101+
);
102+
}
103+
104+
return sprintf(
105+
'%s?rest_route=/wp/%s/%s',
88106
rtrim($this->app->site(), '/'),
89107
self::VERSION,
90-
ltrim($path, '/')
108+
ltrim($path, '/'),
91109
);
92110
}
93111

src/Requests/Tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function list(): array
2626

2727
return array_map(
2828
fn ($data) => TagObject::from($data),
29-
$data
29+
$data,
3030
);
3131
}
3232

src/Requests/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function list(): array
2626

2727
return array_map(
2828
fn ($data) => UserObject::from($data),
29-
$data
29+
$data,
3030
);
3131
}
3232

src/WordPress.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class WordPress
3131

3232
protected ?string $userAgent = null;
3333

34+
protected string $prefix = 'wp-json';
35+
36+
protected bool $prettyUrl = false;
37+
3438
public function __construct(
3539
public Factory $http,
3640
) {
@@ -91,6 +95,30 @@ public function userAgent(): ?string
9195
return $this->userAgent;
9296
}
9397

98+
public function prefix(): string
99+
{
100+
return $this->prefix;
101+
}
102+
103+
public function setPrefix(string $prefix): static
104+
{
105+
$this->prefix = $prefix;
106+
107+
return $this;
108+
}
109+
110+
public function isPrettyUrl(): bool
111+
{
112+
return $this->prettyUrl;
113+
}
114+
115+
public function prettyUrl(): static
116+
{
117+
$this->prettyUrl = true;
118+
119+
return $this;
120+
}
121+
94122
public function withUserAgent(string $userAgent): static
95123
{
96124
$this->userAgent = $userAgent;

0 commit comments

Comments
 (0)