From 7e16b39bbb7cae51c79b4e3fdf3cb2f93ded4d81 Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 3 Mar 2023 09:48:45 +0200 Subject: [PATCH] Remove shortcodes from description --- .../src/Factory/ItemFactory.php | 15 +++++++++-- .../ApiClient/Factory/ItemFactoryTest.php | 26 ++++++++----------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/modules/ppcp-api-client/src/Factory/ItemFactory.php b/modules/ppcp-api-client/src/Factory/ItemFactory.php index c71e7ddfa..0c5e9b542 100644 --- a/modules/ppcp-api-client/src/Factory/ItemFactory.php +++ b/modules/ppcp-api-client/src/Factory/ItemFactory.php @@ -58,7 +58,7 @@ class ItemFactory { mb_substr( $product->get_name(), 0, 127 ), new Money( $price, $this->currency ), $quantity, - substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ) ?: '', + $this->prepare_description( $product->get_description() ), null, $product->get_sku(), ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS @@ -130,7 +130,7 @@ class ItemFactory { mb_substr( $item->get_name(), 0, 127 ), new Money( $price_without_tax_rounded, $currency ), $quantity, - substr( wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ), 0, 127 ) ?: '', + $product instanceof WC_Product ? $this->prepare_description( $product->get_description() ) : '', null, $product instanceof WC_Product ? $product->get_sku() : '', ( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS @@ -198,4 +198,15 @@ class ItemFactory { $category ); } + + /** + * Cleanups the description and prepares it 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 ) ?: ''; + } } diff --git a/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php b/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php index 640dffcdf..3f72dab07 100644 --- a/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php +++ b/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php @@ -43,9 +43,8 @@ class ItemFactoryTest extends TestCase ->expects('get_cart_contents') ->andReturn($items); - expect('wp_strip_all_tags') - ->with('description') - ->andReturn('description'); + expect('wp_strip_all_tags')->andReturnFirstArg(); + expect('strip_shortcodes')->andReturnFirstArg(); $woocommerce = Mockery::mock(\WooCommerce::class); $session = Mockery::mock(\WC_Session::class); @@ -99,9 +98,8 @@ class ItemFactoryTest extends TestCase ->expects('get_cart_contents') ->andReturn($items); - expect('wp_strip_all_tags') - ->with('description') - ->andReturn('description'); + expect('wp_strip_all_tags')->andReturnFirstArg(); + expect('strip_shortcodes')->andReturnFirstArg(); $woocommerce = Mockery::mock(\WooCommerce::class); $session = Mockery::mock(\WC_Session::class); @@ -130,9 +128,9 @@ class ItemFactoryTest extends TestCase $product ->expects('is_virtual') ->andReturn(false); - expect('wp_strip_all_tags') - ->with('description') - ->andReturn('description'); + + expect('wp_strip_all_tags')->andReturnFirstArg(); + expect('strip_shortcodes')->andReturnFirstArg(); $item = Mockery::mock(\WC_Order_Item_Product::class); $item @@ -190,9 +188,8 @@ class ItemFactoryTest extends TestCase ->expects('is_virtual') ->andReturn(true); - expect('wp_strip_all_tags') - ->with('description') - ->andReturn('description'); + expect('wp_strip_all_tags')->andReturnFirstArg(); + expect('strip_shortcodes')->andReturnFirstArg(); $item = Mockery::mock(\WC_Order_Item_Product::class); $item @@ -245,9 +242,8 @@ class ItemFactoryTest extends TestCase ->expects('is_virtual') ->andReturn(true); - expect('wp_strip_all_tags') - ->with($description) - ->andReturn(mb_substr( $description, 0, 127 )); + expect('wp_strip_all_tags')->andReturnFirstArg(); + expect('strip_shortcodes')->andReturnFirstArg(); $item = Mockery::mock(\WC_Order_Item_Product::class); $item