hesabixWpTheme/functions.php
2026-05-22 13:52:15 +03:30

407 lines
15 KiB
PHP

<?php
/**
* Hesabix2 functions and definitions
*
* @package Hesabix2
* @version 0.1
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* راه‌اندازی تم
*/
function hesabix2_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption'));
add_theme_support('automatic-feed-links');
add_theme_support('custom-logo', array(
'height' => 48,
'width' => 48,
'flex-height' => true,
'flex-width' => true,
));
/* آیکون سایت (فاویکون و آیکون در هدر) — از ظاهر → سفارشی‌سازی → هویت سایت قابل آپلود است */
add_theme_support('site-icon');
register_nav_menus(array(
'primary' => __('منوی اصلی', 'hesabix2'),
'footer' => __('منوی فوتر', 'hesabix2'),
));
load_theme_textdomain( 'hesabix2', get_template_directory() . '/languages' );
}
add_action('after_setup_theme', 'hesabix2_setup');
/**
* ثبت ناحیه‌های ویجت — وقتی ویجتی در ستون نباشد، چیدمان تک‌ستونه می‌شود
*/
function hesabix2_register_sidebars() {
register_sidebar(array(
'name' => __('سایدبار اصلی', 'hesabix2'),
'id' => 'sidebar-1',
'description' => __('در صفحات وبلاگ و برگه نمایش داده می‌شود. اگر خالی باشد، محتوا تک‌ستونه نمایش داده می‌شود.', 'hesabix2'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
register_sidebar(array(
'name' => __('فوتر — دسترسی سریع', 'hesabix2'),
'id' => 'footer-1',
'description' => __('ستون اول فوتر. اگر خالی باشد، این ستون نمایش داده نمی‌شود.', 'hesabix2'),
'before_widget' => '<div id="%1$s" class="widget footer-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="footer-heading">',
'after_title' => '</h4>',
));
register_sidebar(array(
'name' => __('فوتر — منابع', 'hesabix2'),
'id' => 'footer-2',
'description' => __('ستون دوم فوتر. اگر خالی باشد، این ستون نمایش داده نمی‌شود.', 'hesabix2'),
'before_widget' => '<div id="%1$s" class="widget footer-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="footer-heading">',
'after_title' => '</h4>',
));
register_sidebar(array(
'name' => __('فوتر — تماس', 'hesabix2'),
'id' => 'footer-3',
'description' => __('ستون سوم فوتر. اگر خالی باشد، این ستون نمایش داده نمی‌شود.', 'hesabix2'),
'before_widget' => '<div id="%1$s" class="widget footer-widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h4 class="footer-heading">',
'after_title' => '</h4>',
));
}
add_action('widgets_init', 'hesabix2_register_sidebars');
/**
* آیا سایدبار اصلی فعال است؟
*/
function hesabix2_has_sidebar() {
return is_active_sidebar('sidebar-1');
}
/**
* بارگذاری استایل‌ها و اسکریپت‌ها (همه به صورت لوکال، بدون وابستگی خارجی)
*/
function hesabix2_scripts() {
$theme_uri = get_template_directory_uri();
$theme_dir = get_template_directory();
$tailwind = $theme_dir . '/assets/css/tailwind.css';
$style_ver = '0.3';
$tailwind_ver = file_exists( $tailwind ) ? (string) filemtime( $tailwind ) : $style_ver;
// Tailwind CSS (فایل بیلدشده)
wp_enqueue_style(
'hesabix2-tailwind',
$theme_uri . '/assets/css/tailwind.css',
array(),
$tailwind_ver
);
// استایل اصلی قالب — شامل متغیرهای رنگ و کلاس دکمه/گرادیان
wp_enqueue_style(
'hesabix2-style',
get_stylesheet_uri(),
array( 'hesabix2-tailwind' ),
$style_ver
);
// استایل صفحات ورود / ثبت‌نام / بازیابی رمز
if ( is_page() ) {
$page_template = get_page_template_slug( get_queried_object_id() );
$auth_templates = array( 'page-login.php', 'page-register.php', 'page-lostpassword.php' );
if ( in_array( $page_template, $auth_templates, true ) ) {
$auth_css = $theme_dir . '/assets/css/auth.css';
wp_enqueue_style(
'hesabix2-auth',
$theme_uri . '/assets/css/auth.css',
array( 'hesabix2-style' ),
file_exists( $auth_css ) ? (string) filemtime( $auth_css ) : '0.1'
);
}
}
// بک‌گراند ابرهای متحرک فقط در صفحه نخست
if ( is_front_page() ) {
wp_enqueue_script(
'hesabix2-hero-clouds',
$theme_uri . '/assets/js/hero-clouds.js',
array(),
'0.1',
true
);
}
}
add_action('wp_enqueue_scripts', 'hesabix2_scripts');
/**
* قالب‌های صفحه ورود، ثبت‌نام و بازیابی رمز — برای نمایش در ویرایشگر برگه
*/
function hesabix2_page_templates( $templates ) {
$templates['page-login.php'] = __( 'ورود', 'hesabix2' );
$templates['page-register.php'] = __( 'ثبت‌نام (عضویت)', 'hesabix2' );
$templates['page-lostpassword.php'] = __( 'بازیابی کلمه عبور', 'hesabix2' );
return $templates;
}
add_filter( 'theme_page_templates', 'hesabix2_page_templates' );
/**
* کلاس بدنه برای صفحات احراز هویت
*/
function hesabix2_auth_body_class( $classes ) {
if ( is_page() ) {
$slug = get_page_template_slug( get_queried_object_id() );
if ( $slug === 'page-login.php' ) {
$classes[] = 'hesabix2-auth-login';
} elseif ( $slug === 'page-register.php' ) {
$classes[] = 'hesabix2-auth-register';
} elseif ( $slug === 'page-lostpassword.php' ) {
$classes[] = 'hesabix2-auth-lostpassword';
}
}
return $classes;
}
add_filter( 'body_class', 'hesabix2_auth_body_class' );
/**
* برگه‌ای که از قالب احراز هویت استفاده می‌کند را پیدا می‌کند (اختیاری؛ وقتی از برگه استفاده شود)
*/
function hesabix2_get_auth_page_id( $template_slug ) {
$pages = get_pages( array(
'meta_key' => '_wp_page_template',
'meta_value' => $template_slug,
'number' => 1,
) );
return ! empty( $pages ) ? (int) $pages[0]->ID : 0;
}
/* ========== استایل و رفتار صفحه پیش‌فرض ورود وردپرس (wp-login.php) ========== */
/**
* بارگذاری استایل و فونت در صفحه ورود/ثبت‌نام/بازیابی رمز پیش‌فرض
*/
function hesabix2_login_enqueue_scripts() {
$theme_uri = get_template_directory_uri();
$theme_dir = get_template_directory();
$login_css = $theme_dir . '/assets/css/login.css';
wp_enqueue_style(
'hesabix2-login',
$theme_uri . '/assets/css/login.css',
array( 'login' ),
file_exists( $login_css ) ? (string) filemtime( $login_css ) : '0.1'
);
}
add_action( 'login_enqueue_scripts', 'hesabix2_login_enqueue_scripts' );
/**
* فونت یکان در head صفحه ورود
*/
function hesabix2_login_head_font() {
$font_base = get_template_directory_uri() . '/assets/fonts';
?>
<style id="hesabix2-login-fonts">
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 400 700;
font-display: swap;
src: url('<?php echo esc_url( $font_base ); ?>/woff2/YekanBakhFaNum-Regular.woff2') format('woff2'),
url('<?php echo esc_url( $font_base ); ?>/woff/YekanBakhFaNum-Regular.woff') format('woff');
}
</style>
<?php
}
add_action( 'login_head', 'hesabix2_login_head_font' );
/**
* لینک لوگو در صفحه ورود → صفحه اصلی سایت
*/
function hesabix2_login_headerurl( $url ) {
return home_url( '/' );
}
add_filter( 'login_headerurl', 'hesabix2_login_headerurl' );
/**
* متن لوگو در صفحه ورود → نام سایت
*/
function hesabix2_login_headertext( $text ) {
return get_bloginfo( 'name', 'display' );
}
add_filter( 'login_headertext', 'hesabix2_login_headertext' );
/**
* پشتیبانی از صفحه نخست ثابت
*/
function hesabix2_front_page_support() {
// در hesabix2_setup ثبت شده
}
add_action('after_setup_theme', 'hesabix2_front_page_support');
/**
* تخمین زمان مطالعه (بر اساس حدود ۲۰۰ کلمه در دقیقه)
*/
function hesabix2_reading_time($content, $wpm = 200) {
$content = strip_tags($content);
$words = preg_split('/\s+/u', $content, -1, PREG_SPLIT_NO_EMPTY);
$count = count($words);
if ($count < 1) {
return '';
}
$minutes = max(1, ceil($count / $wpm));
if ($minutes < 60) {
return $minutes . ' دقیقه مطالعه';
}
$hours = floor($minutes / 60);
$mins = $minutes % 60;
if ($mins === 0) {
return $hours . ' ساعت مطالعه';
}
return $hours . ' ساعت و ' . $mins . ' دقیقه مطالعه';
}
/**
* ساختار نمایش هر نظر
*/
function hesabix2_comment_callback($comment, $args, $depth) {
$avatar_size = isset($args['avatar_size']) ? $args['avatar_size'] : 56;
?>
<article id="div-comment-<?php comment_ID(); ?>" class="single-comment">
<header class="single-comment__head">
<div class="single-comment__avatar">
<?php echo get_avatar($comment, $avatar_size, '', '', array('class' => 'single-comment__avatar-img')); ?>
</div>
<div class="single-comment__info">
<cite class="single-comment__author"><?php comment_author_link(); ?></cite>
<?php if ($comment->comment_approved === '0') : ?>
<p class="single-comment__moderation"><?php esc_html_e('نظر شما در انتظار تایید است.', 'hesabix2'); ?></p>
<?php else : ?>
<time datetime="<?php comment_time('c'); ?>" class="single-comment__date">
<?php
printf(
/* translators: 1: date, 2: time */
esc_html__('%1$s در %2$s', 'hesabix2'),
get_comment_date('', $comment),
get_comment_time()
);
?>
</time>
<?php endif; ?>
</div>
<?php edit_comment_link(__('ویرایش', 'hesabix2'), '<span class="single-comment__edit">', '</span>'); ?>
</header>
<div class="single-comment__text"><?php comment_text(); ?></div>
<footer class="single-comment__actions">
<?php
comment_reply_link(array_merge($args, array(
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '',
'after' => '',
'reply_text' => __('پاسخ', 'hesabix2'),
)));
?>
</footer>
</article>
<?php
}
/**
* سفارشی‌سازی فیلدهای فرم نظر
*/
function hesabix2_comment_form_defaults($defaults) {
$defaults['comment_field'] = '<p class="single-comment-form__field"><label for="comment">' . __('نظر', 'hesabix2') . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="5" maxlength="65525" required placeholder="' . esc_attr__('نظر خود را بنویسید...', 'hesabix2') . '" class="single-comment-form__textarea"></textarea></p>';
return $defaults;
}
add_filter('comment_form_defaults', 'hesabix2_comment_form_defaults');
/**
* سفارشی‌سازی ترتیب فیلدهای فرم نظر (کامنت در انتها)
*/
function hesabix2_comment_form_fields($fields) {
$comment_field = $fields['comment'];
unset($fields['comment']);
$fields['comment'] = $comment_field;
return $fields;
}
add_filter('comment_form_fields', 'hesabix2_comment_form_fields');
/**
* یکپارچه‌سازی WeDocs با قالب Hesabix2
*/
function hesabix2_wedocs_theme_integration() {
if ( ! function_exists( 'wedocs' ) ) {
return;
}
remove_action( 'wedocs_before_main_content', 'wedocs_template_wrapper_start', 10 );
remove_action( 'wedocs_after_main_content', 'wedocs_template_wrapper_end', 10 );
add_action( 'wedocs_before_main_content', 'hesabix2_wedocs_wrapper_start', 10 );
add_action( 'wedocs_after_main_content', 'hesabix2_wedocs_wrapper_end', 10 );
}
add_action( 'after_setup_theme', 'hesabix2_wedocs_theme_integration', 20 );
function hesabix2_wedocs_wrapper_start() {
echo '<div class="wedocs-outer max-w-6xl mx-auto px-4 sm:px-6 lg:px-8 py-10 sm:py-14">';
}
function hesabix2_wedocs_wrapper_end() {
echo '</div>';
}
function hesabix2_wedocs_body_class( $classes ) {
if ( is_singular( 'docs' ) || ( get_post_type() === 'docs' ) ) {
$classes[] = 'is-wedocs';
}
return $classes;
}
add_filter( 'body_class', 'hesabix2_wedocs_body_class' );
/** جلوگیری از لود CSS پیش‌فرض WeDocs در frontend تا استایل قالب اعمال شود */
function hesabix2_dequeue_wedocs_styles() {
wp_dequeue_style( 'wedocs-styles' );
wp_deregister_style( 'wedocs-styles' );
wp_dequeue_style( 'wedocs-styles-css' );
wp_deregister_style( 'wedocs-styles-css' );
}
add_action( 'wp_enqueue_scripts', 'hesabix2_dequeue_wedocs_styles', 100 );
/** در صفحه نقشه راه / اصلی اسناد، بخش لیست اسناد نمایش داده نشود */
function hesabix2_wedocs_hide_docs_list_on_roadmap( $args ) {
$docs_home = function_exists( 'wedocs_get_general_settings' ) ? wedocs_get_general_settings( 'docs_home', '' ) : '';
if ( $docs_home && is_page( (int) $docs_home ) ) {
$args['hide_docs_list'] = true;
}
return $args;
}
add_filter( 'wedocs_get_doc_listing_template_args', 'hesabix2_wedocs_hide_docs_list_on_roadmap' );
/**
* یکپارچه‌سازی AnsPress (پرسش و پاسخ) با قالب Hesabix2
*/
function hesabix2_anspress_body_class( $classes ) {
if ( function_exists( 'is_anspress' ) && is_anspress() ) {
$classes[] = 'is-anspress';
$classes[] = 'hesabix2-anspress';
}
return $classes;
}
add_filter( 'body_class', 'hesabix2_anspress_body_class' );
/** ادغام ووکامرس (در صورت نصب افزونه) */
$hesabix2_wc = get_template_directory() . '/inc/woocommerce-hesabix.php';
if ( is_readable( $hesabix2_wc ) ) {
require_once $hesabix2_wc;
}
$hesabix2_wc_fa = get_template_directory() . '/inc/woocommerce-i18n-fa.php';
if ( is_readable( $hesabix2_wc_fa ) ) {
require_once $hesabix2_wc_fa;
}