<?php

class Super_Product_Filter_CPT {
	public function __construct() {
        $this->include_files();

        add_action('init', array($this, 'register_post_type'));

        /* To add the Custom Column on Custom Post */
        add_filter('manage_swpf-product-filter_posts_columns', array($this, 'columns_head'));
        add_action('manage_swpf-product-filter_posts_custom_column', array($this, 'columns_content'), 10, 2);

        // Get posts by query
        add_filter('plugin_action_links_' . SWPF_BASENAME, array($this, 'add_settings_link'));

        add_filter('get_user_option_screen_layout_swpf-product-filter', function () {
            return 1;
        });
    }

    public function include_files() {
        include SWPF_PATH . 'admin/inc/cpt/metabox/init.php';
    }

    public function register_post_type() {
        $labels = array(
            'name' => _x('Super Product Filter', 'post type general name', 'super-product-filter'),
            'singular_name' => _x('Super Product Filter', 'post type singular name', 'super-product-filter'),
            'menu_name' => _x('Super Product Filter', 'admin menu', 'super-product-filter'),
            'name_admin_bar' => _x('Super Product Filter', 'add new on admin bar', 'super-product-filter'),
            'add_new' => _x('Add New', 'Super Product Filter', 'super-product-filter'),
            'add_new_item' => esc_html__('Add New Filter', 'super-product-filter'),
            'new_item' => esc_html__('New Filter Preset', 'super-product-filter'),
            'edit_item' => esc_html__('Edit Filter Preset', 'super-product-filter'),
            'view_item' => esc_html__('View Filter Preset', 'super-product-filter'),
            'all_items' => esc_html__('All Filter Preset', 'super-product-filter'),
            'search_items' => esc_html__('Search Filter Preset', 'super-product-filter'),
            'parent_item_colon' => esc_html__('Parent Filter Preset', 'super-product-filter'),
            'not_found' => esc_html__('No Filter Preset found.', 'super-product-filter'),
            'not_found_in_trash' => esc_html__('No Filter Preset found in Trash.', 'super-product-filter')
        );

        $args = array(
            'labels' => $labels,
            'description' => esc_html__('Description', 'super-product-filter'),
            'public' => false,
            'publicly_queryable' => false, // hides preview button
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_icon' => 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2MS42IDYxLjUiPjxnPjxwY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WxsOiNhN2FhYWQiLz48L2c+PC9zdmc+',
            'query_var' => true,
            'rewrite' => array('slug' => 'swpf-product-filter'),
            'has_archive' => true,
            'hierarchical' => false,
            'menu_position' => null,
            'supports' => array('title')
        );
        register_post_type('swpf-product-filter', $args);
    }

    public function columns_head($defaults) {
        $defaults['shortcodes'] = esc_html__('Shortcodes', 'super-product-filter');
        $defaults['template'] = esc_html__('Template Include', 'super-product-filter');
        unset($defaults['date']);   // remove it from the columns list
        $defaults['date'] = esc_html__('Date', 'super-product-filter');
        return $defaults;
    }

    public function columns_content($column, $post_ID) {
        if ($column == 'shortcodes') {
            $id = $post_ID;
            ?>
            [swpf_shortcode id="<?php echo esc_attr($id); ?>"]
            <?php
        }
        if ($column == 'template') {
            $id = $post_ID;
            ?>
            &lt;?php echo do_shortcode("[swpf_shortcode id='<?php echo esc_attr($id); ?>']"); ?&gt;
            <?php
        }
    }

    public function add_settings_link($links) {
        $settings_link = '<a href="' . esc_url(get_admin_url(null, 'edit.php?post_type=swpf-product-filter')) . '">' . esc_html__('Settings', 'super-product-filter') . '</a>';
        array_unshift($links, $settings_link);
        return $links;
    }
}

new Super_Product_Filter_CPT();