Skip to content

Commit

Permalink
update featured image decoration and bump to v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Feb 6, 2020
1 parent 2ffe20a commit c218095
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 222 deletions.
18 changes: 11 additions & 7 deletions Malini/Abstracts/PostDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@

abstract class PostDecorator implements PostDecoratorInterface
{

public function __construct(Post $post, array $options = []) {
public function __construct(Post $post, array $options = [])
{
$this->decorate($post, $options);

return $post;
}

public function decorate(Post $post, array $_options = []) : Post {
public function decorate(Post $post, array $_options = []): Post
{
return $post;
}

public function getConfig(string $option_name, array $options, $default) {
public function getConfig(string $option_name, array $options, $default)
{
return isset($options[$option_name])
? $options[$option_name]
: $default;
}

public function filterConfig(Post $post, array $options = [], array $default = []) {
public function filterConfig(Post $post, array $options = [], array $default = [])
{
$fields = $this->getConfig('filter', $options, $default);

$post->addShowFields(
FieldsTree::parse($fields)
);
}

}
}
79 changes: 20 additions & 59 deletions Malini/Accessors/MediaAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

/**
* The `media` accessor retrieves media related to the post.
*
*
* Syntax:
*
*
* @media:{size},{media_meta_key_or_id},{media_property_key}
*
*
* - {size}: media size (full, thumbnail, etc.; custom sizes are accepted as
* long as they have been registered);
* - {media_meta_key_or_id}: if the id of the wanted media is saved inside a
Expand All @@ -27,72 +27,33 @@
*/
class MediaAccessor implements AccessorInterface
{

protected static $media_cache = [];

public function getThumbnailData($thumbnail_id) {
$thumbnail_key = 't_' . $thumbnail_id;
if (!isset(static::$media_cache[$thumbnail_key])) {
static::$media_cache[$thumbnail_key] = (array)wp_prepare_attachment_for_js($thumbnail_id);
}
return static::$media_cache[$thumbnail_key];
}

public function retrieve(Post $post, ...$arguments) {
public function retrieve(Post $post, ...$arguments)
{
$wp_post = $post->wp_post;

$size = isset($arguments[0])
? $arguments[0]
: 'full';

$thumbnail_id = (isset($arguments[1]) && $arguments[1] !== 'this')
? (int)get_post_meta($wp_post->ID, $arguments[1], true)
: get_post_thumbnail_id($wp_post->ID);
$media_id = (isset($arguments[1]) && $arguments[1] !== 'this')
? (int) get_post_meta($wp_post->ID, $arguments[1], true)
: get_post_thumbnail_id($wp_post->ID);

if (empty($thumbnail_id)) {
if (empty($media_id)) {
return null;
}

$media_property_key = isset($arguments[2])
? (string)$arguments[2]
: null;

$thumbnail_data = static::getThumbnailData($thumbnail_id);
if (!isset($thumbnail_data['sizes'][$size])) {
$size = 'full';
}

if (empty($thumbnail_data)) {
return null;
}

$selected_size = $thumbnail_data['sizes'][$size];

$media = [
'name' => $thumbnail_data['name'],
'filename' => $thumbnail_data['filename'],
'alt' => $thumbnail_data['alt'],
'caption' => $thumbnail_data['caption'],
'description' => $thumbnail_data['description'],
'originalurl' => $thumbnail_data['url'],
'mime' => $thumbnail_data['mime'],
'type' => $thumbnail_data['type'],
'subtype' => $thumbnail_data['subtype'],
'filesize' => $thumbnail_data['filesizeInBytes'],
'readablefilesize' => $thumbnail_data['filesizeHumanReadable'],
'size' => $size,
'url' => $selected_size['url'],
'width' => $selected_size['width'],
'height' => $selected_size['height'],
'orientation' => $selected_size['orientation'],
'ratio' => $selected_size['width'] / $selected_size['height']
'meta' => get_post($media_id),
];
if (isset($arguments[0])) {
$sizes = explode($arguments[0]);
} else {
$sizes = get_intermediate_image_sizes();
$sizes[] = 'full';
}

if (!empty($media_property_key) && isset($media[$media_property_key])) {
return $media[$media_property_key];
foreach ($sizes as $size) {
$media[$size] = wp_get_attachment_image_src($media_id, $size);
$media[$size][3] = $media[$size][2] / $media[$size][1];
}

return $media;
}

}
}
19 changes: 9 additions & 10 deletions Malini/Decorators/WithFeaturedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,28 @@

/**
* The `featuredimage` decorator enrich the `Malini\Post` with the data regarding the feature selected for this post.
*
*
* Attributes added:
* - `thumbnail`: the featured image data in its `thumbnail` size;
* - `featuredimage`: the featured image data in its `full` size.
*
*
* Options:
* - `filter`: the attributes we want to retrieve
*/
class WithFeaturedImage extends PostDecorator implements PostDecoratorInterface {

public function decorate(Post $post, array $options = []) : Post {
class WithFeaturedImage extends PostDecorator implements PostDecoratorInterface
{
public function decorate(Post $post, array $options = []): Post
{
$post->addRawAttributes([
'thumbnail' => '@media:thumbnail',
'featuredimage' => '@media:full'
'featuredimage' => '@media',
]);

$this->filterConfig(
$post,
$options,
[ 'thumbnail', 'featuredimage' ]
['featuredimage']
);

return $post;
}

}
}
42 changes: 25 additions & 17 deletions Malini/Helpers/AccessorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,49 @@
/**
* Accessors are utilities that helps retrieve data in a structured way.
* They can be described using simple strings with the following syntax (curly
* brackets indicates placeholders):
*
* brackets indicates placeholders):.
*
* @{accessor_name}:{param_1},{param_2},...
*
*
* - {accessor_name}: is the name of the registered accessor;
* - {param_N}: is the N-th param to pass to the accessor.
*
*
* If no {accessor_name} is specified, the `BaseAccessor` will be used.
*
*
* Examples:
*
*
* - 'post_title' // retrieves the post_title, using the BaseAccessor;
* - '@base:post_title' // retrieves the post_title, using the BaseAccessor;
* - '@meta:_edit_lock' // retrieves the _edit_lock post meta MetaAccessor.
*/
class AccessorRegistry
{

protected static $registered_accessors = [];

public static function has(string $name) {
public static function has(string $name)
{
return isset(static::$registered_accessors[$name]);
}

public static function get(string $name) {
public static function get(string $name)
{
if (!static::has($name)) {
throw new \Exception("Unknown `{$name}` accessor");
}

return static::$registered_accessors[$name];
}

public static function register(string $name, $accessor_class_name) {
public static function register(string $name, $accessor_class_name)
{
if (!in_array(AccessorInterface::class, class_implements($accessor_class_name))) {
throw new \Exception("`{$accessor_class_name}` does not implements the `AccessorInterface`");
}
static::$registered_accessors[$name] = new $accessor_class_name();
}

public static function parse(string $source) {
public static function parse(string $source)
{
if (strpos($source, '@') === 0) {
$pieces = explode(':', substr($source, 1), 2);
$accessor = array_shift($pieces);
Expand All @@ -55,23 +59,27 @@ public static function parse(string $source) {
: explode(',', $pieces[0]);
} else {
$accessor = 'base';
$params = [ $source ];
$params = [$source];
}

return [ $accessor, $params ];
return [$accessor, $params];
}

public static function access(Post $post, string $accessor_name, array $params) {
public static function access(Post $post, string $accessor_name, array $params)
{
$accessor = static::get($accessor_name);

return $accessor->retrieve($post, ...$params);
}

public static function retrieve(Post $post, $source) {
public static function retrieve(Post $post, $source)
{
if (is_callable($source)) {
return $source($post);
}

list($accessor_name, $params) = static::parse($source);

return static::access($post, $accessor_name, $params);
}

}
}
Loading

0 comments on commit c218095

Please sign in to comment.