Content
Page Service
Page Service
Overview
The Page class is part of the Toolstation\Content\Services\Wordpress\Rest namespace and is designed to interact with the WordPress REST API for page-related operations. It provides methods to retrieve pages by ID or slug, get all child pages of a specific parent page, and a placeholder method for future filtering functionality.
Class Definition
Namespace
namespace Toolstation\Content\Services\Wordpress\Rest;
Constants
- PREFIX: A constant that defines the base path for accessing WordPress page endpoints in the REST API.
public const PREFIX = 'wp/v2/pages';
Properties
- $client: An instance of the
RestClientclass used to perform HTTP requests to the WordPress REST API.protected $client;
Constructor
public function __construct(RestClient $client)
- Parameters:
- RestClient $client: The client used to perform REST API requests.
- Description: Initializes the
Pageservice with aRestClientinstance.
Methods
getById($id)
public function getById($id)
- Description: Retrieves a page based on its ID by making an HTTP GET request to the WordPress REST API endpoint. Wraps the response in a PostModel object. request to the WordPress REST API endpoint using the slug parameter. If no category is found, it aborts with a 404 error. Returns the first matching category from the response.
- Parameters:
- int $id: The ID of the page to retrieve.
- Returns:
- PostModel: An instance of
PostModelrepresenting the page.
- PostModel: An instance of
getBySlug($slug)
public function getBySlug($slug)
- Description: Retrieves a page based on its slug by making an HTTP GET request to the WordPress REST API endpoint. Uses the slug to query for the page and wraps the first result in a
PostModelobject. - Parameters:
- string $slug: The slug of the page to retrieve.
- Returns:
- PostModel: An instance of
PostModelrepresenting the page.
- PostModel: An instance of
getAllChildren($parent)
public function getAllChildren($parent)
- Description: Retrieves all child pages of a specified parent page. It makes an HTTP GET request to the WordPress REST API endpoint using the parent page ID and transforms each child page response into a PostModel object.
- Parameters:
- int $id: The ID of the parent page.
- Returns:
- Collection: A collection of PostModel instances representing the child pages.
filter($args)
public function filter($args)
- Description: A placeholder method intended for future implementation to filter pages based on the provided arguments.
- Parameters:
- array $args: The filtering arguments (placeholder for future implementation).
- Returns:
- mixed: The filtered result (placeholder for future implementation).
Example Usage
$restClient = new RestClient(); // Assuming RestClient is properly instantiated
$pageService = new Page($restClient);
// Retrieve a page by ID
$page = $pageService->getById(123);
// Retrieve a page by slug
$page = $pageService->getBySlug('about-us');
// Retrieve all child pages of a parent page
$children = $pageService->getAllChildren(456);
Notes
- Ensure that the RestClient class is properly implemented and configured to make HTTP requests to the WordPress REST API.
- The PostModel class should be designed to represent a WordPress page effectively.
- The filter method is a placeholder and should be implemented as needed for specific filtering requirements.