Fix addition of dividers, do general refactoring

This commit is contained in:
Tautvidas Sipavičius
2015-09-18 19:01:34 +03:00
parent 6a6d2391c1
commit be76e016b3
5 changed files with 61 additions and 49 deletions

View File

@ -9,40 +9,41 @@ class MetaInformationManager {
// Append author and categories above and below contents // Append author and categories above and below contents
foreach (array('above', 'below') as $position) { foreach (array('above', 'below') as $position) {
$position_field = $position . 'Text'; $position_field = $position . 'Text';
$text = ''; $text = array();
if ($args['showAuthor'] === $position_field) { if ($args['showAuthor'] === $position_field) {
$text .= self::getPostAuthor( $text[] = self::getPostAuthor(
$args['authorPrecededBy'], $post->post_author,
$post->post_author $args['authorPrecededBy']
); );
} }
if ($args['showCategories'] === $position_field) { if ($args['showCategories'] === $position_field) {
if (!empty($text)) $text .= '<br />'; $text[] = self::getPostCategories(
$text .= self::getPostCategories( $post->ID,
$args['categoriesPrecededBy'], $post->post_type,
$post $args['categoriesPrecededBy']
); );
} }
if (!empty($text)) $text = '<p>' . $text . '</p>'; if (!empty($text)) {
if ($position === 'above') $content = $text . $content; $text = '<p>' . implode('<br />', $text) . '</p>';
else if ($position === 'below') $content .= $text; if ($position === 'above') $content = $text . $content;
else if ($position === 'below') $content .= $text;
}
} }
return $content; return $content;
} }
private static function getPostCategories($preceded_by, $post) { private static function getPostCategories($post_id, $post_type, $preceded_by) {
$preceded_by = trim($preceded_by); $preceded_by = trim($preceded_by);
$content = '';
// Get categories // Get categories
$categories = wp_get_post_terms( $categories = wp_get_post_terms(
$post->ID, $post_id,
get_object_taxonomies($post->post_type), get_object_taxonomies($post_type),
array('fields' => 'names') array('fields' => 'names')
); );
if(!empty($categories)) { if(!empty($categories)) {
@ -51,13 +52,13 @@ class MetaInformationManager {
$content = stripslashes($preceded_by) . ' '; $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); $author_name = get_the_author_meta('display_name', (int)$author_id);
$preceded_by = trim($preceded_by); $preceded_by = trim($preceded_by);

View File

@ -8,7 +8,9 @@ class PostContentManager {
const MAX_EXCERPT_LENGTH = 60; const MAX_EXCERPT_LENGTH = 60;
function getContent($post, $displayType) { function getContent($post, $displayType) {
if ($displayType === 'excerpt') { if ($displayType === 'titleOnly') {
return '';
} elseif ($displayType === 'excerpt') {
// get excerpt // get excerpt
if(!empty($post->post_excerpt)) { if(!empty($post->post_excerpt)) {
return $post->post_excerpt; return $post->post_excerpt;
@ -41,6 +43,8 @@ class PostContentManager {
); );
$content = strip_tags($content, implode('',$tags_not_being_stripped)); $content = strip_tags($content, implode('',$tags_not_being_stripped));
$content = wpautop($content); $content = wpautop($content);
return $content;
} }
private function generateExcerpt($content) { private function generateExcerpt($content) {

View File

@ -8,21 +8,20 @@ if(!defined('ABSPATH')) exit;
class PostListTransformer { class PostListTransformer {
function __construct($args) { function __construct($args) {
$this->args = $args || array(); $this->args = $args;
$this->transformer = new PostTransformer($args); $this->transformer = new PostTransformer($args);
} }
function transform($posts) { function transform($posts) {
$total_posts = count($posts);
$results = array(); $results = array();
$use_divider = (bool)$this->args['showDivider']; $use_divider = (bool)$this->args['showDivider'];
foreach ($posts as $index => $post) { foreach ($posts as $index => $post) {
$results = array_merge($this->transformer->transform($post), $results); if ($use_divider && $index > 0) {
$results[] = $this->args['divider'];
if ($use_divider && $index + 1 < $total_posts) {
$results[] = $args['divider'];
} }
$results = array_merge($results, $this->transformer->transform($post));
} }
return $results; return $results;

View File

@ -24,20 +24,30 @@ class PostTransformer {
$structure_transformer = new StructureTransformer(); $structure_transformer = new StructureTransformer();
$structure = $structure_transformer->transform($content, (bool)$this->args['imagePadded']); $structure = $structure_transformer->transform($content, (bool)$this->args['imagePadded']);
$featured_image = $this->getFeaturedImage($post, (bool)$this->args['imagePadded']); $structure = $this->appendFeaturedImage($post, (bool)$this->args['imagePadded'], $structure);
if (is_array($featured_image)) { $structure = $this->appendPostTitle($post, $structure);
$structure = array_merge(array($featured_image), $structure); $structure = $this->appendReadMore($post->ID, $structure);
}
$structure = $this->appendPostTitle($structure, $post);
$structure = $this->appendReadMore($structure, $post->ID);
return $structure; return $structure;
} }
private function getFeaturedImage($post, $image_padded) { private function appendFeaturedImage($post, $image_padded, $structure) {
if(has_post_thumbnail($post->ID)) { $featured_image = $this->getFeaturedImage(
$thumbnail_id = get_post_thumbnail_id($post->ID); $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) // get attachment data (src, width, height)
$image_info = wp_get_attachment_image_src( $image_info = wp_get_attachment_image_src(
@ -53,7 +63,7 @@ class PostTransformer {
))); )));
if(strlen($alt_text) === 0) { if(strlen($alt_text) === 0) {
// if the alt text is empty then use the post title // 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( return array(
@ -73,7 +83,7 @@ class PostTransformer {
} }
} }
private function appendPostTitle($structure, $post) { private function appendPostTitle($post, $structure) {
if ($this->args['titlePosition'] === 'inTextBlock') { if ($this->args['titlePosition'] === 'inTextBlock') {
// Attach title to the first text block // Attach title to the first text block
$text_block_index = null; $text_block_index = null;
@ -91,14 +101,14 @@ class PostTransformer {
'text' => $title, 'text' => $title,
); );
} else { } 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; return $structure;
} }
private function appendReadMore($structure, $post_id) { private function appendReadMore($post_id, $structure) {
if ($this->args['readMoreType'] === 'button') { if ($this->args['readMoreType'] === 'button') {
$button = $this->args['readMoreButton']; $button = $this->args['readMoreButton'];
$button['url'] = get_permalink($post_id); $button['url'] = get_permalink($post_id);

View File

@ -45,18 +45,17 @@ class StructureTransformer {
* turns other root children into text blocks * turns other root children into text blocks
*/ */
private function transformTagsToBlocks($root, $image_padded) { private function transformTagsToBlocks($root, $image_padded) {
$structure = array(); return array_map(function($item) use ($image_padded) {
foreach ($root->children as $item) {
if ($item->tag === 'img' || $item->tag === 'a' && $item->query('img')) { if ($item->tag === 'img' || $item->tag === 'a' && $item->query('img')) {
$link = '';
$image = $item;
if ($item->tag === 'a') { if ($item->tag === 'a') {
$link = $item->getAttribute('href'); $link = $item->getAttribute('href');
$image = $item->children[0]; $image = $item->children[0];
} else {
$link = '';
$image = $item;
} }
$structure[] = array( return array(
'type' => 'image', 'type' => 'image',
'link' => $link, 'link' => $link,
'src' => $image->getAttribute('src'), 'src' => $image->getAttribute('src'),
@ -71,14 +70,13 @@ class StructureTransformer {
), ),
); );
} else { } else {
$structure[] = array( return array(
'type' => 'text', 'type' => 'text',
'text' => $item->toString(), 'text' => $item->toString(),
); );
} }
}
return $structure; }, $root->children);
} }
/** /**