Completed
Push — master ( 7b577e...12c582 )
by Claudio
12:55
created

wc-attribute-functions.php ➔ wc_attribute_taxonomy_id_by_name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
113
		$label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
114
115
		if ( ! $label ) {
116
			$label = $name;
117
		}
118
	} elseif ( $product && ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ]['name'] ) ) {
119
		// Attempt to get label from product, as entered by the user
120
		$label = $attributes[ sanitize_title( $name ) ]['name'];
121
	} else {
122
		$label = str_replace( '-', ' ', $name );
123
	}
124
125
	return apply_filters( 'woocommerce_attribute_label', $label, $name, $product );
126
}
127
128
/**
129
 * Get a product attributes orderby setting.
130
 *
131
 * @param mixed $name
132
 * @return string
133
 */
134
function wc_attribute_orderby( $name ) {
135
	global $wc_product_attributes, $wpdb;
136
137
	$name = str_replace( 'pa_', '', sanitize_title( $name ) );
138
139
	if ( isset( $wc_product_attributes[ 'pa_' . $name ] ) ) {
140
		$orderby = $wc_product_attributes[ 'pa_' . $name ]->attribute_orderby;
141
	} else {
142
		$orderby = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_orderby FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
143
	}
144
145
	return apply_filters( 'woocommerce_attribute_orderby', $orderby, $name );
146
}
147
148
/**
149
 * Get an array of product attribute taxonomies.
150
 *
151
 * @return array
152
 */
153
function wc_get_attribute_taxonomy_names() {
154
	$taxonomy_names = array();
155
	$attribute_taxonomies = wc_get_attribute_taxonomies();
156
	if ( $attribute_taxonomies ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attribute_taxonomies of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
157
		foreach ( $attribute_taxonomies as $tax ) {
158
			$taxonomy_names[] = wc_attribute_taxonomy_name( $tax->attribute_name );
159
		}
160
	}
161
	return $taxonomy_names;
162
}
163
164
/**
165
 * Get attribute types.
166
 *
167
 * @since  2.4.0
168
 * @return array
169
 */
170
function wc_get_attribute_types() {
171
	return (array) apply_filters( 'product_attributes_type_selector', array(
172
		'select' => __( 'Select', 'woocommerce' ),
173
		'text'   => __( 'Text', 'woocommerce' )
174
	) );
175
}
176
177
/**
178
 * Check if attribute name is reserved.
179
 * https://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms.
180
 *
181
 * @since  2.4.0
182
 * @param  string $attribute_name
183
 * @return bool
184
 */
185
function wc_check_if_attribute_name_is_reserved( $attribute_name ) {
186
	// Forbidden attribute names
187
	$reserved_terms = array(
188
		'attachment', 'attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and',
189
		'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day',
190
		'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name',
191
		'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm',
192
		'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type',
193
		'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence',
194
		'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id',
195
		'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year',
196
	);
197
198
	return in_array( $attribute_name, $reserved_terms );
199
}
200