Skip to content

Commit 01f5a09

Browse files
Merge pull request woocommerce#23338 from woocommerce/update/23335
More useful actions in during CRUD save events
2 parents 55692cb + 47eee01 commit 01f5a09

14 files changed

+224
-125
lines changed

includes/abstracts/abstract-wc-data.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,32 @@ public function delete( $force_delete = false ) {
200200
* @return int
201201
*/
202202
public function save() {
203-
if ( $this->data_store ) {
204-
// Trigger action before saving to the DB. Allows you to adjust object props before save.
205-
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
203+
if ( ! $this->data_store ) {
204+
return $this->get_id();
205+
}
206206

207-
if ( $this->get_id() ) {
208-
$this->data_store->update( $this );
209-
} else {
210-
$this->data_store->create( $this );
211-
}
207+
/**
208+
* Trigger action before saving to the DB. Allows you to adjust object props before save.
209+
*
210+
* @param WC_Data $this The object being saved.
211+
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
212+
*/
213+
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
214+
215+
if ( $this->get_id() ) {
216+
$this->data_store->update( $this );
217+
} else {
218+
$this->data_store->create( $this );
212219
}
220+
221+
/**
222+
* Trigger action after saving to the DB.
223+
*
224+
* @param WC_Data $this The object being saved.
225+
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
226+
*/
227+
do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
228+
213229
return $this->get_id();
214230
}
215231

includes/abstracts/abstract-wc-order.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,59 @@ public function get_data() {
165165
* @return int order ID
166166
*/
167167
public function save() {
168-
if ( $this->data_store ) {
169-
// Trigger action before saving to the DB. Allows you to adjust object props before save.
168+
if ( ! $this->data_store ) {
169+
return $this->get_id();
170+
}
171+
172+
try {
173+
/**
174+
* Trigger action before saving to the DB. Allows you to adjust object props before save.
175+
*
176+
* @param WC_Data $this The object being saved.
177+
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
178+
*/
170179
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
171180

172181
if ( $this->get_id() ) {
173182
$this->data_store->update( $this );
174183
} else {
175184
$this->data_store->create( $this );
176185
}
186+
187+
$this->save_items();
188+
189+
/**
190+
* Trigger action after saving to the DB.
191+
*
192+
* @param WC_Data $this The object being saved.
193+
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
194+
*/
195+
do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
196+
197+
} catch ( Exception $e ) {
198+
$this->handle_exception( $e, __( 'Error saving order.', 'woocommerce' ) );
177199
}
178-
$this->save_items();
200+
179201
return $this->get_id();
180202
}
181203

204+
/**
205+
* Log an error about this order is exception is encountered.
206+
*
207+
* @param Exception $e Exception object.
208+
* @param string $message Message regarding exception thrown.
209+
* @since 3.7.0
210+
*/
211+
protected function handle_exception( $e, $message = 'Error' ) {
212+
wc_get_logger()->error(
213+
$message,
214+
array(
215+
'order' => $this,
216+
'error' => $e,
217+
)
218+
);
219+
}
220+
182221
/**
183222
* Save all order items which are part of this order.
184223
*/

includes/abstracts/abstract-wc-product.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,19 +1360,36 @@ public function validate_props() {
13601360
public function save() {
13611361
$this->validate_props();
13621362

1363-
if ( $this->data_store ) {
1364-
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
1365-
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
1363+
if ( ! $this->data_store ) {
1364+
return $this->get_id();
1365+
}
13661366

1367-
if ( $this->get_id() ) {
1368-
$this->data_store->update( $this );
1369-
} else {
1370-
$this->data_store->create( $this );
1371-
}
1372-
if ( $this->get_parent_id() ) {
1373-
wc_deferred_product_sync( $this->get_parent_id() );
1374-
}
1367+
/**
1368+
* Trigger action before saving to the DB. Allows you to adjust object props before save.
1369+
*
1370+
* @param WC_Data $this The object being saved.
1371+
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
1372+
*/
1373+
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
1374+
1375+
if ( $this->get_id() ) {
1376+
$this->data_store->update( $this );
1377+
} else {
1378+
$this->data_store->create( $this );
13751379
}
1380+
1381+
if ( $this->get_parent_id() ) {
1382+
wc_deferred_product_sync( $this->get_parent_id() );
1383+
}
1384+
1385+
/**
1386+
* Trigger action after saving to the DB.
1387+
*
1388+
* @param WC_Data $this The object being saved.
1389+
* @param WC_Data_Store_WP $data_store THe data store persisting the data.
1390+
*/
1391+
do_action( 'woocommerce_after_' . $this->object_type . '_object_save', $this, $this->data_store );
1392+
13761393
return $this->get_id();
13771394
}
13781395

includes/class-wc-customer-download.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -332,32 +332,6 @@ public function track_download( $user_id = null, $user_ip_address = null ) {
332332
$download_log->save();
333333
}
334334

335-
/*
336-
|--------------------------------------------------------------------------
337-
| CRUD methods
338-
|--------------------------------------------------------------------------
339-
*/
340-
341-
/**
342-
* Save data to the database.
343-
*
344-
* @since 3.0.0
345-
* @return int Item ID
346-
*/
347-
public function save() {
348-
if ( $this->data_store ) {
349-
// Trigger action before saving to the DB. Use a pointer to adjust object props before save.
350-
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
351-
352-
if ( $this->get_id() ) {
353-
$this->data_store->update( $this );
354-
} else {
355-
$this->data_store->create( $this );
356-
}
357-
}
358-
return $this->get_id();
359-
}
360-
361335
/*
362336
|--------------------------------------------------------------------------
363337
| ArrayAccess/Backwards compatibility.

includes/class-wc-order.php

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ public function payment_complete( $transaction_id = '' ) {
128128
*/
129129
$logger = wc_get_logger();
130130
$logger->error(
131-
sprintf( 'Error completing payment for order #%d', $this->get_id() ), array(
131+
sprintf(
132+
'Error completing payment for order #%d',
133+
$this->get_id()
134+
),
135+
array(
132136
'order' => $this,
133137
'error' => $e,
134138
)
@@ -175,7 +179,7 @@ public function get_formatted_order_total( $tax_display = '', $display_refunded
175179
}
176180

177181
if ( $total_refunded && $display_refunded ) {
178-
$formatted_total = '<del>' . strip_tags( $formatted_total ) . '</del> <ins>' . wc_price( $order_total - $total_refunded, array( 'currency' => $this->get_currency() ) ) . $tax_string . '</ins>';
182+
$formatted_total = '<del>' . wp_strip_all_tags( $formatted_total ) . '</del> <ins>' . wc_price( $order_total - $total_refunded, array( 'currency' => $this->get_currency() ) ) . $tax_string . '</ins>';
179183
} else {
180184
$formatted_total .= $tax_string;
181185
}
@@ -212,36 +216,31 @@ public function get_formatted_order_total( $tax_display = '', $display_refunded
212216
* @return int order ID
213217
*/
214218
public function save() {
215-
try {
216-
$this->maybe_set_user_billing_email();
217-
218-
if ( $this->data_store ) {
219-
// Trigger action before saving to the DB. Allows you to adjust object props before save.
220-
do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store );
221-
222-
if ( $this->get_id() ) {
223-
$this->data_store->update( $this );
224-
} else {
225-
$this->data_store->create( $this );
226-
}
227-
}
228-
229-
$this->save_items();
230-
$this->status_transition();
231-
} catch ( Exception $e ) {
232-
$logger = wc_get_logger();
233-
$logger->error(
234-
sprintf( 'Error saving order #%d', $this->get_id() ), array(
235-
'order' => $this,
236-
'error' => $e,
237-
)
238-
);
239-
$this->add_order_note( __( 'Error saving order.', 'woocommerce' ) . ' ' . $e->getMessage() );
240-
}
219+
$this->maybe_set_user_billing_email();
220+
parent::save();
221+
$this->status_transition();
241222

242223
return $this->get_id();
243224
}
244225

226+
/**
227+
* Log an error about this order is exception is encountered.
228+
*
229+
* @param Exception $e Exception object.
230+
* @param string $message Message regarding exception thrown.
231+
* @since 3.7.0
232+
*/
233+
protected function handle_exception( $e, $message = 'Error' ) {
234+
wc_get_logger()->error(
235+
$message,
236+
array(
237+
'order' => $this,
238+
'error' => $e,
239+
)
240+
);
241+
$this->add_order_note( $message . ' ' . $e->getMessage() );
242+
}
243+
245244
/**
246245
* Set order status.
247246
*
@@ -335,7 +334,11 @@ public function update_status( $new_status, $note = '', $manual = false ) {
335334
} catch ( Exception $e ) {
336335
$logger = wc_get_logger();
337336
$logger->error(
338-
sprintf( 'Error updating status for order #%d', $this->get_id() ), array(
337+
sprintf(
338+
'Error updating status for order #%d',
339+
$this->get_id()
340+
),
341+
array(
339342
'order' => $this,
340343
'error' => $e,
341344
)
@@ -375,7 +378,11 @@ protected function status_transition() {
375378
} catch ( Exception $e ) {
376379
$logger = wc_get_logger();
377380
$logger->error(
378-
sprintf( 'Status transition of order #%d errored!', $this->get_id() ), array(
381+
sprintf(
382+
'Status transition of order #%d errored!',
383+
$this->get_id()
384+
),
385+
array(
379386
'order' => $this,
380387
'error' => $e,
381388
)
@@ -1513,7 +1520,8 @@ public function get_checkout_payment_url( $on_checkout = false ) {
15131520
array(
15141521
'pay_for_order' => 'true',
15151522
'key' => $this->get_order_key(),
1516-
), $pay_url
1523+
),
1524+
$pay_url
15171525
);
15181526
}
15191527

@@ -1545,15 +1553,18 @@ public function get_checkout_order_received_url() {
15451553
*/
15461554
public function get_cancel_order_url( $redirect = '' ) {
15471555
return apply_filters(
1548-
'woocommerce_get_cancel_order_url', wp_nonce_url(
1556+
'woocommerce_get_cancel_order_url',
1557+
wp_nonce_url(
15491558
add_query_arg(
15501559
array(
15511560
'cancel_order' => 'true',
15521561
'order' => $this->get_order_key(),
15531562
'order_id' => $this->get_id(),
15541563
'redirect' => $redirect,
1555-
), $this->get_cancel_endpoint()
1556-
), 'woocommerce-cancel_order'
1564+
),
1565+
$this->get_cancel_endpoint()
1566+
),
1567+
'woocommerce-cancel_order'
15571568
)
15581569
);
15591570
}
@@ -1566,14 +1577,16 @@ public function get_cancel_order_url( $redirect = '' ) {
15661577
*/
15671578
public function get_cancel_order_url_raw( $redirect = '' ) {
15681579
return apply_filters(
1569-
'woocommerce_get_cancel_order_url_raw', add_query_arg(
1580+
'woocommerce_get_cancel_order_url_raw',
1581+
add_query_arg(
15701582
array(
15711583
'cancel_order' => 'true',
15721584
'order' => $this->get_order_key(),
15731585
'order_id' => $this->get_id(),
15741586
'redirect' => $redirect,
15751587
'_wpnonce' => wp_create_nonce( 'woocommerce-cancel_order' ),
1576-
), $this->get_cancel_endpoint()
1588+
),
1589+
$this->get_cancel_endpoint()
15771590
)
15781591
);
15791592
}
@@ -1669,7 +1682,8 @@ public function add_order_note( $note, $is_customer_note = 0, $added_by_user = f
16691682
add_comment_meta( $comment_id, 'is_customer_note', 1 );
16701683

16711684
do_action(
1672-
'woocommerce_new_customer_note', array(
1685+
'woocommerce_new_customer_note',
1686+
array(
16731687
'order_id' => $this->get_id(),
16741688
'customer_note' => $commentdata['comment_content'],
16751689
)

0 commit comments

Comments
 (0)