Content
Post
Overview
The Post class is designed to handle and retrieve data from a WordPress post, accommodating different locales and translations. It provides methods to access the content, title, excerpt, description, and metadata of a post. This class ensures that content is properly localized based on the application's current locale.
Class Definition
namespace Toolstation\Content\Services\Wordpress\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Optional;
Properties
private $data: Stores the raw data for the post, typically obtained from the WordPress API.
Constructor
__construct($data)
- Description: Initializes the Post object with data.
- Parameters:
- data (object): The raw data of the post, usually from WordPress API response.
Methods
getData()
- Description: Retrieves the raw data for the post.
- Return Type: Optional
- Returns: The data of the post or an empty Optional if no data is available.
content()
- Description: Gets the content of the post, taking into account localization and translations.
- Return Type: string|null
- Returns: The rendered content of the post in the current locale, or null if no content is available.
title()
- Description: Retrieves the title of the post, localized if translations are available.
- Return Type: string
- Returns: The rendered title of the post in the current locale.
excerpt()
- Description: Retrieves the excerpt of the post, with localization support.
- Return Type: string
- Returns: The rendered excerpt of the post in the current locale.
description()
- Description: Retrieves the description of the post, considering localization and translations.
- Return Type: string
- Returns: The rendered description of the post in the current locale.
meta()
- Description: Retrieves the meta data for the post, localized based on the current locale.
- Return Type: Optional
- Returns: An Optional containing the meta data or an empty Optional if no meta data is available.
metaByKey($key)
- Description: Retrieves a specific meta value by its key.
- Parameters:
- key (string): The meta key to look up.
- Return Type: string
- Returns: The meta value associated with the provided key or an empty string if not found.
getWpMetaValue(array $fields, array $includeEmpty = )
- Description: Retrieves specific meta values for the post, including empty fields if specified.
- Parameters:
- fields (array): An array of meta fields to retrieve.
- includeEmpty (array, optional): Fields to include in the response even if no data is available.
- Return Type: array
- Returns: An array of matched field data, with each field being either the value or false if not available.
private getLocaleMeta()
- Description: Helper method to get the meta data based on the current locale.
- Return Type: object|null
- Returns: The localized meta data if available, otherwise the default meta data.
Note
- The
Postclass is heavily dependent on the locale set in the Laravel application. Ensure thatApp::getLocale()returns the correct locale to fetch the appropriate localized content and meta data. - The methods
content(),title(),excerpt(), anddescription()handle both default and translated data, falling back to default if translation is not available.