From 27b8155766e45e37c1fa28dd38c0638a398833cb Mon Sep 17 00:00:00 2001 From: Daniel Dudzic Date: Mon, 11 Mar 2024 19:12:24 +0100 Subject: [PATCH] Product Title: Ensure the product title in addition to the product description gets correctly sanitized and trimmed before getting sent to PayPal --- modules/ppcp-api-client/src/Factory/ItemFactory.php | 10 +++++----- modules/ppcp-api-client/src/Helper/ItemTrait.php | 8 ++++---- modules/ppcp-order-tracking/src/Shipment/Shipment.php | 4 ++-- .../src/SubscriptionsApiHandler.php | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/modules/ppcp-api-client/src/Factory/ItemFactory.php b/modules/ppcp-api-client/src/Factory/ItemFactory.php index e8fc98c86..c2b14d0e1 100644 --- a/modules/ppcp-api-client/src/Factory/ItemFactory.php +++ b/modules/ppcp-api-client/src/Factory/ItemFactory.php @@ -61,10 +61,10 @@ class ItemFactory { $price = (float) $item['line_subtotal'] / (float) $item['quantity']; return new Item( - mb_substr( $product->get_name(), 0, 127 ), + $this->prepare_item_string( $product->get_name() ), new Money( $price, $this->currency ), $quantity, - $this->prepare_description( $product->get_description() ), + $this->prepare_item_string( $product->get_description() ), null, $this->prepare_sku( $product->get_sku() ), ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS, @@ -138,10 +138,10 @@ class ItemFactory { $image = $product instanceof WC_Product ? wp_get_attachment_image_src( (int) $product->get_image_id(), 'full' ) : ''; return new Item( - mb_substr( $item->get_name(), 0, 127 ), + $this->prepare_item_string( $item->get_name() ), new Money( $price_without_tax_rounded, $currency ), $quantity, - $product instanceof WC_Product ? $this->prepare_description( $product->get_description() ) : '', + $product instanceof WC_Product ? $this->prepare_item_string( $product->get_description() ) : '', null, $product instanceof WC_Product ? $this->prepare_sku( $product->get_sku() ) : '', ( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS, @@ -160,7 +160,7 @@ class ItemFactory { */ private function from_wc_order_fee( \WC_Order_Item_Fee $item, \WC_Order $order ): Item { return new Item( - $item->get_name(), + $this->prepare_item_string( $item->get_name() ), new Money( (float) $item->get_amount(), $order->get_currency() ), $item->get_quantity(), '', diff --git a/modules/ppcp-api-client/src/Helper/ItemTrait.php b/modules/ppcp-api-client/src/Helper/ItemTrait.php index 3b1e05bd0..4a93e3914 100644 --- a/modules/ppcp-api-client/src/Helper/ItemTrait.php +++ b/modules/ppcp-api-client/src/Helper/ItemTrait.php @@ -12,14 +12,14 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Helper; trait ItemTrait { /** - * Cleanups the description and prepares it for sending to PayPal. + * Cleans up item strings (title and description for example) and prepares them for sending to PayPal. * * @param string $description Item description. * @return string */ - protected function prepare_description( string $description ): string { - $description = strip_shortcodes( wp_strip_all_tags( $description ) ); - return substr( $description, 0, 127 ) ?: ''; + protected function prepare_item_string( string $string ): string { + $string = strip_shortcodes( wp_strip_all_tags( $string ) ); + return substr( $string, 0, 127 ) ?: ''; } /** diff --git a/modules/ppcp-order-tracking/src/Shipment/Shipment.php b/modules/ppcp-order-tracking/src/Shipment/Shipment.php index 099801874..9661d574c 100644 --- a/modules/ppcp-order-tracking/src/Shipment/Shipment.php +++ b/modules/ppcp-order-tracking/src/Shipment/Shipment.php @@ -169,10 +169,10 @@ class Shipment implements ShipmentInterface { $image = wp_get_attachment_image_src( (int) $product->get_image_id(), 'full' ); $ppcp_order_item = new Item( - mb_substr( $item->get_name(), 0, 127 ), + $this->prepare_item_string( $item->get_name() ), new Money( $price_without_tax_rounded, $currency ), $quantity, - $this->prepare_description( $product->get_description() ), + $this->prepare_item_string( $product->get_description() ), null, $this->prepare_sku( $product->get_sku() ), $product->is_virtual() ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS, diff --git a/modules/ppcp-paypal-subscriptions/src/SubscriptionsApiHandler.php b/modules/ppcp-paypal-subscriptions/src/SubscriptionsApiHandler.php index 4d56558ab..2109f13d2 100644 --- a/modules/ppcp-paypal-subscriptions/src/SubscriptionsApiHandler.php +++ b/modules/ppcp-paypal-subscriptions/src/SubscriptionsApiHandler.php @@ -114,7 +114,7 @@ class SubscriptionsApiHandler { */ public function create_product( WC_Product $product ) { try { - $subscription_product = $this->products_endpoint->create( $product->get_title(), $this->prepare_description( $product->get_description() ) ); + $subscription_product = $this->products_endpoint->create( $this->prepare_item_string( $product->get_title() ), $this->prepare_item_string( $product->get_description() ) ); $product->update_meta_data( 'ppcp_subscription_product', $subscription_product->to_array() ); $product->save(); } catch ( RuntimeException $exception ) { @@ -169,7 +169,7 @@ class SubscriptionsApiHandler { $catalog_product_name = $catalog_product->name() ?: ''; $catalog_product_description = $catalog_product->description() ?: ''; - $wc_product_description = $this->prepare_description( $product->get_description() ) ?: $product->get_title(); + $wc_product_description = $this->prepare_item_string( $product->get_description() ) ?: $this->prepare_item_string( $product->get_title() ); if ( $catalog_product_name !== $product->get_title() || $catalog_product_description !== $wc_product_description ) { $data = array();