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