Improve Model\Item::addLanguageInPostopts()

- Use Content\BBCode::toPlaintext method
- Rename $arr parameter to $item
- Rename $lng variable to $languages
This commit is contained in:
Hypolite Petovan 2018-04-21 04:11:32 -04:00
parent ec9baef968
commit abc50eb3ae
1 changed files with 11 additions and 11 deletions

View File

@ -978,35 +978,35 @@ class Item extends BaseObject
* if possible and not already present. * if possible and not already present.
* Expects "body" element to exist in $arr. * Expects "body" element to exist in $arr.
*/ */
private static function addLanguageInPostopts(&$arr) private static function addLanguageInPostopts(&$item)
{ {
if (x($arr, 'postopts')) { if (!empty($item['postopts'])) {
if (strstr($arr['postopts'], 'lang=')) { if (strstr($item['postopts'], 'lang=')) {
// do not override // do not override
return; return;
} }
$postopts = $arr['postopts']; $postopts = $item['postopts'];
} else { } else {
$postopts = ""; $postopts = "";
} }
$naked_body = preg_replace('/\[(.+?)\]/','', $arr['body']); $naked_body = Text\BBCode::toPlaintext($item['body'], false);
$l = new Text_LanguageDetect();
$lng = $l->detect($naked_body, 3);
if (sizeof($lng) > 0) { $languages = (new Text_LanguageDetect())->detect($naked_body, 3);
if ($postopts != "") {
if (sizeof($languages) > 0) {
if ($postopts != '') {
$postopts .= '&'; // arbitrary separator, to be reviewed $postopts .= '&'; // arbitrary separator, to be reviewed
} }
$postopts .= 'lang='; $postopts .= 'lang=';
$sep = ""; $sep = "";
foreach ($lng as $language => $score) { foreach ($languages as $language => $score) {
$postopts .= $sep . $language . ";" . $score; $postopts .= $sep . $language . ";" . $score;
$sep = ':'; $sep = ':';
} }
$arr['postopts'] = $postopts; $item['postopts'] = $postopts;
} }
} }