Fix addition of dividers, do general refactoring
This commit is contained in:
@ -9,40 +9,41 @@ class MetaInformationManager {
|
||||
// Append author and categories above and below contents
|
||||
foreach (array('above', 'below') as $position) {
|
||||
$position_field = $position . 'Text';
|
||||
$text = '';
|
||||
$text = array();
|
||||
|
||||
if ($args['showAuthor'] === $position_field) {
|
||||
$text .= self::getPostAuthor(
|
||||
$args['authorPrecededBy'],
|
||||
$post->post_author
|
||||
$text[] = self::getPostAuthor(
|
||||
$post->post_author,
|
||||
$args['authorPrecededBy']
|
||||
);
|
||||
}
|
||||
|
||||
if ($args['showCategories'] === $position_field) {
|
||||
if (!empty($text)) $text .= '<br />';
|
||||
$text .= self::getPostCategories(
|
||||
$args['categoriesPrecededBy'],
|
||||
$post
|
||||
$text[] = self::getPostCategories(
|
||||
$post->ID,
|
||||
$post->post_type,
|
||||
$args['categoriesPrecededBy']
|
||||
);
|
||||
}
|
||||
|
||||
if (!empty($text)) $text = '<p>' . $text . '</p>';
|
||||
if ($position === 'above') $content = $text . $content;
|
||||
else if ($position === 'below') $content .= $text;
|
||||
if (!empty($text)) {
|
||||
$text = '<p>' . implode('<br />', $text) . '</p>';
|
||||
if ($position === 'above') $content = $text . $content;
|
||||
else if ($position === 'below') $content .= $text;
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
private static function getPostCategories($preceded_by, $post) {
|
||||
private static function getPostCategories($post_id, $post_type, $preceded_by) {
|
||||
$preceded_by = trim($preceded_by);
|
||||
$content = '';
|
||||
|
||||
// Get categories
|
||||
$categories = wp_get_post_terms(
|
||||
$post->ID,
|
||||
get_object_taxonomies($post->post_type),
|
||||
$post_id,
|
||||
get_object_taxonomies($post_type),
|
||||
array('fields' => 'names')
|
||||
);
|
||||
if(!empty($categories)) {
|
||||
@ -51,13 +52,13 @@ class MetaInformationManager {
|
||||
$content = stripslashes($preceded_by) . ' ';
|
||||
}
|
||||
|
||||
$content .= join(', ', $categories);
|
||||
return join(', ', $categories);
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private static function getPostAuthor($preceded_by, $author_id) {
|
||||
private static function getPostAuthor($author_id, $preceded_by) {
|
||||
$author_name = get_the_author_meta('display_name', (int)$author_id);
|
||||
|
||||
$preceded_by = trim($preceded_by);
|
||||
|
@ -8,7 +8,9 @@ class PostContentManager {
|
||||
const MAX_EXCERPT_LENGTH = 60;
|
||||
|
||||
function getContent($post, $displayType) {
|
||||
if ($displayType === 'excerpt') {
|
||||
if ($displayType === 'titleOnly') {
|
||||
return '';
|
||||
} elseif ($displayType === 'excerpt') {
|
||||
// get excerpt
|
||||
if(!empty($post->post_excerpt)) {
|
||||
return $post->post_excerpt;
|
||||
@ -41,6 +43,8 @@ class PostContentManager {
|
||||
);
|
||||
$content = strip_tags($content, implode('',$tags_not_being_stripped));
|
||||
$content = wpautop($content);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function generateExcerpt($content) {
|
||||
|
@ -8,21 +8,20 @@ if(!defined('ABSPATH')) exit;
|
||||
class PostListTransformer {
|
||||
|
||||
function __construct($args) {
|
||||
$this->args = $args || array();
|
||||
$this->args = $args;
|
||||
$this->transformer = new PostTransformer($args);
|
||||
}
|
||||
|
||||
function transform($posts) {
|
||||
$total_posts = count($posts);
|
||||
$results = array();
|
||||
$use_divider = (bool)$this->args['showDivider'];
|
||||
|
||||
foreach ($posts as $index => $post) {
|
||||
$results = array_merge($this->transformer->transform($post), $results);
|
||||
|
||||
if ($use_divider && $index + 1 < $total_posts) {
|
||||
$results[] = $args['divider'];
|
||||
if ($use_divider && $index > 0) {
|
||||
$results[] = $this->args['divider'];
|
||||
}
|
||||
|
||||
$results = array_merge($results, $this->transformer->transform($post));
|
||||
}
|
||||
|
||||
return $results;
|
||||
|
@ -24,20 +24,30 @@ class PostTransformer {
|
||||
$structure_transformer = new StructureTransformer();
|
||||
$structure = $structure_transformer->transform($content, (bool)$this->args['imagePadded']);
|
||||
|
||||
$featured_image = $this->getFeaturedImage($post, (bool)$this->args['imagePadded']);
|
||||
if (is_array($featured_image)) {
|
||||
$structure = array_merge(array($featured_image), $structure);
|
||||
}
|
||||
|
||||
$structure = $this->appendPostTitle($structure, $post);
|
||||
$structure = $this->appendReadMore($structure, $post->ID);
|
||||
$structure = $this->appendFeaturedImage($post, (bool)$this->args['imagePadded'], $structure);
|
||||
$structure = $this->appendPostTitle($post, $structure);
|
||||
$structure = $this->appendReadMore($post->ID, $structure);
|
||||
|
||||
return $structure;
|
||||
}
|
||||
|
||||
private function getFeaturedImage($post, $image_padded) {
|
||||
if(has_post_thumbnail($post->ID)) {
|
||||
$thumbnail_id = get_post_thumbnail_id($post->ID);
|
||||
private function appendFeaturedImage($post, $image_padded, $structure) {
|
||||
$featured_image = $this->getFeaturedImage(
|
||||
$post->ID,
|
||||
$post->post_title,
|
||||
(bool)$image_padded
|
||||
);
|
||||
|
||||
if (is_array($featured_image)) {
|
||||
return array_merge(array($featured_image), $structure);
|
||||
}
|
||||
|
||||
return $structure;
|
||||
}
|
||||
|
||||
private function getFeaturedImage($post_id, $post_title, $image_padded) {
|
||||
if(has_post_thumbnail($post_id)) {
|
||||
$thumbnail_id = get_post_thumbnail_id($post_id);
|
||||
|
||||
// get attachment data (src, width, height)
|
||||
$image_info = wp_get_attachment_image_src(
|
||||
@ -53,7 +63,7 @@ class PostTransformer {
|
||||
)));
|
||||
if(strlen($alt_text) === 0) {
|
||||
// if the alt text is empty then use the post title
|
||||
$alt_text = trim(strip_tags($post->post_title));
|
||||
$alt_text = trim(strip_tags($post_title));
|
||||
}
|
||||
|
||||
return array(
|
||||
@ -73,7 +83,7 @@ class PostTransformer {
|
||||
}
|
||||
}
|
||||
|
||||
private function appendPostTitle($structure, $post) {
|
||||
private function appendPostTitle($post, $structure) {
|
||||
if ($this->args['titlePosition'] === 'inTextBlock') {
|
||||
// Attach title to the first text block
|
||||
$text_block_index = null;
|
||||
@ -91,14 +101,14 @@ class PostTransformer {
|
||||
'text' => $title,
|
||||
);
|
||||
} else {
|
||||
$structure[$text_block_index]['text'] = $title . $updated_structure[$text_block_index]['text'];
|
||||
$structure[$text_block_index]['text'] = $title . $structure[$text_block_index]['text'];
|
||||
}
|
||||
}
|
||||
|
||||
return $structure;
|
||||
}
|
||||
|
||||
private function appendReadMore($structure, $post_id) {
|
||||
private function appendReadMore($post_id, $structure) {
|
||||
if ($this->args['readMoreType'] === 'button') {
|
||||
$button = $this->args['readMoreButton'];
|
||||
$button['url'] = get_permalink($post_id);
|
||||
|
@ -45,18 +45,17 @@ class StructureTransformer {
|
||||
* turns other root children into text blocks
|
||||
*/
|
||||
private function transformTagsToBlocks($root, $image_padded) {
|
||||
$structure = array();
|
||||
|
||||
foreach ($root->children as $item) {
|
||||
return array_map(function($item) use ($image_padded) {
|
||||
if ($item->tag === 'img' || $item->tag === 'a' && $item->query('img')) {
|
||||
$link = '';
|
||||
$image = $item;
|
||||
if ($item->tag === 'a') {
|
||||
$link = $item->getAttribute('href');
|
||||
$image = $item->children[0];
|
||||
} else {
|
||||
$link = '';
|
||||
$image = $item;
|
||||
}
|
||||
|
||||
$structure[] = array(
|
||||
return array(
|
||||
'type' => 'image',
|
||||
'link' => $link,
|
||||
'src' => $image->getAttribute('src'),
|
||||
@ -71,14 +70,13 @@ class StructureTransformer {
|
||||
),
|
||||
);
|
||||
} else {
|
||||
$structure[] = array(
|
||||
return array(
|
||||
'type' => 'text',
|
||||
'text' => $item->toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $structure;
|
||||
}, $root->children);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user