task 17075 | сделал подсчет времени чтения
This commit is contained in:
1 parent
2a90b6957c
commit
2e54c26224
1 file changed
+23
-1
@@ -16,6 +16,26 @@
|
||||
$terms = get_the_terms(get_the_ID(), 'article_category');
|
||||
$article_blocks = get_field('new_blocks') ?: [];
|
||||
$article_content = get_field('article_content') ?: '';
|
||||
$reading_parts = [$article_content];
|
||||
foreach ($article_blocks as $block) {
|
||||
$layout = $block['acf_fc_layout'] ?? '';
|
||||
if ($layout === 'text') {
|
||||
$reading_parts[] = $block['content'] ?? '';
|
||||
} elseif (in_array($layout, ['attention_block', 'attention_blocks', 'quote'], true)) {
|
||||
$reading_parts[] = $block['text'] ?? '';
|
||||
} elseif ($layout === 'block_main') {
|
||||
foreach (($block['cards'] ?? []) as $card) {
|
||||
$reading_parts[] = $card['text_card'] ?? '';
|
||||
}
|
||||
}
|
||||
}
|
||||
// Разделяем HTML-элементы пробелами, чтобы соседние абзацы не склеивались.
|
||||
$reading_text = strip_shortcodes(implode(' ', $reading_parts));
|
||||
$reading_text = preg_replace('@<(script|style)\b[^>]*>.*?</\1>@si', ' ', $reading_text);
|
||||
$reading_text = preg_replace('/<[^>]*>/', ' ', $reading_text);
|
||||
$reading_text = html_entity_decode(wp_strip_all_tags($reading_text), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
$word_count = preg_match_all("/[\p{L}\p{N}]+(?:[’'\x{002D}][\p{L}\p{N}]+)*/u", $reading_text);
|
||||
$reading_minutes = $word_count ? max(1, (int) ceil($word_count / 200)) : 0;
|
||||
$generated = generate_toc($article_content, 'article-content');
|
||||
$content = $generated['content'];
|
||||
$toc_items = $generated['toc_items'];
|
||||
@@ -70,13 +90,15 @@
|
||||
<time datetime="<?php echo esc_attr(get_the_date('c')); ?>"><?php echo esc_html(get_the_date('d.m.Y')); ?></time>
|
||||
</div>
|
||||
|
||||
<?php if ($reading_minutes) : ?>
|
||||
<div class="h-article-hero__meta-item">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
||||
<circle cx="12" cy="13" r="9"></circle>
|
||||
<path d="M12 8v5l3 2M9 2h6"></path>
|
||||
</svg>
|
||||
<span>Время чтения: <strong>—</strong></span>
|
||||
<span>Время чтения: <strong>≈ <?php echo esc_html($reading_minutes); ?> мин</strong></span>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="h-article-hero__meta-item">
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
|
||||
|
||||
Reference in new issue
Block a user