LayoutHelper.php
3 years ago
MetaInformationManager.php
2 months ago
PostContentManager.php
11 months ago
PostListTransformer.php
3 years ago
PostTransformer.php
1 year ago
PostTransformerContentsExtractor.php
2 months ago
StructureTransformer.php
3 years ago
TitleListTransformer.php
1 year ago
Transformer.php
3 years ago
index.php
3 years ago
MetaInformationManager.php
90 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Newsletter\Editor; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class MetaInformationManager { |
| 11 | public function appendMetaInformation($content, $post, $args) { |
| 12 | if ($this->isWcProduct($post)) { |
| 13 | $postId = $post->get_id(); |
| 14 | $postAuthor = null; // Don't display author for WC products |
| 15 | $postType = 'product'; |
| 16 | } else { |
| 17 | $postId = $post->ID; |
| 18 | $postAuthor = $post->post_author; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 19 | $postType = $post->post_type; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 20 | } |
| 21 | |
| 22 | // Append author and categories above and below contents |
| 23 | foreach (['above', 'below'] as $position) { |
| 24 | $positionField = $position . 'Text'; |
| 25 | $text = []; |
| 26 | |
| 27 | if (isset($args['showAuthor']) && $args['showAuthor'] === $positionField) { |
| 28 | $text[] = self::getPostAuthor( |
| 29 | $postAuthor, |
| 30 | $args['authorPrecededBy'] |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | if (isset($args['showCategories']) && $args['showCategories'] === $positionField) { |
| 35 | $text[] = self::getPostCategories( |
| 36 | $postId, |
| 37 | $postType, |
| 38 | $args['categoriesPrecededBy'] |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | if (!empty($text)) { |
| 43 | $text = '<p>' . implode('<br />', $text) . '</p>'; |
| 44 | if ($position === 'above') $content = $text . $content; |
| 45 | else $content .= $text; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return $content; |
| 50 | } |
| 51 | |
| 52 | private static function getPostCategories($postId, $postType, $precededBy) { |
| 53 | $precededBy = trim($precededBy); |
| 54 | |
| 55 | // Get categories |
| 56 | $categories = WPFunctions::get()->wpGetPostTerms( |
| 57 | $postId, |
| 58 | ['category'], |
| 59 | ['fields' => 'names'] |
| 60 | ); |
| 61 | if (!empty($categories)) { |
| 62 | // check if the user specified a label to be displayed before the author's name |
| 63 | if (strlen($precededBy) > 0) { |
| 64 | $content = stripslashes($precededBy) . ' '; |
| 65 | } else { |
| 66 | $content = ''; |
| 67 | } |
| 68 | |
| 69 | return $content . join(', ', $categories); |
| 70 | } else { |
| 71 | return ''; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private static function getPostAuthor($authorId, $precededBy) { |
| 76 | $authorName = WPFunctions::get()->getTheAuthorMeta('display_name', (int)$authorId); |
| 77 | |
| 78 | $precededBy = trim($precededBy); |
| 79 | if (strlen($precededBy) > 0) { |
| 80 | $authorName = stripslashes($precededBy) . ' ' . $authorName; |
| 81 | } |
| 82 | |
| 83 | return $authorName; |
| 84 | } |
| 85 | |
| 86 | private function isWcProduct($post) { |
| 87 | return class_exists('\WC_Product') && $post instanceof \WC_Product; |
| 88 | } |
| 89 | } |
| 90 |