Merge pull request #12762 from woocommerce/deprecated-args-12718

Factory classname handling
This commit is contained in:
Mike Jolley 2017-01-06 11:59:20 +00:00 committed by GitHub
commit 50274e44c4
6 changed files with 80 additions and 64 deletions

View File

@ -439,10 +439,11 @@ jQuery( function( $ ) {
});
var data = {
post_id: woocommerce_admin_meta_boxes.post_id,
data: $( '.product_attributes' ).find( 'input, select, textarea' ).serialize(),
action: 'woocommerce_save_attributes',
security: woocommerce_admin_meta_boxes.save_attributes_nonce
post_id : woocommerce_admin_meta_boxes.post_id,
product_type: $( '#product-type' ).val(),
data : $( '.product_attributes' ).find( 'input, select, textarea' ).serialize(),
action : 'woocommerce_save_attributes',
security : woocommerce_admin_meta_boxes.save_attributes_nonce
};
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function() {

File diff suppressed because one or more lines are too long

View File

@ -254,15 +254,10 @@ class WC_Meta_Box_Product_Data {
public static function save( $post_id, $post ) {
// Process product type first so we have the correct class to run setters.
$product_type = empty( $_POST['product-type'] ) ? 'simple' : sanitize_title( stripslashes( $_POST['product-type'] ) );
$classname = WC_Product_Factory::get_classname_from_product_type( $product_type );
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
$product = new $classname( $post_id );
$attributes = self::prepare_attributes();
$errors = $product->set_props( array(
$classname = WC_Product_Factory::get_product_classname( $post_id, $product_type );
$product = new $classname( $post_id );
$attributes = self::prepare_attributes();
$errors = $product->set_props( array(
'sku' => isset( $_POST['_sku'] ) ? wc_clean( $_POST['_sku'] ) : null,
'purchase_note' => wp_kses_post( stripslashes( $_POST['_purchase_note'] ) ),
'downloadable' => isset( $_POST['_downloadable'] ),

View File

@ -542,9 +542,12 @@ class WC_AJAX {
}
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
$product = wc_get_product( $post_id );
$attributes = WC_Meta_Box_Product_Data::prepare_attributes( $data );
$attributes = WC_Meta_Box_Product_Data::prepare_attributes( $data );
$product_id = absint( $_POST['post_id'] );
$product_type = ! empty( $_POST['product_type'] ) ? wc_clean( $_POST['product_type'] ) : 'simple';
$classname = WC_Product_Factory::get_product_classname( $product_id, $product_type );
$product = new $classname( $product_id );
$product->set_attributes( $attributes );
$product->save();
@ -598,43 +601,40 @@ class WC_AJAX {
$variations = array();
$product = wc_get_product( $post_id );
$attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' );
if ( $product->is_type( 'variable' ) ) {
$attributes = wc_list_pluck( array_filter( $product->get_attributes(), 'wc_attributes_array_filter_variation' ), 'get_slugs' );
if ( ! empty( $attributes ) ) {
// Get existing variations so we don't create duplicates.
$existing_variations = array_map( 'wc_get_product', $product->get_children() );
$existing_attributes = array();
if ( ! empty( $attributes ) ) {
// Get existing variations so we don't create duplicates.
$existing_variations = array_map( 'wc_get_product', $product->get_children() );
$existing_attributes = array();
foreach ( $existing_variations as $existing_variation ) {
$existing_attributes[] = $existing_variation->get_attributes();
}
$added = 0;
$possible_attributes = wc_array_cartesian( $attributes );
foreach ( $possible_attributes as $possible_attribute ) {
if ( in_array( $possible_attribute, $existing_attributes ) ) {
continue;
}
$variation = new WC_Product_Variation();
$variation->set_parent_id( $post_id );
$variation->set_attributes( $possible_attribute );
do_action( 'product_variation_linked', $variation->save() );
if ( ( $added ++ ) > WC_MAX_LINKED_VARIATIONS ) {
break;
}
}
echo $added;
foreach ( $existing_variations as $existing_variation ) {
$existing_attributes[] = $existing_variation->get_attributes();
}
$data_store = $product->get_data_store();
$data_store->sort_all_product_variations( $product->get_id() );
$added = 0;
$possible_attributes = wc_array_cartesian( $attributes );
foreach ( $possible_attributes as $possible_attribute ) {
if ( in_array( $possible_attribute, $existing_attributes ) ) {
continue;
}
$variation = new WC_Product_Variation();
$variation->set_parent_id( $post_id );
$variation->set_attributes( $possible_attribute );
do_action( 'product_variation_linked', $variation->save() );
if ( ( $added ++ ) > WC_MAX_LINKED_VARIATIONS ) {
break;
}
}
echo $added;
}
$data_store = $product->get_data_store();
$data_store->sort_all_product_variations( $product->get_id() );
die();
}

View File

@ -21,35 +21,33 @@ class WC_Product_Factory {
* Get a product.
*
* @param mixed $product_id (default: false)
* @param array $deprecated
* @param array $deprecated Previously used to pass arguments to the factory, e.g. to force a type.
* @return WC_Product|bool Product object or null if the product cannot be loaded.
*/
public function get_product( $product_id = false, $deprecated = array() ) {
$product_id = $this->get_product_id( $product_id );
if ( ! $product_id ) {
if ( ! $product_id = $this->get_product_id( $product_id ) ) {
return false;
}
$product_type = $this->get_product_type( $product_id );
$classname = $this->get_classname_from_product_type( $product_type );
// backwards compat filter
$post_type = 'variation' === $product_type ? 'product_variation' : 'product';
$classname = apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
// Backwards compatibility.
if ( ! empty( $deprecated ) ) {
wc_deprecated_argument( 'args', '2.7', 'Passing args to the product factory is deprecated. If you need to force a type, construct the product class directly.' );
if ( ! $classname ) {
return false;
if ( isset( $deprecated['product_type'] ) ) {
$product_type = $this->get_classname_from_product_type( $deprecated['product_type'] );
}
}
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
$classname = $this->get_product_classname( $product_id, $product_type );
try {
// Try to get from cache, otherwise create a new object,
$product = wp_cache_get( 'product-' . $product_id, 'products' );
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = new $classname( $product_id );
$product = new $classname( $product_id, $deprecated );
wp_cache_set( 'product-' . $product_id, $product, 'products' );
}
@ -59,6 +57,24 @@ class WC_Product_Factory {
}
}
/**
* Gets a product classname and allows filtering. Returns WC_Product_Simple if the class does not exist.
*
* @since 2.7.0
* @param int $product_id
* @param string $product_type
* @return string
*/
public static function get_product_classname( $product_id, $product_type ) {
$classname = apply_filters( 'woocommerce_product_class', self::get_classname_from_product_type( $product_type ), $product_type, 'variation' === $product_type ? 'product_variation' : 'product', $product_id );
if ( ! $classname || ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
return $classname;
}
/**
* Get the product type for a product.
*
@ -87,6 +103,7 @@ class WC_Product_Factory {
/**
* Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class.
*
* @param string $product_type
* @return string|false
*/

View File

@ -93,7 +93,7 @@ function wc_get_products( $args ) {
* @since 2.2.0
*
* @param mixed $the_product Post object or post ID of the product.
* @param array $deprecated
* @param array $deprecated Previously used to pass arguments to the factory, e.g. to force a type.
* @return WC_Product|null
*/
function wc_get_product( $the_product = false, $deprecated = array() ) {
@ -101,7 +101,10 @@ function wc_get_product( $the_product = false, $deprecated = array() ) {
wc_doing_it_wrong( __FUNCTION__, __( 'wc_get_product should not be called before the woocommerce_init action.', 'woocommerce' ), '2.5' );
return false;
}
return WC()->product_factory->get_product( $the_product );
if ( ! empty( $deprecated ) ) {
wc_deprecated_argument( 'args', '2.7', 'Passing args to wc_get_product is deprecated. If you need to force a type, construct the product class directly.' );
}
return WC()->product_factory->get_product( $the_product, $deprecated );
}
/**