From be76e016b3fbcfab8d25863805f966e5bb1f6408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tautvidas=20Sipavi=C4=8Dius?= Date: Fri, 18 Sep 2015 19:01:34 +0300 Subject: [PATCH] Fix addition of dividers, do general refactoring --- .../Editor/MetaInformationManager.php | 39 ++++++++++--------- lib/Newsletter/Editor/PostContentManager.php | 6 ++- lib/Newsletter/Editor/PostListTransformer.php | 11 +++--- lib/Newsletter/Editor/PostTransformer.php | 38 +++++++++++------- .../Editor/StructureTransformer.php | 16 ++++---- 5 files changed, 61 insertions(+), 49 deletions(-) diff --git a/lib/Newsletter/Editor/MetaInformationManager.php b/lib/Newsletter/Editor/MetaInformationManager.php index 8b4abfca73..9a3bb71bc2 100644 --- a/lib/Newsletter/Editor/MetaInformationManager.php +++ b/lib/Newsletter/Editor/MetaInformationManager.php @@ -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 .= '
'; - $text .= self::getPostCategories( - $args['categoriesPrecededBy'], - $post + $text[] = self::getPostCategories( + $post->ID, + $post->post_type, + $args['categoriesPrecededBy'] ); } - if (!empty($text)) $text = '

' . $text . '

'; - if ($position === 'above') $content = $text . $content; - else if ($position === 'below') $content .= $text; + if (!empty($text)) { + $text = '

' . implode('
', $text) . '

'; + 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); diff --git a/lib/Newsletter/Editor/PostContentManager.php b/lib/Newsletter/Editor/PostContentManager.php index c1df15d0d8..c06c94eac6 100644 --- a/lib/Newsletter/Editor/PostContentManager.php +++ b/lib/Newsletter/Editor/PostContentManager.php @@ -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) { diff --git a/lib/Newsletter/Editor/PostListTransformer.php b/lib/Newsletter/Editor/PostListTransformer.php index 6ff9f05db1..1728cfd874 100644 --- a/lib/Newsletter/Editor/PostListTransformer.php +++ b/lib/Newsletter/Editor/PostListTransformer.php @@ -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; diff --git a/lib/Newsletter/Editor/PostTransformer.php b/lib/Newsletter/Editor/PostTransformer.php index ecb527457a..5db4c34e20 100644 --- a/lib/Newsletter/Editor/PostTransformer.php +++ b/lib/Newsletter/Editor/PostTransformer.php @@ -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); diff --git a/lib/Newsletter/Editor/StructureTransformer.php b/lib/Newsletter/Editor/StructureTransformer.php index 8f1e7141fd..b6e6a9f413 100644 --- a/lib/Newsletter/Editor/StructureTransformer.php +++ b/lib/Newsletter/Editor/StructureTransformer.php @@ -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); } /**