abstracts
6 years ago
admin
6 years ago
cli
7 years ago
customizer
8 years ago
data-stores
6 years ago
emails
6 years ago
export
6 years ago
gateways
6 years ago
import
6 years ago
integrations
6 years ago
interfaces
6 years ago
legacy
6 years ago
libraries
6 years ago
log-handlers
7 years ago
payment-tokens
8 years ago
queue
7 years ago
shipping
6 years ago
shortcodes
6 years ago
theme-support
6 years ago
tracks
6 years ago
traits
6 years ago
walkers
8 years ago
wccom-site
6 years ago
widgets
6 years ago
class-wc-ajax.php
6 years ago
class-wc-api.php
6 years ago
class-wc-auth.php
7 years ago
class-wc-autoloader.php
6 years ago
class-wc-background-emailer.php
8 years ago
class-wc-background-updater.php
7 years ago
class-wc-breadcrumb.php
7 years ago
class-wc-cache-helper.php
6 years ago
class-wc-cart-fees.php
6 years ago
class-wc-cart-session.php
6 years ago
class-wc-cart-totals.php
6 years ago
class-wc-cart.php
6 years ago
class-wc-checkout.php
6 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
6 years ago
class-wc-countries.php
6 years ago
class-wc-coupon.php
6 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
6 years ago
class-wc-customer.php
7 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
7 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
6 years ago
class-wc-download-handler.php
6 years ago
class-wc-emails.php
6 years ago
class-wc-embed.php
8 years ago
class-wc-form-handler.php
6 years ago
class-wc-frontend-scripts.php
6 years ago
class-wc-geo-ip.php
8 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
6 years ago
class-wc-https.php
8 years ago
class-wc-install.php
6 years ago
class-wc-integrations.php
6 years ago
class-wc-log-levels.php
7 years ago
class-wc-logger.php
7 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
6 years ago
class-wc-order-item-coupon.php
7 years ago
class-wc-order-item-fee.php
7 years ago
class-wc-order-item-meta.php
7 years ago
class-wc-order-item-product.php
6 years ago
class-wc-order-item-shipping.php
7 years ago
class-wc-order-item-tax.php
6 years ago
class-wc-order-item.php
6 years ago
class-wc-order-query.php
7 years ago
class-wc-order-refund.php
8 years ago
class-wc-order.php
6 years ago
class-wc-payment-gateways.php
7 years ago
class-wc-payment-tokens.php
6 years ago
class-wc-post-data.php
6 years ago
class-wc-post-types.php
7 years ago
class-wc-privacy-background-process.php
8 years ago
class-wc-privacy-erasers.php
8 years ago
class-wc-privacy-exporters.php
7 years ago
class-wc-privacy.php
7 years ago
class-wc-product-attribute.php
7 years ago
class-wc-product-download.php
7 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
7 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
7 years ago
class-wc-product-simple.php
6 years ago
class-wc-product-variable.php
6 years ago
class-wc-product-variation.php
7 years ago
class-wc-query.php
6 years ago
class-wc-rate-limiter.php
6 years ago
class-wc-regenerate-images-request.php
7 years ago
class-wc-regenerate-images.php
7 years ago
class-wc-register-wp-admin-settings.php
8 years ago
class-wc-rest-authentication.php
6 years ago
class-wc-rest-exception.php
6 years ago
class-wc-session-handler.php
6 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
6 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
6 years ago
class-wc-shortcodes.php
6 years ago
class-wc-structured-data.php
6 years ago
class-wc-tax.php
6 years ago
class-wc-template-loader.php
7 years ago
class-wc-tracker.php
6 years ago
class-wc-validation.php
6 years ago
class-wc-webhook.php
6 years ago
class-woocommerce.php
6 years ago
wc-account-functions.php
6 years ago
wc-attribute-functions.php
6 years ago
wc-cart-functions.php
6 years ago
wc-conditional-functions.php
6 years ago
wc-core-functions.php
6 years ago
wc-coupon-functions.php
7 years ago
wc-deprecated-functions.php
7 years ago
wc-formatting-functions.php
6 years ago
wc-notice-functions.php
6 years ago
wc-order-functions.php
6 years ago
wc-order-item-functions.php
6 years ago
wc-page-functions.php
6 years ago
wc-product-functions.php
6 years ago
wc-rest-functions.php
7 years ago
wc-stock-functions.php
6 years ago
wc-template-functions.php
6 years ago
wc-template-hooks.php
6 years ago
wc-term-functions.php
6 years ago
wc-update-functions.php
6 years ago
wc-user-functions.php
6 years ago
wc-webhook-functions.php
7 years ago
wc-widget-functions.php
8 years ago
class-wc-product-attribute.php
330 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Represents a product attribute |
| 4 | * |
| 5 | * Attributes can be global (taxonomy based) or local to the product itself. |
| 6 | * Uses ArrayAccess to be BW compatible with previous ways of reading attributes. |
| 7 | * |
| 8 | * @package WooCommerce/Classes |
| 9 | * @version 3.0.0 |
| 10 | * @since 3.0.0 |
| 11 | */ |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Product attribute class. |
| 17 | */ |
| 18 | class WC_Product_Attribute implements ArrayAccess { |
| 19 | |
| 20 | /** |
| 21 | * Data array. |
| 22 | * |
| 23 | * @since 3.0.0 |
| 24 | * @var array |
| 25 | */ |
| 26 | protected $data = array( |
| 27 | 'id' => 0, |
| 28 | 'name' => '', |
| 29 | 'options' => array(), |
| 30 | 'position' => 0, |
| 31 | 'visible' => false, |
| 32 | 'variation' => false, |
| 33 | ); |
| 34 | |
| 35 | /** |
| 36 | * Return if this attribute is a taxonomy. |
| 37 | * |
| 38 | * @return boolean |
| 39 | */ |
| 40 | public function is_taxonomy() { |
| 41 | return 0 < $this->get_id(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get taxonomy name if applicable. |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | public function get_taxonomy() { |
| 50 | return $this->is_taxonomy() ? $this->get_name() : ''; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get taxonomy object. |
| 55 | * |
| 56 | * @return array|null |
| 57 | */ |
| 58 | public function get_taxonomy_object() { |
| 59 | global $wc_product_attributes; |
| 60 | return $this->is_taxonomy() ? $wc_product_attributes[ $this->get_name() ] : null; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets terms from the stored options. |
| 65 | * |
| 66 | * @return array|null |
| 67 | */ |
| 68 | public function get_terms() { |
| 69 | if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) { |
| 70 | return null; |
| 71 | } |
| 72 | $terms = array(); |
| 73 | foreach ( $this->get_options() as $option ) { |
| 74 | if ( is_int( $option ) ) { |
| 75 | $term = get_term_by( 'id', $option, $this->get_name() ); |
| 76 | } else { |
| 77 | // Term names get escaped in WP. See sanitize_term_field. |
| 78 | $term = get_term_by( 'name', $option, $this->get_name() ); |
| 79 | |
| 80 | if ( ! $term || is_wp_error( $term ) ) { |
| 81 | $new_term = wp_insert_term( $option, $this->get_name() ); |
| 82 | $term = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $this->get_name() ); |
| 83 | } |
| 84 | } |
| 85 | if ( $term && ! is_wp_error( $term ) ) { |
| 86 | $terms[] = $term; |
| 87 | } |
| 88 | } |
| 89 | return $terms; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Gets slugs from the stored options, or just the string if text based. |
| 94 | * |
| 95 | * @return array |
| 96 | */ |
| 97 | public function get_slugs() { |
| 98 | if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) { |
| 99 | return $this->get_options(); |
| 100 | } |
| 101 | $terms = array(); |
| 102 | foreach ( $this->get_options() as $option ) { |
| 103 | if ( is_int( $option ) ) { |
| 104 | $term = get_term_by( 'id', $option, $this->get_name() ); |
| 105 | } else { |
| 106 | $term = get_term_by( 'name', $option, $this->get_name() ); |
| 107 | |
| 108 | if ( ! $term || is_wp_error( $term ) ) { |
| 109 | $new_term = wp_insert_term( $option, $this->get_name() ); |
| 110 | $term = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $this->get_name() ); |
| 111 | } |
| 112 | } |
| 113 | if ( $term && ! is_wp_error( $term ) ) { |
| 114 | $terms[] = $term->slug; |
| 115 | } |
| 116 | } |
| 117 | return $terms; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns all data for this object. |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function get_data() { |
| 126 | return array_merge( |
| 127 | $this->data, |
| 128 | array( |
| 129 | 'is_visible' => $this->get_visible() ? 1 : 0, |
| 130 | 'is_variation' => $this->get_variation() ? 1 : 0, |
| 131 | 'is_taxonomy' => $this->is_taxonomy() ? 1 : 0, |
| 132 | 'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ), |
| 133 | ) |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | |-------------------------------------------------------------------------- |
| 139 | | Setters |
| 140 | |-------------------------------------------------------------------------- |
| 141 | */ |
| 142 | |
| 143 | /** |
| 144 | * Set ID (this is the attribute ID). |
| 145 | * |
| 146 | * @param int $value Attribute ID. |
| 147 | */ |
| 148 | public function set_id( $value ) { |
| 149 | $this->data['id'] = absint( $value ); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Set name (this is the attribute name or taxonomy). |
| 154 | * |
| 155 | * @param int $value Attribute name. |
| 156 | */ |
| 157 | public function set_name( $value ) { |
| 158 | $this->data['name'] = $value; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Set options. |
| 163 | * |
| 164 | * @param array $value Attribute options. |
| 165 | */ |
| 166 | public function set_options( $value ) { |
| 167 | $this->data['options'] = $value; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Set position. |
| 172 | * |
| 173 | * @param int $value Attribute position. |
| 174 | */ |
| 175 | public function set_position( $value ) { |
| 176 | $this->data['position'] = absint( $value ); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set if visible. |
| 181 | * |
| 182 | * @param bool $value If is visible on Product's additional info tab. |
| 183 | */ |
| 184 | public function set_visible( $value ) { |
| 185 | $this->data['visible'] = wc_string_to_bool( $value ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Set if variation. |
| 190 | * |
| 191 | * @param bool $value If is used for variations. |
| 192 | */ |
| 193 | public function set_variation( $value ) { |
| 194 | $this->data['variation'] = wc_string_to_bool( $value ); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | |-------------------------------------------------------------------------- |
| 199 | | Getters |
| 200 | |-------------------------------------------------------------------------- |
| 201 | */ |
| 202 | |
| 203 | /** |
| 204 | * Get the ID. |
| 205 | * |
| 206 | * @return int |
| 207 | */ |
| 208 | public function get_id() { |
| 209 | return $this->data['id']; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Get name. |
| 214 | * |
| 215 | * @return string |
| 216 | */ |
| 217 | public function get_name() { |
| 218 | return $this->data['name']; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get options. |
| 223 | * |
| 224 | * @return array |
| 225 | */ |
| 226 | public function get_options() { |
| 227 | return $this->data['options']; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Get position. |
| 232 | * |
| 233 | * @return int |
| 234 | */ |
| 235 | public function get_position() { |
| 236 | return $this->data['position']; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get if visible. |
| 241 | * |
| 242 | * @return bool |
| 243 | */ |
| 244 | public function get_visible() { |
| 245 | return $this->data['visible']; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Get if variation. |
| 250 | * |
| 251 | * @return bool |
| 252 | */ |
| 253 | public function get_variation() { |
| 254 | return $this->data['variation']; |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | |-------------------------------------------------------------------------- |
| 259 | | ArrayAccess/Backwards compatibility. |
| 260 | |-------------------------------------------------------------------------- |
| 261 | */ |
| 262 | |
| 263 | /** |
| 264 | * OffsetGet. |
| 265 | * |
| 266 | * @param string $offset Offset. |
| 267 | * @return mixed |
| 268 | */ |
| 269 | public function offsetGet( $offset ) { |
| 270 | switch ( $offset ) { |
| 271 | case 'is_variation': |
| 272 | return $this->get_variation() ? 1 : 0; |
| 273 | case 'is_visible': |
| 274 | return $this->get_visible() ? 1 : 0; |
| 275 | case 'is_taxonomy': |
| 276 | return $this->is_taxonomy() ? 1 : 0; |
| 277 | case 'value': |
| 278 | return $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ); |
| 279 | default: |
| 280 | if ( is_callable( array( $this, "get_$offset" ) ) ) { |
| 281 | return $this->{"get_$offset"}(); |
| 282 | } |
| 283 | break; |
| 284 | } |
| 285 | return ''; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * OffsetSet. |
| 290 | * |
| 291 | * @param string $offset Offset. |
| 292 | * @param mixed $value Value. |
| 293 | */ |
| 294 | public function offsetSet( $offset, $value ) { |
| 295 | switch ( $offset ) { |
| 296 | case 'is_variation': |
| 297 | $this->set_variation( $value ); |
| 298 | break; |
| 299 | case 'is_visible': |
| 300 | $this->set_visible( $value ); |
| 301 | break; |
| 302 | case 'value': |
| 303 | $this->set_options( $value ); |
| 304 | break; |
| 305 | default: |
| 306 | if ( is_callable( array( $this, "set_$offset" ) ) ) { |
| 307 | return $this->{"set_$offset"}( $value ); |
| 308 | } |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * OffsetUnset. |
| 315 | * |
| 316 | * @param string $offset Offset. |
| 317 | */ |
| 318 | public function offsetUnset( $offset ) {} |
| 319 | |
| 320 | /** |
| 321 | * OffsetExists. |
| 322 | * |
| 323 | * @param string $offset Offset. |
| 324 | * @return bool |
| 325 | */ |
| 326 | public function offsetExists( $offset ) { |
| 327 | return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ) ), true ); |
| 328 | } |
| 329 | } |
| 330 |