<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! defined( 'WT_RELATED_PRODUCTS_VERSION' ) || 'no' === get_option( 'pwbe_linked_products', 'no' ) ) {
    return;
}

if ( ! class_exists( 'PWBE_WT_Related_Products' ) ) :

final class PWBE_WT_Related_Products {

    private $related_product_fields = array(
        '_crp_related_product_cats',
        '_crp_related_product_tags',
        '_crp_related_ids',
        '_crp_excluded_cats',
    );

    function __construct() {
        add_filter( 'pwbe_product_columns', array( $this, 'pwbe_product_columns' ) );
        add_filter( 'pwbe_select_options', array( $this, 'pwbe_select_options' ), 11 ); // Priority 11 to run after /plugins/woocommerce-linked-products.php
        add_filter( 'pwbe_save_field_value', array( $this, 'pwbe_save_field_value' ), 10, 2 );
    }

    function pwbe_product_columns( $columns ) {
        $insert_after_column = '_crosssell_ids___';

        $new_columns = array();

        $new_columns[] = array(
            'name' => __( 'Related categories', 'wt-woocommerce-related-products' ),
            'type' => 'multiselect',
            'table' => 'meta',
            'field' => '_crp_related_product_cats',
            'readonly' => 'false',
            'visibility' => 'parent',
            'sortable' => 'true',
            'views' => array( 'all' ),
        );

        $new_columns[] = array(
            'name' => __( 'Related tags', 'wt-woocommerce-related-products' ),
            'type' => 'multiselect',
            'table' => 'meta',
            'field' => '_crp_related_product_tags',
            'readonly' => 'false',
            'visibility' => 'parent',
            'sortable' => 'true',
            'views' => array( 'all' ),
        );

        $new_columns[] = array(
            'name' => __( 'Related products', 'wt-woocommerce-related-products' ),
            'type' => 'multiselect',
            'table' => 'meta',
            'field' => '_crp_related_ids',
            'readonly' => 'false',
            'visibility' => 'parent',
            'sortable' => 'true',
            'views' => array( 'all' ),
        );

        $new_columns[] = array(
            'name' => __( 'Exclude categories', 'wt-woocommerce-related-products' ),
            'type' => 'multiselect',
            'table' => 'meta',
            'field' => '_crp_excluded_cats',
            'readonly' => 'false',
            'visibility' => 'parent',
            'sortable' => 'true',
            'views' => array( 'all' ),
        );

        if ( !empty( $new_columns ) ) {
            // Insert after the "Sale End Date" column.
            $start_index = 1;
            foreach ( $columns as $index => $column ) {
                if ( $column['field'] === $insert_after_column ) {
                    $start_index = $index + 1;
                    break;
                }
            }

            array_splice( $columns, $start_index, 0, $new_columns );
        }

        return $columns;
    }

    function pwbe_select_options( $select_options ) {
        // Make sure each field is defined initially.
        foreach ( $this->related_product_fields as $field_name ) {
            $select_options[ $field_name ] = array();
        }

        $terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false ) );
        if ( !is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                $select_options['_crp_related_product_cats'][ $term->term_id ]['name'] = $term->name;
                $select_options['_crp_related_product_cats'][ $term->term_id ]['visibility'] = 'both';

                $select_options['_crp_excluded_cats'][ $term->term_id ]['name'] = $term->name;
                $select_options['_crp_excluded_cats'][ $term->term_id ]['visibility'] = 'both';
            }
        }

        $tags = get_terms( array( 'taxonomy' => 'product_tag', 'hide_empty' => false ) );
        if ( !is_wp_error( $tags ) ) {
            foreach ( $tags as $tag ) {
                $select_options['_crp_related_product_tags'][ $tag->term_id ]['name'] = $tag->name;
                $select_options['_crp_related_product_tags'][ $tag->term_id ]['visibility'] = 'both';
            }
        }

        foreach ( $select_options['_crosssell_ids___'] as $product_id => $option ) {
            $select_options['_crp_related_ids'][ $product_id ]['name'] = $option['name'];
            $select_options['_crp_related_ids'][ $product_id ]['visibility'] = $option['visibility'];
        }

        return $select_options;
    }

    function pwbe_save_field_value( $value, $field ) {
        if ( !empty( $value ) && in_array( $field['field'], $this->related_product_fields ) ) {
            $value = explode( ',', $value );
            $value = array_map( 'absint', $value );
        }

        return $value;
    }
}

new PWBE_WT_Related_Products();

endif;
