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