Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/Endpoint/AbstractWpEndpoint.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,24 @@ public function save(array $data)

throw new RuntimeException('Unexpected response');
}

/**
* @param int $id
* @return array
*/
public function delete($id = null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this code! In my project we need to delete terms and that needs param force = true. Maybe add params?

{
$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');
}
}
34 changes: 34 additions & 0 deletions src/Endpoint/Media.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Vnn\WpApiClient\Endpoint;

use GuzzleHttp\Psr7\Request;
use RuntimeException;

/**
* Class Media
* @package Vnn\WpApiClient\Endpoint
Expand All @@ -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');
}
}