Product data store loop protection

This commit is contained in:
Mike Jolley 2017-04-15 21:04:41 +01:00
parent a2e0535f2f
commit 27c9006f77
1 changed files with 12 additions and 2 deletions

View File

@ -168,7 +168,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// Only update the post when the post data changes.
if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug' ), array_keys( $changes ) ) ) {
$post_data = array(
'ID' => $product->get_id(),
'post_content' => $product->get_description( 'edit' ),
'post_excerpt' => $product->get_short_description( 'edit' ),
'post_title' => $product->get_name( 'edit' ),
@ -189,7 +188,18 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$post_data['post_modified'] = current_time( 'mysql' );
$post_data['post_modified_gmt'] = current_time( 'mysql', 1 );
}
wp_update_post( $post_data );
/**
* When updating this object, to prevent infinite loops, use WPDB when doing an update during save_post action.
*
* This ensures hooks are fired by either WP itself (admin screen save), or an update purely from CRUD.
*/
if ( doing_action( 'save_post' ) ) {
$GLOBALS['wpdb']->update( $wpdb->posts, $post_data, array( 'ID' => $product->get_id() ) );
clean_post_cache( $product->get_id() );
} else {
wp_update_post( array_merge( array( 'ID' => $product->get_id() ), $post_data ) );
}
$product->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook.
}