From 2dec4ef13f6b3aa7b85303325df76ec499bf53f8 Mon Sep 17 00:00:00 2001 From: Kornel Kovacs Date: Tue, 26 Mar 2019 12:40:08 +0100 Subject: [PATCH 1/2] page/post delete + upload to media library --- src/Endpoint/AbstractWpEndpoint.php | 20 +++++++++++++++++ src/Endpoint/Media.php | 34 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) mode change 100644 => 100755 src/Endpoint/AbstractWpEndpoint.php mode change 100644 => 100755 src/Endpoint/Media.php diff --git a/src/Endpoint/AbstractWpEndpoint.php b/src/Endpoint/AbstractWpEndpoint.php old mode 100644 new mode 100755 index d1664b6..8fefe00 --- a/src/Endpoint/AbstractWpEndpoint.php +++ b/src/Endpoint/AbstractWpEndpoint.php @@ -74,4 +74,24 @@ public function save(array $data) throw new RuntimeException('Unexpected response'); } + + /** + * @param int $id + * @return array + */ + public function delete($id = null) + { + $uri = $this->getEndpoint(); + $uri .= (is_null($id)?'': '/' . $id); + + $request = new Request('DELETE', $uri); + $response = $this->client->send($request); + + if ($response->hasHeader('Content-Type') + && substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') { + return json_decode($response->getBody()->getContents(), true); + } + + throw new RuntimeException('Unexpected response'); + } } diff --git a/src/Endpoint/Media.php b/src/Endpoint/Media.php old mode 100644 new mode 100755 index 6647ffd..b69b498 --- a/src/Endpoint/Media.php +++ b/src/Endpoint/Media.php @@ -2,6 +2,9 @@ namespace Vnn\WpApiClient\Endpoint; +use GuzzleHttp\Psr7\Request; +use RuntimeException; + /** * Class Media * @package Vnn\WpApiClient\Endpoint @@ -15,4 +18,35 @@ protected function getEndpoint() { return '/wp-json/wp/v2/media'; } + + /** + * @param string $filePath - absolute path of file to upload + * @param array $data + * @return array + */ + + public upload($filePath, $data = []) + { + $url = $this->getEndpoint(); + + if (isset($data['id'])) { + $url .= '/' . $data['id']; + unset($data['id']); + } + + $fileName = basename($filePath); + $fileHandle = fopen($filePath, "r"); + + if ($fileHandle !== false) { + $mimeType = mime_content_type($filePath); + $request = new \GuzzleHttp\Psr7\Request('POST', $url, ['Content-Type' => $mimeType, 'Content-Disposition' => 'attachment; filename="'.$fileName.'"'], $fileHandle); + $response = $this->client->send($request); + fclose($fileHandle); + if ($response->hasHeader('Content-Type') + && substr($response->getHeader('Content-Type')[0], 0, 16) === 'application/json') { + return json_decode($response->getBody()->getContents(), true); + } + } + throw new RuntimeException('Unexpected response'); + } } From 9dd5c7a5e3145037f168b8f29db05e94e392b513 Mon Sep 17 00:00:00 2001 From: Kornel Kovacs Date: Mon, 20 May 2019 08:16:22 +0200 Subject: [PATCH 2/2] function definition fix --- src/Endpoint/Media.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Endpoint/Media.php b/src/Endpoint/Media.php index b69b498..83348ea 100755 --- a/src/Endpoint/Media.php +++ b/src/Endpoint/Media.php @@ -25,7 +25,7 @@ protected function getEndpoint() * @return array */ - public upload($filePath, $data = []) + public function upload($filePath, $data = []) { $url = $this->getEndpoint();