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 2 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 basePath()
* @method static \Storipress\WordPress\WordPress setBasePath(string $basePath)
* @method static \Storipress\WordPress\WordPress nonPrettyUrl()
* @method static \Storipress\WordPress\WordPress isPrettyUrl()
*/
class WordPress extends Facade
{
Expand Down
12 changes: 10 additions & 2 deletions src/Requests/GeneralRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ 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 $basePath, bool $pretty): string
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
{
return sprintf('%s/wp-json/%s',
if ($pretty) {
return sprintf('%s/%s/%s',
rtrim($this->app->site(), '/'),
$basePath,
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
ltrim($path, '/')
);
}

return sprintf('%s?rest_route=%s',
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
rtrim($this->app->site(), '/'),
ltrim($path, '/')
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down
24 changes: 15 additions & 9 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,10 @@ 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->basePath(), $this->app->isPrettyUrl()),
$options
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
);

if (!$response->successful()) {
$this->error(
Expand All @@ -65,17 +63,25 @@ 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 $basePath, bool $pretty): string
{
return sprintf('%s/wp-json/wp/%s/%s',
if ($pretty) {
return sprintf('%s/%s/wp/%s/%s',
rtrim($this->app->site(), '/'),
$basePath,
self::VERSION,
ltrim($path, '/')
);
Lyrisbee marked this conversation as resolved.
Show resolved Hide resolved
}

return sprintf('%s?rest_route=/wp/%s/%s',
rtrim($this->app->site(), '/'),
self::VERSION,
ltrim($path, '/')
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 $basePath = 'wp-json';

protected bool $prettyUrl = true;

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

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

public function setBasePath(string $basePath): static
{
$this->basePath = $basePath;

return $this;
}

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

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

return $this;
}

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