Permalink
Browse files

Prevent variations being created/returned without parents

Nasty bug discovered by @RiaanKnoetze where it would get a variation
without parent, and due to the return, it wouldn’t be ‘seen’ as a
variation, therefore creating fatal errors when props are called.

To prevent this, lets allow product constructors to throw exceptions.
If they do, the factory class will catch them and return false instead
of an invalid product object.

@claudiosmweb Thoughts on this one before merge?
  • Loading branch information...
mikejolley committed May 13, 2016
1 parent 6e94df6 commit d42d7c13b6d472454084e19d7e3d7085453b6e8f
Showing with 20 additions and 15 deletions.
  1. +18 −13 includes/class-wc-product-factory.php
  2. +2 −2 includes/class-wc-product-variation.php
@@ -18,30 +18,35 @@
class WC_Product_Factory {
/**
* Get product.
* Get a product.
*
* @param bool $the_product (default: false)
* @param array $args (default: array())
* @return WC_Product|bool false if the product cannot be loaded
*/
public function get_product( $the_product = false, $args = array() ) {
$the_product = $this->get_product_object( $the_product );
try {
$the_product = $this->get_product_object( $the_product );
if ( ! $the_product ) {
return false;
}
if ( ! $the_product ) {
throw new Exception( 'Product object does not exist', 422 );
}
$classname = $this->get_product_class( $the_product, $args );
$classname = $this->get_product_class( $the_product, $args );
if ( ! $classname ) {
return false;
}
if ( ! $classname ) {
throw new Exception( 'Missing classname', 422 );
}
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
return new $classname( $the_product, $args );
return new $classname( $the_product, $args );
} catch ( Exception $e ) {
return false;
}
}
/**
@@ -80,9 +80,9 @@ public function __construct( $variation, $args = array() ) {
/* Get main product data from parent (args) */
$this->id = ! empty( $args['parent_id'] ) ? intval( $args['parent_id'] ) : wp_get_post_parent_id( $this->variation_id );
// The post doesn't have a parent id, therefore its invalid.
// The post doesn't have a parent id, therefore its invalid and we should prevent this being created.
if ( empty( $this->id ) ) {
return;
throw new Exception( sprintf( 'No parent product set for variation #%d', $this->variation_id ), 422 );
}
$this->product_type = 'variation';

0 comments on commit d42d7c1

Please sign in to comment.