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 media endpoint #4

Merged
merged 5 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "library",
"require": {
"php": "^8.1",
"ext-fileinfo": "*",
"illuminate/http": "^10.0",
"illuminate/support": "^10.0",
"justinrainbow/json-schema": "^5.2"
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidPostIdException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Exceptions;

class InvalidPostIdException extends WordPressException
{
//
}
2 changes: 2 additions & 0 deletions src/Facades/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Facade;
use Storipress\WordPress\Requests\Category;
use Storipress\WordPress\Requests\GeneralRequest;
use Storipress\WordPress\Requests\Media;
use Storipress\WordPress\Requests\Post;
use Storipress\WordPress\Requests\Tag;
use Storipress\WordPress\Requests\User;
Expand All @@ -17,6 +18,7 @@
* @method static Post post()
* @method static Category category()
* @method static Tag tag()
* @method static Media media()
* @method static \Storipress\WordPress\WordPress instance()
* @method static \Storipress\WordPress\WordPress site()
* @method static \Storipress\WordPress\WordPress setSite(string $site)
Expand Down
76 changes: 76 additions & 0 deletions src/Objects/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Objects;

use stdClass;

class Media extends WordPressObject
{
public int $id;

public string $date;

public string $date_gmt;

public Render $guid;

public string $modified;

public string $modified_gmt;

public string $slug;

public string $status;

public string $type;

public string $link;

public Render $title;

public int $author;

public string $comment_status;

public string $ping_status;

public string $template;

public Render $description;

public Render $caption;

/**
* @var array<mixed>
*/
public array $meta;

public string $alt_text;

public string $media_type;

public string $mime_type;

public MediaDetails $media_details;

public ?int $post;

public string $source_url;

public static function from(stdClass $data): static
{
$data->guid = Render::from($data->guid);

$data->title = Render::from($data->title);

$data->description = Render::from($data->description);

$data->caption = Render::from($data->caption);

$data->media_details = MediaDetails::from($data->media_details);

return parent::from($data);
}
}
22 changes: 22 additions & 0 deletions src/Objects/MediaDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Objects;

use stdClass;

class MediaDetails extends WordPressObject
{
public int $width;

public int $height;

public string $file;

public int $filesize;

public stdClass $sizes;

public stdClass $image_meta;
}
108 changes: 108 additions & 0 deletions src/Requests/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Storipress\WordPress\Requests;

use Illuminate\Http\UploadedFile;
use Storipress\WordPress\Exceptions\WordPressException;
use Storipress\WordPress\Objects\Media as MediaObject;

class Media extends Request
{
/**
* https://developer.wordpress.org/rest-api/reference/media/#list-media
*
* @return array<int, MediaObject>
*
* @throws WordPressException
*/
public function list(): array
{
$data = $this->request('get', '/media');

if (!is_array($data)) {
throw $this->unexpectedValueException();
}

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

/**
* https://developer.wordpress.org/rest-api/reference/media/#create-a-media-item
*
* @param array<string, mixed> $arguments
*
* @throws WordPressException
*/
public function create(UploadedFile $file, array $arguments): MediaObject
{
$arguments['file'] = $file;

$data = $this->request('post', '/media', $arguments);

if (is_array($data)) {
throw $this->unexpectedValueException();
}

return MediaObject::from($data);
}

/**
* https://developer.wordpress.org/rest-api/reference/media/#retrieve-a-media-item
*
*
* @throws WordPressException
*/
public function retrieve(int $mediaId, string $context = 'view'): MediaObject
{
$uri = sprintf('/media/%d', $mediaId);

$data = $this->request('get', $uri, [
'context' => $context,
]);

if (is_array($data)) {
throw $this->unexpectedValueException();
}

return MediaObject::from($data);
}

/**
* https://developer.wordpress.org/rest-api/reference/media/#update-a-media-item
*
* @param array<string, mixed> $arguments
*
* @throws WordPressException
*/
public function update(int $mediaId, array $arguments): MediaObject
{
$uri = sprintf('/media/%d', $mediaId);

$data = $this->request('patch', $uri, $arguments);

if (is_array($data)) {
throw $this->unexpectedValueException();
}

return MediaObject::from($data);
}

/**
* https://developer.wordpress.org/rest-api/reference/media/#delete-a-media-item
*
* @throws WordPressException
*/
public function delete(int $mediaId): bool
{
$uri = sprintf('/media/%s', $mediaId);

return $this->request('delete', $uri, [
'force' => true,
]);
}
}
11 changes: 10 additions & 1 deletion src/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Storipress\WordPress\Requests;

use Illuminate\Http\Client\Response;
use Illuminate\Http\UploadedFile;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
use stdClass;
Expand All @@ -13,6 +14,7 @@
use Storipress\WordPress\Exceptions\CannotUpdateException;
use Storipress\WordPress\Exceptions\DuplicateTermSlugException;
use Storipress\WordPress\Exceptions\ForbiddenException;
use Storipress\WordPress\Exceptions\InvalidPostIdException;
use Storipress\WordPress\Exceptions\NoRouteException;
use Storipress\WordPress\Exceptions\NotFoundException;
use Storipress\WordPress\Exceptions\TermExistsException;
Expand All @@ -36,7 +38,7 @@ public function __construct(
/**
* @param 'get'|'post'|'patch'|'delete' $method
* @param non-empty-string $path
* @param array<mixed> $options
* @param array<string, mixed> $options
* @return ($method is 'delete' ? bool : stdClass|array<int, stdClass>)
*
* @throws UnexpectedValueException|WordPressException
Expand All @@ -55,6 +57,12 @@ protected function request(
$http->withUserAgent($this->app->userAgent());
}

if (isset($options['file']) && $options['file'] instanceof UploadedFile) {
$http->attach('file', $options['file']->getContent(), $options['file']->getClientOriginalName());

unset($options['file']);
}

$response = $http->{$method}(
$this->getUrl(
$path,
Expand Down Expand Up @@ -124,6 +132,7 @@ protected function error(stdClass $payload, string $message, int $status): void
'rest_cannot_create' => new CannotCreateException($error, $status),
'rest_cannot_update' => new CannotUpdateException($error, $status),
'rest_no_route' => new NoRouteException($error, $status),
'rest_post_invalid_id' => new InvalidPostIdException($error, $status),
default => new UnknownException($error, $status),
};
}
Expand Down
10 changes: 10 additions & 0 deletions src/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Client\Factory;
use Storipress\WordPress\Requests\Category;
use Storipress\WordPress\Requests\GeneralRequest;
use Storipress\WordPress\Requests\Media;
use Storipress\WordPress\Requests\Post;
use Storipress\WordPress\Requests\Tag;
use Storipress\WordPress\Requests\User;
Expand All @@ -23,6 +24,8 @@ class WordPress

protected readonly Tag $tag;

protected readonly Media $media;

protected string $site;

protected string $username;
Expand All @@ -47,6 +50,8 @@ public function __construct(
$this->category = new Category($this);

$this->tag = new Tag($this);

$this->media = new Media($this);
}

public function instance(): static
Expand Down Expand Up @@ -150,4 +155,9 @@ public function tag(): Tag
{
return $this->tag;
}

public function media(): Media
{
return $this->media;
}
}
Loading