hesabixWpTheme/inc/blog-helpers.php

300 lines
10 KiB
PHP

<?php
/**
* توابع مشترک وبلاگ — کارت، آرشیو، TOC، نویسنده
*
* @package Hesabix2
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* آدرس صفحه وبلاگ
*/
function hesabix2_blog_url() {
$blog_id = (int) get_option('page_for_posts');
return $blog_id ? get_permalink($blog_id) : home_url('/blog/');
}
/**
* صفحات وبلاگ (لیست، آرشیو، تک‌نوشته)
*/
function hesabix2_is_blog_screen() {
if (is_singular('post')) {
return true;
}
if (is_home()) {
return true;
}
if (is_category() || is_tag() || is_author() || is_date()) {
return true;
}
return false;
}
/**
* کلاس چیدمان تمام‌عرض وبلاگ
*/
function hesabix2_blog_layout_class() {
return 'layout-wrap layout--single blog-layout--full';
}
/**
* سایدبار ویجت وردپرس — در وبلاگ غیرفعال (سایدبار داخلی تک‌نوشته).
*/
function hesabix2_show_layout_sidebar() {
if (hesabix2_is_blog_screen()) {
return false;
}
return is_active_sidebar('sidebar-1');
}
/**
* زمان مطالعه (ترجیحاً از خلاصه برای کارایی در آرشیو)
*/
function hesabix2_post_reading_time($post_id = 0) {
$post_id = $post_id ?: get_the_ID();
if (!$post_id) {
return '';
}
$post = get_post($post_id);
if (!$post) {
return '';
}
$text = $post->post_excerpt ?: $post->post_content;
return hesabix2_reading_time($text);
}
/**
* تعداد بازدید پست
*/
function hesabix2_post_views_label($post_id = 0) {
$post_id = $post_id ?: get_the_ID();
if (function_exists('hesabix2_get_post_views')) {
return (string) hesabix2_get_post_views($post_id);
}
return (string) (get_post_meta($post_id, 'post_views_count', true) ?: '0');
}
/**
* شناسه دسته فعال از URL آرشیو
*/
function hesabix2_blog_current_category_id() {
if (is_category()) {
return (int) get_queried_object_id();
}
if (isset($_GET['cat'])) {
return absint($_GET['cat']);
}
return 0;
}
/**
* فیلتر دسته‌ها (chip)
*/
function hesabix2_render_blog_category_filters($current_cat = 0) {
$categories = get_categories(array(
'orderby' => 'count',
'order' => 'DESC',
'hide_empty' => true,
'number' => 12,
));
if (empty($categories)) {
return;
}
$blog_url = hesabix2_blog_url();
?>
<nav class="blog-cat-filters" aria-label="<?php esc_attr_e('دسته‌بندی وبلاگ', 'hesabix2'); ?>">
<ul class="blog-cat-filters__list">
<li>
<a href="<?php echo esc_url($blog_url); ?>"
class="blog-cat-filters__chip<?php echo $current_cat ? '' : ' is-active'; ?>">
<?php esc_html_e('همه', 'hesabix2'); ?>
</a>
</li>
<?php foreach ($categories as $cat) : ?>
<li>
<a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>"
class="blog-cat-filters__chip<?php echo (int) $current_cat === (int) $cat->term_id ? ' is-active' : ''; ?>">
<?php echo esc_html($cat->name); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
<?php
}
/**
* هیروی آرشیو وبلاگ
*/
function hesabix2_render_blog_archive_hero($title, $description = '') {
?>
<div class="blog-archive-hero relative bg-gradient-to-br from-brand-500 via-violet-500 to-brand-600 overflow-hidden">
<div class="absolute inset-0 bg-black/30" aria-hidden="true"></div>
<div class="relative container mx-auto px-4 py-14 lg:py-18">
<div class="blog-archive-hero__inner text-center text-white">
<?php if (function_exists('hesabix2_seo_render_breadcrumb')) {
hesabix2_seo_render_breadcrumb('hesabix2-breadcrumb hesabix2-breadcrumb--on-dark blog-archive-hero__crumb');
} ?>
<h1 class="blog-hero-title text-3xl lg:text-5xl font-bold mb-4"><?php echo esc_html($title); ?></h1>
<?php if ($description) : ?>
<p class="blog-hero-desc text-lg text-white/90"><?php echo esc_html($description); ?></p>
<?php endif; ?>
</div>
</div>
</div>
<?php
}
/**
* صفحه‌بندی وبلاگ
*/
function hesabix2_render_blog_pagination($query, $search = '') {
if (!$query instanceof WP_Query || $query->max_num_pages <= 1) {
return;
}
$paged = max(1, (int) $query->get('paged'));
$big = 999999999;
$base = str_replace($big, '%#%', esc_url(get_pagenum_link($big)));
$args = array(
'base' => $base,
'format' => '?paged=%#%',
'current' => $paged,
'total' => $query->max_num_pages,
'prev_text' => '← ' . __('قبلی', 'hesabix2'),
'next_text' => __('بعدی', 'hesabix2') . ' →',
'type' => 'list',
'end_size' => 2,
'mid_size' => 1,
);
if ($search) {
$args['add_args'] = array('s' => urlencode($search));
}
$links = paginate_links($args);
if (!$links) {
return;
}
$links = str_replace('<ul class=\'page-numbers\'>', '<div class="flex items-center gap-1 flex-wrap justify-center">', $links);
$links = str_replace('</ul>', '</div>', $links);
$links = str_replace('<li>', '', $links);
$links = str_replace('</li>', '', $links);
$links = str_replace('class="page-numbers"', 'class="blog-pagination-num px-4 py-2 text-sm font-medium text-slate-600 bg-white border border-slate-300 rounded-lg hover:bg-slate-50 hover:text-slate-800 transition-colors"', $links);
$links = str_replace('class="page-numbers current"', 'class="blog-pagination-num blog-pagination-num--current px-4 py-2 text-sm font-medium text-white bg-brand-600 border border-brand-600 rounded-lg"', $links);
$links = str_replace('class="prev page-numbers"', 'class="blog-pagination-num blog-pagination-prev px-4 py-2 text-sm font-medium text-slate-600 bg-white border border-slate-300 rounded-lg hover:bg-slate-50"', $links);
$links = str_replace('class="next page-numbers"', 'class="blog-pagination-num blog-pagination-next px-4 py-2 text-sm font-medium text-slate-600 bg-white border border-slate-300 rounded-lg hover:bg-slate-50"', $links);
?>
<nav class="blog-pagination flex justify-center mt-12" aria-label="<?php esc_attr_e('صفحه‌بندی', 'hesabix2'); ?>">
<?php echo $links; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</nav>
<?php
}
/**
* افزودن id به سرفصل‌های محتوا + استخراج TOC
*
* @return array{html:string, toc:array<int,array{level:int,id:string,text:string}>}
*/
function hesabix2_prepare_post_content_with_toc($content) {
$toc = array();
$index = 0;
$html = preg_replace_callback(
'/<h([2-3])([^>]*)>(.*?)<\/h\1>/is',
static function ($m) use (&$toc, &$index) {
$level = (int) $m[1];
$text = wp_strip_all_tags($m[3]);
if ($text === '') {
return $m[0];
}
$attrs = $m[2];
if (preg_match('/\sid=["\']([^"\']+)["\']/i', $attrs, $id_match)) {
$id = $id_match[1];
} else {
$index++;
$id = 'section-' . $index;
$attrs .= ' id="' . esc_attr($id) . '"';
}
$toc[] = array(
'level' => $level,
'id' => $id,
'text' => $text,
);
return '<h' . $level . $attrs . '>' . $m[3] . '</h' . $level . '>';
},
$content
);
return array(
'html' => $html ?: $content,
'toc' => $toc,
);
}
/**
* فهرست مطالب سایدبار
*/
function hesabix2_render_post_toc($toc) {
if (empty($toc)) {
return;
}
?>
<nav class="single-toc" aria-label="<?php esc_attr_e('فهرست مطالب', 'hesabix2'); ?>">
<h3 class="single-toc__title"><?php esc_html_e('فهرست مطالب', 'hesabix2'); ?></h3>
<ol class="single-toc__list">
<?php foreach ($toc as $item) : ?>
<li class="single-toc__item single-toc__item--h<?php echo (int) $item['level']; ?>">
<a href="#<?php echo esc_attr($item['id']); ?>"><?php echo esc_html($item['text']); ?></a>
</li>
<?php endforeach; ?>
</ol>
</nav>
<?php
}
/**
* باکس نویسنده
*/
function hesabix2_render_author_box($post_id = 0) {
$post_id = $post_id ?: get_the_ID();
$author_id = (int) get_post_field('post_author', $post_id);
if (!$author_id) {
return;
}
$name = get_the_author_meta('display_name', $author_id);
$bio = get_the_author_meta('description', $author_id);
$url = get_author_posts_url($author_id);
?>
<aside class="single-author-box">
<div class="single-author-box__avatar">
<?php echo get_avatar($author_id, 72, '', '', array('class' => 'single-author-box__img')); ?>
</div>
<div class="single-author-box__body">
<p class="single-author-box__label"><?php esc_html_e('نویسنده', 'hesabix2'); ?></p>
<h3 class="single-author-box__name">
<a href="<?php echo esc_url($url); ?>"><?php echo esc_html($name); ?></a>
</h3>
<?php if ($bio) : ?>
<p class="single-author-box__bio"><?php echo esc_html($bio); ?></p>
<?php endif; ?>
<a href="<?php echo esc_url($url); ?>" class="single-author-box__more">
<?php esc_html_e('سایر مطالب این نویسنده', 'hesabix2'); ?> ←
</a>
</div>
</aside>
<?php
}
/**
* CTA پایان مطلب
*/
function hesabix2_render_blog_cta() {
?>
<div class="single-blog-cta">
<h3 class="single-blog-cta__title"><?php esc_html_e('حسابیکس را امتحان کنید', 'hesabix2'); ?></h3>
<p class="single-blog-cta__text"><?php esc_html_e('مدیریت مالی، CRM و عملیات کسب‌وکار را در یک پلتفرم یکپارچه تجربه کنید.', 'hesabix2'); ?></p>
<a href="<?php echo esc_url(home_url('/')); ?>" class="single-blog-cta__btn">
<?php esc_html_e('شروع رایگان', 'hesabix2'); ?>
</a>
</div>
<?php
}