Emails
2 months ago
Integrations
2 months ago
MultichannelMarketing
2 years ago
TransactionalEmails
10 months ago
WooCommerceBookings
1 month ago
WooCommerceSubscriptions
2 months ago
CouponPreProcessor.php
2 months ago
Emails.php
8 months ago
Helper.php
1 month ago
MailPoetTask.php
2 years ago
NonPersistablePreviewData.php
1 month ago
OrderAttributionFields.php
1 month ago
OrderAttributionRevenueReader.php
2 weeks ago
OrderAttributionWriter.php
1 month ago
RandomCouponCodeGenerator.php
2 months ago
Settings.php
1 year ago
SubscriberEngagement.php
3 years ago
Subscription.php
2 months ago
Tracker.php
2 years ago
TransactionalEmailHooks.php
1 year ago
TransactionalEmails.php
1 year ago
WooSystemInfo.php
1 month ago
WooSystemInfoController.php
1 year ago
index.php
3 years ago
NonPersistablePreviewData.php
81 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WooCommerce; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Logging\LoggerFactory; |
| 9 | |
| 10 | /** |
| 11 | * Makes an in-memory WooCommerce data object (order, customer, product, ...) |
| 12 | * safe to use as email or automation preview sample data. |
| 13 | * |
| 14 | * Preview helpers assign the object a placeholder ID (e.g. 12345) so templates |
| 15 | * render realistic content. Without this trait two things go wrong if a hook or |
| 16 | * integration touches the object while the preview renders: |
| 17 | * |
| 18 | * - save() / save_meta_data() / delete() treat the placeholder ID as an |
| 19 | * existing record and overwrite or remove the real order/customer 12345. |
| 20 | * - read_meta_data() lazy-loads the real record's meta from the database, |
| 21 | * leaking it into the preview. |
| 22 | * |
| 23 | * Mixing this trait into a thin subclass of the WooCommerce type turns those |
| 24 | * operations into safe no-ops. |
| 25 | */ |
| 26 | trait NonPersistablePreviewData { |
| 27 | // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- declares the WC_Data::get_id() dependency. |
| 28 | abstract public function get_id(); |
| 29 | |
| 30 | /** |
| 31 | * Accepts an optional argument so the signature stays compatible with |
| 32 | * WooCommerce types whose save() takes a parameter (e.g. WC_Booking::save($status_transition)). |
| 33 | * |
| 34 | * @param bool $statusTransition Ignored; nothing is written to the database. |
| 35 | * @return int|string The placeholder ID; nothing is written to the database. |
| 36 | */ |
| 37 | public function save($statusTransition = true) { |
| 38 | $this->logPreviewPersistAttempt('save'); |
| 39 | return $this->get_id(); |
| 40 | } |
| 41 | |
| 42 | public function save_meta_data() { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- overrides WC_Data::save_meta_data(). |
| 43 | $this->logPreviewPersistAttempt('save_meta_data'); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param bool $forceDelete |
| 48 | * @return bool Always false; preview data is never removed from the database. |
| 49 | */ |
| 50 | public function delete($forceDelete = false) { |
| 51 | $this->logPreviewPersistAttempt('delete'); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Prevents lazy-loading the real record's meta for the placeholder ID. |
| 57 | * |
| 58 | * @param bool $forceRead |
| 59 | */ |
| 60 | public function read_meta_data($forceRead = false) { // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps -- overrides WC_Data::read_meta_data(). |
| 61 | if (!is_array($this->meta_data)) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 62 | $this->meta_data = []; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private function logPreviewPersistAttempt(string $method): void { |
| 67 | try { |
| 68 | LoggerFactory::getInstance()->getLogger(LoggerFactory::TOPIC_EMAIL_EDITOR)->error( |
| 69 | sprintf( |
| 70 | 'Blocked %s() on preview-only WooCommerce data (%s, ID %s). An integration tried to persist email-preview sample data.', |
| 71 | $method, |
| 72 | static::class, |
| 73 | (string)$this->get_id() |
| 74 | ) |
| 75 | ); |
| 76 | } catch (\Throwable $e) { |
| 77 | // Ignore: logging failures must not affect preview rendering. |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |