/includes/wc-attribute-functions.php

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