<?php

if (!defined('ABSPATH')) {
    exit;
}

class WPDD_Post_Types {
    
    public static function init() {
        // Register immediately since we're already in the init hook
        self::register_post_types();
        self::register_taxonomies();
        add_filter('post_type_link', array(__CLASS__, 'product_permalink'), 10, 2);
    }
    
    public static function register_post_types() {
        $labels = array(
            'name' => __('Products', 'wp-digital-download'),
            'singular_name' => __('Product', 'wp-digital-download'),
            'menu_name' => __('Digital Products', 'wp-digital-download'),
            'add_new' => __('Add New', 'wp-digital-download'),
            'add_new_item' => __('Add New Product', 'wp-digital-download'),
            'edit_item' => __('Edit Product', 'wp-digital-download'),
            'new_item' => __('New Product', 'wp-digital-download'),
            'view_item' => __('View Product', 'wp-digital-download'),
            'view_items' => __('View Products', 'wp-digital-download'),
            'search_items' => __('Search Products', 'wp-digital-download'),
            'not_found' => __('No products found', 'wp-digital-download'),
            'not_found_in_trash' => __('No products found in Trash', 'wp-digital-download'),
            'all_items' => __('All Products', 'wp-digital-download'),
            'archives' => __('Product Archives', 'wp-digital-download'),
            'attributes' => __('Product Attributes', 'wp-digital-download'),
            'insert_into_item' => __('Insert into product', 'wp-digital-download'),
            'uploaded_to_this_item' => __('Uploaded to this product', 'wp-digital-download'),
            'featured_image' => __('Product Image', 'wp-digital-download'),
            'set_featured_image' => __('Set product image', 'wp-digital-download'),
            'remove_featured_image' => __('Remove product image', 'wp-digital-download'),
            'use_featured_image' => __('Use as product image', 'wp-digital-download'),
        );
        
        $args = array(
            'labels' => $labels,
            'public' => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'product'),
            'capability_type' => 'post',
            'capabilities' => array(
                'edit_post' => 'edit_wpdd_product',
                'read_post' => 'read_wpdd_product',
                'delete_post' => 'delete_wpdd_product',
                'edit_posts' => 'edit_wpdd_products',
                'edit_others_posts' => 'edit_others_wpdd_products',
                'publish_posts' => 'publish_wpdd_products',
                'read_private_posts' => 'read_private_wpdd_products',
                'delete_posts' => 'delete_wpdd_products',
                'delete_private_posts' => 'delete_private_wpdd_products',
                'delete_published_posts' => 'delete_published_wpdd_products',
                'delete_others_posts' => 'delete_others_wpdd_products',
                'edit_private_posts' => 'edit_private_wpdd_products',
                'edit_published_posts' => 'edit_published_wpdd_products',
            ),
            'map_meta_cap' => true,
            'has_archive' => true,
            'hierarchical' => false,
            'menu_position' => 25,
            'menu_icon' => 'dashicons-download',
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'author'),
            'show_in_rest' => true,
        );
        
        register_post_type('wpdd_product', $args);
    }
    
    public static function register_taxonomies() {
        $labels = array(
            'name' => __('Product Categories', 'wp-digital-download'),
            'singular_name' => __('Product Category', 'wp-digital-download'),
            'search_items' => __('Search Categories', 'wp-digital-download'),
            'all_items' => __('All Categories', 'wp-digital-download'),
            'parent_item' => __('Parent Category', 'wp-digital-download'),
            'parent_item_colon' => __('Parent Category:', 'wp-digital-download'),
            'edit_item' => __('Edit Category', 'wp-digital-download'),
            'update_item' => __('Update Category', 'wp-digital-download'),
            'add_new_item' => __('Add New Category', 'wp-digital-download'),
            'new_item_name' => __('New Category Name', 'wp-digital-download'),
            'menu_name' => __('Categories', 'wp-digital-download'),
        );
        
        $args = array(
            'labels' => $labels,
            'hierarchical' => true,
            'public' => true,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'product-category'),
            'show_in_rest' => true,
        );
        
        register_taxonomy('wpdd_product_category', 'wpdd_product', $args);
        
        $tag_labels = array(
            'name' => __('Product Tags', 'wp-digital-download'),
            'singular_name' => __('Product Tag', 'wp-digital-download'),
            'search_items' => __('Search Tags', 'wp-digital-download'),
            'popular_items' => __('Popular Tags', 'wp-digital-download'),
            'all_items' => __('All Tags', 'wp-digital-download'),
            'edit_item' => __('Edit Tag', 'wp-digital-download'),
            'update_item' => __('Update Tag', 'wp-digital-download'),
            'add_new_item' => __('Add New Tag', 'wp-digital-download'),
            'new_item_name' => __('New Tag Name', 'wp-digital-download'),
            'separate_items_with_commas' => __('Separate tags with commas', 'wp-digital-download'),
            'add_or_remove_items' => __('Add or remove tags', 'wp-digital-download'),
            'choose_from_most_used' => __('Choose from the most used tags', 'wp-digital-download'),
            'menu_name' => __('Tags', 'wp-digital-download'),
        );
        
        $tag_args = array(
            'labels' => $tag_labels,
            'hierarchical' => false,
            'public' => true,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'product-tag'),
            'show_in_rest' => true,
        );
        
        register_taxonomy('wpdd_product_tag', 'wpdd_product', $tag_args);
    }
    
    public static function product_permalink($permalink, $post) {
        if ($post->post_type !== 'wpdd_product') {
            return $permalink;
        }
        
        return $permalink;
    }
}