class-wc-product-attribute.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Represents a product attribute
  4. *
  5. * Attributes can be global (taxonomy based) or local to the product itself.
  6. * Uses ArrayAccess to be BW compatible with previous ways of reading attributes.
  7. *
  8. * @package WooCommerce/Classes
  9. * @version 3.0.0
  10. * @since 3.0.0
  11. */
  12. defined( 'ABSPATH' ) || exit;
  13. /**
  14. * Product attribute class.
  15. */
  16. class WC_Product_Attribute implements ArrayAccess {
  17. /**
  18. * Data array.
  19. *
  20. * @since 3.0.0
  21. * @var array
  22. */
  23. protected $data = array(
  24. 'id' => 0,
  25. 'name' => '',
  26. 'options' => array(),
  27. 'position' => 0,
  28. 'visible' => false,
  29. 'variation' => false,
  30. );
  31. /**
  32. * Return if this attribute is a taxonomy.
  33. *
  34. * @return boolean
  35. */
  36. public function is_taxonomy() {
  37. return 0 < $this->get_id();
  38. }
  39. /**
  40. * Get taxonomy name if applicable.
  41. *
  42. * @return string
  43. */
  44. public function get_taxonomy() {
  45. return $this->is_taxonomy() ? $this->get_name() : '';
  46. }
  47. /**
  48. * Get taxonomy object.
  49. *
  50. * @return array|null
  51. */
  52. public function get_taxonomy_object() {
  53. global $wc_product_attributes;
  54. return $this->is_taxonomy() ? $wc_product_attributes[ $this->get_name() ] : null;
  55. }
  56. /**
  57. * Gets terms from the stored options.
  58. *
  59. * @return array|null
  60. */
  61. public function get_terms() {
  62. if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) {
  63. return null;
  64. }
  65. $terms = array();
  66. foreach ( $this->get_options() as $option ) {
  67. if ( is_int( $option ) ) {
  68. $term = get_term_by( 'id', $option, $this->get_name() );
  69. } else {
  70. // Term names get escaped in WP. See sanitize_term_field.
  71. $term = get_term_by( 'name', $option, $this->get_name() );
  72. if ( ! $term || is_wp_error( $term ) ) {
  73. $new_term = wp_insert_term( $option, $this->get_name() );
  74. $term = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $this->get_name() );
  75. }
  76. }
  77. if ( $term && ! is_wp_error( $term ) ) {
  78. $terms[] = $term;
  79. }
  80. }
  81. return $terms;
  82. }
  83. /**
  84. * Gets slugs from the stored options, or just the string if text based.
  85. *
  86. * @return array
  87. */
  88. public function get_slugs() {
  89. if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) {
  90. return $this->get_options();
  91. }
  92. $terms = array();
  93. foreach ( $this->get_options() as $option ) {
  94. if ( is_int( $option ) ) {
  95. $term = get_term_by( 'id', $option, $this->get_name() );
  96. } else {
  97. $term = get_term_by( 'name', $option, $this->get_name() );
  98. if ( ! $term || is_wp_error( $term ) ) {
  99. $new_term = wp_insert_term( $option, $this->get_name() );
  100. $term = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $this->get_name() );
  101. }
  102. }
  103. if ( $term && ! is_wp_error( $term ) ) {
  104. $terms[] = $term->slug;
  105. }
  106. }
  107. return $terms;
  108. }
  109. /**
  110. * Returns all data for this object.
  111. *
  112. * @return array
  113. */
  114. public function get_data() {
  115. return array_merge(
  116. $this->data, array(
  117. 'is_visible' => $this->get_visible() ? 1 : 0,
  118. 'is_variation' => $this->get_variation() ? 1 : 0,
  119. 'is_taxonomy' => $this->is_taxonomy() ? 1 : 0,
  120. 'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ),
  121. )
  122. );
  123. }
  124. /*
  125. |--------------------------------------------------------------------------
  126. | Setters
  127. |--------------------------------------------------------------------------
  128. */
  129. /**
  130. * Set ID (this is the attribute ID).
  131. *
  132. * @param int $value Attribute ID.
  133. */
  134. public function set_id( $value ) {
  135. $this->data['id'] = absint( $value );
  136. }
  137. /**
  138. * Set name (this is the attribute name or taxonomy).
  139. *
  140. * @param int $value Attribute name.
  141. */
  142. public function set_name( $value ) {
  143. $this->data['name'] = $value;
  144. }
  145. /**
  146. * Set options.
  147. *
  148. * @param array $value Attribute options.
  149. */
  150. public function set_options( $value ) {
  151. $this->data['options'] = $value;
  152. }
  153. /**
  154. * Set position.
  155. *
  156. * @param int $value Attribute position.
  157. */
  158. public function set_position( $value ) {
  159. $this->data['position'] = absint( $value );
  160. }
  161. /**
  162. * Set if visible.
  163. *
  164. * @param bool $value If is visible on Product's additional info tab.
  165. */
  166. public function set_visible( $value ) {
  167. $this->data['visible'] = wc_string_to_bool( $value );
  168. }
  169. /**
  170. * Set if variation.
  171. *
  172. * @param bool $value If is used for variations.
  173. */
  174. public function set_variation( $value ) {
  175. $this->data['variation'] = wc_string_to_bool( $value );
  176. }
  177. /*
  178. |--------------------------------------------------------------------------
  179. | Getters
  180. |--------------------------------------------------------------------------
  181. */
  182. /**
  183. * Get the ID.
  184. *
  185. * @return int
  186. */
  187. public function get_id() {
  188. return $this->data['id'];
  189. }
  190. /**
  191. * Get name.
  192. *
  193. * @return int
  194. */
  195. public function get_name() {
  196. return $this->data['name'];
  197. }
  198. /**
  199. * Get options.
  200. *
  201. * @return array
  202. */
  203. public function get_options() {
  204. return $this->data['options'];
  205. }
  206. /**
  207. * Get position.
  208. *
  209. * @return int
  210. */
  211. public function get_position() {
  212. return $this->data['position'];
  213. }
  214. /**
  215. * Get if visible.
  216. *
  217. * @return bool
  218. */
  219. public function get_visible() {
  220. return $this->data['visible'];
  221. }
  222. /**
  223. * Get if variation.
  224. *
  225. * @return bool
  226. */
  227. public function get_variation() {
  228. return $this->data['variation'];
  229. }
  230. /*
  231. |--------------------------------------------------------------------------
  232. | ArrayAccess/Backwards compatibility.
  233. |--------------------------------------------------------------------------
  234. */
  235. /**
  236. * OffsetGet.
  237. *
  238. * @param string $offset Offset.
  239. * @return mixed
  240. */
  241. public function offsetGet( $offset ) {
  242. switch ( $offset ) {
  243. case 'is_variation':
  244. return $this->get_variation() ? 1 : 0;
  245. case 'is_visible':
  246. return $this->get_visible() ? 1 : 0;
  247. case 'is_taxonomy':
  248. return $this->is_taxonomy() ? 1 : 0;
  249. case 'value':
  250. return $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() );
  251. default:
  252. if ( is_callable( array( $this, "get_$offset" ) ) ) {
  253. return $this->{"get_$offset"}();
  254. }
  255. break;
  256. }
  257. return '';
  258. }
  259. /**
  260. * OffsetSet.
  261. *
  262. * @param string $offset Offset.
  263. * @param mixed $value Value.
  264. */
  265. public function offsetSet( $offset, $value ) {
  266. switch ( $offset ) {
  267. case 'is_variation':
  268. $this->set_variation( $value );
  269. break;
  270. case 'is_visible':
  271. $this->set_visible( $value );
  272. break;
  273. case 'value':
  274. $this->set_options( $value );
  275. break;
  276. default:
  277. if ( is_callable( array( $this, "set_$offset" ) ) ) {
  278. return $this->{"set_$offset"}( $value );
  279. }
  280. break;
  281. }
  282. }
  283. /**
  284. * OffsetUnset.
  285. *
  286. * @param string $offset Offset.
  287. */
  288. public function offsetUnset( $offset ) {}
  289. /**
  290. * OffsetExists.
  291. *
  292. * @param string $offset Offset.
  293. * @return bool
  294. */
  295. public function offsetExists( $offset ) {
  296. return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ) ), true );
  297. }
  298. }