Refactor WooCommerce product loop templates to support AJAX quick add-to-cart, wishlist functionality, and improved visual badges. Added new static pages for About and Contact. - Refactor `content-product.php` to include quick actions and discount badges - Update `archive-product.php` and `front-page.php` for improved product display - Add `motayeb_get_rating_html` helper function in `functions.php` - Add `page-about.php` and `page-contact.php` - Fix product card width issues in `header.php` - Implement dynamic banner option in `front-page.php`
486 lines
26 KiB
PHP
486 lines
26 KiB
PHP
<?php
|
||
/**
|
||
* توابع اختصاصی تم مطیب
|
||
*
|
||
* @package Motayeb
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
// جلوگیری از دسترسی مستقیم
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
// ============================================================
|
||
// ۱. تنظیمات اولیه تم (Theme Setup)
|
||
// ============================================================
|
||
function motayeb_theme_setup() {
|
||
add_theme_support( 'title-tag' );
|
||
add_theme_support( 'post-thumbnails' );
|
||
add_theme_support( 'custom-logo', array(
|
||
'height' => 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' => '<section id="%1$s" class="widget %2$s">',
|
||
'after_widget' => '</section>',
|
||
'before_title' => '<h3 class="widget-title">',
|
||
'after_title' => '</h3>',
|
||
) );
|
||
if ( class_exists( 'WooCommerce' ) ) {
|
||
register_sidebar( array(
|
||
'name' => esc_html__( 'سایدبار فروشگاه', 'motayeb' ),
|
||
'id' => 'sidebar-shop',
|
||
'before_widget' => '<section id="%1$s" class="widget %2$s">',
|
||
'after_widget' => '</section>',
|
||
'before_title' => '<h3 class="widget-title">',
|
||
'after_title' => '</h3>',
|
||
) );
|
||
}
|
||
for ( $i = 1; $i <= 4; $i++ ) {
|
||
register_sidebar( array(
|
||
'name' => esc_html__( 'فوتر - ستون ' . $i, 'motayeb' ),
|
||
'id' => 'footer-' . $i,
|
||
'before_widget' => '<div id="%1$s" class="footer-widget %2$s">',
|
||
'after_widget' => '</div>',
|
||
'before_title' => '<h4 class="footer-widget-title">',
|
||
'after_title' => '</h4>',
|
||
) );
|
||
}
|
||
}
|
||
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 .= '<ul class="absolute top-full right-0 min-w-[200px] bg-white dark:bg-dark-card shadow-xl rounded-xl p-2 mt-2 opacity-0 invisible group-hover:opacity-100 group-hover:visible transition-all duration-300 border border-cream-200 dark:border-dark-border">';
|
||
}
|
||
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 .= '<li class="' . esc_attr( $classes ) . '">';
|
||
$output .= '<a href="' . esc_url( $item->url ) . '" class="block px-1 py-2">' . esc_html( $item->title ) . '</a>';
|
||
}
|
||
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
|
||
$output .= '</li>';
|
||
}
|
||
}
|
||
class Motayeb_Mobile_Walker extends Walker_Nav_Menu {
|
||
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
|
||
$output .= '<a href="' . esc_url( $item->url ) . '" onclick="closeMobileMenu()" class="flex items-center gap-3 px-4 py-3 rounded-xl hover:bg-forest-50 dark:hover:bg-forest-900/30 transition">';
|
||
$output .= '<iconify-icon icon="lucide:chevron-left" width="20" class="text-forest-500 dark:text-forest-300"></iconify-icon>';
|
||
$output .= '<span class="font-medium">' . esc_html( $item->title ) . '</span>';
|
||
$output .= '</a>';
|
||
}
|
||
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 '<p><label for="ingredients_field">متن ترکیبات را وارد کنید (میتوانید از تگهای HTML ساده استفاده کنید):</label></p>';
|
||
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' => 'نیلی' );
|
||
?>
|
||
<div id="motayeb-benefits-wrapper">
|
||
<p><label for="benefits_title_field"><strong>عنوان این بخش (در تب محصول):</strong></label>
|
||
<input type="text" id="benefits_title_field" name="benefits_title_field" value="<?php echo esc_attr( $benefits_title ); ?>" style="width:100%;max-width:400px;" placeholder="مثلاً: خواص سلامت، مزایا، ویژگیها..."></p>
|
||
<hr>
|
||
<p>هر آیتم شامل عنوان، توضیحات، آیکون و رنگ است. برای افزودن آیتم جدید، دکمه زیر را بزنید.</p>
|
||
<div id="motayeb-benefits-container">
|
||
<?php if ( empty( $benefits ) ) : ?>
|
||
<div class="motayeb-benefit-item" style="background:#f9f9f9;padding:15px;margin-bottom:10px;border:1px solid #ddd;border-radius:4px;">
|
||
<p><label>عنوان: <input type="text" name="benefit_title[]" value="" style="width:100%;" /></label></p>
|
||
<p><label>توضیحات: <textarea name="benefit_desc[]" rows="2" style="width:100%;"></textarea></label></p>
|
||
<p><label>آیکون: <select name="benefit_icon[]" style="width:100%;"><?php foreach ( $icon_list as $value => $label ) { echo '<option value="' . esc_attr( $value ) . '">' . esc_html( $label ) . '</option>'; } ?></select></label></p>
|
||
<p><label>رنگ: <select name="benefit_color[]" style="width:100%;"><?php foreach ( $color_list as $value => $label ) { echo '<option value="' . esc_attr( $value ) . '">' . esc_html( $label ) . '</option>'; } ?></select></label></p>
|
||
<button type="button" class="button remove-benefit">حذف این آیتم</button>
|
||
</div>
|
||
<?php else : ?>
|
||
<?php foreach ( $benefits as $item ) : ?>
|
||
<div class="motayeb-benefit-item" style="background:#f9f9f9;padding:15px;margin-bottom:10px;border:1px solid #ddd;border-radius:4px;">
|
||
<p><label>عنوان: <input type="text" name="benefit_title[]" value="<?php echo esc_attr( $item['title'] ); ?>" style="width:100%;" /></label></p>
|
||
<p><label>توضیحات: <textarea name="benefit_desc[]" rows="2" style="width:100%;"><?php echo esc_textarea( $item['description'] ); ?></textarea></label></p>
|
||
<p><label>آیکون: <select name="benefit_icon[]" style="width:100%;"><?php foreach ( $icon_list as $value => $label ) { echo '<option value="' . esc_attr( $value ) . '" ' . selected( $item['icon'], $value, false ) . '>' . esc_html( $label ) . '</option>'; } ?></select></label></p>
|
||
<p><label>رنگ: <select name="benefit_color[]" style="width:100%;"><?php foreach ( $color_list as $value => $label ) { echo '<option value="' . esc_attr( $value ) . '" ' . selected( $item['color'], $value, false ) . '>' . esc_html( $label ) . '</option>'; } ?></select></label></p>
|
||
<button type="button" class="button remove-benefit">حذف این آیتم</button>
|
||
</div>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
</div>
|
||
<button type="button" id="add-benefit" class="button button-primary">افزودن آیتم جدید</button>
|
||
</div>
|
||
<style>.motayeb-benefit-item { background: #f9f9f9; padding: 15px; margin-bottom: 10px; border: 1px solid #ddd; border-radius: 4px; } .motayeb-benefit-item input, .motayeb-benefit-item textarea, .motayeb-benefit-item select { width: 100%; } .motayeb-benefit-item select { max-width: 300px; }</style>
|
||
<script>
|
||
jQuery(document).ready(function($) {
|
||
$('#add-benefit').on('click', function() {
|
||
var html = `<div class="motayeb-benefit-item" style="background:#f9f9f9;padding:15px;margin-bottom:10px;border:1px solid #ddd;border-radius:4px;">
|
||
<p><label>عنوان: <input type="text" name="benefit_title[]" value="" style="width:100%;" /></label></p>
|
||
<p><label>توضیحات: <textarea name="benefit_desc[]" rows="2" style="width:100%;"></textarea></label></p>
|
||
<p><label>آیکون: <select name="benefit_icon[]" style="width:100%;"><?php foreach ( $icon_list as $value => $label ) { echo '<option value="' . esc_attr( $value ) . '">' . esc_html( $label ) . '</option>'; } ?></select></label></p>
|
||
<p><label>رنگ: <select name="benefit_color[]" style="width:100%;"><?php foreach ( $color_list as $value => $label ) { echo '<option value="' . esc_attr( $value ) . '">' . esc_html( $label ) . '</option>'; } ?></select></label></p>
|
||
<button type="button" class="button remove-benefit">حذف این آیتم</button>
|
||
</div>`;
|
||
$('#motayeb-benefits-container').append(html);
|
||
});
|
||
$(document).on('click', '.remove-benefit', function() {
|
||
if (confirm('آیا از حذف این آیتم اطمینان دارید؟')) { $(this).closest('.motayeb-benefit-item').remove(); }
|
||
});
|
||
});
|
||
</script>
|
||
<?php
|
||
}
|
||
|
||
function motayeb_save_product_metaboxes( $post_id ) {
|
||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
|
||
if ( ! current_user_can( 'edit_post', $post_id ) ) { return; }
|
||
if ( isset( $_POST['motayeb_ingredients_nonce'] ) && wp_verify_nonce( $_POST['motayeb_ingredients_nonce'], 'motayeb_ingredients_nonce' ) && isset( $_POST['ingredients_field'] ) ) {
|
||
update_post_meta( $post_id, '_ingredients', wp_kses_post( $_POST['ingredients_field'] ) );
|
||
}
|
||
if ( isset( $_POST['motayeb_health_benefits_nonce'] ) && wp_verify_nonce( $_POST['motayeb_health_benefits_nonce'], 'motayeb_health_benefits_nonce' ) ) {
|
||
if ( isset( $_POST['benefits_title_field'] ) ) { update_post_meta( $post_id, '_health_benefits_title', sanitize_text_field( $_POST['benefits_title_field'] ) ); }
|
||
$titles = isset( $_POST['benefit_title'] ) ? array_map( 'sanitize_text_field', $_POST['benefit_title'] ) : array();
|
||
$descs = isset( $_POST['benefit_desc'] ) ? array_map( 'sanitize_textarea_field', $_POST['benefit_desc'] ) : array();
|
||
$icons = isset( $_POST['benefit_icon'] ) ? array_map( 'sanitize_text_field', $_POST['benefit_icon'] ) : array();
|
||
$colors = isset( $_POST['benefit_color'] ) ? array_map( 'sanitize_text_field', $_POST['benefit_color'] ) : array();
|
||
$benefits = array();
|
||
foreach ( $titles as $index => $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 '<p>تنظیمات بنر و ویژگیهای نمایش داده شده در صفحه فروشگاه را در اینجا مدیریت کنید.</p>';
|
||
}
|
||
|
||
function motayeb_shop_banner_callback() {
|
||
$value = get_option( 'motayeb_shop_banner', '' );
|
||
?>
|
||
<div class="motayeb-media-uploader">
|
||
<input type="text" id="motayeb_shop_banner" name="motayeb_shop_banner" value="<?php echo esc_attr( $value ); ?>" style="width: 70%;" class="regular-text" placeholder="آدرس تصویر را وارد کنید یا انتخاب کنید">
|
||
<button type="button" class="button motayeb-upload-btn" data-target="motayeb_shop_banner">انتخاب تصویر</button>
|
||
<br><br>
|
||
<img src="<?php echo esc_attr( $value ); ?>" style="max-width: 300px; height: auto; display: <?php echo empty($value) ? 'none' : 'block'; ?>;" id="preview-motayeb_shop_banner" />
|
||
</div>
|
||
<script>
|
||
jQuery(document).ready(function($){
|
||
$('.motayeb-upload-btn').click(function(e) {
|
||
e.preventDefault();
|
||
var button = $(this);
|
||
var targetId = button.data('target');
|
||
var customUploader = wp.media({
|
||
title: 'انتخاب تصویر بنر',
|
||
button: { text: 'استفاده از این تصویر' },
|
||
multiple: false
|
||
}).on('select', function() {
|
||
var attachment = customUploader.state().get('selection').first().toJSON();
|
||
$('#' + targetId).val(attachment.url);
|
||
$('#preview-' + targetId).attr('src', attachment.url).show();
|
||
}).open();
|
||
});
|
||
});
|
||
</script>
|
||
<?php
|
||
}
|
||
|
||
function motayeb_text_field_callback( $args ) {
|
||
$id = $args['id'];
|
||
$default = $args['default'];
|
||
$value = get_option( $id, $default );
|
||
echo '<input type="text" id="' . esc_attr( $id ) . '" name="' . esc_attr( $id ) . '" value="' . esc_attr( $value ) . '" style="width: 100%; max-width: 400px;" />';
|
||
}
|
||
|
||
/**
|
||
* ۵. نمایش صفحه تنظیمات در ادمین
|
||
*/
|
||
function motayeb_settings_page_callback() {
|
||
?>
|
||
<div class="wrap">
|
||
<h1>تنظیمات تم مطیب</h1>
|
||
<form method="post" action="options.php">
|
||
<?php
|
||
settings_fields( 'motayeb_settings' );
|
||
do_settings_sections( 'motayeb_settings' );
|
||
submit_button();
|
||
?>
|
||
</form>
|
||
</div>
|
||
<?php
|
||
}
|
||
|
||
// ============================================================
|
||
// ۱۰. غیرفعال کردن استایلهای پیشفرض ووکامرس
|
||
// ============================================================
|
||
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
|
||
|
||
// ============================================================
|
||
// ۹. نمایش ستارههای امتیاز (تابع کمکی برای صفحه محصول)
|
||
// ============================================================
|
||
function motayeb_get_rating_html( $rating, $count = 0 ) {
|
||
if ( $rating <= 0 ) {
|
||
return '<span class="text-xs text-cream-400 dark:text-dark-muted">بدون امتیاز</span>';
|
||
}
|
||
|
||
$full_stars = floor( $rating );
|
||
$half_star = ( $rating - $full_stars ) >= 0.5 ? 1 : 0;
|
||
$empty_stars = 5 - $full_stars - $half_star;
|
||
|
||
$html = '<span class="flex items-center gap-0.5 text-honey-400 text-sm">';
|
||
|
||
for ( $i = 0; $i < $full_stars; $i++ ) {
|
||
$html .= '★';
|
||
}
|
||
|
||
if ( $half_star ) {
|
||
$html .= '<span class="relative">★<span class="absolute inset-0 overflow-hidden" style="width:50%;color:#D4A574;">★</span></span>';
|
||
}
|
||
|
||
for ( $i = 0; $i < $empty_stars; $i++ ) {
|
||
$html .= '<span class="text-cream-300 dark:text-dark-muted">★</span>';
|
||
}
|
||
|
||
if ( $count > 0 ) {
|
||
$html .= ' <span class="text-xs text-cream-400 dark:text-dark-muted mr-1">(' . esc_html( $count ) . ')</span>';
|
||
}
|
||
|
||
$html .= '</span>';
|
||
|
||
return $html;
|
||
}
|