Skip to content

Commit f748fc4

Browse files
committed
Fix tags not being properly displayed
1 parent f958848 commit f748fc4

2 files changed

Lines changed: 121 additions & 26 deletions

File tree

qa_hashtagger.php

Lines changed: 119 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ class qa_hashtagger
2626
*/
2727
private static $notification;
2828

29+
/**
30+
* The list of tags to set to the question
31+
*
32+
* @var array
33+
*/
34+
private static $questionTags;
35+
36+
/** @var bool */
37+
private static $newTagsPresent = false;
38+
2939
/**
3040
* Filter question
3141
*
@@ -169,12 +179,12 @@ private function set_notification($question, $oldrow, $type)
169179
*/
170180
private function set_question_tags($question)
171181
{
172-
if (self::$hashtags) {
182+
if (!empty(self::$questionTags)) {
173183
if (isset($question['tags'])) {
174-
$new_tags = array_merge($question['tags'], self::$hashtags);
184+
$new_tags = array_merge($question['tags'], self::$questionTags);
175185
$question['tags'] = array_unique($new_tags);
176186
} else {
177-
$question['tags'] = self::$hashtags;
187+
$question['tags'] = self::$questionTags;
178188
}
179189
}
180190

@@ -210,26 +220,40 @@ private function show_html_links($match)
210220
/**
211221
* Build tag link and set it for global variable
212222
*
213-
* @param array $match
223+
* @param array $matches
224+
* @param boolean $htmlFormat
214225
*
215226
* @return string
216227
*/
217-
private static function build_tag_link($match)
228+
private static function build_tag_link(array $matches, $htmlFormat)
218229
{
219230
require_once QA_INCLUDE_DIR . 'util/string.php';
231+
require_once QA_INCLUDE_DIR . 'db/post-create.php';
232+
233+
$tag = qa_strtolower($matches['word']);
220234

221-
$tag = qa_strtolower($match['word']);
235+
if ($htmlFormat) {
236+
$tag = html_entity_decode($tag);
237+
}
222238

223239
$linkText = qa_opt('plugin_hashtagger/keep_hash_symbol') ? "#" : '';
224240
$linkText .= $tag;
225241

226-
if (!in_array($tag, self::$hashtags)) {
227-
self::$hashtags[] = $tag;
242+
$tagWord = qa_db_single_select(qa_db_tag_word_selectspec($tag));
243+
244+
self::$questionTags[] = $tag;
245+
246+
if (is_null($tagWord)) {
247+
$tagWord = qa_db_word_mapto_ids_add(array($tagWord));
228248
}
229249

230250
$url = qa_html(qa_path_absolute('tag/' . $tag));
231251

232-
return sprintf('<a href="%s">%s</a>', $url, $linkText);
252+
if (!in_array($tag, self::$hashtags)) {
253+
self::$hashtags[$tagWord['wordid']] = $url;
254+
}
255+
256+
return sprintf('${hashtagger-open-link-tag-%s}%s${hashtagger-close-link}', $tagWord['wordid'], $linkText);
233257
}
234258

235259
/**
@@ -240,13 +264,12 @@ private static function build_tag_link($match)
240264
*
241265
* @return string
242266
*/
243-
private static function build_user_link($match, $htmlFormat)
267+
private static function build_user_link(array $match, $htmlFormat)
244268
{
245269
$handle = $match['name'];
246270

247271
if ($htmlFormat) {
248272
$handle = html_entity_decode($handle);
249-
echo $handle;
250273
}
251274

252275
$userid = qa_handle_to_userid($handle);
@@ -313,6 +336,7 @@ private function init_filter(&$row, $type)
313336
// Redefine variables
314337
self::$hashtags = array();
315338
self::$userids = array();
339+
self::$questionTags = array();
316340

317341
$htmlContent = $row['content'];
318342
$isInHtmlFormat = $row['format'] === 'html';
@@ -324,18 +348,12 @@ private function init_filter(&$row, $type)
324348

325349
// Convert hashtags
326350
if ($convert_hashtags) {
327-
$htmlContent = $this->preg_call('%#(?P<word>[\w\-]+?)#%u', 'build_tag_link', $htmlContent);
351+
$htmlContent = $this->parseTags($isInHtmlFormat, $htmlContent);
328352
}
329353

330354
// Convert usernames
331355
if ($convert_usernames) {
332-
$htmlContent = preg_replace_callback(
333-
'%@(?P<name>.+?)[@/+]%u',
334-
function ($matches) use ($isInHtmlFormat) {
335-
return self::build_user_link($matches, $isInHtmlFormat);
336-
},
337-
$htmlContent
338-
);
356+
$htmlContent = $this->parseUsers($isInHtmlFormat, $htmlContent);
339357
}
340358

341359
// Unhide links
@@ -351,17 +369,94 @@ function ($matches) use ($isInHtmlFormat) {
351369
$htmlContent = qa_html($htmlContent, true);
352370
}
353371

372+
$htmlContent = $this->updateUsers($htmlContent);
373+
$htmlContent = $this->updateTags($htmlContent);
374+
$htmlContent = $this->updateCloseTags($htmlContent);
375+
376+
// Let's force the object to use HTML format if it has created links
377+
if (!$isInHtmlFormat) {
378+
$row['format'] = 'html';
379+
}
380+
$row['content'] = $htmlContent;
381+
}
382+
383+
/**
384+
* @param bool $isInHtmlFormat
385+
* @param string $htmlContent
386+
*
387+
* @return string
388+
*/
389+
private function parseTags($isInHtmlFormat, $htmlContent)
390+
{
391+
$htmlContent = preg_replace_callback(
392+
'%#(?P<word>[\w\-]+?)#%u',
393+
function ($matches) use ($isInHtmlFormat) {
394+
return self::build_tag_link($matches, $isInHtmlFormat);
395+
},
396+
$htmlContent
397+
);
398+
399+
return $htmlContent;
400+
}
401+
402+
/**
403+
* @param bool $isInHtmlFormat
404+
* @param string $htmlContent
405+
*
406+
* @return string
407+
*/
408+
private function parseUsers($isInHtmlFormat, $htmlContent)
409+
{
410+
$htmlContent = preg_replace_callback(
411+
'%@(?P<name>.+?)[@/+]%u',
412+
function ($matches) use ($isInHtmlFormat) {
413+
return self::build_user_link($matches, $isInHtmlFormat);
414+
},
415+
$htmlContent
416+
);
417+
418+
return $htmlContent;
419+
}
420+
421+
/**
422+
* @param string $htmlContent
423+
*
424+
* @return string
425+
*/
426+
private function updateUsers($htmlContent)
427+
{
354428
foreach (self::$userids as $userid => $url) {
355429
$openLinkSearch = sprintf('${hashtagger-open-link-user-%s}', $userid);
356430
$replaceWith = sprintf('<a href="%s">', $url);
357431
$htmlContent = str_replace($openLinkSearch, $replaceWith, $htmlContent);
358432
}
359-
$htmlContent = str_replace('${hashtagger-close-link}', '</a>', $htmlContent);
360433

361-
// Let's force the object to use HTML format if it has created links
362-
if (!$isInHtmlFormat) {
363-
$row['format'] = 'html';
434+
return $htmlContent;
435+
}
436+
437+
/**
438+
* @param string $htmlContent
439+
*
440+
* @return string
441+
*/
442+
public function updateTags($htmlContent)
443+
{
444+
foreach (self::$hashtags as $tagId => $url) {
445+
$openLinkSearch = sprintf('${hashtagger-open-link-tag-%s}', $tagId);
446+
$replaceWith = sprintf('<a href="%s">', $url);
447+
$htmlContent = str_replace($openLinkSearch, $replaceWith, $htmlContent);
364448
}
365-
$row['content'] = $htmlContent;
449+
450+
return $htmlContent;
451+
}
452+
453+
/**
454+
* @param $htmlContent
455+
*
456+
* @return string
457+
*/
458+
public function updateCloseTags($htmlContent)
459+
{
460+
return str_replace('${hashtagger-close-link}', '</a>', $htmlContent);
366461
}
367462
}

qa_hashtagger_lang_default.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
'filter_questions' => 'Handle questions',
77
'filter_comments' => 'Handle comments',
88
'filter_answers' => 'Handle answers',
9-
'convert_hashtags' => 'Enable hashtags (convert <i>#some_word</i> to tag link)',
9+
'convert_hashtags' => 'Enable hashtags (convert <i>#some_word#</i> to tag link)',
1010
'keep_hash_symbol' => 'Keep "#" symbol for tags names',
11-
'convert_usernames' => 'Enable mentions (convert <i>@some_name</i> to user profile link)',
11+
'convert_usernames' => 'Enable mentions (convert <i>@some_name@</i> to user profile link)',
1212
'notify_users' => 'Notify the user when it is mentioned',
1313
'user_mentioned_title' => 'You have been mentioned on "^site_title"',
1414
'user_mentioned_body' => '^author_name mentioned you on his message: ^mentioned_url',

0 commit comments

Comments
 (0)