Initial look through getters and setters and abstract data

This commit is contained in:
Mike Jolley 2016-10-17 14:46:46 +01:00
parent 4c0cc54f79
commit eb61503210
1 changed files with 116 additions and 120 deletions

View File

@ -29,11 +29,11 @@ class WC_Product extends WC_Abstract_Legacy_Product {
protected $data = array( protected $data = array(
'name' => '', 'name' => '',
'slug' => '', 'slug' => '',
'permalink' => '', //'permalink' => '',
'date_created' => '', 'date_created' => '',
'date_modified' => '', 'date_modified' => '',
'status' => '', 'status' => '',
'featured' => 'no', 'featured' => false,
'catalog_visibility' => 'hidden', 'catalog_visibility' => 'hidden',
'description' => '', 'description' => '',
'short_description' => '', 'short_description' => '',
@ -45,17 +45,17 @@ class WC_Product extends WC_Abstract_Legacy_Product {
'total_sales' => '', 'total_sales' => '',
'tax_status' => 'taxable', 'tax_status' => 'taxable',
'tax_class' => '', 'tax_class' => '',
'manage_stock' => 'no', 'manage_stock' => false,
'stock_quantity' => null, 'stock_quantity' => null,
'stock_status' => '', 'stock_status' => '',
'backorders' => 'no', 'backorders' => 'no',
'sold_individually' => 'no', 'sold_individually' => false,
'weight' => '', 'weight' => '',
'length' => '', 'length' => '',
'width' => '', 'width' => '',
'height' => '', 'height' => '',
'upsell_ids' => '', 'upsell_ids' => array(),
'cross_sell_ids' => '', 'cross_sell_ids' => array(),
'parent_id' => 0, 'parent_id' => 0,
'reviews_allowed' => true, 'reviews_allowed' => true,
'purchase_note' => '', 'purchase_note' => '',
@ -96,6 +96,23 @@ class WC_Product extends WC_Abstract_Legacy_Product {
} }
} }
/**
* Get internal type.
* @since 2.7.0
* @return string
*/
public function get_type() {
return 'simple';
}
/**
* Product permalink.
* @return string
*/
public function get_permalink() {
return get_permalink( $this->get_id() );
}
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Getters | Getters
@ -114,27 +131,8 @@ class WC_Product extends WC_Abstract_Legacy_Product {
return apply_filters( 'woocommerce_product_get_name', $this->data['name'], $this ); return apply_filters( 'woocommerce_product_get_name', $this->data['name'], $this );
} }
/**
* Get internal type.
* @since 2.7.0
* @return string
*/
public function get_type() {
return 'simple';
}
// @todo below.
/** /**
* Get product slug. * Get product slug.
*
* @since 2.7.0 * @since 2.7.0
* @return string * @return string
*/ */
@ -142,15 +140,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
return $this->data['slug']; return $this->data['slug'];
} }
/**
* Product permalink.
*
* @return string
*/
public function get_permalink() {
return $this->data['permalink'];
}
/** /**
* Get product created date. * Get product created date.
* *
@ -185,7 +174,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* If the product is featured. * If the product is featured.
* *
* @since 2.7.0 * @since 2.7.0
* @return string * @return boolean
*/ */
public function get_featured() { public function get_featured() {
return $this->data['featured']; return $this->data['featured'];
@ -300,7 +289,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Return if product manage stock. * Return if product manage stock.
* *
* @since 2.7.0 * @since 2.7.0
* @return string * @return boolean
*/ */
public function get_manage_stock() { public function get_manage_stock() {
return $this->data['manage_stock']; return $this->data['manage_stock'];
@ -309,7 +298,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
/** /**
* Returns number of items available for sale. * Returns number of items available for sale.
* *
* @return int * @return int|null
*/ */
public function get_stock_quantity() { public function get_stock_quantity() {
return apply_filters( 'woocommerce_get_stock_quantity', $this->get_manage_stock() ? wc_stock_amount( $this->data['stock_quantity'] ) : null, $this ); return apply_filters( 'woocommerce_get_stock_quantity', $this->get_manage_stock() ? wc_stock_amount( $this->data['stock_quantity'] ) : null, $this );
@ -329,7 +318,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Get backorders. * Get backorders.
* *
* @since 2.7.0 * @since 2.7.0
* @return string * @return string yes no or notify
*/ */
public function get_backorders() { public function get_backorders() {
return $this->data['backorders']; return $this->data['backorders'];
@ -339,7 +328,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Return if should be sold individually. * Return if should be sold individually.
* *
* @since 2.7.0 * @since 2.7.0
* @return string * @return boolean
*/ */
public function get_sold_individually() { public function get_sold_individually() {
return $this->data['sold_individually']; return $this->data['sold_individually'];
@ -352,7 +341,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
*/ */
public function get_weight() { public function get_weight() {
// Legacy filter. // Legacy filter.
$weight = apply_filters( 'woocommerce_product_weight', $this->data['weight'], $this ); $weight = apply_filters( 'woocommerce_product_weight', $this->data['weight'], $this ); // @todo standardize these filter names and move BW compat to deprecated class file.
// New filter. // New filter.
return apply_filters( 'woocommerce_product_get_weight', $weight, $this ); return apply_filters( 'woocommerce_product_get_weight', $weight, $this );
@ -398,20 +387,20 @@ class WC_Product extends WC_Abstract_Legacy_Product {
} }
/** /**
* Get Upseels IDs. * Get upsel IDs.
* *
* @since 2.7.0 * @since 2.7.0
* @return string * @return array
*/ */
public function get_upsell_ids() { public function get_upsell_ids() {
return $this->data['upsell_ids']; return $this->data['upsell_ids'];
} }
/** /**
* Get Upseels IDs. * Get cross sell IDs.
* *
* @since 2.7.0 * @since 2.7.0
* @return string * @return array
*/ */
public function get_cross_sell_ids() { public function get_cross_sell_ids() {
return $this->data['cross_sell_ids']; return $this->data['cross_sell_ids'];
@ -447,37 +436,13 @@ class WC_Product extends WC_Abstract_Legacy_Product {
return $this->data['purchase_note']; return $this->data['purchase_note'];
} }
/**
* Returns the product categories.
*
* @param string $sep (default: ', ').
* @param string $before (default: '').
* @param string $after (default: '').
* @return string
*/
public function get_categories( $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list( $this->get_id(), 'product_cat', $before, $sep, $after );
}
/**
* Returns the product tags.
*
* @param string $sep (default: ', ').
* @param string $before (default: '').
* @param string $after (default: '').
* @return array
*/
public function get_tags( $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list( $this->get_id(), 'product_tag', $before, $sep, $after );
}
/** /**
* Returns product attributes. * Returns product attributes.
* *
* @return array * @return array
*/ */
public function get_attributes() { public function get_attributes() {
$attributes = array_filter( (array) maybe_unserialize( $this->data['product_attributes'] ) ); $attributes = $this->data['product_attributes'];
$taxonomies = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_name' ); $taxonomies = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_name' );
// Check for any attributes which have been removed globally // Check for any attributes which have been removed globally
@ -542,16 +507,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
$this->data['slug'] = $slug; $this->data['slug'] = $slug;
} }
/**
* Set product permalink.
*
* @since 2.7.0
* @param string $permalink Product permalink.
*/
public function set_permalink( $permalink ) {
$this->data['permalink'] = $permalink;
}
/** /**
* Set product created date. * Set product created date.
* *
@ -586,10 +541,10 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Set if the product is featured. * Set if the product is featured.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $featured Options: 'yes' or 'no'. * @param bool|string
*/ */
public function set_featured( $featured ) { public function set_featured( $featured ) {
$this->data['featured'] = $featured; $this->data['featured'] = wc_string_to_bool( $featured );
} }
/** /**
@ -604,7 +559,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
if ( ! in_array( $visibility, $options, true ) ) { if ( ! in_array( $visibility, $options, true ) ) {
$this->error( 'product_invalid_catalog_visibility', __( 'Invalid catalog visibility option.', 'woocommerce' ) ); $this->error( 'product_invalid_catalog_visibility', __( 'Invalid catalog visibility option.', 'woocommerce' ) );
} }
$this->data['catalog_visibility'] = $visibility; $this->data['catalog_visibility'] = $visibility;
} }
@ -639,7 +593,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
if ( ! empty( $sku ) && ! wc_product_has_unique_sku( $this->get_id(), $sku ) ) { if ( ! empty( $sku ) && ! wc_product_has_unique_sku( $this->get_id(), $sku ) ) {
$this->error( 'product_invalid_sku', __( 'Invalid or duplicated SKU.', 'woocommerce' ) ); $this->error( 'product_invalid_sku', __( 'Invalid or duplicated SKU.', 'woocommerce' ) );
} }
$this->data['sku'] = $sku; $this->data['sku'] = $sku;
} }
@ -650,7 +603,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @param string $price Regular price. * @param string $price Regular price.
*/ */
public function set_regular_price( $price ) { public function set_regular_price( $price ) {
$this->data['regular_price'] = $price; $this->data['regular_price'] = wc_format_decimal( $price );
} }
/** /**
@ -660,27 +613,27 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @param string $price sale price. * @param string $price sale price.
*/ */
public function set_sale_price( $price ) { public function set_sale_price( $price ) {
$this->data['sale_price'] = $price; $this->data['sale_price'] = wc_format_decimal( $price );
} }
/** /**
* Set date on sale from. * Set date on sale from.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $data Sale from date. * @param string $timestamp Sale from date.
*/ */
public function set_date_on_sale_from( $date ) { public function set_date_on_sale_from( $timestamp ) {
$this->data['date_on_sale_from'] = $date; $this->data['date_on_sale_from'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
} }
/** /**
* Set date on sale to. * Set date on sale to.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $data Sale to date. * @param string $timestamp Sale to date.
*/ */
public function set_date_on_sale_to( $date ) { public function set_date_on_sale_to( $date ) {
return $this->data['date_on_sale_to'] = $date; return $this->data['date_on_sale_to'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
} }
/** /**
@ -733,10 +686,10 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Set if product manage stock. * Set if product manage stock.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $manage_stock Options: 'yes' or 'no'. * @param bool
*/ */
public function set_manage_stock( $manage_stock ) { public function set_manage_stock( $manage_stock ) {
$this->data['manage_stock'] = $manage_stock; $this->data['manage_stock'] = wc_string_to_bool( $manage_stock );
} }
/** /**
@ -785,10 +738,10 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* Set if should be sold individually. * Set if should be sold individually.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $sold_individually Options: 'yes' or 'no'. * @param bool
*/ */
public function set_sold_individually( $sold_individually ) { public function set_sold_individually( $sold_individually ) {
$this->data['sold_individually'] = $sold_individually; $this->data['sold_individually'] = wc_string_to_bool( $sold_individually );
} }
/** /**
@ -832,23 +785,23 @@ class WC_Product extends WC_Abstract_Legacy_Product {
} }
/** /**
* Set Upseels IDs. * Set upsell IDs.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $upsell_ids IDs from the up-sell products. * @param string $upsell_ids IDs from the up-sell products.
*/ */
public function set_upsell_ids( $upsell_ids ) { public function set_upsell_ids( $upsell_ids ) {
$this->data['upsell_ids'] = $upsell_ids; $this->data['upsell_ids'] = array_filter( (array) $upsell_ids );
} }
/** /**
* Set Upseels IDs. * Set crosssell IDs.
* *
* @since 2.7.0 * @since 2.7.0
* @param string $cross_sell_ids IDs from the cross-sell products. * @param string $cross_sell_ids IDs from the cross-sell products.
*/ */
public function set_cross_sell_ids( $cross_sell_ids ) { public function set_cross_sell_ids( $cross_sell_ids ) {
$this->data['cross_sell_ids'] = $cross_sell_ids; $this->data['cross_sell_ids'] = array_filter( (array) $cross_sell_ids );
} }
/** /**
@ -868,7 +821,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @param bool $reviews_allowed Reviews allowed or not. * @param bool $reviews_allowed Reviews allowed or not.
*/ */
public function set_reviews_allowed( $reviews_allowed ) { public function set_reviews_allowed( $reviews_allowed ) {
$this->data['reviews_allowed'] = (bool) $reviews_allowed; $this->data['reviews_allowed'] = wc_string_to_bool( $reviews_allowed );
} }
/** /**
@ -881,26 +834,6 @@ class WC_Product extends WC_Abstract_Legacy_Product {
$this->data['purchase_note'] = $purchase_note; $this->data['purchase_note'] = $purchase_note;
} }
/**
* Set the product categories.
*
* @since 2.7.0
* @param array $terms_id List of terms IDs.
*/
public function set_categories( $terms_id ) {
$this->save_taxonomy_terms( $terms_id, 'cat' );
}
/**
* Set the product tags.
*
* @since 2.7.0
* @param array $terms_id List of terms IDs.
*/
public function set_tags( $terms_id ) {
$this->save_taxonomy_terms( $terms_id, 'tag' );
}
/** /**
* Returns product attributes. * Returns product attributes.
* *
@ -908,7 +841,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @param array $attributes List of product attributes. * @param array $attributes List of product attributes.
*/ */
public function set_attributes( $attributes ) { public function set_attributes( $attributes ) {
$this->data['product_attributes'] = $attributes; $this->data['product_attributes'] = $attributes; // @todo ensure unserialised, array, and filtered out empty values
} }
/** /**
@ -931,6 +864,69 @@ class WC_Product extends WC_Abstract_Legacy_Product {
$this->data['menu_order'] = intval( $menu_order ); $this->data['menu_order'] = intval( $menu_order );
} }
/**
* Returns the product categories. @todo store in class and save?
*
* @param string $sep (default: ', ').
* @param string $before (default: '').
* @param string $after (default: '').
* @return string
*/
public function get_categories( $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list( $this->get_id(), 'product_cat', $before, $sep, $after );
}
/**
* Returns the product tags. @todo store in class and save?
*
* @param string $sep (default: ', ').
* @param string $before (default: '').
* @param string $after (default: '').
* @return array
*/
public function get_tags( $sep = ', ', $before = '', $after = '' ) {
return get_the_term_list( $this->get_id(), 'product_tag', $before, $sep, $after );
}
/**
* Set the product categories. @todo store in class and save?
*
* @since 2.7.0
* @param array $terms_id List of terms IDs.
*/
public function set_categories( $terms_id ) {
$this->save_taxonomy_terms( $terms_id, 'cat' );
}
/**
* Set the product tags. @todo store in class and save?
*
* @since 2.7.0
* @param array $terms_id List of terms IDs.
*/
public function set_tags( $terms_id ) {
$this->save_taxonomy_terms( $terms_id, 'tag' );
}
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| CRUD methods | CRUD methods