Files
piratepoet/lib/Newsletter/Editor/MetaInformationManager.php
Jan Jakeš 54549ff037 Convert variable names to camel case
[MAILPOET-1796]
2020-01-14 15:22:42 +01:00

75 lines
1.9 KiB
PHP

<?php
namespace MailPoet\Newsletter\Editor;
use MailPoet\WP\Functions as WPFunctions;
class MetaInformationManager {
public function appendMetaInformation($content, $post, $args) {
// Append author and categories above and below contents
foreach (['above', 'below'] as $position) {
$positionField = $position . 'Text';
$text = [];
if (isset($args['showAuthor']) && $args['showAuthor'] === $positionField) {
$text[] = self::getPostAuthor(
$post->postAuthor,
$args['authorPrecededBy']
);
}
if (isset($args['showCategories']) && $args['showCategories'] === $positionField) {
$text[] = self::getPostCategories(
$post->ID,
$post->postType,
$args['categoriesPrecededBy']
);
}
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($postId, $postType, $precededBy) {
$precededBy = trim($precededBy);
// Get categories
$categories = WPFunctions::get()->wpGetPostTerms(
$postId,
['category'],
['fields' => 'names']
);
if (!empty($categories)) {
// check if the user specified a label to be displayed before the author's name
if (strlen($precededBy) > 0) {
$content = stripslashes($precededBy) . ' ';
} else {
$content = '';
}
return $content . join(', ', $categories);
} else {
return '';
}
}
private static function getPostAuthor($authorId, $precededBy) {
$authorName = WPFunctions::get()->getTheAuthorMeta('display_name', (int)$authorId);
$precededBy = trim($precededBy);
if (strlen($precededBy) > 0) {
$authorName = stripslashes($precededBy) . ' ' . $authorName;
}
return $authorName;
}
}