Do not set property when calling __get()

SHA: 100b95a tweaked the `__get()` magic method in `WC_Product` to:
 * only call `get_post_meta()` once; and
 * set `$this->$key` to save the overhead of calling `__get()` every
   time that property needs to be accessed.

However, in the process, it also set an empty value (`''`) - the default
return value of `get_post_meta()` for every single value accessed on
the product object, meaning that any calls to `isset()` after attempting
to get that value would return `true`, even if `metadata_exists()` for
that property would return false (and no value is set in memory).
This commit is contained in:
Brent Shepherd 2015-01-23 15:01:10 -08:00
parent b61cd62a07
commit 95d87e28d6
1 changed files with 12 additions and 8 deletions

View File

@ -91,30 +91,34 @@ class WC_Product {
* @return mixed
*/
public function __get( $key ) {
$this->$key = get_post_meta( $this->id, '_' . $key, true );
$value = get_post_meta( $this->id, '_' . $key, true );
// Get values or default if not set
if ( in_array( $key, array( 'downloadable', 'virtual', 'backorders', 'manage_stock', 'featured', 'sold_individually' ) ) ) {
$this->$key = $this->$key ? $this->$key : 'no';
$value = $value ? $value : 'no';
} elseif ( in_array( $key, array( 'product_attributes', 'crosssell_ids', 'upsell_ids' ) ) ) {
$this->$key = $this->$key ? $this->$key : array();
$value = $value ? $value : array();
} elseif ( 'visibility' === $key ) {
$this->$key = $this->$key ? $this->$key : 'hidden';
$value = $value ? $value : 'hidden';
} elseif ( 'stock' === $key ) {
$this->$key = $this->$key ? $this->$key : 0;
$value = $value ? $value : 0;
} elseif ( 'stock_status' === $key ) {
$this->$key = $this->$key ? $this->$key : 'instock';
$value = $value ? $value : 'instock';
} elseif ( 'tax_status' === $key ) {
$this->$key = $this->$key ? $this->$key : 'taxable';
$value = $value ? $value : 'taxable';
}
return $this->$key;
if ( ! empty( $value ) ) {
$this->$key = $value;
}
return $value;
}
/**