Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add non pretty url #3

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Facades/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
* @method static \Storipress\WordPress\WordPress setPassword(string $password)
* @method static \Storipress\WordPress\WordPress userAgent()
* @method static \Storipress\WordPress\WordPress withUserAgent(string $userAgent)
* @method static \Storipress\WordPress\WordPress prefix()
* @method static \Storipress\WordPress\WordPress setPrefix(string $prefix)
* @method static \Storipress\WordPress\WordPress prettyUrl()
* @method static \Storipress\WordPress\WordPress isPrettyUrl()
*/
class WordPress extends Facade
{
Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function list(): array

return array_map(
fn ($data) => CategoryObject::from($data),
$data
$data,
);
}

Expand Down
16 changes: 13 additions & 3 deletions src/Requests/GeneralRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ public function delete(string $path, array $arguments): bool
return $this->request('delete', $path, $arguments);
}

public function getUrl(string $path): string
public function getUrl(string $path, string $prefix, bool $pretty): string
{
return sprintf('%s/wp-json/%s',
if ($pretty) {
return sprintf(
'%s/%s/%s',
rtrim($this->app->site(), '/'),
$prefix,
ltrim($path, '/'),
);
}

return sprintf(
'%s?rest_route=/%s',
rtrim($this->app->site(), '/'),
ltrim($path, '/')
ltrim($path, '/'),
);
}
}
32 changes: 22 additions & 10 deletions src/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Storipress\WordPress\Requests;

use Illuminate\Http\Client\Response;
use stdClass;
use Storipress\WordPress\Exceptions\HttpException;
use Storipress\WordPress\Exceptions\HttpUnknownError;
Expand Down Expand Up @@ -45,11 +44,14 @@ protected function request(
$http->withUserAgent($this->app->userAgent());
}

$response = $http->{$method}($this->getUrl($path), $options);

if (!($response instanceof Response)) {
throw new UnexpectedValueException();
}
$response = $http->{$method}(
$this->getUrl(
$path,
$this->app->prefix(),
$this->app->isPrettyUrl(),
),
$options,
);

if (!$response->successful()) {
$this->error(
Expand All @@ -65,20 +67,30 @@ protected function request(

$data = $response->object();

// @phpstan-ignore-next-line
if (!($data instanceof stdClass) && !is_array($data)) {
throw new UnexpectedValueException();
}

return $data;
}

public function getUrl(string $path): string
public function getUrl(string $path, string $prefix, bool $pretty): string
{
return sprintf('%s/wp-json/wp/%s/%s',
if ($pretty) {
return sprintf(
'%s/%s/wp/%s/%s',
rtrim($this->app->site(), '/'),
$prefix,
self::VERSION,
ltrim($path, '/'),
);
}

return sprintf(
'%s?rest_route=/wp/%s/%s',
rtrim($this->app->site(), '/'),
self::VERSION,
ltrim($path, '/')
ltrim($path, '/'),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Requests/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function list(): array

return array_map(
fn ($data) => TagObject::from($data),
$data
$data,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Requests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function list(): array

return array_map(
fn ($data) => UserObject::from($data),
$data
$data,
);
}

Expand Down
28 changes: 28 additions & 0 deletions src/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class WordPress

protected ?string $userAgent = null;

protected string $prefix = 'wp-json';

protected bool $prettyUrl = false;

public function __construct(
public Factory $http,
) {
Expand Down Expand Up @@ -91,6 +95,30 @@ public function userAgent(): ?string
return $this->userAgent;
}

public function prefix(): string
{
return $this->prefix;
}

public function setPrefix(string $prefix): static
{
$this->prefix = $prefix;

return $this;
}

public function isPrettyUrl(): bool
{
return $this->prettyUrl;
}

public function prettyUrl(): static
{
$this->prettyUrl = true;

return $this;
}

public function withUserAgent(string $userAgent): static
{
$this->userAgent = $userAgent;
Expand Down
Loading