Add $is_quote_share parameter to convert share callback

- Restore former behavior when content is present before [share] block
This commit is contained in:
Hypolite Petovan 2018-10-17 08:20:56 -04:00
parent 9239ecc5e0
commit 057385a4ec
1 changed files with 22 additions and 18 deletions

View File

@ -861,13 +861,14 @@ class BBCode extends BaseObject
/** /**
* This function converts a [share] block to text according to a provided callback function whose signature is: * This function converts a [share] block to text according to a provided callback function whose signature is:
* *
* function(array $attributes, array $author_contact, string $content): string * function(array $attributes, array $author_contact, string $content, boolean $is_quote_share): string
* *
* Where: * Where:
* - $attributes is an array of attributes of the [share] block itself. Missing keys will be completed by the contact * - $attributes is an array of attributes of the [share] block itself. Missing keys will be completed by the contact
* data lookup * data lookup
* - $author_contact is a contact record array * - $author_contact is a contact record array
* - $content is the inner content of the [share] block * - $content is the inner content of the [share] block
* - $is_quote_share indicates whether there's any content before the [share] block
* - Return value is the string that should replace the [share] block in the provided text * - Return value is the string that should replace the [share] block in the provided text
* *
* This function is intended to be used by addon connector to format a share block like the target network is expecting it. * This function is intended to be used by addon connector to format a share block like the target network is expecting it.
@ -879,9 +880,9 @@ class BBCode extends BaseObject
public static function convertShare($text, callable $callback) public static function convertShare($text, callable $callback)
{ {
$return = preg_replace_callback( $return = preg_replace_callback(
"/\[share(.*?)\](.*?)\[\/share\]/ism", "/(.*?)\[share(.*?)\](.*?)\[\/share\]/ism",
function ($match) use ($callback) { function ($match) use ($callback) {
$attribute_string = $match[1]; $attribute_string = $match[2];
$attributes = []; $attributes = [];
foreach(['author', 'profile', 'avatar', 'link', 'posted'] as $field) { foreach(['author', 'profile', 'avatar', 'link', 'posted'] as $field) {
@ -905,7 +906,7 @@ class BBCode extends BaseObject
$attributes['avatar'] = ProxyUtils::proxifyUrl($attributes['avatar'], false, ProxyUtils::SIZE_THUMB); $attributes['avatar'] = ProxyUtils::proxifyUrl($attributes['avatar'], false, ProxyUtils::SIZE_THUMB);
} }
return $callback($attributes, $author_contact, $match[2]); return $callback($attributes, $author_contact, $match[3], trim($match[1]) != '');
}, },
$text $text
); );
@ -922,27 +923,28 @@ class BBCode extends BaseObject
* @param array $attributes [share] block attribute values * @param array $attributes [share] block attribute values
* @param array $author_contact Contact row of the shared author * @param array $author_contact Contact row of the shared author
* @param string $content Inner content of the [share] block * @param string $content Inner content of the [share] block
* @param boolean $is_quote_share Whether there is content before the [share] block
* @param integer $simplehtml Mysterious integer value depending on the target network/formatting style * @param integer $simplehtml Mysterious integer value depending on the target network/formatting style
* @return string * @return string
*/ */
private static function convertShareCallback(array $attributes, array $author_contact, $content, $simplehtml) private static function convertShareCallback(array $attributes, array $author_contact, $content, $is_quote_share, $simplehtml)
{ {
$mention = Protocol::formatMention($attributes['profile'], $attributes['author']); $mention = Protocol::formatMention($attributes['profile'], $attributes['author']);
switch ($simplehtml) { switch ($simplehtml) {
case 1: case 1:
$text = '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' <a href="' . $attributes['profile'] . '">' . $mention . '</a>: </p>' . "\n" . '«' . $content . '»'; $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' <a href="' . $attributes['profile'] . '">' . $mention . '</a>: </p>' . "\n" . '«' . $content . '»';
break; break;
case 2: case 2:
$text = '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content; $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content;
break; break;
case 3: // Diaspora case 3: // Diaspora
$headline = '<p><b>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . $mention . ':</b></p>' . "\n"; $headline = '<p><b>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . $mention . ':</b></p>' . "\n";
if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0) { if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0) {
$text = '<p><a href="' . $attributes['link'] . '">' . $attributes['link'] . '</a></p>' . "\n"; $text = ($is_quote_share? '<hr />' : '') . '<p><a href="' . $attributes['link'] . '">' . $attributes['link'] . '</a></p>' . "\n";
} else { } else {
$text = $headline . '<blockquote>' . trim($content) . '</blockquote>' . "\n"; $text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote>' . trim($content) . '</blockquote>' . "\n";
if ($attributes['link'] != '') { if ($attributes['link'] != '') {
$text .= '<p><a href="' . $attributes['link'] . '">[l]</a></p>' . "\n"; $text .= '<p><a href="' . $attributes['link'] . '">[l]</a></p>' . "\n";
@ -955,17 +957,17 @@ class BBCode extends BaseObject
$headline .= L10n::t('<a href="%1$s" target="_blank">%2$s</a> %3$s', $attributes['link'], $mention, $attributes['posted']); $headline .= L10n::t('<a href="%1$s" target="_blank">%2$s</a> %3$s', $attributes['link'], $mention, $attributes['posted']);
$headline .= ':</b></p>' . "\n"; $headline .= ':</b></p>' . "\n";
$text = $headline . '<blockquote class="shared_content">' . trim($content) . '</blockquote>' . "\n"; $text = ($is_quote_share? '<hr />' : '') . $headline . '<blockquote class="shared_content">' . trim($content) . '</blockquote>' . "\n";
break; break;
case 5: case 5:
$text = '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content; $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n" . $content;
break; break;
case 7: // statusnet/GNU Social case 7: // statusnet/GNU Social
$text = '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '</p>' . "\n"; $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' @' . $author_contact['addr'] . ': ' . $content . '</p>' . "\n";
break; break;
case 9: // Google+ case 9: // Google+
$text = '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n"; $text = ($is_quote_share? '<br />' : '') . '<p>' . html_entity_decode('&#x2672; ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ': </p>' . "\n";
$text .= '<p>' . $content . '</p>' . "\n"; $text .= '<p>' . $content . '</p>' . "\n";
if ($attributes['link'] != '') { if ($attributes['link'] != '') {
@ -976,13 +978,15 @@ class BBCode extends BaseObject
// Transforms quoted tweets in rich attachments to avoid nested tweets // Transforms quoted tweets in rich attachments to avoid nested tweets
if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0 && OEmbed::isAllowedURL($attributes['link'])) { if (stripos(normalise_link($attributes['link']), 'http://twitter.com/') === 0 && OEmbed::isAllowedURL($attributes['link'])) {
try { try {
$text = OEmbed::getHTML($attributes['link']); $text = ($is_quote_share? '<br />' : '') . OEmbed::getHTML($attributes['link']);
} catch (Exception $e) { } catch (Exception $e) {
$text = sprintf('[bookmark=%s]%s[/bookmark]', $attributes['link'], $content); $text = ($is_quote_share? '<br />' : '') . sprintf('[bookmark=%s]%s[/bookmark]', $attributes['link'], $content);
} }
} else { } else {
$text = ($is_quote_share? "\n" : '');
$tpl = get_markup_template('shared_content.tpl'); $tpl = get_markup_template('shared_content.tpl');
$text = replace_macros($tpl, [ $text .= replace_macros($tpl, [
'$profile' => $attributes['profile'], '$profile' => $attributes['profile'],
'$avatar' => $attributes['avatar'], '$avatar' => $attributes['avatar'],
'$author' => $attributes['author'], '$author' => $attributes['author'],
@ -1572,8 +1576,8 @@ class BBCode extends BaseObject
// Shared content // Shared content
$text = self::convertShare( $text = self::convertShare(
$text, $text,
function (array $attributes, array $author_contact, string $content) use ($simple_html) { function (array $attributes, array $author_contact, $content, $is_quote_share) use ($simple_html) {
return self::convertShareCallback($attributes, $author_contact, $content, $simple_html); return self::convertShareCallback($attributes, $author_contact, $content, $is_quote_share, $simple_html);
} }
); );