<?php
/**
 * Media Library Taxonomies for WooCommerce (Iron SEO 3 FREE patch)
 * - Registers new Media-specific taxonomies
 * - Attaches WooCommerce product taxonomies to 'attachment' (product_cat, product_tag, pa_*)
 * - Attaches product_brand if present (WooCommerce Brands or equivalent)
 */

defined('ABSPATH') || exit;

if ( ! defined('MEDIA_WC_BRAND_TAXONOMY') ) {
    define('MEDIA_WC_BRAND_TAXONOMY', 'product_brand');
}

add_action('init', function() {

    $base_args = function($hierarchical) {
        return [
            'hierarchical'      => (bool) $hierarchical,
            'show_ui'           => true,
            'show_in_menu'      => true,
            'show_admin_column' => true,
            'query_var'         => true,
            'show_in_quick_edit'=> false,
            'show_in_nav_menus' => false,
            'show_tagcloud'     => true,
            'show_in_rest'      => true,
            'rewrite'           => [ 'with_front' => false ],
        ];
    };

    $defs = [
        'media_product' => [
            'args'   => array_merge( $base_args(false), [
                'labels' => [
                    'name'          => __('Product (Media)', 'iron-seo-3core'),
                    'singular_name' => __('Product (Media)', 'iron-seo-3core'),
                    'menu_name'     => __('Product (Media)', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'brand_category' => [
            'args'   => array_merge( $base_args(true), [
                'labels' => [
                    'name'          => __('Brand Categories', 'iron-seo-3core'),
                    'singular_name' => __('Brand Category', 'iron-seo-3core'),
                    'menu_name'     => __('Brand Categories', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'brand_tag' => [
            'args'   => array_merge( $base_args(false), [
                'labels' => [
                    'name'          => __('Brand Tags', 'iron-seo-3core'),
                    'singular_name' => __('Brand Tag', 'iron-seo-3core'),
                    'menu_name'     => __('Brand Tags', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'brand_pattern' => [
            'args'   => array_merge( $base_args(true), [
                'labels' => [
                    'name'          => __('Brand Patterns', 'iron-seo-3core'),
                    'singular_name' => __('Brand Pattern', 'iron-seo-3core'),
                    'menu_name'     => __('Brand Patterns', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'media_review' => [
            'args'   => array_merge( $base_args(false), [
                'labels' => [
                    'name'          => __('Reviews (Media)', 'iron-seo-3core'),
                    'singular_name' => __('Review (Media)', 'iron-seo-3core'),
                    'menu_name'     => __('Reviews (Media)', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'media_review_cat' => [
            'args'   => array_merge( $base_args(true), [
                'labels' => [
                    'name'          => __('Reviews Categories', 'iron-seo-3core'),
                    'singular_name' => __('Reviews Category', 'iron-seo-3core'),
                    'menu_name'     => __('Reviews Categories', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'media_review_tag' => [
            'args'   => array_merge( $base_args(false), [
                'labels' => [
                    'name'          => __('Reviews Tags', 'iron-seo-3core'),
                    'singular_name' => __('Reviews Tag', 'iron-seo-3core'),
                    'menu_name'     => __('Reviews Tags', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'attribute_category' => [
            'args'   => array_merge( $base_args(true), [
                'labels' => [
                    'name'          => __('Attributes Categories', 'iron-seo-3core'),
                    'singular_name' => __('Attributes Category', 'iron-seo-3core'),
                    'menu_name'     => __('Attributes Categories', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
        'attribute_tag' => [
            'args'   => array_merge( $base_args(false), [
                'labels' => [
                    'name'          => __('Attributes Tags', 'iron-seo-3core'),
                    'singular_name' => __('Attributes Tag', 'iron-seo-3core'),
                    'menu_name'     => __('Attributes Tags', 'iron-seo-3core'),
                ],
            ]),
            'attach' => true,
        ],
    ];

    foreach ($defs as $tax => $def) {
        if ( ! taxonomy_exists($tax) ) {
            register_taxonomy($tax, ['attachment'], $def['args']);
        }
        register_taxonomy_for_object_type($tax, 'attachment');
    }

    foreach (['product_cat', 'product_tag'] as $wc_tax) {
        if ( taxonomy_exists($wc_tax) ) {
            register_taxonomy_for_object_type($wc_tax, 'attachment');
        }
    }

    if ( taxonomy_exists(MEDIA_WC_BRAND_TAXONOMY) ) {
        register_taxonomy_for_object_type(MEDIA_WC_BRAND_TAXONOMY, 'attachment');
    }

    if ( function_exists('wc_get_attribute_taxonomies') ) {
        $attribute_taxonomies = wc_get_attribute_taxonomies();
        if ( ! empty($attribute_taxonomies) ) {
            foreach ( $attribute_taxonomies as $tax_obj ) {
                if ( ! empty($tax_obj->attribute_name) ) {
                    $tax = wc_attribute_taxonomy_name( $tax_obj->attribute_name );
                    if ( taxonomy_exists($tax) ) {
                        register_taxonomy_for_object_type($tax, 'attachment');
                    }
                }
            }
        }
    }

}, 11);

add_filter('register_taxonomy_args', function($args, $taxonomy){
    if ( ! isset($args['meta_box_cb']) ) {
        $args['meta_box_cb'] = null;
    }
    return $args;
}, 10, 2);

if ( function_exists('register_activation_hook') ) {
    register_activation_hook(__FILE__, function() {
        do_action('init');
        flush_rewrite_rules();
    });
}
