EmailPreview.php
4 weeks ago
EmailPreviewRestController.php
4 weeks ago
PreviewOrder.php
4 weeks ago
PreviewOrder.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Non-persistent WC_Order used for email preview. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin\EmailPreview; |
| 9 | |
| 10 | use WC_Order; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * WC_Order subclass for the email preview. |
| 16 | * |
| 17 | * The preview needs an order to render, but it must never read from or write |
| 18 | * to a real order in the database. The key to that is the id: order methods |
| 19 | * that touch the database key off get_id() and resolve to nothing when it is |
| 20 | * 0 (add_order_note(), remove_order_items(), get_refunds(), the refund totals, |
| 21 | * item/meta reads, etc.). So this order keeps id 0 and exposes the preview's |
| 22 | * display number through get_order_number() instead. |
| 23 | * |
| 24 | * Two cases aren't safe at id 0 and are handled explicitly: |
| 25 | * - save() and its siblings would insert a new row, so they are no-ops. |
| 26 | * - get_customer_order_notes() passes the id to get_comments(), which treats |
| 27 | * 0 as "no filter" and would return every order note on the site, so it is |
| 28 | * overridden to return nothing. |
| 29 | * |
| 30 | * The item/meta caches are pre-filled as empty too, as a guard against any |
| 31 | * future read path that doesn't check the id first. |
| 32 | */ |
| 33 | class PreviewOrder extends WC_Order { |
| 34 | |
| 35 | /** |
| 36 | * The order number shown in the preview. Not a real order id, so it can't |
| 37 | * collide with a row in the database. |
| 38 | */ |
| 39 | const PREVIEW_ORDER_NUMBER = '12345'; |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | * |
| 44 | * @param int|object|WC_Order $order Order to read. Defaults to 0 (a new, empty order). |
| 45 | */ |
| 46 | public function __construct( $order = 0 ) { |
| 47 | parent::__construct( $order ); |
| 48 | |
| 49 | foreach ( $this->item_types_to_group as $group ) { |
| 50 | $this->items[ $group ] = array(); |
| 51 | } |
| 52 | $this->meta_data = array(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the order number to display. |
| 57 | * |
| 58 | * The real id stays 0, so this provides a representative number for the |
| 59 | * preview without tying the order to a database row. |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function get_order_number() { |
| 64 | return self::PREVIEW_ORDER_NUMBER; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Block save(). A preview order should never be written to the database. |
| 69 | * |
| 70 | * @return int The order id (unchanged). |
| 71 | */ |
| 72 | public function save() { |
| 73 | wc_get_logger()->warning( |
| 74 | 'Email preview order save() blocked to prevent writing to the database.', |
| 75 | array( 'source' => 'email-preview' ) |
| 76 | ); |
| 77 | return $this->get_id(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Block save_meta_data(). Extensions sometimes call update_meta_data() |
| 82 | * followed by save_meta_data() directly, bypassing save(). |
| 83 | */ |
| 84 | public function save_meta_data(): void { |
| 85 | // Intentionally empty. |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Block delete(). A preview order has no row to delete. |
| 90 | * |
| 91 | * @param bool $force_delete Should the order be deleted permanently. |
| 92 | * @return bool Always false. |
| 93 | */ |
| 94 | public function delete( $force_delete = false ) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * A preview order has no customer notes. The parent passes the id straight |
| 100 | * to get_comments(), which treats id 0 as "return every order note". |
| 101 | * |
| 102 | * @return array |
| 103 | */ |
| 104 | public function get_customer_order_notes() { |
| 105 | return array(); |
| 106 | } |
| 107 | } |
| 108 |