motayeb/functions.php
Kazem Alghasi f408faac54 build(theme): scaffold initial theme directory structure and assets
Initialize the base WordPress theme files, including core template
hierarchy (index, page, single, archive, 404), WooCommerce template
overrides, and essential asset directories.
2026-08-15 22:11:08 +03:30

263 lines
19 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// جلوگیری از دسترسی مستقیم
if ( ! defined( 'ABSPATH' ) ) exit;
// ۱. فعال‌سازی قابلیت‌های پایه وردپرس
function motayeb_setup() {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script' ) );
add_theme_support( 'woocommerce' );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
register_nav_menus( array(
'primary' => __( 'منوی اصلی', 'motayeb' ),
'footer' => __( 'منوی فوتر', 'motayeb' ),
) );
}
add_action( 'after_setup_theme', 'motayeb_setup' );
// ۲. لود فایل‌های CSS و JS
function motayeb_scripts() {
wp_enqueue_style( 'vazirmatn', 'https://fonts.googleapis.com/css2?family=Vazirmatn:wght@300;400;500;600;700;800;900&display=swap', array(), null );
wp_enqueue_script( 'tailwind-cdn', 'https://cdn.tailwindcss.com', array(), null, false );
wp_add_inline_script( 'tailwind-cdn', "
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
forest: { 50:'#e8f5ec', 100:'#c6e6cf', 200:'#9ed0ae', 300:'#6fb88a', 400:'#4a9a6b', 500:'#2D5F3F', 600:'#254d34', 700:'#1c3b28', 800:'#13291c', 900:'#0a1710' },
cream: { 50:'#FDFBF7', 100:'#F5F0E8', 200:'#EBE3D5', 300:'#DDD2BF', 400:'#C4B49A', 500:'#A99478' },
honey: { 50:'#FDF5ED', 100:'#F9E6D0', 200:'#F2CC9E', 300:'#E8AC6A', 400:'#D4A574', 500:'#C08B52', 600:'#A07040', 700:'#7D5630', 800:'#5A3D20', 900:'#3A2612' },
earth: { 50:'#F5EDE7', 100:'#E8D5C7', 200:'#D4B5A0', 300:'#BF9276', 400:'#8B5A3C', 500:'#734A30', 600:'#5C3B25', 700:'#452C1B', 800:'#2E1D12', 900:'#170E09' },
dark: { bg:'#1A1D23', card:'#22262E', border:'#2E333D', text:'#E8E6E3', muted:'#9CA3AF' }
},
fontFamily: { vazir: ['Vazirmatn', 'sans-serif'], inter: ['Inter', 'sans-serif'] }
}
}
}
" );
wp_enqueue_style( 'motayeb-main', get_template_directory_uri() . '/assets/css/main.css', array(), '1.0.0' );
wp_enqueue_script( 'iconify', 'https://code.iconify.design/iconify-icon/1.0.7/iconify-icon.min.js', array(), null, true );
wp_enqueue_script( 'motayeb-main', get_template_directory_uri() . '/assets/js/main.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'motayeb_scripts' );
// ۳. حذف استایل پیش‌فرض ووکامرس
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
// ۴. بازنویسی تگ‌های Wraper ووکامرس
function motayeb_woo_wrapper_start() {
echo '<main id="primary" class="pt-16 md:pt-[104px]"><div class="max-w-7xl mx-auto px-4 md:px-6 py-8">';
}
function motayeb_woo_wrapper_end() {
echo '</div></main>';
}
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
add_action( 'woocommerce_before_main_content', 'motayeb_woo_wrapper_start', 10 );
add_action( 'woocommerce_after_main_content', 'motayeb_woo_wrapper_end', 10 );
// ۵. تنظیمات فروشگاه
add_filter( 'loop_shop_columns', function() { return 4; } );
add_filter( 'loop_shop_per_page', function() { return 12; } );
// ۶. اضافه کردن کلاس nav-link به منو
add_filter('nav_menu_link_attributes', 'motayeb_add_nav_link_class', 10, 3);
function motayeb_add_nav_link_class($atts, $item, $args) {
if ($args->theme_location === 'primary') {
$atts['class'] = isset($atts['class']) ? $atts['class'] . ' nav-link' : 'nav-link';
}
return $atts;
}
// ۸. فیلدهای صفحه اصلی (ACF)
if ( function_exists( 'acf_add_local_field_group' ) ) {
acf_add_local_field_group( array(
'key' => 'group_motayeb_homepage',
'title' => 'تنظیمات صفحه اصلی مطیب',
'fields' => array(
array( 'key' => 'field_hero_tab', 'label' => 'بخش هیرو', 'type' => 'tab' ),
array( 'key' => 'field_hero_title', 'name' => 'hero_title', 'label' => 'عنوان اصلی', 'type' => 'wysiwyg', 'tabs' => 'all', 'toolbar' => 'full', 'media_upload' => false, 'default_value' => '<span style="color: #8B5A3C;">طعم</span> <span style="color: #2D5F3F;">واقعی</span><br><span style="color: #D4A574;">طبیعت</span>' ),
array( 'key' => 'field_hero_subtitle', 'name' => 'hero_subtitle', 'label' => 'زیرعنوان', 'type' => 'textarea', 'rows' => 2, 'default_value' => 'از عسل کوهستان تا ارده سنتی، هر محصول مطیب سفری به دل طبیعت ایران است.' ),
array( 'key' => 'field_hero_image', 'name' => 'hero_image', 'label' => 'تصویر هیرو', 'type' => 'image', 'return_format' => 'array' ),
array( 'key' => 'field_hero_btn_text', 'name' => 'hero_btn_text', 'label' => 'متن دکمه', 'type' => 'text', 'default_value' => 'مشاهده محصولات' ),
array( 'key' => 'field_stats_tab', 'label' => 'آمار', 'type' => 'tab' ),
array( 'key' => 'field_stats', 'name' => 'stats', 'label' => 'آمار', 'type' => 'repeater', 'layout' => 'horizontal', 'button_label' => 'افزودن', 'sub_fields' => array(
array( 'key' => 'field_stat_number', 'name' => 'number', 'label' => 'عدد', 'type' => 'text' ),
array( 'key' => 'field_stat_label', 'name' => 'label', 'label' => 'توضیح', 'type' => 'text' ),
)),
array( 'key' => 'field_cats_tab', 'label' => 'دسته‌بندی‌ها', 'type' => 'tab' ),
array( 'key' => 'field_categories', 'name' => 'home_categories', 'label' => 'دسته‌بندی‌ها', 'type' => 'repeater', 'layout' => 'table', 'button_label' => 'افزودن', 'sub_fields' => array(
array( 'key' => 'field_cat_title', 'name' => 'title', 'label' => 'نام', 'type' => 'text' ),
array( 'key' => 'field_cat_icon', 'name' => 'icon', 'label' => 'آیکون', 'type' => 'text' ),
array( 'key' => 'field_cat_color', 'name' => 'color', 'label' => 'رنگ', 'type' => 'select', 'choices' => array( 'honey' => 'عسلی', 'forest' => 'سبز', 'red' => 'قرمز', 'emerald' => 'سبز روشن', 'amber' => 'کهربایی' ) ),
array( 'key' => 'field_cat_link', 'name' => 'link', 'label' => 'لینک', 'type' => 'url' ),
array( 'key' => 'field_cat_count', 'name' => 'count', 'label' => 'تعداد', 'type' => 'text' ),
)),
array( 'key' => 'field_bestsellers_tab', 'label' => 'پرفروش‌ها', 'type' => 'tab' ),
array( 'key' => 'field_bestsellers', 'name' => 'bestseller_ids', 'label' => 'محصولات پرفروش', 'type' => 'relationship', 'post_type' => array( 'product' ), 'filters' => array( 'search' ), 'elements' => array( 'featured_image' ), 'min' => 1, 'max' => 8, 'return_format' => 'id' ),
array( 'key' => 'field_story_tab', 'label' => 'داستان برند', 'type' => 'tab' ),
array( 'key' => 'field_story_title', 'name' => 'story_title', 'label' => 'عنوان', 'type' => 'text', 'default_value' => 'داستان مطیب، از طبیعت تا سفره شما' ),
array( 'key' => 'field_story_text', 'name' => 'story_text', 'label' => 'متن', 'type' => 'textarea', 'rows' => 4 ),
array( 'key' => 'field_story_image', 'name' => 'story_image', 'label' => 'تصویر', 'type' => 'image', 'return_format' => 'array' ),
array( 'key' => 'field_story_values', 'name' => 'story_values', 'label' => 'ویژگی‌ها', 'type' => 'repeater', 'layout' => 'table', 'button_label' => 'افزودن', 'sub_fields' => array(
array( 'key' => 'field_val_icon', 'name' => 'icon', 'label' => 'آیکون', 'type' => 'text' ),
array( 'key' => 'field_val_title', 'name' => 'title', 'label' => 'عنوان', 'type' => 'text' ),
array( 'key' => 'field_val_desc', 'name' => 'desc', 'label' => 'توضیح', 'type' => 'text' ),
)),
array( 'key' => 'field_story_badges', 'name' => 'certificates', 'label' => 'گواهی‌ها', 'type' => 'repeater', 'layout' => 'horizontal', 'button_label' => 'افزودن', 'sub_fields' => array(
array( 'key' => 'field_badge_text', 'name' => 'text', 'label' => 'متن', 'type' => 'text' ),
)),
array( 'key' => 'field_testimonials_tab', 'label' => 'نظرات', 'type' => 'tab' ),
array( 'key' => 'field_testimonials', 'name' => 'testimonials', 'label' => 'نظرات', 'type' => 'repeater', 'layout' => 'block', 'button_label' => 'افزودن', 'sub_fields' => array(
array( 'key' => 'field_test_text', 'name' => 'text', 'label' => 'متن', 'type' => 'textarea', 'rows' => 3 ),
array( 'key' => 'field_test_name', 'name' => 'name', 'label' => 'نام', 'type' => 'text' ),
array( 'key' => 'field_test_city', 'name' => 'city', 'label' => 'شهر', 'type' => 'text' ),
array( 'key' => 'field_test_init', 'name' => 'initials', 'label' => 'حروف', 'type' => 'text' ),
)),
array( 'key' => 'field_newsletter_tab', 'label' => 'خبرنامه', 'type' => 'tab' ),
array( 'key' => 'field_news_title', 'name' => 'newsletter_title', 'label' => 'عنوان', 'type' => 'text', 'default_value' => 'از تخفیف‌ها باخبر شوید' ),
array( 'key' => 'field_news_text', 'name' => 'newsletter_text', 'label' => 'توضیح', 'type' => 'text', 'default_value' => 'با عضویت در خبرنامه، از جدیدترین محصولات مطلع شوید.' ),
),
'location' => array(
array( array( 'param' => 'page_template', 'operator' => '==', 'value' => 'front-page.php' ) ),
),
) );
}
// ===================================================================
// تابع کمکی برای ظاهر کامنت‌ها (بدون خطای Cannot redeclare)
// ===================================================================
function motayeb_comment_callback($comment, $args, $depth) {
$author = get_comment_author($comment);
$initial = mb_substr($author, 0, 1);
$date = get_comment_date('Y/m/d');
$text = wpautop( esc_html( get_comment_text($comment) ) );
$is_pending = ('0' == $comment->comment_approved);
echo '<article class="bg-cream-50 dark:bg-dark-card rounded-2xl p-5 border border-cream-200/50 dark:border-dark-border/50 hover:shadow-md transition">';
echo '<div class="flex items-center justify-between mb-4">';
echo '<div class="flex items-center gap-3">';
echo '<div class="w-12 h-12 bg-forest-100 dark:bg-forest-800 rounded-full flex items-center justify-center font-bold text-sm text-forest-500 dark:text-forest-300 flex-shrink-0">' . $initial . '</div>';
echo '<div>';
echo '<div class="font-bold text-sm text-earth-400 dark:text-dark-text">' . $author . '</div>';
echo '<div class="text-xs text-cream-400 dark:text-dark-muted flex items-center gap-1.5"><span>' . $date . '</span>';
echo edit_comment_link('ویرایش', ' <span class="text-xs text-forest-500 dark:text-forest-300 hover:underline">', '</span>');
echo '</div></div></div>';
comment_reply_link(array_merge($args, array('reply_text' => '<iconify-icon icon="lucide:reply" width="16"></iconify-icon>', 'depth' => $depth, 'max_depth' => 3, 'before' => '')));
echo '</div>';
if ($is_pending) {
echo '<p class="text-xs text-amber-600 dark:text-amber-400 bg-amber-50 dark:bg-amber-900/20 px-4 py-2 rounded-lg mb-3">نظر شما در انتظار تایید مدیریت است.</p>';
}
echo '<div class="text-sm text-cream-500 dark:text-dark-muted leading-7">' . $text . '</div>';
echo '</article>';
}
// ===================================================================
// دیتای تستی (فقط با زدن ?run_motayeb_seed در آدرس سایت اجرا می‌شود)
// ===================================================================
add_action('init', 'motayeb_seed_test_data');
function motayeb_seed_test_data() {
if (get_option('motayeb_seeded_done') || !isset($_GET['run_motayeb_seed'])) {
return;
}
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$categories = array('عسل طبیعی', 'ارده و حلوا', 'سرکه و ترشی', 'دمنوش گیاهی', 'خشکبار و آجیل', 'روغن طبیعی', 'شیره طبیعی');
$cat_ids = array();
foreach ($categories as $cat_name) {
$term = term_exists($cat_name, 'product_cat');
if (!$term) { $term = wp_insert_term($cat_name, 'product_cat'); }
$cat_ids[$cat_name] = is_array($term) ? $term['term_id'] : $term;
}
$products_data = array(
array('name' => 'عسل طبیعی کوهستان', 'desc' => 'عسل خالص و طبیعی جمع‌آوری شده از دامنه‌های زاگرس.', 'price' => 295000, 'sale_price' => 249000, 'cat' => 'عسل طبیعی', 'img' => 'https://images.unsplash.com/photo-1587049352846-4a222e784d38?w=600&q=80'),
array('name' => 'عسل آویشن و گون', 'desc' => 'عسل تک‌گلی آویشن و گون با عطر و طعمی بی‌نظیر.', 'price' => 345000, 'sale_price' => '', 'cat' => 'عسل طبیعی', 'img' => 'https://images.unsplash.com/photo-1558642452-9d2a7deb7f62?w=600&q=80'),
array('name' => 'ارده کنجد سنتی', 'desc' => 'ارده کنجد سنگ‌فرش شده با دستگاه‌های سنتی.', 'price' => 185000, 'sale_price' => '', 'cat' => 'ارده و حلوا', 'img' => 'https://images.unsplash.com/photo-1595475207225-428b62bda831?w=600&q=80'),
array('name' => 'حلوا ارده شیرین', 'desc' => 'حلوا اردهای سنتی با ترکیب ارده کنجد مرغوب.', 'price' => 210000, 'sale_price' => 189000, 'cat' => 'ارده و حلوا', 'img' => 'https://images.unsplash.com/photo-1590080875515-8a3a8dc5735e?w=600&q=80'),
array('name' => 'سرکه انگور طبیعی', 'desc' => 'سرکه انگور تخمیر شده به روش سنتی.', 'price' => 125000, 'sale_price' => '', 'cat' => 'سرکه و ترشی', 'img' => 'https://images.unsplash.com/photo-1596040033229-a9821ebd058d?w=600&q=80'),
array('name' => 'دمنوش بابونه', 'desc' => 'ترکیب آرامش‌بخش گل‌های بابونه و اسطوخودوس کوهی.', 'price' => 89000, 'sale_price' => '', 'cat' => 'دمنوش گیاهی', 'img' => 'https://images.unsplash.com/photo-1556679343-c7306c1976bc?w=600&q=80'),
array('name' => 'آجیل مخلوط لوکس', 'desc' => 'ترکیبی از پسته، بادام، فندق و گردو درجه یک.', 'price' => 485000, 'sale_price' => 429000, 'cat' => 'خشکبار و آجیل', 'img' => 'https://images.unsplash.com/photo-1599599810769-bcde5a160d32?w=600&q=80'),
array('name' => 'روغن زیتون فرابکر', 'desc' => 'روغن زیتون فرابکر کشت شده در باغ‌های گیلان.', 'price' => 275000, 'sale_price' => '', 'cat' => 'روغن طبیعی', 'img' => 'https://images.unsplash.com/photo-1474979266404-7eaabda50445?w=600&q=80'),
array('name' => 'شیره خرما طبیعی', 'desc' => 'شیره خالص خرما بدون شکر اضافه.', 'price' => 95000, 'sale_price' => 79000, 'cat' => 'شیره طبیعی', 'img' => 'https://images.unsplash.com/photo-1590165482129-1b8b27698780?w=600&q=80'),
array('name' => 'خشک انجیر درجه یک', 'desc' => 'انجیر خشک شده به روش آفتابی از باغ‌های استهبان.', 'price' => 165000, 'sale_price' => '', 'cat' => 'خشکبار و آجیل', 'img' => 'https://images.unsplash.com/photo-1596591868264-4ef737fbd18b?w=600&q=80')
);
foreach ($products_data as $p_data) {
$attach_id = media_sideload_image($p_data['img'], 0, $p_data['name'], 'id');
if (is_wp_error($attach_id)) { $attach_id = ''; }
$product_id = wp_insert_post(array(
'post_title' => $p_data['name'], 'post_content' => $p_data['desc'],
'post_status' => 'publish', 'post_type' => 'product',
));
if (!is_wp_error($product_id)) {
wp_set_object_terms($product_id, 'simple', 'product_type');
if (isset($cat_ids[$p_data['cat']])) {
wp_set_object_terms($product_id, array((int)$cat_ids[$p_data['cat']]), 'product_cat');
}
update_post_meta($product_id, '_price', $p_data['sale_price'] ? $p_data['sale_price'] : $p_data['price']);
update_post_meta($product_id, '_regular_price', $p_data['price']);
update_post_meta($product_id, '_sale_price', $p_data['sale_price']);
update_post_meta($product_id, '_stock_status', 'instock');
update_post_meta($product_id, '_manage_stock', 'no');
if ($attach_id) { set_post_thumbnail($product_id, $attach_id); }
}
}
$blog_cat = term_exists('سلامت و تغذیه', 'category');
if (!$blog_cat) {
$blog_cat = wp_insert_term('سلامت و تغذیه', 'category');
$blog_cat_id = is_array($blog_cat) ? $blog_cat['term_id'] : $blog_cat;
} else {
$blog_cat_id = is_array($blog_cat) ? $blog_cat['term_id'] : $blog_cat;
}
$posts_data = array(
array('title' => '۱۰ فایده شگفت‌انگیز عسل طبیعی', 'content' => '<p>عسل طبیعی یکی از قدیمی‌ترین مواد غذایی است.</p>', 'img' => 'https://images.unsplash.com/photo-1558642452-9d2a7deb7f62?w=800&q=80'),
array('title' => 'طرز تهیه حلوا ارده خانگی', 'content' => '<p>حلوا ارده یکی از شیرینی‌های سنتی ایرانی است.</p>', 'img' => 'https://images.unsplash.com/photo-1590080875515-8a3a8dc5735e?w=800&q=80')
);
foreach ($posts_data as $post_data) {
$attach_id = media_sideload_image($post_data['img'], 0, $post_data['title'], 'id');
if (is_wp_error($attach_id)) { $attach_id = ''; }
$post_id = wp_insert_post(array(
'post_title' => $post_data['title'], 'post_content' => $post_data['content'],
'post_status' => 'publish', 'post_type' => 'post',
));
if (!is_wp_error($post_id)) {
wp_set_object_terms($post_id, array((int)$blog_cat_id), 'category');
if ($attach_id) { set_post_thumbnail($post_id, $attach_id); }
}
}
update_option('motayeb_seeded_done', true);
wp_redirect(admin_url('admin.php?seed_success=1'));
exit;
}
add_action('admin_notices', 'motayeb_seed_success_notice');
function motayeb_seed_success_notice() {
if (isset($_GET['seed_success'])) {
echo '<div class="notice notice-success is-dismissible"><p><strong>موفقیت‌آمیز!</strong> دیتای تستی اضافه شد.</p></div>';
}
}
// ===================================================================
// تابع کمکی برای ظاهر کامنت‌ها (بدون خطای Cannot redeclare)
// ===================================================================
add_action('init', function() {
remove_action('comment_template', 'comment_form');
});