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
47 changes: 47 additions & 0 deletions src/Endpoint/AbstractWpSubEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Vnn\WpApiClient\Endpoint;

/**
* Class AbstractWpSubEndpoint.
* Is the base class for endpoints that "have" a parent entity, e.g. post revisions.
* The setParent() method must be called before the get() method.
*
* Example:
* $client->postRevisions()->setParent(45)->get();
*
* @package Vnn\WpApiClient\Endpoint
*/
abstract class AbstractWpSubEndpoint extends AbstractWpEndpoint
{
/**
* The ID of the parent entity.
* @var int
*/
private $parentId = null;

/**
* Set the ID of the parent entity.
* @param int $parent
* @return AbstractWpEndpoint
*/
public function setParent($parent)
{
$this->parentId = $parent;
return $this;
}

/**
* Builds the actual endpoint URL using the provided parent ID and the endpoint pattern.
* @param string $pattern
* @return string
* @throws \LogicException
*/
protected function buildEndpoint($pattern)
{
if ($this->parentId === null) {
throw new \LogicException(static::class . '::setParent() not called!', 1539454211);
}
return sprintf($pattern, $this->parentId);
}
}
18 changes: 18 additions & 0 deletions src/Endpoint/PostRevisions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Vnn\WpApiClient\Endpoint;

/**
* Class PostRevisions
* @package Vnn\WpApiClient\Endpoint
*/
class PostRevisions extends AbstractWpSubEndpoint
{
/**
* {@inheritdoc}
*/
protected function getEndpoint()
{
return $this->buildEndpoint('/wp-json/wp/v2/posts/%d/revisions');
}
}
1 change: 1 addition & 0 deletions src/WpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @method Endpoint\PostTypes postTypes()
* @method Endpoint\Tags tags()
* @method Endpoint\Users users()
* @method Endpoint\PostRevisions postRevisions()
*/
class WpClient
{
Expand Down