60,
'width' => 200,
'flex-height' => true,
'flex-width' => true,
'header-text' => array( 'site-title', 'site-description' ),
) );
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' );
add_theme_support( 'html5', array(
'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script',
) );
add_theme_support( 'align-wide' );
add_theme_support( 'responsive-embeds' );
register_nav_menus( array(
'primary' => esc_html__( 'منوی اصلی', 'motayeb' ),
'footer' => esc_html__( 'منوی فوتر', 'motayeb' ),
) );
add_image_size( 'motayeb-product-grid', 400, 400, true );
add_image_size( 'motayeb-product-single', 600, 600, true );
}
add_action( 'after_setup_theme', 'motayeb_theme_setup' );
// ============================================================
// ۲. بارگذاری فایلهای استایل و اسکریپت (Enqueue)
// ============================================================
function motayeb_scripts() {
wp_enqueue_style( 'motayeb-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
if ( class_exists( 'WooCommerce' ) ) {
wp_enqueue_style( 'motayeb-woocommerce', get_theme_file_uri( '/assets/css/woocommerce.css' ), array( 'motayeb-style' ), wp_get_theme()->get( 'Version' ) );
}
wp_enqueue_style( 'motayeb-responsive', get_theme_file_uri( '/assets/css/responsive.css' ), array( 'motayeb-style' ), wp_get_theme()->get( 'Version' ) );
wp_enqueue_script( 'motayeb-main', get_theme_file_uri( '/assets/js/main.js' ), array( 'jquery' ), wp_get_theme()->get( 'Version' ), true );
if ( class_exists( 'WooCommerce' ) ) {
wp_enqueue_script( 'motayeb-woocommerce', get_theme_file_uri( '/assets/js/woocommerce.js' ), array( 'jquery', 'motayeb-main' ), wp_get_theme()->get( 'Version' ), true );
}
wp_enqueue_script( 'motayeb-darkmode', get_theme_file_uri( '/assets/js/darkmode.js' ), array( 'jquery' ), wp_get_theme()->get( 'Version' ), true );
wp_localize_script( 'motayeb-main', 'motayeb_ajax', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'motayeb_nonce' ),
'checkout_url' => wc_get_checkout_url(),
'cart_url' => wc_get_cart_url(),
) );
if ( is_product() ) { wp_enqueue_script( 'wc-single-product' ); }
if ( is_cart() ) { wp_enqueue_script( 'wc-cart' ); }
if ( is_checkout() ) { wp_enqueue_script( 'wc-checkout' ); }
}
add_action( 'wp_enqueue_scripts', 'motayeb_scripts' );
// ============================================================
// ۳. ثبت ناحیههای ویجت (Widget Areas)
// ============================================================
function motayeb_widgets_init() {
register_sidebar( array(
'name' => esc_html__( 'سایدبار اصلی', 'motayeb' ),
'id' => 'sidebar-main',
'before_widget' => '',
'before_title' => '
',
) );
if ( class_exists( 'WooCommerce' ) ) {
register_sidebar( array(
'name' => esc_html__( 'سایدبار فروشگاه', 'motayeb' ),
'id' => 'sidebar-shop',
'before_widget' => '',
'before_title' => '',
) );
}
for ( $i = 1; $i <= 4; $i++ ) {
register_sidebar( array(
'name' => esc_html__( 'فوتر - ستون ' . $i, 'motayeb' ),
'id' => 'footer-' . $i,
'before_widget' => '',
'before_title' => '',
) );
}
}
add_action( 'widgets_init', 'motayeb_widgets_init' );
// ============================================================
// ۴. توابع کمکی ووکامرس
// ============================================================
add_filter( 'loop_shop_columns', function() { return 4; } );
add_filter( 'loop_shop_per_page', function() { return 12; } );
function motayeb_body_classes( $classes ) {
if ( class_exists( 'WooCommerce' ) ) {
if ( is_product() ) { $classes[] = 'motayeb-product-page'; }
if ( is_shop() ) { $classes[] = 'motayeb-shop-page'; }
}
if ( isset( $_COOKIE['motayeb_darkmode'] ) && 'on' === $_COOKIE['motayeb_darkmode'] ) {
$classes[] = 'dark-mode';
}
return $classes;
}
add_filter( 'body_class', 'motayeb_body_classes' );
// ============================================================
// ۵. کلاسهای Walker برای منو
// ============================================================
class Motayeb_Desktop_Walker extends Walker_Nav_Menu {
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$output .= '';
}
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$classes = 'relative group';
if ( $depth === 0 ) {
$classes .= ' nav-link text-sm font-medium text-earth-400 dark:text-dark-text hover:text-forest-500 dark:hover:text-forest-300 transition';
}
$output .= '- ';
$output .= '' . esc_html( $item->title ) . '';
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= '
';
}
}
class Motayeb_Mobile_Walker extends Walker_Nav_Menu {
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= '';
$output .= '';
$output .= '' . esc_html( $item->title ) . '';
$output .= '';
}
public function end_el( &$output, $item, $depth = 0, $args = array() ) {}
}
// ============================================================
// ۶. توابع Ajax برای جستجو و دریافت محصولات
// ============================================================
function motayeb_ajax_get_product_data() {
check_ajax_referer( 'motayeb_nonce', 'nonce' );
$product_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
if ( ! $product_id ) { wp_send_json_error( 'شناسه محصول نامعتبر است' ); }
$product = wc_get_product( $product_id );
if ( ! $product ) { wp_send_json_error( 'محصول یافت نشد' ); }
$image = wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' );
wp_send_json_success( array(
'id' => $product->get_id(),
'name' => $product->get_name(),
'price' => $product->get_price() ? floatval( $product->get_price() ) : 0,
'img' => $image ? $image[0] : 'default-product',
'stock' => $product->get_stock_quantity(),
) );
}
add_action( 'wp_ajax_get_product_data', 'motayeb_ajax_get_product_data' );
add_action( 'wp_ajax_nopriv_get_product_data', 'motayeb_ajax_get_product_data' );
function motayeb_ajax_search_products() {
check_ajax_referer( 'motayeb_nonce', 'nonce' );
$term = isset( $_GET['term'] ) ? sanitize_text_field( $_GET['term'] ) : '';
if ( strlen( $term ) < 2 ) { wp_send_json_success( array() ); }
$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 's' => $term );
$query = new WP_Query( $args );
$results = array();
while ( $query->have_posts() ) {
$query->the_post();
$product = wc_get_product( get_the_ID() );
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail' );
$results[] = array(
'id' => get_the_ID(),
'name' => get_the_title(),
'price' => $product->get_price() ? floatval( $product->get_price() ) : 0,
'image' => $image ? $image[0] : '',
'category' => 'محصول',
);
}
wp_reset_postdata();
wp_send_json_success( $results );
}
add_action( 'wp_ajax_search_products', 'motayeb_ajax_search_products' );
add_action( 'wp_ajax_nopriv_search_products', 'motayeb_ajax_search_products' );
// ============================================================
// ۷. متاباکسهای سفارشی برای محصولات
// ============================================================
function motayeb_add_product_metaboxes() {
add_meta_box( 'motayeb_ingredients', 'ترکیبات محصول', 'motayeb_ingredients_metabox_callback', 'product', 'normal', 'high' );
add_meta_box( 'motayeb_health_benefits', 'خواص سلامت (هر آیتم در یک خط + آیکون انتخابی)', 'motayeb_health_benefits_metabox_callback', 'product', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'motayeb_add_product_metaboxes' );
function motayeb_ingredients_metabox_callback( $post ) {
wp_nonce_field( 'motayeb_ingredients_nonce', 'motayeb_ingredients_nonce' );
$value = get_post_meta( $post->ID, '_ingredients', true );
echo '';
wp_editor( $value, 'ingredients_field', array(
'textarea_name' => 'ingredients_field', 'textarea_rows' => 8, 'media_buttons' => false, 'teeny' => true, 'quicktags' => true
) );
}
function motayeb_health_benefits_metabox_callback( $post ) {
wp_nonce_field( 'motayeb_health_benefits_nonce', 'motayeb_health_benefits_nonce' );
$benefits = get_post_meta( $post->ID, '_health_benefits', true );
if ( ! is_array( $benefits ) ) { $benefits = array(); }
$benefits_title = get_post_meta( $post->ID, '_health_benefits_title', true );
$icon_list = array( 'heart-pulse' => '❤️ تپش قلب', 'flame' => '🔥 شعله', 'stomach' => '🫀 معده', 'sparkles' => '✨ جرقه', 'shield-check' => '🛡️ سپر', 'droplets' => '💧 قطره', 'leaf' => '🍃 برگ', 'sun' => '☀️ خورشید', 'zap' => '⚡ برق', 'moon' => '🌙 ماه', 'star' => '⭐ ستاره', 'smile' => '😊 لبخند' );
$color_list = array( 'text-red-400' => 'قرمز', 'text-orange-400' => 'نارنجی', 'text-green-400' => 'سبز', 'text-purple-400' => 'بنفش', 'text-blue-400' => 'آبی', 'text-amber-400' => 'عنبری', 'text-emerald-400' => 'زمردی', 'text-rose-400' => 'گلبهی', 'text-pink-400' => 'صورتی', 'text-indigo-400' => 'نیلی' );
?>
هر آیتم شامل عنوان، توضیحات، آیکون و رنگ است. برای افزودن آیتم جدید، دکمه زیر را بزنید.
$title ) {
if ( ! empty( $title ) && isset( $descs[ $index ] ) ) {
$benefits[] = array(
'title' => $title,
'description' => $descs[ $index ],
'icon' => isset( $icons[ $index ] ) ? $icons[ $index ] : 'heart-pulse',
'color' => isset( $colors[ $index ] ) ? $colors[ $index ] : 'text-red-400',
);
}
}
update_post_meta( $post_id, '_health_benefits', $benefits );
}
}
add_action( 'save_post_product', 'motayeb_save_product_metaboxes' );
add_action( 'comment_post', 'motayeb_save_rating_field' );
function motayeb_save_rating_field( $comment_id ) {
if ( isset( $_POST['rating'] ) && '' !== $_POST['rating'] ) {
add_comment_meta( $comment_id, 'rating', intval( $_POST['rating'] ) );
}
}
// ============================================================
// ۸. تنظیمات تم مطیب (با استفاده از Settings API) - بخش جدید
// ============================================================
/**
* ۱. ایجاد صفحه تنظیمات در پنل ادمین
*/
function motayeb_add_admin_menu() {
add_options_page(
'تنظیمات تم مطیب',
'تنظیمات مطیب',
'manage_options',
'motayeb_settings',
'motayeb_settings_page_callback'
);
}
add_action( 'admin_menu', 'motayeb_add_admin_menu' );
/**
* ۲. بارگذاری اسکریپتهای مدیا آپلودر در صفحه تنظیمات
*/
function motayeb_admin_scripts( $hook ) {
if ( 'settings_page_motayeb_settings' !== $hook ) {
return;
}
wp_enqueue_media();
wp_enqueue_script( 'jquery' );
}
add_action( 'admin_enqueue_scripts', 'motayeb_admin_scripts' );
/**
* ۳. ثبت تنظیمات، بخشها و فیلدها
*/
function motayeb_register_settings() {
// ثبت یک بخش
add_settings_section(
'motayeb_shop_section',
'تنظیمات صفحه فروشگاه',
'motayeb_shop_section_callback',
'motayeb_settings'
);
// ۱. فیلد بنر فروشگاه
add_settings_field(
'motayeb_shop_banner',
'عکس بنر فروشگاه',
'motayeb_shop_banner_callback',
'motayeb_settings',
'motayeb_shop_section'
);
register_setting( 'motayeb_settings', 'motayeb_shop_banner', 'esc_url_raw' );
// ۲. فیلدهای ویژگیها (آیکون و متن)
for ( $i = 1; $i <= 4; $i++ ) {
// آیکون
add_settings_field(
'motayeb_feature_' . $i . '_icon',
'آیکون ویژگی ' . $i . ' (مثلاً: lucide:leaf)',
'motayeb_text_field_callback',
'motayeb_settings',
'motayeb_shop_section',
array( 'id' => 'motayeb_feature_' . $i . '_icon', 'default' => 'lucide:leaf' )
);
register_setting( 'motayeb_settings', 'motayeb_feature_' . $i . '_icon', 'sanitize_text_field' );
// متن
add_settings_field(
'motayeb_feature_' . $i . '_text',
'متن ویژگی ' . $i,
'motayeb_text_field_callback',
'motayeb_settings',
'motayeb_shop_section',
array( 'id' => 'motayeb_feature_' . $i . '_text', 'default' => 'ویژگی ' . $i )
);
register_setting( 'motayeb_settings', 'motayeb_feature_' . $i . '_text', 'sanitize_text_field' );
}
}
add_action( 'admin_init', 'motayeb_register_settings' );
/**
* ۴. توابع بازگشتی (Callback) برای نمایش فیلدها در ادمین
*/
function motayeb_shop_section_callback() {
echo 'تنظیمات بنر و ویژگیهای نمایش داده شده در صفحه فروشگاه را در اینجا مدیریت کنید.
';
}
function motayeb_shop_banner_callback() {
$value = get_option( 'motayeb_shop_banner', '' );
?>
';
}
/**
* ۵. نمایش صفحه تنظیمات در ادمین
*/
function motayeb_settings_page_callback() {
?>
تنظیمات تم مطیب