use anonymous function

This commit is contained in:
Jean-Christian Paul Denis 2022-12-01 10:39:06 +01:00
parent 09131d5593
commit 9669c42338
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951

View File

@ -14,226 +14,206 @@ if (!defined('DC_RC_PATH')) {
return null; return null;
} }
dcCore::app()->addBehavior( dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, context $_ctx) {
'publicEntryAfterContent', dcCore::app()->blog->settings->addNamespace('fac');
['facPublic', 'publicEntryAfterContent']
);
/** # Not active or not a post
* @ingroup DC_PLUGIN_FAC if (!dcCore::app()->blog->settings->fac->fac_active
* @brief Linked feed to entries - public methods. || !dcCore::app()->ctx->exists('posts')) {
* @since 2.6 return null;
*/ }
class facPublic
{
/**
* Add feed after entry
*
* @param dcCore $core dcCore instance
* @param context $_ctx context instance
*/
public static function publicEntryAfterContent(dcCore $core, context $_ctx)
{
dcCore::app()->blog->settings->addNamespace('fac');
# Not active or not a post # Not in page to show
if (!dcCore::app()->blog->settings->fac->fac_active $types = @unserialize((string) dcCore::app()->blog->settings->fac->fac_public_tpltypes);
|| !dcCore::app()->ctx->exists('posts')) { if (!is_array($types)
return null; || !in_array(dcCore::app()->url->type, $types)) {
} return null;
}
# Not in page to show # Get related feed
$types = @unserialize((string) dcCore::app()->blog->settings->fac->fac_public_tpltypes); $fac_url = dcCore::app()->meta->getMetadata([
if (!is_array($types) 'meta_type' => 'fac',
|| !in_array(dcCore::app()->url->type, $types)) { 'post_id' => dcCore::app()->ctx->posts->post_id,
return null; 'limit' => 1,
} ]);
if ($fac_url->isEmpty()) {
return null;
}
# Get related feed # Get related format
$fac_url = dcCore::app()->meta->getMetadata([ $fac_format = dcCore::app()->meta->getMetadata([
'meta_type' => 'fac', 'meta_type' => 'facformat',
'post_id' => dcCore::app()->ctx->posts->post_id, 'post_id' => dcCore::app()->ctx->posts->post_id,
'limit' => 1, 'limit' => 1,
]); ]);
if ($fac_url->isEmpty()) { if ($fac_format->isEmpty()) {
return null; return null;
} }
# Get related format # Get format info
$fac_format = dcCore::app()->meta->getMetadata([ $default_format = [
'meta_type' => 'facformat', 'name' => 'default',
'post_id' => dcCore::app()->ctx->posts->post_id, 'dateformat' => '',
'limit' => 1, 'lineslimit' => '5',
]); 'linestitletext' => '%T',
if ($fac_format->isEmpty()) { 'linestitleover' => '%D',
return null; 'linestitlelength' => '150',
} 'showlinesdescription' => '0',
'linesdescriptionlength' => '350',
'linesdescriptionnohtml' => '1',
'showlinescontent' => '0',
'linescontentlength' => '350',
'linescontentnohtml' => '1',
];
# Get format info $formats = @unserialize((string) dcCore::app()->blog->settings->fac->fac_formats);
$default_format = [ if (empty($formats)
'name' => 'default', || !is_array($formats)
'dateformat' => '', || !isset($formats[$fac_format->meta_id])) {
'lineslimit' => '5', $format = $default_format;
'linestitletext' => '%T', } else {
'linestitleover' => '%D', $format = array_merge(
'linestitlelength' => '150', $default_format,
'showlinesdescription' => '0', $formats[$fac_format->meta_id]
'linesdescriptionlength' => '350', );
'linesdescriptionnohtml' => '1', }
'showlinescontent' => '0',
'linescontentlength' => '350',
'linescontentnohtml' => '1',
];
$formats = @unserialize((string) dcCore::app()->blog->settings->fac->fac_formats); # Read feed url
if (empty($formats) $cache = is_dir(DC_TPL_CACHE . '/fac') ? DC_TPL_CACHE . '/fac' : null;
|| !is_array($formats)
|| !isset($formats[$fac_format->meta_id])) {
$format = $default_format;
} else {
$format = array_merge(
$default_format,
$formats[$fac_format->meta_id]
);
}
# Read feed url try {
$cache = is_dir(DC_TPL_CACHE . '/fac') ? DC_TPL_CACHE . '/fac' : null; $feed = feedReader::quickParse($fac_url->meta_id, $cache);
} catch (Exception $e) {
$feed = null;
}
try { # No entries
$feed = feedReader::quickParse($fac_url->meta_id, $cache); if (!$feed) {
} catch (Exception $e) { return null;
$feed = null; }
}
# No entries # Feed title
if (!$feed) { $feedtitle = '';
return null; if ('' != dcCore::app()->blog->settings->fac->fac_defaultfeedtitle) {
} $feedtitle = '<h3>' . html::escapeHTML(
empty($feed->title) ?
str_replace(
'%T',
__('a related feed'),
dcCore::app()->blog->settings->fac->fac_defaultfeedtitle
) :
str_replace(
'%T',
$feed->title,
dcCore::app()->blog->settings->fac->fac_defaultfeedtitle
)
) . '</h3>';
}
# Feed title # Feed desc
$feedtitle = ''; $feeddesc = '';
if ('' != dcCore::app()->blog->settings->fac->fac_defaultfeedtitle) { if (dcCore::app()->blog->settings->fac->fac_showfeeddesc
$feedtitle = '<h3>' . html::escapeHTML( && '' != $feed->description) {
empty($feed->title) ? $feeddesc = '<p>' . context::global_filters(
str_replace( $feed->description,
['encode_xml', 'remove_html']
) . '</p>';
}
# Date format
$dateformat = '' != $format['dateformat'] ?
$format['dateformat'] :
dcCore::app()->blog->settings->system->date_format . ',' . dcCore::app()->blog->settings->system->time_format;
# Enrties limit
$entrieslimit = abs((int) $format['lineslimit']);
$uselimit = $entrieslimit > 0 ? true : false;
echo
'<div class="post-fac">' .
$feedtitle . $feeddesc .
'<dl>';
$i = 0;
foreach ($feed->items as $item) {
# Format date
$date = dt::dt2str($dateformat, $item->pubdate);
# Entries title
$title = context::global_filters(
str_replace(
[
'%D',
'%T', '%T',
__('a related feed'), '%A',
dcCore::app()->blog->settings->fac->fac_defaultfeedtitle '%E',
) : '%C',
str_replace( ],
[
$date,
$item->title,
$item->creator,
$item->description,
$item->content,
],
$format['linestitletext']
),
['remove_html', 'cut_string' => abs((int) $format['linestitlelength'])],
);
# Entries over title
$overtitle = context::global_filters(
str_replace(
[
'%D',
'%T', '%T',
$feed->title, '%A',
dcCore::app()->blog->settings->fac->fac_defaultfeedtitle '%E',
) '%C',
) . '</h3>'; ],
[
$date,
$item->title,
$item->creator,
$item->description,
$item->content,
],
$format['linestitleover']
),
['remove_html', 'cut_string' => 350],
);
# Entries description
$description = '';
if ($format['showlinesdescription']
&& '' != $item->description) {
$description = '<dd>' .
context::global_filters(
$item->description,
['remove_html' => (int) $format['linesdescriptionnohtml'], 'cut_string' => abs((int) $format['linesdescriptionlength'])]
) . '</dd>';
} }
# Feed desc # Entries content
$feeddesc = ''; $content = '';
if (dcCore::app()->blog->settings->fac->fac_showfeeddesc if ($format['showlinescontent']
&& '' != $feed->description) { && '' != $item->content) {
$feeddesc = '<p>' . context::global_filters( $content = '<dd>' .
$feed->description, context::global_filters(
['encode_xml', 'remove_html'] $item->content,
) . '</p>'; ['remove_html' => (int) $format['linescontentnohtml'], 'cut_string' => abs((int) $format['linescontentlength'])]
) . '</dd>';
} }
# Date format
$dateformat = '' != $format['dateformat'] ?
$format['dateformat'] :
dcCore::app()->blog->settings->system->date_format . ',' . dcCore::app()->blog->settings->system->time_format;
# Enrties limit
$entrieslimit = abs((int) $format['lineslimit']);
$uselimit = $entrieslimit > 0 ? true : false;
echo echo
'<div class="post-fac">' . '<dt><a href="' . $item->link . '" ' .
$feedtitle . $feeddesc . 'title="' . $overtitle . '">' . $title . '</a></dt>' .
'<dl>'; $description . $content;
$i = 0; $i++;
foreach ($feed->items as $item) { if ($uselimit && $i == $entrieslimit) {
# Format date break;
$date = dt::dt2str($dateformat, $item->pubdate);
# Entries title
$title = context::global_filters(
str_replace(
[
'%D',
'%T',
'%A',
'%E',
'%C',
],
[
$date,
$item->title,
$item->creator,
$item->description,
$item->content,
],
$format['linestitletext']
),
['remove_html', 'cut_string' => abs((int) $format['linestitlelength'])],
);
# Entries over title
$overtitle = context::global_filters(
str_replace(
[
'%D',
'%T',
'%A',
'%E',
'%C',
],
[
$date,
$item->title,
$item->creator,
$item->description,
$item->content,
],
$format['linestitleover']
),
['remove_html', 'cut_string' => 350],
);
# Entries description
$description = '';
if ($format['showlinesdescription']
&& '' != $item->description) {
$description = '<dd>' .
context::global_filters(
$item->description,
['remove_html' => (int) $format['linesdescriptionnohtml'], 'cut_string' => abs((int) $format['linesdescriptionlength'])]
) . '</dd>';
}
# Entries content
$content = '';
if ($format['showlinescontent']
&& '' != $item->content) {
$content = '<dd>' .
context::global_filters(
$item->content,
['remove_html' => (int) $format['linescontentnohtml'], 'cut_string' => abs((int) $format['linescontentlength'])]
) . '</dd>';
}
echo
'<dt><a href="' . $item->link . '" ' .
'title="' . $overtitle . '">' . $title . '</a></dt>' .
$description . $content;
$i++;
if ($uselimit && $i == $entrieslimit) {
break;
}
} }
echo '</dl></div>';
} }
} echo '</dl></div>';
});