Create text terms if they do not exist

Closes #12631
This commit is contained in:
Mike Jolley 2016-12-19 13:09:42 +00:00
parent 51fc839515
commit b81b6c1a44
1 changed files with 38 additions and 6 deletions

View File

@ -18,13 +18,14 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Data array.
*
* @since 2.7.0
* @var array
*/
protected $data = array(
'id' => 0,
'name' => '',
'options' => '',
'options' => array(),
'position' => 0,
'visible' => false,
'variation' => false,
@ -32,6 +33,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Return if this attribute is a taxonomy.
*
* @return boolean
*/
public function is_taxonomy() {
@ -40,6 +42,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get taxonomy name if applicable.
*
* @return string
*/
public function get_taxonomy() {
@ -48,6 +51,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get taxonomy object.
*
* @return array|null
*/
public function get_taxonomy_object() {
@ -64,13 +68,18 @@ class WC_Product_Attribute implements ArrayAccess {
if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) {
return null;
}
$terms = array();
foreach ( $this->get_options() as $option ) {
if ( is_int( $option ) ) {
$term = get_term_by( 'id', $option, $this->get_name() );
} else {
$term = get_term_by( 'name', $option, $this->get_name() );
}
if ( ! $term || is_wp_error( $term ) ) {
$new_term = wp_insert_term( $option, $this->get_name() );
$term = get_term_by( 'id', $new_term['term_id'], $this->get_name() );
}
}
if ( $term && ! is_wp_error( $term ) ) {
$terms[] = $term;
}
@ -87,11 +96,17 @@ class WC_Product_Attribute implements ArrayAccess {
if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) {
return $this->get_options();
}
$terms = array();
foreach ( $this->get_options() as $option ) {
if ( is_int( $option ) ) {
$term = get_term_by( 'id', $option, $this->get_name() );
} else {
$term = get_term_by( 'name', $option, $this->get_name() );
if ( ! $term || is_wp_error( $term ) ) {
$new_term = wp_insert_term( $option, $this->get_name() );
$term = get_term_by( 'id', $new_term['term_id'], $this->get_name() );
}
}
if ( $term && ! is_wp_error( $term ) ) {
$terms[] = $term->slug;
@ -102,6 +117,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Returns all data for this object.
*
* @return array
*/
public function get_data() {
@ -121,6 +137,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set ID (this is the attribute ID).
*
* @param int $value
*/
public function set_id( $value ) {
@ -129,6 +146,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set name (this is the attribute name or taxonomy).
*
* @param int $value
*/
public function set_name( $value ) {
@ -137,6 +155,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set options.
*
* @param array $value
*/
public function set_options( $value ) {
@ -145,6 +164,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set position.
*
* @param int $value
*/
public function set_position( $value ) {
@ -153,6 +173,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set if visible.
*
* @param bool $value
*/
public function set_visible( $value ) {
@ -161,6 +182,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Set if variation.
*
* @param bool $value
*/
public function set_variation( $value ) {
@ -175,6 +197,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get the ID.
*
* @return int
*/
public function get_id() {
@ -183,6 +206,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get name.
*
* @return int
*/
public function get_name() {
@ -191,6 +215,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get options.
*
* @return array
*/
public function get_options() {
@ -199,6 +224,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get position.
*
* @return int
*/
public function get_position() {
@ -207,6 +233,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get if visible.
*
* @return bool
*/
public function get_visible() {
@ -215,6 +242,7 @@ class WC_Product_Attribute implements ArrayAccess {
/**
* Get if variation.
*
* @return bool
*/
public function get_variation() {
@ -228,7 +256,8 @@ class WC_Product_Attribute implements ArrayAccess {
*/
/**
* offsetGet
* offsetGet.
*
* @param string $offset
* @return mixed
*/
@ -256,7 +285,8 @@ class WC_Product_Attribute implements ArrayAccess {
}
/**
* offsetSet
* offsetSet.
*
* @param string $offset
* @param mixed $value
*/
@ -280,13 +310,15 @@ class WC_Product_Attribute implements ArrayAccess {
}
/**
* offsetUnset
* offsetUnset.
*
* @param string $offset
*/
public function offsetUnset( $offset ) {}
/**
* offsetExists
* offsetExists.
*
* @param string $offset
* @return bool
*/