PHP 8.0.30
Preview: class-wc-product-attribute.php Size: 7.87 KB
/home/certprox/zang.certproxywizard.com/wp/wp-content/plugins/woocommerce/includes/class-wc-product-attribute.php

<?php
/**
 * Represents a product attribute
 *
 * Attributes can be global (taxonomy based) or local to the product itself.
 * Uses ArrayAccess to be BW compatible with previous ways of reading attributes.
 *
 * @package WooCommerce\Classes
 * @version 3.0.0
 * @since   3.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Product attribute class.
 */
class WC_Product_Attribute implements ArrayAccess {

	/**
	 * Data array.
	 *
	 * @since 3.0.0
	 * @var array
	 */
	protected $data = array(
		'id'        => 0,
		'name'      => '',
		'options'   => array(),
		'position'  => 0,
		'visible'   => false,
		'variation' => false,
	);

	/**
	 * Extra data array.
	 *
	 * @since 10.6.0
	 * @var array
	 */
	protected $extra_data = array();

	/**
	 * Return if this attribute is a taxonomy.
	 *
	 * @return boolean
	 */
	public function is_taxonomy() {
		return 0 < $this->get_id();
	}

	/**
	 * Get taxonomy name if applicable.
	 *
	 * @return string
	 */
	public function get_taxonomy() {
		return $this->is_taxonomy() ? $this->get_name() : '';
	}

	/**
	 * Get taxonomy object.
	 *
	 * @return array|null
	 */
	public function get_taxonomy_object() {
		global $wc_product_attributes;
		return $this->is_taxonomy() ? $wc_product_attributes[ $this->get_name() ] : null;
	}

	/**
	 * Gets terms from the stored options.
	 *
	 * @return array|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 names get escaped in WP. See sanitize_term_field.
				$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;
	}

	/**
	 * Gets slugs from the stored options, or just the string if text based.
	 *
	 * @return array
	 */
	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;
	}

	/**
	 * Returns all data for this object.
	 *
	 * @return array
	 */
	public function get_data() {
		return array_merge(
			$this->extra_data,
			$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() ),
			)
		);
	}

	/*
	|--------------------------------------------------------------------------
	| Setters
	|--------------------------------------------------------------------------
	*/

	/**
	 * Set extra data by key.
	 *
	 * @since 10.6.0
	 * @param string $key   Extra data key.
	 * @param mixed  $value Extra data value.
	 */
	public function set_extra_data( string $key, $value ): void {
		$this->extra_data[ $key ] = $value;
	}

	/**
	 * Set ID (this is the attribute ID).
	 *
	 * @param int $value Attribute ID.
	 */
	public function set_id( $value ) {
		$this->data['id'] = absint( $value );
	}

	/**
	 * Set name (this is the attribute name or taxonomy).
	 *
	 * @param string $value Attribute name.
	 */
	public function set_name( $value ) {
		$this->data['name'] = $value;
	}

	/**
	 * Set options.
	 *
	 * @param array $value Attribute options.
	 */
	public function set_options( $value ) {
		$this->data['options'] = $value;
	}

	/**
	 * Set position.
	 *
	 * @param int $value Attribute position.
	 */
	public function set_position( $value ) {
		$this->data['position'] = absint( $value );
	}

	/**
	 * Set if visible.
	 *
	 * @param bool $value If is visible on Product's additional info tab.
	 */
	public function set_visible( $value ) {
		$this->data['visible'] = wc_string_to_bool( $value );
	}

	/**
	 * Set if variation.
	 *
	 * @param bool $value If is used for variations.
	 */
	public function set_variation( $value ) {
		$this->data['variation'] = wc_string_to_bool( $value );
	}

	/*
	|--------------------------------------------------------------------------
	| Getters
	|--------------------------------------------------------------------------
	*/

	/**
	 * Get all extra data.
	 *
	 * @since 10.6.0
	 * @return array
	 */
	public function get_all_extra_data() {
		return $this->extra_data;
	}

	/**
	 * Get extra data by key.
	 *
	 * @since 10.6.0
	 * @param string $key Extra data key.
	 * @return mixed
	 */
	public function get_extra_data( string $key ) {
		return $this->extra_data[ $key ] ?? null;
	}

	/**
	 * Get the ID.
	 *
	 * @return int
	 */
	public function get_id() {
		return $this->data['id'];
	}

	/**
	 * Get name.
	 *
	 * @return string
	 */
	public function get_name() {
		return $this->data['name'];
	}

	/**
	 * Get options.
	 *
	 * @return array
	 */
	public function get_options() {
		return $this->data['options'];
	}

	/**
	 * Get position.
	 *
	 * @return int
	 */
	public function get_position() {
		return $this->data['position'];
	}

	/**
	 * Get if visible.
	 *
	 * @return bool
	 */
	public function get_visible() {
		return $this->data['visible'];
	}

	/**
	 * Get if variation.
	 *
	 * @return bool
	 */
	public function get_variation() {
		return $this->data['variation'];
	}

	/*
	|--------------------------------------------------------------------------
	| ArrayAccess/Backwards compatibility.
	|--------------------------------------------------------------------------
	*/

	/**
	 * OffsetGet.
	 *
	 * @param string $offset Offset.
	 * @return mixed
	 */
	#[\ReturnTypeWillChange]
	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"}();
				}
				if ( isset( $this->extra_data[ $offset ] ) ) {
					return $this->extra_data[ $offset ];
				}
				break;
		}
		return '';
	}

	/**
	 * OffsetSet.
	 *
	 * @param string $offset Offset.
	 * @param mixed  $value  Value.
	 */
	#[\ReturnTypeWillChange]
	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" ) ) ) {
					$this->{"set_$offset"}( $value );
					break;
				}
				$this->extra_data[ $offset ] = $value;
				break;
		}
	}

	/**
	 * OffsetUnset.
	 *
	 * @param string $offset Offset.
	 */
	#[\ReturnTypeWillChange]
	public function offsetUnset( $offset ) {}

	/**
	 * OffsetExists.
	 *
	 * @param string $offset Offset.
	 * @return bool
	 */
	#[\ReturnTypeWillChange]
	public function offsetExists( $offset ) {
		return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ), array_keys( $this->extra_data ) ), true );
	}
}

Directory Contents

Dirs: 28 × Files: 115

Name Size Perms Modified Actions
abstracts DIR
- drwxr-xr-x 2026-03-30 10:22:29
Edit Download
admin DIR
- drwxr-xr-x 2026-03-30 10:22:31
Edit Download
blocks DIR
- drwxr-xr-x 2026-03-30 10:22:31
Edit Download
cli DIR
- drwxr-xr-x 2026-03-30 10:22:33
Edit Download
- drwxr-xr-x 2026-03-30 10:22:33
Edit Download
- drwxr-xr-x 2026-03-30 10:22:34
Edit Download
emails DIR
- drwxr-xr-x 2026-03-30 10:22:34
Edit Download
export DIR
- drwxr-xr-x 2026-03-30 10:22:34
Edit Download
gateways DIR
- drwxr-xr-x 2026-03-30 10:22:34
Edit Download
import DIR
- drwxr-xr-x 2026-03-30 10:22:34
Edit Download
- drwxr-xr-x 2026-03-30 10:21:54
Edit Download
- drwxr-xr-x 2026-03-30 10:22:34
Edit Download
legacy DIR
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
libraries DIR
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
queue DIR
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
- drwxr-xr-x 2026-03-30 10:22:35
Edit Download
rest-api DIR
- drwxr-xr-x 2026-03-30 10:22:36
Edit Download
shipping DIR
- drwxr-xr-x 2026-03-30 10:21:54
Edit Download
- drwxr-xr-x 2026-03-30 10:22:36
Edit Download
- drwxr-xr-x 2026-03-30 10:22:36
Edit Download
tracks DIR
- drwxr-xr-x 2026-03-30 10:22:36
Edit Download
traits DIR
- drwxr-xr-x 2026-03-30 10:22:37
Edit Download
walkers DIR
- drwxr-xr-x 2026-03-30 10:22:37
Edit Download
- drwxr-xr-x 2026-03-30 10:22:37
Edit Download
widgets DIR
- drwxr-xr-x 2026-03-30 10:22:38
Edit Download
123.43 KB lrw-r--r-- 2026-03-30 10:22:31
Edit Download
12.69 KB lrw-r--r-- 2026-03-30 10:22:31
Edit Download
5.27 KB lrw-r--r-- 2026-03-30 10:22:31
Edit Download
4.58 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
3.45 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
1.78 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
6.89 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
34.80 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
10.41 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
12.69 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
3.37 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
25.38 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
28.48 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
74.21 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
50.15 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
3.34 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
23.08 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
50.67 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
43.87 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
3.37 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
10.34 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
33.20 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
1.29 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
6.59 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
2.26 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
6.59 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
7.34 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
36.64 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
28.37 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
39.09 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
4.24 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
48.17 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
34.36 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
30.43 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
1.99 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
11.34 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
4.33 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
116.06 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
1.28 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
3.90 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
9.41 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
2.21 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
8.66 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
4.08 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
9.99 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
5.80 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
17.54 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
9.58 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
6.49 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
21.39 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
2.55 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
5.99 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
77.53 KB lrw-r--r-- 2026-03-30 10:22:32
Edit Download
16.56 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
6.24 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
36.87 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
32.89 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
1.79 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
13.61 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
14.69 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
17.22 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
7.87 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
13.18 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
4.98 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
4.54 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
6.75 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
2.27 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
2.70 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
23.70 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
20.18 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
33.50 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
4.00 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
7.74 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
15.44 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
5.05 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
21.55 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
276 B lrw-r--r-- 2026-03-30 10:22:33
Edit Download
24.15 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
9.34 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
13.08 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
5.00 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
12.85 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
18.82 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
24.42 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
39.74 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
20.42 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
51.50 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
5.79 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
30.08 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
62.17 KB lrw-r--r-- 2026-03-30 10:22:33
Edit Download
14.15 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
21.85 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
4.17 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
20.81 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
15.53 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
78.71 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
5.56 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
39.78 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
49.90 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
2.17 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
8.49 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
43.41 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
5.03 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
5.97 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
9.43 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
67.38 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
13.93 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
17.43 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
141.89 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
12.84 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
24.57 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
101.89 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
34.17 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
5.77 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download
2.01 KB lrw-r--r-- 2026-03-30 10:22:37
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).