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..83348ea --- 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 function 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'); + } }