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 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0-rc.1 3.0.2 6.1.0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7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / wc-attribute-functions.php
woocommerce / includes Last commit date
abstracts 1 month ago admin 1 month ago blocks 1 month ago cli 2 months ago customizer 6 months ago data-stores 1 month ago emails 1 month ago export 1 month ago gateways 2 months ago import 1 month ago integrations 1 month ago interfaces 6 months ago legacy 1 month ago libraries 2 months ago log-handlers 1 year ago payment-tokens 6 years ago product-usage 1 year ago queue 6 months ago react-admin 1 month ago rest-api 1 month ago shipping 5 months ago shortcodes 1 month ago theme-support 2 years ago tracks 2 months ago traits 5 years ago walkers 5 years ago wccom-site 2 months ago widgets 1 month ago class-wc-ajax.php 1 month ago class-wc-auth.php 2 years ago class-wc-autoloader.php 10 months ago class-wc-background-emailer.php 3 months ago class-wc-background-updater.php 6 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 month ago class-wc-breadcrumb.php 6 months ago class-wc-cache-helper.php 2 months ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 5 months ago class-wc-cart-totals.php 1 year ago class-wc-cart.php 1 month ago class-wc-checkout.php 1 month ago class-wc-cli.php 1 year ago class-wc-comments.php 6 months ago class-wc-countries.php 3 months ago class-wc-coupon.php 3 months ago class-wc-customer-download-log.php 6 years ago class-wc-customer-download.php 2 years ago class-wc-customer.php 1 month 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 5 months ago class-wc-discounts.php 1 year ago class-wc-download-handler.php 1 month ago class-wc-emails.php 1 month ago class-wc-embed.php 1 year ago class-wc-form-handler.php 1 month ago class-wc-frontend-scripts.php 4 weeks ago class-wc-geo-ip.php 10 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 1 month ago class-wc-https.php 3 years ago class-wc-install.php 1 month ago class-wc-integrations.php 6 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 6 months ago class-wc-meta-data.php 2 months ago class-wc-order-factory.php 3 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 1 month ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 months ago class-wc-order-item-shipping.php 7 months ago class-wc-order-item-tax.php 2 months ago class-wc-order-item.php 1 month ago class-wc-order-query.php 2 months ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 month ago class-wc-payment-gateways.php 1 month ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 month ago class-wc-post-types.php 1 month ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 5 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 6 months ago class-wc-product-download.php 1 month ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 5 months ago class-wc-product-grouped.php 1 month ago class-wc-product-query.php 6 months ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 2 months ago class-wc-product-variation.php 2 months ago class-wc-query.php 1 month ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 month 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 5 months ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 9 months ago class-wc-shipping.php 1 month ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 month ago class-wc-tax.php 1 month ago class-wc-template-loader.php 9 months ago class-wc-tracker.php 1 month ago class-wc-validation.php 1 month ago class-wc-webhook.php 2 months ago class-woocommerce.php 4 weeks ago wc-account-functions.php 2 months ago wc-attribute-functions.php 1 month ago wc-brands-functions.php 1 year ago wc-cart-functions.php 2 months ago wc-conditional-functions.php 1 year ago wc-core-functions.php 3 months ago wc-coupon-functions.php 7 months ago wc-deprecated-functions.php 2 months ago wc-formatting-functions.php 1 month ago wc-interactivity-api-functions.php 3 months ago wc-notice-functions.php 7 months ago wc-order-functions.php 3 months ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 6 months ago wc-page-functions.php 1 month ago wc-product-functions.php 1 month ago wc-rest-functions.php 9 months ago wc-stock-functions.php 1 month ago wc-template-functions.php 2 months ago wc-template-hooks.php 1 year ago wc-term-functions.php 1 month ago wc-update-functions.php 1 month ago wc-user-functions.php 1 month ago wc-webhook-functions.php 2 months ago wc-widget-functions.php 6 years ago
wc-attribute-functions.php
812 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 maximum byte length allowed for a product attribute slug.
149 *
150 * WordPress's register_taxonomy() rejects taxonomy names longer than 32 bytes
151 * (see wp-includes/taxonomy.php), and WooCommerce prefixes attribute slugs with
152 * 'pa_'. That leaves the slug itself a budget of 32 bytes minus the prefix length.
153 * The limit is measured in bytes, not characters: multibyte characters (Cyrillic,
154 * Chinese, etc.) consume 2-4 bytes each.
155 *
156 * @since 11.0.0
157 * @return int Maximum attribute slug length, in bytes.
158 */
159 function wc_get_attribute_slug_max_byte_length() {
160 return 32 - strlen( 'pa_' );
161 }
162
163 /**
164 * Get the attribute name used when storing values in post meta.
165 *
166 * @since 2.6.0
167 * @param string $attribute_name Attribute name.
168 * @return string
169 */
170 function wc_variation_attribute_name( $attribute_name ) {
171 return 'attribute_' . sanitize_title( $attribute_name );
172 }
173
174 /**
175 * Get a product attribute name by ID.
176 *
177 * @since 2.4.0
178 * @param int $attribute_id Attribute ID.
179 * @return string Return an empty string if attribute doesn't exist.
180 */
181 function wc_attribute_taxonomy_name_by_id( $attribute_id ) {
182 $taxonomy_ids = wc_get_attribute_taxonomy_ids();
183 $attribute_name = (string) array_search( $attribute_id, $taxonomy_ids, true );
184 return wc_attribute_taxonomy_name( $attribute_name );
185 }
186
187 /**
188 * Get a product attribute ID by name.
189 *
190 * @since 2.6.0
191 * @param string $name Attribute name.
192 * @return int
193 */
194 function wc_attribute_taxonomy_id_by_name( $name ) {
195 $name = wc_attribute_taxonomy_slug( $name );
196 $taxonomy_ids = wc_get_attribute_taxonomy_ids();
197
198 return isset( $taxonomy_ids[ $name ] ) ? $taxonomy_ids[ $name ] : 0;
199 }
200
201 /**
202 * Get a product attributes label.
203 *
204 * @param string $name Attribute name.
205 * @param WC_Product $product Product data.
206 * @return string
207 */
208 function wc_attribute_label( $name, $product = '' ) {
209 if ( taxonomy_is_product_attribute( $name ) ) {
210 $slug = wc_attribute_taxonomy_slug( $name );
211 $all_labels = wc_get_attribute_taxonomy_labels();
212 $label = isset( $all_labels[ $slug ] ) ? $all_labels[ $slug ] : $slug;
213 } elseif ( $product ) {
214 if ( $product->is_type( ProductType::VARIATION ) ) {
215 $product = wc_get_product( $product->get_parent_id() );
216 }
217 $attributes = array();
218
219 if ( false !== $product ) {
220 $attributes = $product->get_attributes();
221 }
222
223 // Attempt to get label from product, as entered by the user.
224 if ( $attributes && isset( $attributes[ sanitize_title( $name ) ] ) ) {
225 $label = $attributes[ sanitize_title( $name ) ]->get_name();
226 } else {
227 $label = $name;
228 }
229 } else {
230 $label = $name;
231 }//end if
232
233 return apply_filters( 'woocommerce_attribute_label', $label, $name, $product );
234 }
235
236 /**
237 * Get a product attributes orderby setting.
238 *
239 * @param string $name Attribute name.
240 * @return string
241 */
242 function wc_attribute_orderby( $name ) {
243 $name = wc_attribute_taxonomy_slug( $name );
244 $id = wc_attribute_taxonomy_id_by_name( $name );
245 $taxonomies = wc_get_attribute_taxonomies();
246
247 return apply_filters( 'woocommerce_attribute_orderby', isset( $taxonomies[ 'id:' . $id ] ) ? $taxonomies[ 'id:' . $id ]->attribute_orderby : 'menu_order', $name );
248 }
249
250 /**
251 * Get an array of product attribute taxonomies.
252 *
253 * @return array
254 */
255 function wc_get_attribute_taxonomy_names() {
256 $taxonomy_names = array();
257 $attribute_taxonomies = wc_get_attribute_taxonomies();
258 if ( ! empty( $attribute_taxonomies ) ) {
259 foreach ( $attribute_taxonomies as $tax ) {
260 $taxonomy_names[] = wc_attribute_taxonomy_name( $tax->attribute_name );
261 }
262 }
263 return $taxonomy_names;
264 }
265
266 /**
267 * Get attribute types.
268 *
269 * @since 2.4.0
270 * @return array
271 */
272 function wc_get_attribute_types() {
273 $attribute_types = array(
274 'select' => __( 'Text', 'woocommerce' ),
275 );
276
277 $allow_visual_attribute_type =
278 wp_is_block_theme() &&
279 FeaturesUtil::feature_is_enabled( 'wc-visual-attribute' );
280
281 // If the store already has some visual attributes, let's allow them even
282 // if the current theme is not a block theme.
283 if ( ! $allow_visual_attribute_type ) {
284 foreach ( wc_get_attribute_taxonomies() as $attribute_taxonomy ) {
285 if ( isset( $attribute_taxonomy->attribute_type ) && 'wc-visual' === $attribute_taxonomy->attribute_type ) {
286 $allow_visual_attribute_type = true;
287 break;
288 }
289 }
290 }
291
292 if ( $allow_visual_attribute_type ) {
293 $attribute_types['wc-visual'] = __( 'Color / image', 'woocommerce' );
294 }
295
296 return (array) apply_filters(
297 'product_attributes_type_selector',
298 $attribute_types
299 );
300 }
301
302 /**
303 * Check if there are attribute types different than the default `select` type.
304 * Note: `wc-visual` is considered a custom attribute type.
305 *
306 * @since 3.3.2
307 * @return bool True if there are custom types, otherwise false.
308 */
309 function wc_has_custom_attribute_types() {
310 $types = wc_get_attribute_types();
311
312 return 1 < count( $types ) || ! array_key_exists( 'select', $types );
313 }
314
315 /**
316 * Get attribute type label.
317 *
318 * @since 3.0.0
319 * @param string $type Attribute type slug.
320 * @return string
321 */
322 function wc_get_attribute_type_label( $type ) {
323 $types = wc_get_attribute_types();
324
325 return isset( $types[ $type ] ) ? $types[ $type ] : __( 'Text', 'woocommerce' );
326 }
327
328 /**
329 * Check if attribute name is reserved.
330 * https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms.
331 *
332 * @since 2.4.0
333 * @param string $attribute_name Attribute name.
334 * @return bool
335 */
336 function wc_check_if_attribute_name_is_reserved( $attribute_name ) {
337 // Forbidden attribute names.
338 $reserved_terms = array(
339 'attachment',
340 'attachment_id',
341 'author',
342 'author_name',
343 'calendar',
344 'cat',
345 'category',
346 'category__and',
347 'category__in',
348 'category__not_in',
349 'category_name',
350 'comments_per_page',
351 'comments_popup',
352 'cpage',
353 'day',
354 'debug',
355 'error',
356 'exact',
357 'feed',
358 'hour',
359 'link_category',
360 'm',
361 'minute',
362 'monthnum',
363 'more',
364 'name',
365 'nav_menu',
366 'nopaging',
367 'offset',
368 'order',
369 'orderby',
370 'p',
371 'page',
372 'page_id',
373 'paged',
374 'pagename',
375 'pb',
376 'perm',
377 'post',
378 'post__in',
379 'post__not_in',
380 'post_format',
381 'post_mime_type',
382 'post_status',
383 'post_tag',
384 'post_type',
385 'posts',
386 'posts_per_archive_page',
387 'posts_per_page',
388 'preview',
389 'robots',
390 's',
391 'search',
392 'second',
393 'sentence',
394 'showposts',
395 'static',
396 'subpost',
397 'subpost_id',
398 'tag',
399 'tag__and',
400 'tag__in',
401 'tag__not_in',
402 'tag_id',
403 'tag_slug__and',
404 'tag_slug__in',
405 'taxonomy',
406 'tb',
407 'term',
408 'type',
409 'w',
410 'withcomments',
411 'withoutcomments',
412 'year',
413 );
414
415 return in_array( $attribute_name, $reserved_terms, true );
416 }
417
418 /**
419 * Callback for array filter to get visible only.
420 *
421 * @since 3.0.0
422 * @param WC_Product_Attribute $attribute Attribute data.
423 * @return bool
424 */
425 function wc_attributes_array_filter_visible( $attribute ) {
426 return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_visible() && ( ! $attribute->is_taxonomy() || taxonomy_exists( $attribute->get_name() ) );
427 }
428
429 /**
430 * Callback for array filter to get variation attributes only.
431 *
432 * @since 3.0.0
433 * @param WC_Product_Attribute $attribute Attribute data.
434 * @return bool
435 */
436 function wc_attributes_array_filter_variation( $attribute ) {
437 return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_variation();
438 }
439
440 /**
441 * Check if an attribute is included in the attributes area of a variation name.
442 *
443 * @since 3.0.2
444 * @param string $attribute Attribute value to check for.
445 * @param string $name Product name to check in.
446 * @return bool
447 */
448 function wc_is_attribute_in_product_name( $attribute, $name ) {
449 $is_in_name = stristr( $name, ' ' . $attribute . ',' ) || 0 === stripos( strrev( $name ), strrev( ' ' . $attribute ) );
450 return apply_filters( 'woocommerce_is_attribute_in_product_name', $is_in_name, $attribute, $name );
451 }
452
453 /**
454 * Callback for array filter to get default attributes. Will allow for '0' string values, but regard all other
455 * class PHP FALSE equivalents normally.
456 *
457 * @since 3.1.0
458 * @param mixed $attribute Attribute being considered for exclusion from parent array.
459 * @return bool
460 */
461 function wc_array_filter_default_attributes( $attribute ) {
462 return is_scalar( $attribute ) && ( ! empty( $attribute ) || '0' === $attribute );
463 }
464
465 /**
466 * Get attribute data by ID.
467 *
468 * @since 3.2.0
469 * @param int $id Attribute ID.
470 * @return stdClass|null
471 */
472 function wc_get_attribute( $id ) {
473 $attributes = wc_get_attribute_taxonomies();
474
475 if ( ! isset( $attributes[ 'id:' . $id ] ) ) {
476 return null;
477 }
478
479 $data = $attributes[ 'id:' . $id ];
480 $attribute = new stdClass();
481 $attribute->id = (int) $data->attribute_id;
482 $attribute->name = $data->attribute_label;
483 $attribute->slug = wc_attribute_taxonomy_name( $data->attribute_name );
484 $attribute->type = $data->attribute_type;
485 $attribute->order_by = $data->attribute_orderby;
486 $attribute->has_archives = (bool) $data->attribute_public;
487 return $attribute;
488 }
489
490 /**
491 * Create attribute.
492 *
493 * @since 3.2.0
494 * @param array $args Attribute arguments {
495 * Array of attribute parameters.
496 *
497 * @type int $id Unique identifier, used to update an attribute.
498 * @type string $name Attribute name. Always required.
499 * @type string $slug Attribute alphanumeric identifier.
500 * @type string $type Type of attribute.
501 * Core by default accepts: 'select' and
502 * 'wc-visual' (which is experimental).
503 * Default to 'select'.
504 * @type string $order_by Sort order.
505 * Accepts: 'menu_order', 'name', 'name_num' and 'id'.
506 * Default to 'menu_order'.
507 * @type bool $has_archives Enable or disable attribute archives. False by default.
508 * }
509 * @return int|WP_Error
510 */
511 function wc_create_attribute( $args ) {
512 global $wpdb;
513
514 $args = wp_unslash( $args );
515 $id = ! empty( $args['id'] ) ? intval( $args['id'] ) : 0;
516 $format = array( '%s', '%s', '%s', '%s', '%d' );
517
518 // Name is required.
519 if ( empty( $args['name'] ) ) {
520 return new WP_Error( 'missing_attribute_name', __( 'Please, provide an attribute name.', 'woocommerce' ), array( 'status' => 400 ) );
521 }
522
523 // Set the attribute slug.
524 if ( empty( $args['slug'] ) ) {
525 $slug = wc_sanitize_taxonomy_name( $args['name'] );
526 } else {
527 $slug = (string) preg_replace( '/^pa\_/', '', wc_sanitize_taxonomy_name( $args['slug'] ) );
528 }
529
530 // Validate slug length against the byte budget left by WordPress's 32-byte taxonomy
531 // name limit once the 'pa_' prefix is accounted for (see wc_get_attribute_slug_max_byte_length()).
532 $slug_byte_length = strlen( $slug );
533 $max_byte_length = wc_get_attribute_slug_max_byte_length();
534 if ( $slug_byte_length > $max_byte_length ) {
535 return new WP_Error(
536 'invalid_product_attribute_slug_too_long',
537 /* translators: %s: attribute slug */
538 sprintf( __( 'Slug "%s" is too long. Please use a shorter slug.', 'woocommerce' ), $slug ),
539 array(
540 'status' => 400,
541 'slug_byte_length' => $slug_byte_length,
542 'slug_byte_length_limit' => $max_byte_length,
543 )
544 );
545 } elseif ( wc_check_if_attribute_name_is_reserved( $slug ) ) {
546 /* translators: %s: attribute slug */
547 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 ) );
548 } 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 ) ) ) ) {
549 /* translators: %s: attribute slug */
550 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 ) );
551 }
552
553 // Validate type.
554 if ( empty( $args['type'] ) || ! array_key_exists( $args['type'], wc_get_attribute_types() ) ) {
555 $args['type'] = 'select';
556 }
557
558 // Validate order by.
559 if ( empty( $args['order_by'] ) || ! in_array( $args['order_by'], array( 'menu_order', 'name', 'name_num', 'id' ), true ) ) {
560 $args['order_by'] = 'menu_order';
561 }
562
563 $data = array(
564 'attribute_label' => $args['name'],
565 'attribute_name' => $slug,
566 'attribute_type' => $args['type'],
567 'attribute_orderby' => $args['order_by'],
568 'attribute_public' => isset( $args['has_archives'] ) ? (int) $args['has_archives'] : 0,
569 );
570
571 // Create or update.
572 if ( 0 === $id ) {
573 $results = $wpdb->insert(
574 $wpdb->prefix . 'woocommerce_attribute_taxonomies',
575 $data,
576 $format
577 );
578
579 if ( is_wp_error( $results ) ) {
580 return new WP_Error( 'cannot_create_attribute', $results->get_error_message(), array( 'status' => 400 ) );
581 }
582
583 $id = $wpdb->insert_id;
584
585 /**
586 * Attribute added.
587 *
588 * @param int $id Added attribute ID.
589 * @param array $data Attribute data.
590 */
591 do_action( 'woocommerce_attribute_added', $id, $data );
592 } else {
593 $results = $wpdb->update(
594 $wpdb->prefix . 'woocommerce_attribute_taxonomies',
595 $data,
596 array( 'attribute_id' => $id ),
597 $format,
598 array( '%d' )
599 );
600
601 if ( false === $results ) {
602 return new WP_Error( 'cannot_update_attribute', __( 'Could not update the attribute.', 'woocommerce' ), array( 'status' => 400 ) );
603 }
604
605 // Set old slug to check for database changes.
606 $old_slug = ! empty( $args['old_slug'] ) ? wc_sanitize_taxonomy_name( $args['old_slug'] ) : $slug;
607
608 /**
609 * Attribute updated.
610 *
611 * @param int $id Added attribute ID.
612 * @param array $data Attribute data.
613 * @param string $old_slug Attribute old name.
614 */
615 do_action( 'woocommerce_attribute_updated', $id, $data, $old_slug );
616
617 if ( $old_slug !== $slug ) {
618 // Update taxonomies in the wp term taxonomy table.
619 $wpdb->update(
620 $wpdb->term_taxonomy,
621 array( 'taxonomy' => wc_attribute_taxonomy_name( $data['attribute_name'] ) ),
622 array( 'taxonomy' => 'pa_' . $old_slug )
623 );
624
625 // Update taxonomy ordering term meta.
626 $wpdb->update(
627 $wpdb->termmeta,
628 array( 'meta_key' => 'order' ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
629 array( 'meta_key' => 'order_pa_' . sanitize_title( $old_slug ) ) // WPCS: slow query ok.
630 );
631
632 // Update product attributes which use this taxonomy.
633 $old_taxonomy_name = 'pa_' . $old_slug;
634 $new_taxonomy_name = 'pa_' . $data['attribute_name'];
635 $old_attribute_key = sanitize_title( $old_taxonomy_name ); // @see WC_Product::set_attributes().
636 $new_attribute_key = sanitize_title( $new_taxonomy_name ); // @see WC_Product::set_attributes().
637 $metadatas = $wpdb->get_results(
638 $wpdb->prepare(
639 "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_product_attributes' AND meta_value LIKE %s",
640 '%' . $wpdb->esc_like( $old_taxonomy_name ) . '%'
641 ),
642 ARRAY_A
643 );
644 foreach ( $metadatas as $metadata ) {
645 $product_id = $metadata['post_id'];
646 $unserialized_data = maybe_unserialize( $metadata['meta_value'] );
647
648 if ( ! $unserialized_data || ! is_array( $unserialized_data ) || ! isset( $unserialized_data[ $old_attribute_key ] ) ) {
649 continue;
650 }
651
652 $unserialized_data[ $new_attribute_key ] = $unserialized_data[ $old_attribute_key ];
653 unset( $unserialized_data[ $old_attribute_key ] );
654 $unserialized_data[ $new_attribute_key ]['name'] = $new_taxonomy_name;
655 update_post_meta( $product_id, '_product_attributes', wp_slash( $unserialized_data ) );
656 }
657
658 // Update variations which use this taxonomy.
659 $wpdb->update(
660 $wpdb->postmeta,
661 array( 'meta_key' => 'attribute_pa_' . sanitize_title( $data['attribute_name'] ) ), // WPCS: slow query ok.
662 array( 'meta_key' => 'attribute_pa_' . sanitize_title( $old_slug ) ) // WPCS: slow query ok.
663 );
664
665 // Update global vars to reflect migration. This ensures any functions dealing with terms later in this request
666 // use the correct info.
667 global $wc_product_attributes;
668 if ( isset( $wc_product_attributes[ $old_taxonomy_name ] ) && ! isset( $wc_product_attributes[ $new_taxonomy_name ] ) ) {
669 $wc_product_attributes[ $new_taxonomy_name ] = $wc_product_attributes[ $old_taxonomy_name ];
670 }
671 global $wp_taxonomies;
672 if ( isset( $wp_taxonomies[ $old_taxonomy_name ] ) && ! isset( $wp_taxonomies[ $new_taxonomy_name ] ) ) {
673 $wp_taxonomies[ $new_taxonomy_name ] = $wp_taxonomies[ $old_taxonomy_name ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
674 }
675 }//end if
676 }//end if
677
678 // Clear cache and flush rewrite rules.
679 wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
680 delete_transient( 'wc_attribute_taxonomies' );
681 WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
682
683 return $id;
684 }
685
686 /**
687 * Update an attribute.
688 *
689 * For available args see wc_create_attribute().
690 *
691 * @since 3.2.0
692 * @param int $id Attribute ID.
693 * @param array $args Attribute arguments.
694 * @return int|WP_Error
695 */
696 function wc_update_attribute( $id, $args ) {
697 global $wpdb;
698
699 $attribute = wc_get_attribute( $id );
700
701 $args['id'] = $attribute ? $attribute->id : 0;
702
703 // When updating an existing attribute, populate any undefined args with the existing value.
704 // This prevents those values from being reset to their respective defaults.
705 if ( $args['id'] ) {
706 $args['has_archives'] = $args['has_archives'] ?? $attribute->has_archives;
707 $args['name'] = $args['name'] ?? $attribute->name;
708 $args['order_by'] = $args['order_by'] ?? $attribute->order_by;
709 $args['slug'] = $args['slug'] ?? $attribute->slug;
710 $args['type'] = $args['type'] ?? $attribute->type;
711 }
712
713 if ( $args['id'] && empty( $args['name'] ) ) {
714 $args['name'] = $attribute->name;
715 }
716
717 $args['old_slug'] = $wpdb->get_var(
718 $wpdb->prepare(
719 "
720 SELECT attribute_name
721 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
722 WHERE attribute_id = %d
723 ",
724 $args['id']
725 )
726 );
727
728 return wc_create_attribute( $args );
729 }
730
731 /**
732 * Delete attribute by ID.
733 *
734 * @since 3.2.0
735 * @param int $id Attribute ID.
736 * @return bool
737 */
738 function wc_delete_attribute( $id ) {
739 global $wpdb;
740
741 $name = $wpdb->get_var(
742 $wpdb->prepare(
743 "
744 SELECT attribute_name
745 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
746 WHERE attribute_id = %d
747 ",
748 $id
749 )
750 );
751
752 $taxonomy = wc_attribute_taxonomy_name( $name );
753
754 /**
755 * Before deleting an attribute.
756 *
757 * @param int $id Attribute ID.
758 * @param string $name Attribute name.
759 * @param string $taxonomy Attribute taxonomy name.
760 */
761 do_action( 'woocommerce_before_attribute_delete', $id, $name, $taxonomy );
762
763 if ( $name && $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d", $id ) ) ) {
764 if ( taxonomy_exists( $taxonomy ) ) {
765 $terms = get_terms( $taxonomy, 'orderby=name&hide_empty=0' );
766 foreach ( $terms as $term ) {
767 wp_delete_term( $term->term_id, $taxonomy );
768 }
769 }
770
771 /**
772 * After deleting an attribute.
773 *
774 * @param int $id Attribute ID.
775 * @param string $name Attribute name.
776 * @param string $taxonomy Attribute taxonomy name.
777 */
778 do_action( 'woocommerce_attribute_deleted', $id, $name, $taxonomy );
779 wp_schedule_single_event( time(), 'woocommerce_flush_rewrite_rules' );
780 delete_transient( 'wc_attribute_taxonomies' );
781 WC_Cache_Helper::invalidate_cache_group( 'woocommerce-attributes' );
782
783 return true;
784 }//end if
785
786 return false;
787 }
788
789 /**
790 * Get an unprefixed product attribute name.
791 *
792 * @since 3.6.0
793 *
794 * @param string $attribute_name Attribute name.
795 * @return string
796 */
797 function wc_attribute_taxonomy_slug( $attribute_name ) {
798 $prefix = WC_Cache_Helper::get_cache_prefix( 'woocommerce-attributes' );
799 $cache_key = $prefix . 'slug-' . $attribute_name;
800 $cache_value = wp_cache_get( $cache_key, 'woocommerce-attributes' );
801
802 if ( false !== $cache_value ) {
803 return $cache_value;
804 }
805
806 $attribute_name = wc_sanitize_taxonomy_name( $attribute_name );
807 $attribute_slug = 0 === strpos( $attribute_name, 'pa_' ) ? substr( $attribute_name, 3 ) : $attribute_name;
808 wp_cache_set( $cache_key, $attribute_slug, 'woocommerce-attributes' );
809
810 return $attribute_slug;
811 }
812