<?php

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

/**
 * Implements available wiaas countries
 *
 * Class Wiaas_Measurement_Units
 */
class Wiaas_Measurement_Units {


    public static function init() {

        add_action('woocommerce_after_register_taxonomy', array(__CLASS__, 'register_measurement_units_taxonomy'));
    }

    private static $available_units = array(
        'Piece' => array(
            'id' => 1,
            'name' => 'piece',
            'unit' => 'piece',
        ),
        'Meter' => array(
            'id' => 1,
            'name' => 'meter',
            'unit' => 'm',
        ),
        'Square' => array(
            'id' => 1,
            'name' => 'square_meter',
            'unit' => 'm2',
        ),
        'Centimeter' => array(
            'id' => 1,
            'name' => 'centimeter',
            'unit' => 'cm',
        ),
    );

    /**
     * Registers product taxonomy for avaiable countries
     */
    public static function register_measurement_units_taxonomy() {

        $labels = array(
            'name' => _x('Unit', 'taxonomy general name', 'wiaas'),
            'singular_name' => _x('Unit', 'taxonomy singular name', 'wiaas'),
            'menu_name' => _x('Unit', 'Admin menu name', 'wiaas'),
            'search_items' => __('Search Units', 'wiaas'),
            'all_items' => __('All Units', 'wiaas'),
            'parent_item' => __('Parent Unit', 'wiaas'),
            'parent_item_colon' => __('Parent Unit:', 'wiaas'),
            'edit_item' => __('Edit Unit', 'wiaas'),
            'update_item' => __('Update Unit', 'wiaas'),
            'add_new_item' => __('Add New Unit', 'wiaas'),
            'new_item_name' => __('New Unit Name', 'wiaas'),
        );

        $args = array(
            'hierarchical' => false,
            'label' => __('Units', 'wiaas'),
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'meta_box_cb' => false,
            'query_var' => true,
            'rewrite' => array('slug' => 'wiaas_units'),
        );

        register_taxonomy('wiaas_units', array('product'), $args);

        foreach (self::$available_units as $available_unit) {
            if (!term_exists($available_unit, 'wiaas_units')) {
                wp_insert_term($available_unit['unit'], 'wiaas_units');
            }
        }
    }
}

Wiaas_Measurement_Units::init();
