暫無描述

class-wc-product-attribute.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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,
  117. array(
  118. 'is_visible' => $this->get_visible() ? 1 : 0,
  119. 'is_variation' => $this->get_variation() ? 1 : 0,
  120. 'is_taxonomy' => $this->is_taxonomy() ? 1 : 0,
  121. 'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ),
  122. )
  123. );
  124. }
  125. /*
  126. |--------------------------------------------------------------------------
  127. | Setters
  128. |--------------------------------------------------------------------------
  129. */
  130. /**
  131. * Set ID (this is the attribute ID).
  132. *
  133. * @param int $value Attribute ID.
  134. */
  135. public function set_id( $value ) {
  136. $this->data['id'] = absint( $value );
  137. }
  138. /**
  139. * Set name (this is the attribute name or taxonomy).
  140. *
  141. * @param int $value Attribute name.
  142. */
  143. public function set_name( $value ) {
  144. $this->data['name'] = $value;
  145. }
  146. /**
  147. * Set options.
  148. *
  149. * @param array $value Attribute options.
  150. */
  151. public function set_options( $value ) {
  152. $this->data['options'] = $value;
  153. }
  154. /**
  155. * Set position.
  156. *
  157. * @param int $value Attribute position.
  158. */
  159. public function set_position( $value ) {
  160. $this->data['position'] = absint( $value );
  161. }
  162. /**
  163. * Set if visible.
  164. *
  165. * @param bool $value If is visible on Product's additional info tab.
  166. */
  167. public function set_visible( $value ) {
  168. $this->data['visible'] = wc_string_to_bool( $value );
  169. }
  170. /**
  171. * Set if variation.
  172. *
  173. * @param bool $value If is used for variations.
  174. */
  175. public function set_variation( $value ) {
  176. $this->data['variation'] = wc_string_to_bool( $value );
  177. }
  178. /*
  179. |--------------------------------------------------------------------------
  180. | Getters
  181. |--------------------------------------------------------------------------
  182. */
  183. /**
  184. * Get the ID.
  185. *
  186. * @return int
  187. */
  188. public function get_id() {
  189. return $this->data['id'];
  190. }
  191. /**
  192. * Get name.
  193. *
  194. * @return string
  195. */
  196. public function get_name() {
  197. return $this->data['name'];
  198. }
  199. /**
  200. * Get options.
  201. *
  202. * @return array
  203. */
  204. public function get_options() {
  205. return $this->data['options'];
  206. }
  207. /**
  208. * Get position.
  209. *
  210. * @return int
  211. */
  212. public function get_position() {
  213. return $this->data['position'];
  214. }
  215. /**
  216. * Get if visible.
  217. *
  218. * @return bool
  219. */
  220. public function get_visible() {
  221. return $this->data['visible'];
  222. }
  223. /**
  224. * Get if variation.
  225. *
  226. * @return bool
  227. */
  228. public function get_variation() {
  229. return $this->data['variation'];
  230. }
  231. /*
  232. |--------------------------------------------------------------------------
  233. | ArrayAccess/Backwards compatibility.
  234. |--------------------------------------------------------------------------
  235. */
  236. /**
  237. * OffsetGet.
  238. *
  239. * @param string $offset Offset.
  240. * @return mixed
  241. */
  242. public function offsetGet( $offset ) {
  243. switch ( $offset ) {
  244. case 'is_variation':
  245. return $this->get_variation() ? 1 : 0;
  246. case 'is_visible':
  247. return $this->get_visible() ? 1 : 0;
  248. case 'is_taxonomy':
  249. return $this->is_taxonomy() ? 1 : 0;
  250. case 'value':
  251. return $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() );
  252. default:
  253. if ( is_callable( array( $this, "get_$offset" ) ) ) {
  254. return $this->{"get_$offset"}();
  255. }
  256. break;
  257. }
  258. return '';
  259. }
  260. /**
  261. * OffsetSet.
  262. *
  263. * @param string $offset Offset.
  264. * @param mixed $value Value.
  265. */
  266. public function offsetSet( $offset, $value ) {
  267. switch ( $offset ) {
  268. case 'is_variation':
  269. $this->set_variation( $value );
  270. break;
  271. case 'is_visible':
  272. $this->set_visible( $value );
  273. break;
  274. case 'value':
  275. $this->set_options( $value );
  276. break;
  277. default:
  278. if ( is_callable( array( $this, "set_$offset" ) ) ) {
  279. return $this->{"set_$offset"}( $value );
  280. }
  281. break;
  282. }
  283. }
  284. /**
  285. * OffsetUnset.
  286. *
  287. * @param string $offset Offset.
  288. */
  289. public function offsetUnset( $offset ) {}
  290. /**
  291. * OffsetExists.
  292. *
  293. * @param string $offset Offset.
  294. * @return bool
  295. */
  296. public function offsetExists( $offset ) {
  297. return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ) ), true );
  298. }
  299. }