/*
Theme Name:   Blocksy Child
Theme URI:    https://creativethemes.com/blocksy/
Description:  A custom child theme for the Blocksy WordPress theme.
Author:       Your Name
Author URI:   https://binadar.com/
Template:     blocksy
Version:      1.0.0
License:      GNU General Public License v2 or later
License URI:  http://www.gnu.org/licenses/gpl-2.0.html
*/

/* يمكنك إضافة تنسيقات الـ CSS المخصصة الخاصة بك هنا */

function pwb_brands_grid_shortcode() {
    $taxonomy = 'pwb-brand';

    // جلب جميع البراندات المسجلة
    $terms = get_terms(array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => false, // اجعلها true إذا كنت تريد إخفاء البراندات التي لا تحتوي على منتجات
    ));

    if (empty($terms) || is_wp_error($terms)) {
        return '<p style="text-align:center;">لم يتم العثور على براندات لعرضها.</p>';
    }

    // بدء بناء التصميم بـ HTML و CSS مدمج لسهولة الاستخدام
    $output = '
    <style>
        .pwb-brands-section {
            background-color: #dcdde1; /* الخلفية الرمادية كما في الصورة */
            padding: 40px 20px;
            border-radius: 8px;
        }
        .pwb-brands-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); /* توزيع تلقائي متجاوب */
            gap: 20px; /* المسافات بين البطاقات */
            justify-content: center;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }
        .pwb-brand-card {
            background-color: #ffffff; /* لون البطاقة الأبيض */
            border-radius: 15px; /* زوايا دائرية ملحوظة */
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 110px; /* ارتفاع موحد للبطاقات */
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05); /* ظل خفيف جداً */
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .pwb-brand-card:hover {
            transform: translateY(-5px); /* تأثير حركة عند تمرير الماوس */
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
        }
        .pwb-brand-card img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain; /* المحافظة على أبعاد الشعار الأصلي دون تشويه */
        }
    </style>
    ';

    $output .= '<div class="pwb-brands-section">';
    $output .= '<div class="pwb-brands-grid">';

    foreach ($terms as $term) {
        // جلب معرف الصورة الخاص بـ Perfect WooCommerce Brands
        $attachment_id = get_term_meta($term->term_id, 'pwb_brand_image', true);
        
        // رابط أرشيف البراند عند الضغط عليه
        $brand_link = get_term_link($term);

        if ($attachment_id) {
            // جلب رابط الصورة الفعلي بالحجم الكامل
            $image_url = wp_get_attachment_image_url($attachment_id, 'full');
            
            if ($image_url) {
                $output .= '<a href="' . esc_url($brand_link) . '" class="pwb-brand-card" title="' . esc_attr($term->name) . '">';
                $output .= '<img src="' . esc_url($image_url) . '" alt="' . esc_attr($term->name) . '" loading="lazy" />';
                $output .= '</a>';
            }
        }
    }

    $output .= '</div>';
    $output .= '</div>';

    return $output;
}
// تسجيل الشورت كود في الوردبريس
add_shortcode('pwb_brands_grid', 'pwb_brands_grid_shortcode');