|
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_map( 'trim', explode( WC_DELIMITER, html_entity_decode( $raw_attributes, ENT_QUOTES, get_bloginfo( 'charset' ) ) ) ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get attribute taxonomies. |
|
27
|
|
|
* |
|
28
|
|
|
* @return array of objects |
|
29
|
|
|
*/ |
|
30
|
|
|
function wc_get_attribute_taxonomies() { |
|
31
|
|
|
if ( false === ( $attribute_taxonomies = get_transient( 'wc_attribute_taxonomies' ) ) ) { |
|
32
|
|
|
global $wpdb; |
|
33
|
|
|
|
|
34
|
|
|
$attribute_taxonomies = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies order by attribute_name ASC;" ); |
|
35
|
|
|
|
|
36
|
|
|
set_transient( 'wc_attribute_taxonomies', $attribute_taxonomies ); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return (array) array_filter( apply_filters( 'woocommerce_attribute_taxonomies', $attribute_taxonomies ) ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get a product attribute name. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $attribute_name Attribute name. |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
function wc_attribute_taxonomy_name( $attribute_name ) { |
|
49
|
|
|
return 'pa_' . wc_sanitize_taxonomy_name( $attribute_name ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the attribute name used when storing values in post meta. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $attribute_name Attribute name. |
|
56
|
|
|
* @since 2.6.0 |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
function wc_variation_attribute_name( $attribute_name ) { |
|
60
|
|
|
return 'attribute_' . sanitize_title( $attribute_name ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get a product attribute name by ID. |
|
65
|
|
|
* |
|
66
|
|
|
* @since 2.4.0 |
|
67
|
|
|
* @param int $attribute_id Attribute ID. |
|
68
|
|
|
* @return string Return an empty string if attribute doesn't exist. |
|
69
|
|
|
*/ |
|
70
|
|
|
function wc_attribute_taxonomy_name_by_id( $attribute_id ) { |
|
71
|
|
|
global $wpdb; |
|
72
|
|
|
|
|
73
|
|
|
$attribute_name = $wpdb->get_var( $wpdb->prepare( " |
|
74
|
|
|
SELECT attribute_name |
|
75
|
|
|
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies |
|
76
|
|
|
WHERE attribute_id = %d |
|
77
|
|
|
", $attribute_id ) ); |
|
78
|
|
|
|
|
79
|
|
|
if ( $attribute_name && ! is_wp_error( $attribute_name ) ) { |
|
80
|
|
|
return wc_attribute_taxonomy_name( $attribute_name ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return ''; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get a product attribute ID by name. |
|
88
|
|
|
* |
|
89
|
|
|
* @since 2.6.0 |
|
90
|
|
|
* @param string $name Attribute name. |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
|
|
function wc_attribute_taxonomy_id_by_name( $name ) { |
|
94
|
|
|
$name = str_replace( 'pa_', '', $name ); |
|
95
|
|
|
$taxonomies = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_id', 'attribute_name' ); |
|
96
|
|
|
|
|
97
|
|
|
return isset( $taxonomies[ $name ] ) ? (int) $taxonomies[ $name ] : 0; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get a product attributes label. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $name |
|
104
|
|
|
* @param object $product object Optional |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
function wc_attribute_label( $name, $product = '' ) { |
|
108
|
|
|
global $wpdb; |
|
109
|
|
|
|
|
110
|
|
|
if ( taxonomy_is_product_attribute( $name ) ) { |
|
111
|
|
|
$name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) ); |
|
112
|
|
|
$all_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ); |
|
113
|
|
|
$label = isset( $all_labels[ $name ] ) ? $all_labels[ $name ] : $name; |
|
114
|
|
|
} elseif ( $product && ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ]['name'] ) ) { |
|
115
|
|
|
// Attempt to get label from product, as entered by the user |
|
116
|
|
|
$label = $attributes[ sanitize_title( $name ) ]['name']; |
|
117
|
|
|
} else { |
|
118
|
|
|
$label = str_replace( '-', ' ', $name ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return apply_filters( 'woocommerce_attribute_label', $label, $name, $product ); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get a product attributes orderby setting. |
|
126
|
|
|
* |
|
127
|
|
|
* @param mixed $name |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
|
|
function wc_attribute_orderby( $name ) { |
|
131
|
|
|
global $wc_product_attributes, $wpdb; |
|
132
|
|
|
|
|
133
|
|
|
$name = str_replace( 'pa_', '', sanitize_title( $name ) ); |
|
134
|
|
|
|
|
135
|
|
|
if ( isset( $wc_product_attributes[ 'pa_' . $name ] ) ) { |
|
136
|
|
|
$orderby = $wc_product_attributes[ 'pa_' . $name ]->attribute_orderby; |
|
137
|
|
|
} else { |
|
138
|
|
|
$orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get an array of product attribute taxonomies. |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
function wc_get_attribute_taxonomy_names() { |
|
150
|
|
|
$taxonomy_names = array(); |
|
151
|
|
|
$attribute_taxonomies = wc_get_attribute_taxonomies(); |
|
152
|
|
|
if ( ! empty( $attribute_taxonomies ) ) { |
|
153
|
|
|
foreach ( $attribute_taxonomies as $tax ) { |
|
154
|
|
|
$taxonomy_names[] = wc_attribute_taxonomy_name( $tax->attribute_name ); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
return $taxonomy_names; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Get attribute types. |
|
162
|
|
|
* |
|
163
|
|
|
* @since 2.4.0 |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
function wc_get_attribute_types() { |
|
167
|
|
|
return (array) apply_filters( 'product_attributes_type_selector', array( |
|
168
|
|
|
'select' => __( 'Select', 'woocommerce' ), |
|
169
|
|
|
'text' => __( 'Text', 'woocommerce' ), |
|
170
|
|
|
) ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Check if attribute name is reserved. |
|
175
|
|
|
* https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms. |
|
176
|
|
|
* |
|
177
|
|
|
* @since 2.4.0 |
|
178
|
|
|
* @param string $attribute_name |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
|
|
function wc_check_if_attribute_name_is_reserved( $attribute_name ) { |
|
182
|
|
|
// Forbidden attribute names |
|
183
|
|
|
$reserved_terms = array( |
|
184
|
|
|
'attachment', |
|
185
|
|
|
'attachment_id', |
|
186
|
|
|
'author', |
|
187
|
|
|
'author_name', |
|
188
|
|
|
'calendar', |
|
189
|
|
|
'cat', |
|
190
|
|
|
'category', |
|
191
|
|
|
'category__and', |
|
192
|
|
|
'category__in', |
|
193
|
|
|
'category__not_in', |
|
194
|
|
|
'category_name', |
|
195
|
|
|
'comments_per_page', |
|
196
|
|
|
'comments_popup', |
|
197
|
|
|
'cpage', |
|
198
|
|
|
'day', |
|
199
|
|
|
'debug', |
|
200
|
|
|
'error', |
|
201
|
|
|
'exact', |
|
202
|
|
|
'feed', |
|
203
|
|
|
'hour', |
|
204
|
|
|
'link_category', |
|
205
|
|
|
'm', |
|
206
|
|
|
'minute', |
|
207
|
|
|
'monthnum', |
|
208
|
|
|
'more', |
|
209
|
|
|
'name', |
|
210
|
|
|
'nav_menu', |
|
211
|
|
|
'nopaging', |
|
212
|
|
|
'offset', |
|
213
|
|
|
'order', |
|
214
|
|
|
'orderby', |
|
215
|
|
|
'p', |
|
216
|
|
|
'page', |
|
217
|
|
|
'page_id', |
|
218
|
|
|
'paged', |
|
219
|
|
|
'pagename', |
|
220
|
|
|
'pb', |
|
221
|
|
|
'perm', |
|
222
|
|
|
'post', |
|
223
|
|
|
'post__in', |
|
224
|
|
|
'post__not_in', |
|
225
|
|
|
'post_format', |
|
226
|
|
|
'post_mime_type', |
|
227
|
|
|
'post_status', |
|
228
|
|
|
'post_tag', |
|
229
|
|
|
'post_type', |
|
230
|
|
|
'posts', |
|
231
|
|
|
'posts_per_archive_page', |
|
232
|
|
|
'posts_per_page', |
|
233
|
|
|
'preview', |
|
234
|
|
|
'robots', |
|
235
|
|
|
's', |
|
236
|
|
|
'search', |
|
237
|
|
|
'second', |
|
238
|
|
|
'sentence', |
|
239
|
|
|
'showposts', |
|
240
|
|
|
'static', |
|
241
|
|
|
'subpost', |
|
242
|
|
|
'subpost_id', |
|
243
|
|
|
'tag', |
|
244
|
|
|
'tag__and', |
|
245
|
|
|
'tag__in', |
|
246
|
|
|
'tag__not_in', |
|
247
|
|
|
'tag_id', |
|
248
|
|
|
'tag_slug__and', |
|
249
|
|
|
'tag_slug__in', |
|
250
|
|
|
'taxonomy', |
|
251
|
|
|
'tb', |
|
252
|
|
|
'term', |
|
253
|
|
|
'type', |
|
254
|
|
|
'w', |
|
255
|
|
|
'withcomments', |
|
256
|
|
|
'withoutcomments', |
|
257
|
|
|
'year', |
|
258
|
|
|
); |
|
259
|
|
|
|
|
260
|
|
|
return in_array( $attribute_name, $reserved_terms ); |
|
261
|
|
|
} |
|
262
|
|
|
|