File "class-wc-product-attribute.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/woocommerce/includes/class-wc-product-attribute.php
File size: 5.1 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

defined('ABSPATH') || exit;
class WC_Product_Attribute implements ArrayAccess
{
    protected $data = array('id' => 0, 'name' => '', 'options' => array(), 'position' => 0, 'visible' => false, 'variation' => false);
    public function is_taxonomy()
    {
        return 0 < $this->get_id();
    }
    public function get_taxonomy()
    {
        return $this->is_taxonomy() ? $this->get_name() : '';
    }
    public function get_taxonomy_object()
    {
        global $wc_product_attributes;
        return $this->is_taxonomy() ? $wc_product_attributes[$this->get_name()] : null;
    }
    public function get_terms()
    {
        if (!$this->is_taxonomy() || !taxonomy_exists($this->get_name())) {
            return null;
        }
        $terms = array();
        foreach ($this->get_options() as $option) {
            if (is_int($option)) {
                $term = get_term_by('id', $option, $this->get_name());
            } else {
                $term = get_term_by('name', $option, $this->get_name());
                if (!$term || is_wp_error($term)) {
                    $new_term = wp_insert_term($option, $this->get_name());
                    $term = is_wp_error($new_term) ? false : get_term_by('id', $new_term['term_id'], $this->get_name());
                }
            }
            if ($term && !is_wp_error($term)) {
                $terms[] = $term;
            }
        }
        return $terms;
    }
    public function get_slugs()
    {
        if (!$this->is_taxonomy() || !taxonomy_exists($this->get_name())) {
            return $this->get_options();
        }
        $terms = array();
        foreach ($this->get_options() as $option) {
            if (is_int($option)) {
                $term = get_term_by('id', $option, $this->get_name());
            } else {
                $term = get_term_by('name', $option, $this->get_name());
                if (!$term || is_wp_error($term)) {
                    $new_term = wp_insert_term($option, $this->get_name());
                    $term = is_wp_error($new_term) ? false : get_term_by('id', $new_term['term_id'], $this->get_name());
                }
            }
            if ($term && !is_wp_error($term)) {
                $terms[] = $term->slug;
            }
        }
        return $terms;
    }
    public function get_data()
    {
        return array_merge($this->data, array('is_visible' => $this->get_visible() ? 1 : 0, 'is_variation' => $this->get_variation() ? 1 : 0, 'is_taxonomy' => $this->is_taxonomy() ? 1 : 0, 'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes($this->get_options())));
    }
    public function set_id($value)
    {
        $this->data['id'] = absint($value);
    }
    public function set_name($value)
    {
        $this->data['name'] = $value;
    }
    public function set_options($value)
    {
        $this->data['options'] = $value;
    }
    public function set_position($value)
    {
        $this->data['position'] = absint($value);
    }
    public function set_visible($value)
    {
        $this->data['visible'] = wc_string_to_bool($value);
    }
    public function set_variation($value)
    {
        $this->data['variation'] = wc_string_to_bool($value);
    }
    public function get_id()
    {
        return $this->data['id'];
    }
    public function get_name()
    {
        return $this->data['name'];
    }
    public function get_options()
    {
        return $this->data['options'];
    }
    public function get_position()
    {
        return $this->data['position'];
    }
    public function get_visible()
    {
        return $this->data['visible'];
    }
    public function get_variation()
    {
        return $this->data['variation'];
    }
    public function offsetGet($offset)
    {
        switch ($offset) {
            case 'is_variation':
                return $this->get_variation() ? 1 : 0;
            case 'is_visible':
                return $this->get_visible() ? 1 : 0;
            case 'is_taxonomy':
                return $this->is_taxonomy() ? 1 : 0;
            case 'value':
                return $this->is_taxonomy() ? '' : wc_implode_text_attributes($this->get_options());
            default:
                if (is_callable(array($this, "get_{$offset}"))) {
                    return $this->{"get_{$offset}"}();
                }
                break;
        }
        return '';
    }
    public function offsetSet($offset, $value)
    {
        switch ($offset) {
            case 'is_variation':
                $this->set_variation($value);
                break;
            case 'is_visible':
                $this->set_visible($value);
                break;
            case 'value':
                $this->set_options($value);
                break;
            default:
                if (is_callable(array($this, "set_{$offset}"))) {
                    return $this->{"set_{$offset}"}($value);
                }
                break;
        }
    }
    public function offsetUnset($offset)
    {
    }
    public function offsetExists($offset)
    {
        return in_array($offset, array_merge(array('is_variation', 'is_visible', 'is_taxonomy', 'value'), array_keys($this->data)), true);
    }
}