abstracts
9 years ago
admin
9 years ago
api
9 years ago
cli
9 years ago
data-stores
9 years ago
emails
9 years ago
gateways
9 years ago
interfaces
9 years ago
legacy
9 years ago
libraries
9 years ago
log-handlers
9 years ago
payment-tokens
9 years ago
shipping
9 years ago
shortcodes
9 years ago
theme-support
9 years ago
vendor
9 years ago
walkers
9 years ago
widgets
9 years ago
class-wc-ajax.php
9 years ago
class-wc-api.php
9 years ago
class-wc-auth.php
9 years ago
class-wc-autoloader.php
9 years ago
class-wc-background-emailer.php
9 years ago
class-wc-background-updater.php
9 years ago
class-wc-breadcrumb.php
9 years ago
class-wc-cache-helper.php
9 years ago
class-wc-cart.php
9 years ago
class-wc-checkout.php
9 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
9 years ago
class-wc-countries.php
9 years ago
class-wc-coupon.php
9 years ago
class-wc-customer-download.php
9 years ago
class-wc-customer.php
9 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
9 years ago
class-wc-datetime.php
9 years ago
class-wc-deprecated-action-hooks.php
9 years ago
class-wc-deprecated-filter-hooks.php
9 years ago
class-wc-download-handler.php
9 years ago
class-wc-emails.php
9 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
9 years ago
class-wc-frontend-scripts.php
9 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
9 years ago
class-wc-https.php
9 years ago
class-wc-install.php
9 years ago
class-wc-integrations.php
9 years ago
class-wc-legacy-api.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
9 years ago
class-wc-order-factory.php
9 years ago
class-wc-order-item-coupon.php
9 years ago
class-wc-order-item-fee.php
9 years ago
class-wc-order-item-meta.php
9 years ago
class-wc-order-item-product.php
9 years ago
class-wc-order-item-shipping.php
9 years ago
class-wc-order-item-tax.php
9 years ago
class-wc-order-item.php
9 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
9 years ago
class-wc-payment-gateways.php
9 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
9 years ago
class-wc-post-types.php
9 years ago
class-wc-product-attribute.php
9 years ago
class-wc-product-download.php
9 years ago
class-wc-product-external.php
9 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
9 years ago
class-wc-product-simple.php
9 years ago
class-wc-product-variable.php
9 years ago
class-wc-product-variation.php
9 years ago
class-wc-query.php
9 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
9 years ago
class-wc-shipping-rate.php
9 years ago
class-wc-shipping-zone.php
9 years ago
class-wc-shipping-zones.php
9 years ago
class-wc-shipping.php
9 years ago
class-wc-shortcodes.php
9 years ago
class-wc-structured-data.php
9 years ago
class-wc-tax.php
9 years ago
class-wc-template-loader.php
9 years ago
class-wc-tracker.php
9 years ago
class-wc-validation.php
9 years ago
class-wc-webhook.php
9 years ago
wc-account-functions.php
9 years ago
wc-attribute-functions.php
9 years ago
wc-cart-functions.php
9 years ago
wc-conditional-functions.php
9 years ago
wc-core-functions.php
9 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
9 years ago
wc-formatting-functions.php
9 years ago
wc-notice-functions.php
9 years ago
wc-order-functions.php
9 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
9 years ago
wc-product-functions.php
9 years ago
wc-rest-functions.php
9 years ago
wc-stock-functions.php
9 years ago
wc-template-functions.php
9 years ago
wc-template-hooks.php
9 years ago
wc-term-functions.php
9 years ago
wc-update-functions.php
9 years ago
wc-user-functions.php
9 years ago
wc-webhook-functions.php
9 years ago
wc-widget-functions.php
9 years ago
wc-attribute-functions.php
324 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Attribute Functions |
| 4 | * |
| 5 | * @author WooThemes |
| 6 | * @category Core |
| 7 | * @package WooCommerce/Functions |
| 8 | * @version 2.1.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Gets text attributes from a string. |
| 17 | * |
| 18 | * @since 2.4 |
| 19 | * @return array |
| 20 | */ |
| 21 | function wc_get_text_attributes( $raw_attributes ) { |
| 22 | 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' ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * See if an attribute is actually valid. |
| 27 | * @since 3.0.0 |
| 28 | * @param string $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 | * @since 3.0.0 |
| 38 | * @param array $attributes |
| 39 | * @return string |
| 40 | */ |
| 41 | function wc_implode_text_attributes( $attributes ) { |
| 42 | return implode( ' ' . WC_DELIMITER . ' ', $attributes ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get attribute taxonomies. |
| 47 | * |
| 48 | * @return array of objects |
| 49 | */ |
| 50 | function wc_get_attribute_taxonomies() { |
| 51 | if ( false === ( $attribute_taxonomies = get_transient( 'wc_attribute_taxonomies' ) ) ) { |
| 52 | global $wpdb; |
| 53 | |
| 54 | $attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies order by attribute_name ASC;" ); |
| 55 | |
| 56 | set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies ); |
| 57 | } |
| 58 | |
| 59 | return (array) array_filter( apply_filters( 'woocommerce_attribute_taxonomies', $attribute_taxonomies ) ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get a product attribute name. |
| 64 | * |
| 65 | * @param string $attribute_name Attribute name. |
| 66 | * @return string |
| 67 | */ |
| 68 | function wc_attribute_taxonomy_name( $attribute_name ) { |
| 69 | return 'pa_' . wc_sanitize_taxonomy_name( $attribute_name ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the attribute name used when storing values in post meta. |
| 74 | * |
| 75 | * @param string $attribute_name Attribute name. |
| 76 | * @since 2.6.0 |
| 77 | * @return string |
| 78 | */ |
| 79 | function wc_variation_attribute_name( $attribute_name ) { |
| 80 | return 'attribute_' . sanitize_title( $attribute_name ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get a product attribute name by ID. |
| 85 | * |
| 86 | * @since 2.4.0 |
| 87 | * @param int $attribute_id Attribute ID. |
| 88 | * @return string Return an empty string if attribute doesn't exist. |
| 89 | */ |
| 90 | function wc_attribute_taxonomy_name_by_id( $attribute_id ) { |
| 91 | global $wpdb; |
| 92 | |
| 93 | $attribute_name = $wpdb->get_var( $wpdb->prepare( " |
| 94 | SELECT attribute_name |
| 95 | FROM {$wpdb->prefix}woocommerce_attribute_taxonomies |
| 96 | WHERE attribute_id = %d |
| 97 | ", $attribute_id ) ); |
| 98 | |
| 99 | if ( $attribute_name && ! is_wp_error( $attribute_name ) ) { |
| 100 | return wc_attribute_taxonomy_name( $attribute_name ); |
| 101 | } |
| 102 | |
| 103 | return ''; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get a product attribute ID by name. |
| 108 | * |
| 109 | * @since 2.6.0 |
| 110 | * @param string $name Attribute name. |
| 111 | * @return int |
| 112 | */ |
| 113 | function wc_attribute_taxonomy_id_by_name( $name ) { |
| 114 | $name = str_replace( 'pa_', '', $name ); |
| 115 | $taxonomies = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_id', 'attribute_name' ); |
| 116 | |
| 117 | return isset( $taxonomies[ $name ] ) ? (int) $taxonomies[ $name ] : 0; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get a product attributes label. |
| 122 | * |
| 123 | * @param string $name |
| 124 | * @param object $product object Optional |
| 125 | * @return string |
| 126 | */ |
| 127 | function wc_attribute_label( $name, $product = '' ) { |
| 128 | global $wpdb; |
| 129 | |
| 130 | if ( taxonomy_is_product_attribute( $name ) ) { |
| 131 | $name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) ); |
| 132 | $all_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ); |
| 133 | $label = isset( $all_labels[ $name ] ) ? $all_labels[ $name ] : $name; |
| 134 | } elseif ( $product ) { |
| 135 | if ( $product->is_type( 'variation' ) ) { |
| 136 | $product = wc_get_product( $product->get_parent_id() ); |
| 137 | } |
| 138 | // Attempt to get label from product, as entered by the user. |
| 139 | if ( ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ] ) ) { |
| 140 | $label = $attributes[ sanitize_title( $name ) ]->get_name(); |
| 141 | } else { |
| 142 | $label = $name; |
| 143 | } |
| 144 | } else { |
| 145 | $label = $name; |
| 146 | } |
| 147 | |
| 148 | return apply_filters( 'woocommerce_attribute_label', $label, $name, $product ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get a product attributes orderby setting. |
| 153 | * |
| 154 | * @param mixed $name |
| 155 | * @return string |
| 156 | */ |
| 157 | function wc_attribute_orderby( $name ) { |
| 158 | global $wc_product_attributes, $wpdb; |
| 159 | |
| 160 | $name = str_replace( 'pa_', '', sanitize_title( $name ) ); |
| 161 | |
| 162 | if ( isset( $wc_product_attributes[ 'pa_' . $name ] ) ) { |
| 163 | $orderby = $wc_product_attributes[ 'pa_' . $name ]->attribute_orderby; |
| 164 | } else { |
| 165 | $orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); |
| 166 | } |
| 167 | |
| 168 | return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get an array of product attribute taxonomies. |
| 173 | * |
| 174 | * @return array |
| 175 | */ |
| 176 | function wc_get_attribute_taxonomy_names() { |
| 177 | $taxonomy_names = array(); |
| 178 | $attribute_taxonomies = wc_get_attribute_taxonomies(); |
| 179 | if ( ! empty( $attribute_taxonomies ) ) { |
| 180 | foreach ( $attribute_taxonomies as $tax ) { |
| 181 | $taxonomy_names[] = wc_attribute_taxonomy_name( $tax->attribute_name ); |
| 182 | } |
| 183 | } |
| 184 | return $taxonomy_names; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get attribute types. |
| 189 | * |
| 190 | * @since 2.4.0 |
| 191 | * @return array |
| 192 | */ |
| 193 | function wc_get_attribute_types() { |
| 194 | return (array) apply_filters( 'product_attributes_type_selector', array( |
| 195 | 'select' => __( 'Select', 'woocommerce' ), |
| 196 | 'text' => __( 'Text', 'woocommerce' ), |
| 197 | ) ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Get attribute type label. |
| 202 | * |
| 203 | * @since 3.0.0 |
| 204 | * @param string $type Attribute type slug. |
| 205 | * @return string |
| 206 | */ |
| 207 | function wc_get_attribute_type_label( $type ) { |
| 208 | $types = wc_get_attribute_types(); |
| 209 | |
| 210 | return isset( $types[ $type ] ) ? $types[ $type ] : ucfirst( $type ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Check if attribute name is reserved. |
| 215 | * https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms. |
| 216 | * |
| 217 | * @since 2.4.0 |
| 218 | * @param string $attribute_name |
| 219 | * @return bool |
| 220 | */ |
| 221 | function wc_check_if_attribute_name_is_reserved( $attribute_name ) { |
| 222 | // Forbidden attribute names |
| 223 | $reserved_terms = array( |
| 224 | 'attachment', |
| 225 | 'attachment_id', |
| 226 | 'author', |
| 227 | 'author_name', |
| 228 | 'calendar', |
| 229 | 'cat', |
| 230 | 'category', |
| 231 | 'category__and', |
| 232 | 'category__in', |
| 233 | 'category__not_in', |
| 234 | 'category_name', |
| 235 | 'comments_per_page', |
| 236 | 'comments_popup', |
| 237 | 'cpage', |
| 238 | 'day', |
| 239 | 'debug', |
| 240 | 'error', |
| 241 | 'exact', |
| 242 | 'feed', |
| 243 | 'hour', |
| 244 | 'link_category', |
| 245 | 'm', |
| 246 | 'minute', |
| 247 | 'monthnum', |
| 248 | 'more', |
| 249 | 'name', |
| 250 | 'nav_menu', |
| 251 | 'nopaging', |
| 252 | 'offset', |
| 253 | 'order', |
| 254 | 'orderby', |
| 255 | 'p', |
| 256 | 'page', |
| 257 | 'page_id', |
| 258 | 'paged', |
| 259 | 'pagename', |
| 260 | 'pb', |
| 261 | 'perm', |
| 262 | 'post', |
| 263 | 'post__in', |
| 264 | 'post__not_in', |
| 265 | 'post_format', |
| 266 | 'post_mime_type', |
| 267 | 'post_status', |
| 268 | 'post_tag', |
| 269 | 'post_type', |
| 270 | 'posts', |
| 271 | 'posts_per_archive_page', |
| 272 | 'posts_per_page', |
| 273 | 'preview', |
| 274 | 'robots', |
| 275 | 's', |
| 276 | 'search', |
| 277 | 'second', |
| 278 | 'sentence', |
| 279 | 'showposts', |
| 280 | 'static', |
| 281 | 'subpost', |
| 282 | 'subpost_id', |
| 283 | 'tag', |
| 284 | 'tag__and', |
| 285 | 'tag__in', |
| 286 | 'tag__not_in', |
| 287 | 'tag_id', |
| 288 | 'tag_slug__and', |
| 289 | 'tag_slug__in', |
| 290 | 'taxonomy', |
| 291 | 'tb', |
| 292 | 'term', |
| 293 | 'type', |
| 294 | 'w', |
| 295 | 'withcomments', |
| 296 | 'withoutcomments', |
| 297 | 'year', |
| 298 | ); |
| 299 | |
| 300 | return in_array( $attribute_name, $reserved_terms ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Callback for array filter to get visible only. |
| 305 | * |
| 306 | * @since 3.0.0 |
| 307 | * @param WC_Product $product |
| 308 | * @return bool |
| 309 | */ |
| 310 | function wc_attributes_array_filter_visible( $attribute ) { |
| 311 | return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_visible() && ( ! $attribute->is_taxonomy() || taxonomy_exists( $attribute->get_name() ) ); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Callback for array filter to get variation attributes only. |
| 316 | * |
| 317 | * @since 3.0.0 |
| 318 | * @param WC_Product $product |
| 319 | * @return bool |
| 320 | */ |
| 321 | function wc_attributes_array_filter_variation( $attribute ) { |
| 322 | return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_variation(); |
| 323 | } |
| 324 |