From 6be6803983a1f52f8e20eb879f1048710a6d34fb Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 May 2021 11:41:10 +0000 Subject: [PATCH] Improved code structure --- src/Model/Item.php | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index 2d481f423f..64bd266c83 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -3275,23 +3275,31 @@ class Item public static function improveSharedDataInBody(array $item) { $shared = BBCode::fetchShareAttributes($item['body']); - if (!empty($shared['link'])) { - $id = self::fetchByLink($shared['link']); - Logger::info('Fetched shared post', ['uri-id' => $item['uri-id'], 'id' => $id, 'author' => $shared['profile'], 'url' => $shared['link'], 'guid' => $shared['guid'], 'callstack' => System::callstack()]); - if ($id) { - $shared_item = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'title', 'body'], ['id' => $id]); - $shared_content = BBCode::getShareOpeningTag($shared_item['author-name'], $shared_item['author-link'], $shared_item['author-avatar'], $shared_item['plink'], $shared_item['created'], $shared_item['guid']); - - if (!empty($shared_item['title'])) { - $shared_content .= '[h3]'.$shared_item['title'].'[/h3]'."\n"; - } - - $shared_content .= $shared_item['body']; - - $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content . '[/share]', $item['body']); - Logger::info('New shared data', ['uri-id' => $item['uri-id'], 'id' => $id, 'shared_item' => $shared_item]); - } + if (empty($shared['link'])) { + return $item['body']; } + + $id = self::fetchByLink($shared['link']); + Logger::info('Fetched shared post', ['uri-id' => $item['uri-id'], 'id' => $id, 'author' => $shared['profile'], 'url' => $shared['link'], 'guid' => $shared['guid'], 'callstack' => System::callstack()]); + if (!$id) { + return $item['body']; + } + + $shared_item = Post::selectFirst(['author-name', 'author-link', 'author-avatar', 'plink', 'created', 'guid', 'title', 'body'], ['id' => $id]); + if (!DBA::isResult($shared_item)) { + return $item['body']; + } + + $shared_content = BBCode::getShareOpeningTag($shared_item['author-name'], $shared_item['author-link'], $shared_item['author-avatar'], $shared_item['plink'], $shared_item['created'], $shared_item['guid']); + + if (!empty($shared_item['title'])) { + $shared_content .= '[h3]'.$shared_item['title'].'[/h3]'."\n"; + } + + $shared_content .= $shared_item['body']; + + $item['body'] = preg_replace("/\[share.*?\](.*)\[\/share\]/ism", $shared_content . '[/share]', $item['body']); + Logger::info('New shared data', ['uri-id' => $item['uri-id'], 'id' => $id, 'shared_item' => $shared_item]); return $item['body']; } }