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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / wc-attribute-functions.php
woocommerce / includes Last commit date
abstracts 4 weeks ago admin 4 days ago blocks 11 months ago cli 4 weeks ago customizer 4 months ago data-stores 4 weeks ago emails 4 days ago export 1 year ago gateways 4 weeks ago import 3 months ago integrations 2 months ago interfaces 4 months ago legacy 4 months ago libraries 4 weeks ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 months ago react-admin 4 weeks ago rest-api 4 weeks ago shipping 3 months ago shortcodes 4 weeks ago theme-support 2 years ago tracks 4 weeks ago traits 5 years ago walkers 5 years ago wccom-site 4 weeks ago widgets 2 months ago class-wc-ajax.php 4 weeks ago class-wc-auth.php 1 year ago class-wc-autoloader.php 9 months ago class-wc-background-emailer.php 2 months ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 4 weeks ago class-wc-breadcrumb.php 4 months ago class-wc-cache-helper.php 4 weeks ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 3 months ago class-wc-cart-totals.php 11 months ago class-wc-cart.php 4 weeks ago class-wc-checkout.php 4 weeks ago class-wc-cli.php 10 months ago class-wc-comments.php 4 months ago class-wc-countries.php 2 months ago class-wc-coupon.php 2 months ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 4 weeks ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 months ago class-wc-discounts.php 11 months ago class-wc-download-handler.php 4 weeks ago class-wc-emails.php 4 weeks ago class-wc-embed.php 1 year ago class-wc-form-handler.php 3 months ago class-wc-frontend-scripts.php 2 months ago class-wc-geo-ip.php 9 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 2 months ago class-wc-https.php 3 years ago class-wc-install.php 4 days ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 4 months ago class-wc-meta-data.php 4 weeks ago class-wc-order-factory.php 2 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 5 months ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 4 weeks ago class-wc-order-item-shipping.php 5 months ago class-wc-order-item-tax.php 4 weeks ago class-wc-order-item.php 4 weeks ago class-wc-order-query.php 4 weeks ago class-wc-order-refund.php 1 year ago class-wc-order.php 4 weeks ago class-wc-payment-gateways.php 4 weeks ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 2 months ago class-wc-post-types.php 4 weeks ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 10 months ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 months ago class-wc-product-download.php 4 months ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 3 months ago class-wc-product-grouped.php 3 months ago class-wc-product-query.php 4 months ago class-wc-product-simple.php 11 months ago class-wc-product-variable.php 4 weeks ago class-wc-product-variation.php 4 weeks ago class-wc-query.php 4 weeks ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 2 years ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 2 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 3 months ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 7 months ago class-wc-shipping.php 2 months ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 4 weeks ago class-wc-tax.php 2 months ago class-wc-template-loader.php 7 months ago class-wc-tracker.php 4 weeks ago class-wc-validation.php 2 years ago class-wc-webhook.php 4 weeks ago class-woocommerce.php 4 days ago wc-account-functions.php 4 weeks ago wc-attribute-functions.php 4 weeks ago wc-brands-functions.php 1 year ago wc-cart-functions.php 4 weeks ago wc-conditional-functions.php 11 months ago wc-core-functions.php 2 months ago wc-coupon-functions.php 5 months ago wc-deprecated-functions.php 4 weeks ago wc-formatting-functions.php 4 weeks ago wc-interactivity-api-functions.php 2 months ago wc-notice-functions.php 5 months ago wc-order-functions.php 1 month ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 4 months ago wc-page-functions.php 1 month ago wc-product-functions.php 2 months ago wc-rest-functions.php 7 months ago wc-stock-functions.php 7 months ago wc-template-functions.php 4 weeks ago wc-template-hooks.php 10 months ago wc-term-functions.php 2 months ago wc-update-functions.php 4 days ago wc-user-functions.php 4 weeks ago wc-webhook-functions.php 4 weeks ago wc-widget-functions.php 5 years ago
wc-attribute-functions.php
785 lines
1 <?php
2 /**
3 * WooCommerce Attribute Functions
4 *
5 * @package WooCommerce\Functions
6 * @version 2.1.0
7 */
8
9 use Automattic\WooCommerce\Enums\ProductType;
10 use Automattic\WooCommerce\Utilities\FeaturesUtil;
11
12 defined( 'ABSPATH' ) || exit;
13
14 /**
15 * Gets text attributes from a string.
16 *
17 * @since 2.4
18 * @param string $raw_attributes Raw attributes.
19 * @return array
20 */
21 function wc_get_text_attributes( $raw_attributes ) {
22 if ( ! is_string( $raw_attributes ) ) {
23 return array();
24 }
25 return array_filter( array_map( 'trim', explode( WC_DELIMITER, html_entity_decode( $raw_attributes, ENT_QUOTES, get_bloginfo( 'charset' ) ) ) ), 'wc_get_text_attributes_filter_callback' );
26 }
27
28 /**
29 * See if an attribute is actually valid.
30 *
31 * @since 3.0.0
32 * @param string $value Value.
33 * @return bool
34 */
35 function wc_get_text_attributes_filter_callback( $value ) {
36 return '' !== $value;
37 }
38
39 /**
40 * Implode an array of attributes using WC_DELIMITER.
41 *
42 * @since 3.0.0
43 * @param array $attributes Attributes list.
44 * @return string
45 */
46 function wc_implode_text_attributes( $attributes ) {
47 return implode( ' ' . WC_DELIMITER . ' ', $attributes );
48 }
49
50 /**
51 * Get attribute taxonomies.
52 *
53 * @return array of objects, @since 3.6.0 these are also indexed by ID.
54 */
55 function wc_get_attribute_taxonomies() {
56 $prefix = WC_Cache_Helper::get_cache_prefix( 'woocommerce-attributes' );
57 $cache_key = $prefix . 'attributes';
58 $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' );
59
60 if ( false !== $cache_value ) {
61 return $cache_value;
62 }
63
64 $raw_attribute_taxonomies = get_transient( 'wc_attribute_taxonomies' );
65
66 if ( false === $raw_attribute_taxonomies ) {
67 global $wpdb;
68
69 $raw_attribute_taxonomies = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name != '' ORDER BY attribute_name ASC;" );
70
71 set_transient( 'wc_attribute_taxonomies', $raw_attribute_taxonomies );
72 }
73
74 /**
75 * Filter attribute taxonomies.
76 *
77 * @param array $attribute_taxonomies Results of the DB query. Each taxonomy is an object.
78 */
79 $raw_attribute_taxonomies = (array) array_filter( apply_filters( 'woocommerce_attribute_taxonomies', $raw_attribute_taxonomies ) );
80
81 // Index by ID for easier lookups.
82 $attribute_taxonomies = array();
83
84 foreach ( $raw_attribute_taxonomies as $result ) {
85 $attribute_taxonomies[ 'id:' . $result->attribute_id ] = $result;
86 }
87
88 wp_cache_set( $cache_key, $attribute_taxonomies, 'woocommerce-attributes' );
89
90 return $attribute_taxonomies;
91 }
92
93 /**
94 * Get (cached) attribute taxonomy ID and name pairs.
95 *
96 * @since 3.6.0
97 * @return array
98 */
99 function wc_get_attribute_taxonomy_ids() {
100 $prefix = WC_Cache_Helper::get_cache_prefix( 'woocommerce-attributes' );
101 $cache_key = $prefix . 'ids';
102 $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' );
103
104 if ( false !== $cache_value ) {
105 return $cache_value;
106 }
107
108 $taxonomy_ids = array_map( 'absint', wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_id', 'attribute_name' ) );
109
110 wp_cache_set( $cache_key, $taxonomy_ids, 'woocommerce-attributes' );
111
112 return $taxonomy_ids;
113 }
114
115 /**
116 * Get (cached) attribute taxonomy label and name pairs.
117 *
118 * @since 3.6.0
119 * @return array
120 */
121 function wc_get_attribute_taxonomy_labels() {
122 $prefix = WC_Cache_Helper::get_cache_prefix( 'woocommerce-attributes' );
123 $cache_key = $prefix . 'labels';
124 $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' );
125
126 if ( false !== $cache_value ) {
127 return $cache_value;
128 }
129
130 $taxonomy_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
131
132 wp_cache_set( $cache_key, $taxonomy_labels, 'woocommerce-attributes' );
133
134 return $taxonomy_labels;
135 }
136
137 /**
138 * Get a product attribute name.
139 *
140 * @param string $attribute_name Attribute name.
141 * @return string
142 */
143 function wc_attribute_taxonomy_name( $attribute_name ) {
144 return $attribute_name ? 'pa_' . wc_sanitize_taxonomy_name( $attribute_name ) : '';
145 }
146
147 /**
148 * Get the attribute name used when storing values in post meta.
149 *
150 * @since 2.6.0
151 * @param string $attribute_name Attribute name.
152 * @return string
153 */
154 function wc_variation_attribute_name( $attribute_name ) {
155 return 'attribute_' . sanitize_title( $attribute_name );
156 }
157
158 /**
159 * Get a product attribute name by ID.
160 *
161 * @since 2.4.0
162 * @param int $attribute_id Attribute ID.
163 * @return string Return an empty string if attribute doesn't exist.
164 */
165 function wc_attribute_taxonomy_name_by_id( $attribute_id ) {
166 $taxonomy_ids = wc_get_attribute_taxonomy_ids();
167 $attribute_name = (string) array_search( $attribute_id, $taxonomy_ids, true );
168 return wc_attribute_taxonomy_name( $attribute_name );
169 }
170
171 /**
172 * Get a product attribute ID by name.
173 *
174 * @since 2.6.0
175 * @param string $name Attribute name.
176 * @return int
177 */
178 function wc_attribute_taxonomy_id_by_name( $name ) {
179 $name = wc_attribute_taxonomy_slug( $name );
180 $taxonomy_ids = wc_get_attribute_taxonomy_ids();
181
182 return isset( $taxonomy_ids[ $name ] ) ? $taxonomy_ids[ $name ] : 0;
183 }
184
185 /**
186 * Get a product attributes label.
187 *
188 * @param string $name Attribute name.
189 * @param WC_Product $product Product data.
190 * @return string
191 */
192 function wc_attribute_label( $name, $product = '' ) {
193 if ( taxonomy_is_product_attribute( $name ) ) {
194 $slug = wc_attribute_taxonomy_slug( $name );
195 $all_labels = wc_get_attribute_taxonomy_labels();
196 $label = isset( $all_labels[ $slug ] ) ? $all_labels[ $slug ] : $slug;
197 } elseif ( $product ) {
198 if ( $product->is_type( ProductType::VARIATION ) ) {
199 $product = wc_get_product( $product->get_parent_id() );
200 }
201 $attributes = array();
202
203 if ( false !== $product ) {
204 $attributes = $product->get_attributes();
205 }
206
207 // Attempt to get label from product, as entered by the user.
208 if ( $attributes && isset( $attributes[ sanitize_title( $name ) ] ) ) {
209 $label = $attributes[ sanitize_title( $name ) ]->get_name();
210 } else {
211 $label = $name;
212 }
213 } else {
214 $label = $name;
215 }//end if
216
217 return apply_filters( 'woocommerce_attribute_label', $label, $name, $product );
218 }
219
220 /**
221 * Get a product attributes orderby setting.
222 *
223 * @param string $name Attribute name.
224 * @return string
225 */
226 function wc_attribute_orderby( $name ) {
227 $name = wc_attribute_taxonomy_slug( $name );
228 $id = wc_attribute_taxonomy_id_by_name( $name );
229 $taxonomies = wc_get_attribute_taxonomies();
230
231 return apply_filters( 'woocommerce_attribute_orderby', isset( $taxonomies[ 'id:' . $id ] ) ? $taxonomies[ 'id:' . $id ]->attribute_orderby : 'menu_order', $name );
232 }
233
234 /**
235 * Get an array of product attribute taxonomies.
236 *
237 * @return array
238 */
239 function wc_get_attribute_taxonomy_names() {
240 $taxonomy_names = array();
241 $attribute_taxonomies = wc_get_attribute_taxonomies();
242 if ( ! empty( $attribute_taxonomies ) ) {
243 foreach ( $attribute_taxonomies as $tax ) {
244 $taxonomy_names[] = wc_attribute_taxonomy_name( $tax->attribute_name );
245 }
246 }
247 return $taxonomy_names;
248 }
249
250 /**
251 * Get attribute types.
252 *
253 * @since 2.4.0
254 * @return array
255 */
256 function wc_get_attribute_types() {
257 $attribute_types = array(
258 'select' => __( 'Text', 'woocommerce' ),
259 );
260
261 $allow_visual_attribute_type =
262 wp_is_block_theme() &&
263 FeaturesUtil::feature_is_enabled( 'wc-visual-attribute' );
264
265 // If the store already has some visual attributes, let's allow them even
266 // if the current theme is not a block theme.
267 if ( ! $allow_visual_attribute_type ) {
268 foreach ( wc_get_attribute_taxonomies() as $attribute_taxonomy ) {
269 if ( isset( $attribute_taxonomy->attribute_type ) && 'wc-visual' === $attribute_taxonomy->attribute_type ) {
270 $allow_visual_attribute_type = true;
271 break;
272 }
273 }
274 }
275
276 if ( $allow_visual_attribute_type ) {
277 $attribute_types['wc-visual'] = __( 'Color / image', 'woocommerce' );
278 }
279
280 return (array) apply_filters(
281 'product_attributes_type_selector',
282 $attribute_types
283 );
284 }
285
286 /**
287 * Check if there are attribute types different than the default `select` type.
288 * Note: `wc-visual` is considered a custom attribute type.
289 *
290 * @since 3.3.2
291 * @return bool True if there are custom types, otherwise false.
292 */
293 function wc_has_custom_attribute_types() {
294 $types = wc_get_attribute_types();
295
296 return 1 < count( $types ) || ! array_key_exists( 'select', $types );
297 }
298
299 /**
300 * Get attribute type label.
301 *
302 * @since 3.0.0
303 * @param string $type Attribute type slug.
304 * @return string
305 */
306 function wc_get_attribute_type_label( $type ) {
307 $types = wc_get_attribute_types();
308
309 return isset( $types[ $type ] ) ? $types[ $type ] : __( 'Text', 'woocommerce' );
310 }
311
312 /**
313 * Check if attribute name is reserved.
314 * https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms.
315 *
316 * @since 2.4.0
317 * @param string $attribute_name Attribute name.
318 * @return bool
319 */
320 function wc_check_if_attribute_name_is_reserved( $attribute_name ) {
321 // Forbidden attribute names.
322 $reserved_terms = array(
323 'attachment',
324 'attachment_id',
325 'author',
326 'author_name',
327 'calendar',
328 'cat',
329 'category',
330 'category__and',
331 'category__in',
332 'category__not_in',
333 'category_name',
334 'comments_per_page',
335 'comments_popup',
336 'cpage',
337 'day',
338 'debug',
339 'error',
340 'exact',
341 'feed',
342 'hour',
343 'link_category',
344 'm',
345 'minute',
346 'monthnum',
347 'more',
348 'name',
349 'nav_menu',
350 'nopaging',
351 'offset',
352 'order',
353 'orderby',
354 'p',
355 'page',
356 'page_id',
357 'paged',
358 'pagename',
359 'pb',
360 'perm',
361 'post',
362 'post__in',
363 'post__not_in',
364 'post_format',
365 'post_mime_type',
366 'post_status',
367 'post_tag',
368 'post_type',
369 'posts',
370 'posts_per_archive_page',
371 'posts_per_page',
372 'preview',
373 'robots',
374 's',
375 'search',
376 'second',
377 'sentence',
378 'showposts',
379 'static',
380 'subpost',
381 'subpost_id',
382 'tag',
383 'tag__and',
384 'tag__in',
385 'tag__not_in',
386 'tag_id',
387 'tag_slug__and',
388 'tag_slug__in',
389 'taxonomy',
390 'tb',
391 'term',
392 'type',
393 'w',
394 'withcomments',
395 'withoutcomments',
396 'year',
397 );
398
399 return in_array( $attribute_name, $reserved_terms, true );
400 }
401
402 /**
403 * Callback for array filter to get visible only.
404 *
405 * @since 3.0.0
406 * @param WC_Product_Attribute $attribute Attribute data.
407 * @return bool
408 */
409 function wc_attributes_array_filter_visible( $attribute ) {
410 return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_visible() && ( ! $attribute->is_taxonomy() || taxonomy_exists( $attribute->get_name() ) );
411 }
412
413 /**
414 * Callback for array filter to get variation attributes only.
415 *
416 * @since 3.0.0
417 * @param WC_Product_Attribute $attribute Attribute data.
418 * @return bool
419 */
420 function wc_attributes_array_filter_variation( $attribute ) {
421 return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_variation();
422 }
423
424 /**
425 * Check if an attribute is included in the attributes area of a variation name.
426 *
427 * @since 3.0.2
428 * @param string $attribute Attribute value to check for.
429 * @param string $name Product name to check in.
430 * @return bool
431 */
432 function wc_is_attribute_in_product_name( $attribute, $name ) {
433 $is_in_name = stristr( $name, ' ' . $attribute . ',' ) || 0 === stripos( strrev( $name ), strrev( ' ' . $attribute ) );
434 return apply_filters( 'woocommerce_is_attribute_in_product_name', $is_in_name, $attribute, $name );
435 }
436
437 /**
438 * Callback for array filter to get default attributes. Will allow for '0' string values, but regard all other
439 * class PHP FALSE equivalents normally.
440 *
441 * @since 3.1.0
442 * @param mixed $attribute Attribute being considered for exclusion from parent array.
443 * @return bool
444 */
445 function wc_array_filter_default_attributes( $attribute ) {
446 return is_scalar( $attribute ) && ( ! empty( $attribute ) || '0' === $attribute );
447 }
448
449 /**
450 * Get attribute data by ID.
451 *
452 * @since 3.2.0
453 * @param int $id Attribute ID.
454 * @return stdClass|null
455 */
456 function wc_get_attribute( $id ) {
457 $attributes = wc_get_attribute_taxonomies();
458
459 if ( ! isset( $attributes[ 'id:' . $id ] ) ) {
460 return null;
461 }
462
463 $data = $attributes[ 'id:' . $id ];
464 $attribute = new stdClass();
465 $attribute->id = (int) $data->attribute_id;
466 $attribute->name = $data->attribute_label;
467 $attribute->slug = wc_attribute_taxonomy_name( $data->attribute_name );
468 $attribute->type = $data->attribute_type;
469 $attribute->order_by = $data->attribute_orderby;
470 $attribute->has_archives = (bool) $data->attribute_public;
471 return $attribute;
472 }
473
474 /**
475 * Create attribute.
476 *
477 * @since 3.2.0
478 * @param array $args Attribute arguments {
479 * Array of attribute parameters.
480 *
481 * @type int $id Unique identifier, used to update an attribute.
482 * @type string $name Attribute name. Always required.
483 * @type string $slug Attribute alphanumeric identifier.
484 * @type string $type Type of attribute.
485 * Core by default accepts: 'select' and
486 * 'wc-visual' (which is experimental).
487 * Default to 'select'.
488 * @type string $order_by Sort order.
489 * Accepts: 'menu_order', 'name', 'name_num' and 'id'.
490 * Default to 'menu_order'.
491 * @type bool $has_archives Enable or disable attribute archives. False by default.
492 * }
493 * @return int|WP_Error
494 */
495 function wc_create_attribute( $args ) {
496 global $wpdb;
497
498 $args = wp_unslash( $args );
499 $id = ! empty( $args['id'] ) ? intval( $args['id'] ) : 0;
500 $format = array( '%s', '%s', '%s', '%s', '%d' );
501
502 // Name is required.
503 if ( empty( $args['name'] ) ) {
504 return new WP_Error( 'missing_attribute_name', __( 'Please, provide an attribute name.', 'woocommerce' ), array( 'status' => 400 ) );
505 }
506
507 // Set the attribute slug.
508 if ( empty( $args['slug'] ) ) {
509 $slug = wc_sanitize_taxonomy_name( $args['name'] );
510 } else {
511 $slug = preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( $args['slug'] ) );
512 }
513
514 // Validate slug.
515 if ( strlen( $slug ) > 28 ) {
516 /* translators: %s: attribute slug */
517 return new WP_Error( 'invalid_product_attribute_slug_too_long', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
518 } elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
519 /* translators: %s: attribute slug */
520 return new WP_Error( 'invalid_product_attribute_slug_reserved_name', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
521 } elseif ( ( 0 === $id && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) || ( isset( $args['old_slug'] ) && $args['old_slug'] !== $slug && taxonomy_exists( wc_attribute_taxonomy_name( $slug ) ) ) ) {
522 /* translators: %s: attribute slug */
523 return new WP_Error( 'invalid_product_attribute_slug_already_exists', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), $slug ), array( 'status' => 400 ) );
524 }
525
526 // Validate type.
527 if ( empty( $args['type'] ) || ! array_key_exists( $args['type'], wc_get_attribute_types() ) ) {
528 $args['type'] = 'select';
529 }
530
531 // Validate order by.
532 if ( empty( $args['order_by'] ) || ! in_array( $args['order_by'], array( 'menu_order', 'name', 'name_num', 'id' ), true ) ) {
533 $args['order_by'] = 'menu_order';
534 }
535
536 $data = array(
537 'attribute_label' => $args['name'],
538 'attribute_name' => $slug,
539 'attribute_type' => $args['type'],
540 'attribute_orderby' => $args['order_by'],
541 'attribute_public' => isset( $args['has_archives'] ) ? (int) $args['has_archives'] : 0,
542 );
543
544 // Create or update.
545 if ( 0 === $id ) {
546 $results = $wpdb->insert(
547 $wpdb->prefix . 'woocommerce_attribute_taxonomies',
548 $data,
549 $format
550 );
551
552 if ( is_wp_error( $results ) ) {
553 return new WP_Error( 'cannot_create_attribute', $results->get_error_message(), array( 'status' => 400 ) );
554 }
555
556 $id = $wpdb->insert_id;
557
558 /**
559 * Attribute added.
560 *
561 * @param int $id Added attribute ID.
562 * @param array $data Attribute data.
563 */
564 do_action( 'woocommerce_attribute_added', $id, $data );
565 } else {
566 $results = $wpdb->update(
567 $wpdb->prefix . 'woocommerce_attribute_taxonomies',
568 $data,
569 array( 'attribute_id' => $id ),
570 $format,
571 array( '%d' )
572 );
573
574 if ( false === $results ) {
575 return new WP_Error( 'cannot_update_attribute', __( 'Could not update the attribute.', 'woocommerce' ), array( 'status' => 400 ) );
576 }
577
578 // Set old slug to check for database changes.
579 $old_slug = ! empty( $args['old_slug'] ) ? wc_sanitize_taxonomy_name( $args['old_slug'] ) : $slug;
580
581 /**
582 * Attribute updated.
583 *
584 * @param int $id Added attribute ID.
585 * @param array $data Attribute data.
586 * @param string $old_slug Attribute old name.
587 */
588 do_action( 'woocommerce_attribute_updated', $id, $data, $old_slug );
589
590 if ( $old_slug !== $slug ) {
591 // Update taxonomies in the wp term taxonomy table.
592 $wpdb->update(
593 $wpdb->term_taxonomy,
594 array( 'taxonomy' => wc_attribute_taxonomy_name( $data['attribute_name'] ) ),
595 array( 'taxonomy' => 'pa_' . $old_slug )
596 );
597
598 // Update taxonomy ordering term meta.
599 $wpdb->update(
600 $wpdb->termmeta,
601 array( 'meta_key' => 'order' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
602 array( 'meta_key' => 'order_pa_' . sanitize_title( $old_slug ) ) // WPCS: slow query ok.
603 );
604
605 // Update product attributes which use this taxonomy.
606 $old_taxonomy_name = 'pa_' . $old_slug;
607 $new_taxonomy_name = 'pa_' . $data['attribute_name'];
608 $old_attribute_key = sanitize_title( $old_taxonomy_name ); // @see WC_Product::set_attributes().
609 $new_attribute_key = sanitize_title( $new_taxonomy_name ); // @see WC_Product::set_attributes().
610 $metadatas = $wpdb->get_results(
611 $wpdb->prepare(
612 "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' AND meta_value LIKE %s",
613 '%' . $wpdb->esc_like( $old_taxonomy_name ) . '%'
614 ),
615 ARRAY_A
616 );
617 foreach ( $metadatas as $metadata ) {
618 $product_id = $metadata['post_id'];
619 $unserialized_data = maybe_unserialize( $metadata['meta_value'] );
620
621 if ( ! $unserialized_data || ! is_array( $unserialized_data ) || ! isset( $unserialized_data[ $old_attribute_key ] ) ) {
622 continue;
623 }
624
625 $unserialized_data[ $new_attribute_key ] = $unserialized_data[ $old_attribute_key ];
626 unset( $unserialized_data[ $old_attribute_key ] );
627 $unserialized_data[ $new_attribute_key ]['name'] = $new_taxonomy_name;
628 update_post_meta( $product_id, '_product_attributes', wp_slash( $unserialized_data ) );
629 }
630
631 // Update variations which use this taxonomy.
632 $wpdb->update(
633 $wpdb->postmeta,
634 array( 'meta_key' => 'attribute_pa_' . sanitize_title( $data['attribute_name'] ) ), // WPCS: slow query ok.
635 array( 'meta_key' => 'attribute_pa_' . sanitize_title( $old_slug ) ) // WPCS: slow query ok.
636 );
637
638 // Update global vars to reflect migration. This ensures any functions dealing with terms later in this request
639 // use the correct info.
640 global $wc_product_attributes;
641 if ( isset( $wc_product_attributes[ $old_taxonomy_name ] ) && ! isset( $wc_product_attributes[ $new_taxonomy_name ] ) ) {
642 $wc_product_attributes[ $new_taxonomy_name ] = $wc_product_attributes[ $old_taxonomy_name ];
643 }
644 global $wp_taxonomies;
645 if ( isset( $wp_taxonomies[ $old_taxonomy_name ] ) && ! isset( $wp_taxonomies[ $new_taxonomy_name ] ) ) {
646 $wp_taxonomies[ $new_taxonomy_name ] = $wp_taxonomies[ $old_taxonomy_name ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
647 }
648 }//end if
649 }//end if
650
651 // Clear cache and flush rewrite rules.
652 wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
653 delete_transient( 'wc_attribute_taxonomies' );
654 WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
655
656 return $id;
657 }
658
659 /**
660 * Update an attribute.
661 *
662 * For available args see wc_create_attribute().
663 *
664 * @since 3.2.0
665 * @param int $id Attribute ID.
666 * @param array $args Attribute arguments.
667 * @return int|WP_Error
668 */
669 function wc_update_attribute( $id, $args ) {
670 global $wpdb;
671
672 $attribute = wc_get_attribute( $id );
673
674 $args['id'] = $attribute ? $attribute->id : 0;
675
676 // When updating an existing attribute, populate any undefined args with the existing value.
677 // This prevents those values from being reset to their respective defaults.
678 if ( $args['id'] ) {
679 $args['has_archives'] = $args['has_archives'] ?? $attribute->has_archives;
680 $args['name'] = $args['name'] ?? $attribute->name;
681 $args['order_by'] = $args['order_by'] ?? $attribute->order_by;
682 $args['slug'] = $args['slug'] ?? $attribute->slug;
683 $args['type'] = $args['type'] ?? $attribute->type;
684 }
685
686 if ( $args['id'] && empty( $args['name'] ) ) {
687 $args['name'] = $attribute->name;
688 }
689
690 $args['old_slug'] = $wpdb->get_var(
691 $wpdb->prepare(
692 "
693 SELECT attribute_name
694 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
695 WHERE attribute_id = %d
696 ",
697 $args['id']
698 )
699 );
700
701 return wc_create_attribute( $args );
702 }
703
704 /**
705 * Delete attribute by ID.
706 *
707 * @since 3.2.0
708 * @param int $id Attribute ID.
709 * @return bool
710 */
711 function wc_delete_attribute( $id ) {
712 global $wpdb;
713
714 $name = $wpdb->get_var(
715 $wpdb->prepare(
716 "
717 SELECT attribute_name
718 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
719 WHERE attribute_id = %d
720 ",
721 $id
722 )
723 );
724
725 $taxonomy = wc_attribute_taxonomy_name( $name );
726
727 /**
728 * Before deleting an attribute.
729 *
730 * @param int $id Attribute ID.
731 * @param string $name Attribute name.
732 * @param string $taxonomy Attribute taxonomy name.
733 */
734 do_action( 'woocommerce_before_attribute_delete', $id, $name, $taxonomy );
735
736 if ( $name && $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $id ) ) ) {
737 if ( taxonomy_exists( $taxonomy ) ) {
738 $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' );
739 foreach ( $terms as $term ) {
740 wp_delete_term( $term->term_id, $taxonomy );
741 }
742 }
743
744 /**
745 * After deleting an attribute.
746 *
747 * @param int $id Attribute ID.
748 * @param string $name Attribute name.
749 * @param string $taxonomy Attribute taxonomy name.
750 */
751 do_action( 'woocommerce_attribute_deleted', $id, $name, $taxonomy );
752 wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
753 delete_transient( 'wc_attribute_taxonomies' );
754 WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
755
756 return true;
757 }//end if
758
759 return false;
760 }
761
762 /**
763 * Get an unprefixed product attribute name.
764 *
765 * @since 3.6.0
766 *
767 * @param string $attribute_name Attribute name.
768 * @return string
769 */
770 function wc_attribute_taxonomy_slug( $attribute_name ) {
771 $prefix = WC_Cache_Helper::get_cache_prefix( 'woocommerce-attributes' );
772 $cache_key = $prefix . 'slug-' . $attribute_name;
773 $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' );
774
775 if ( false !== $cache_value ) {
776 return $cache_value;
777 }
778
779 $attribute_name = wc_sanitize_taxonomy_name( $attribute_name );
780 $attribute_slug = 0 === strpos( $attribute_name, 'pa_' ) ? substr( $attribute_name, 3 ) : $attribute_name;
781 wp_cache_set( $cache_key, $attribute_slug, 'woocommerce-attributes' );
782
783 return $attribute_slug;
784 }
785