From af79e4522744ec74b8a6cbaf5bf64ca7c75e02a9 Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:29:52 -0500 Subject: [PATCH 01/10] Add unit tests for get_fb_description --- tests/Unit/fbproductTest.php | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/Unit/fbproductTest.php diff --git a/tests/Unit/fbproductTest.php b/tests/Unit/fbproductTest.php new file mode 100644 index 000000000..11b870951 --- /dev/null +++ b/tests/Unit/fbproductTest.php @@ -0,0 +1,83 @@ +set_description( 'fb description' ); + $description = $facebook_product->get_fb_description(); + + $this->assertEquals( $description, 'fb description'); + } + + /** + * Test it gets description from parent product if it is a variation. + * @return void + */ + public function test_get_fb_description_variable_product() { + $variable_product = WC_Helper_Product::create_variation_product(); + $variable_product->set_description('parent description'); + $variable_product->save(); + + $parent_fb_product = new \WC_Facebook_Product($variable_product); + $variation = wc_get_product($variable_product->get_children()[0]); + + $facebook_product = new \WC_Facebook_Product( $variation, $parent_fb_product ); + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, 'parent description' ); + + $variation->set_description( 'variation description' ); + $variation->save(); + + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, 'variation description' ); + } + + /** + * Tests that if no description is found from meta or variation, it gets description from post + * + * @return void + */ + public function test_get_fb_description_from_post_content() { + $product = WC_Helper_Product::create_simple_product(); + + // Gets description from title + $facebook_product = new \WC_Facebook_Product( $product ); + $description = $facebook_product->get_fb_description(); + + $this->assertEquals( $description, get_post( $product->get_id() )->post_title ); + + // Gets description from excerpt (product short description) + $product->set_short_description( 'short description' ); + $product->save(); + + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, get_post( $product->get_id() )->post_excerpt ); + + // Gets description from content (product description) + $product->set_description( 'product description' ); + $product->save(); + + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, get_post( $product->get_id() )->post_content ); + + // Gets description from excerpt ignoring content when short mode is set + add_option( + WC_Facebookcommerce_Integration::SETTING_PRODUCT_DESCRIPTION_MODE, + WC_Facebookcommerce_Integration::PRODUCT_DESCRIPTION_MODE_SHORT + ); + + $facebook_product = new \WC_Facebook_Product( $product ); + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, get_post( $product->get_id() )->post_excerpt ); + } +} From c9b9135ec8c77be477c71438cf9d8e4f12475f69 Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:30:54 -0500 Subject: [PATCH 02/10] Refactor get_fb_description to have a single return point so that we can add a filter on it. --- includes/fbproduct.php | 67 ++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/includes/fbproduct.php b/includes/fbproduct.php index b38dd16a1..d3e05ef46 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -391,53 +391,50 @@ public function set_use_parent_image( $setting ) { } public function get_fb_description() { + $description = ''; + if ( $this->fb_description ) { - return $this->fb_description; + $description = $this->fb_description; } - $description = get_post_meta( - $this->id, - self::FB_PRODUCT_DESCRIPTION, - true - ); - - if ( $description ) { - return $description; + if ( empty( $description ) ) { + // Try to get description from post meta + $description = get_post_meta( + $this->id, + self::FB_PRODUCT_DESCRIPTION, + true + ); } - if ( WC_Facebookcommerce_Utils::is_variation_type( $this->woo_product->get_type() ) ) { - + // Check if the product type is a variation and no description is found yet + if ( empty( $description ) && WC_Facebookcommerce_Utils::is_variation_type( $this->woo_product->get_type() ) ) { $description = WC_Facebookcommerce_Utils::clean_string( $this->woo_product->get_description() ); - if ( $description ) { - return $description; - } - if ( $this->main_description ) { - return $this->main_description; + // Fallback to main description + if ( empty( $description ) && $this->main_description ) { + $description = $this->main_description; } } - $post = $this->get_post_data(); + // If no description is found from meta or variation, get from post + if ( empty( $description ) ) { + $post = $this->get_post_data(); + $post_content = WC_Facebookcommerce_Utils::clean_string( $post->post_content ); + $post_excerpt = WC_Facebookcommerce_Utils::clean_string( $post->post_excerpt ); + $post_title = WC_Facebookcommerce_Utils::clean_string( $post->post_title ); - $post_content = WC_Facebookcommerce_Utils::clean_string( - $post->post_content - ); - $post_excerpt = WC_Facebookcommerce_Utils::clean_string( - $post->post_excerpt - ); - $post_title = WC_Facebookcommerce_Utils::clean_string( - $post->post_title - ); + // Prioritize content, then excerpt, then title + if ( ! empty( $post_content ) ) { + $description = $post_content; + } - // Sanitize description - if ( $post_content ) { - $description = $post_content; - } - if ( $this->sync_short_description || ( $description == '' && $post_excerpt ) ) { - $description = $post_excerpt; - } - if ( $description == '' ) { - $description = $post_title; + if ( $this->sync_short_description || ( empty( $description ) && ! empty( $post_excerpt ) ) ) { + $description = $post_excerpt; + } + + if ( empty( $description ) ) { + $description = $post_title; + } } return $description; From c7c3d08648069e165d96944f394aa6f2a0fb823e Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Tue, 25 Jun 2024 17:14:23 -0500 Subject: [PATCH 03/10] Add filter facebook_for_woocommerce_fb_product_description to filter the return of get_fb_description --- includes/fbproduct.php | 11 +++++++++-- tests/Unit/fbproductTest.php | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/includes/fbproduct.php b/includes/fbproduct.php index d3e05ef46..eba52b958 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -436,8 +436,15 @@ public function get_fb_description() { $description = $post_title; } } - - return $description; + /** + * Filters the FB product description. + * + * @since x.x.x + * + * @param string $description Facebook product description. + * @param int $id WooCommerce Product ID. + */ + return apply_filters( 'facebook_for_woocommerce_fb_product_description', $description, $this->id ); } /** diff --git a/tests/Unit/fbproductTest.php b/tests/Unit/fbproductTest.php index 11b870951..6d8d7b730 100644 --- a/tests/Unit/fbproductTest.php +++ b/tests/Unit/fbproductTest.php @@ -80,4 +80,27 @@ public function test_get_fb_description_from_post_content() { $description = $facebook_product->get_fb_description(); $this->assertEquals( $description, get_post( $product->get_id() )->post_excerpt ); } + + /** + * Test it filters description. + * @return void + */ + public function test_filter_fb_description() { + $product = WC_Helper_Product::create_simple_product(); + $facebook_product = new \WC_Facebook_Product( $product ); + $facebook_product->set_description( 'fb description' ); + + add_filter( 'facebook_for_woocommerce_fb_product_description', function( $description ) { + return 'filtered description'; + }); + + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, 'filtered description' ); + + remove_all_filters( 'facebook_for_woocommerce_fb_product_description' ); + + $description = $facebook_product->get_fb_description(); + $this->assertEquals( $description, 'fb description' ); + + } } From 0201e23533f38a8bb697323f131f5610621a03da Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Thu, 11 Jul 2024 14:44:23 +0200 Subject: [PATCH 04/10] Align PHP require version to composer.json --- facebook-for-woocommerce.php | 2 +- readme.txt | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/facebook-for-woocommerce.php b/facebook-for-woocommerce.php index 4770f033f..1407f26d1 100644 --- a/facebook-for-woocommerce.php +++ b/facebook-for-woocommerce.php @@ -12,7 +12,7 @@ * Author: Facebook * Author URI: https://www.facebook.com/ * Version: 3.2.5 - * Requires at least: 5.6 + * Requires at least: 7.4 * Text Domain: facebook-for-woocommerce * Requires Plugins: woocommerce * Tested up to: 6.6 diff --git a/readme.txt b/readme.txt index 58b572b7a..9a2726802 100644 --- a/readme.txt +++ b/readme.txt @@ -2,9 +2,9 @@ Contributors: facebook, automattic, woothemes Tags: facebook, woocommerce, marketing, product catalog feed, pixel Requires at least: 4.4 -Tested up to: 6.6 -Stable tag: 3.2.5 -Requires PHP: 5.6 or greater +Tested up to: 6.6 +Stable tag: 3.2.5 +Requires PHP: 7.4 MySQL: 5.6 or greater License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html @@ -40,7 +40,7 @@ When opening a bug on GitHub, please give us as many details as possible. == Changelog == -= 3.2.5 - 2024-07-10 = += 3.2.5 - 2024-07-10 = * Tweak - WC 9.1 compatibility. * Tweak - WP 6.6 compatibility. From 166eb16d7a102ee971b72a9ee91146cf299eedef Mon Sep 17 00:00:00 2001 From: Rodrigue Date: Thu, 11 Jul 2024 15:29:35 +0200 Subject: [PATCH 05/10] Correct requires header Co-authored-by: Kader Ibrahim S --- facebook-for-woocommerce.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/facebook-for-woocommerce.php b/facebook-for-woocommerce.php index 1407f26d1..269ece243 100644 --- a/facebook-for-woocommerce.php +++ b/facebook-for-woocommerce.php @@ -12,7 +12,8 @@ * Author: Facebook * Author URI: https://www.facebook.com/ * Version: 3.2.5 - * Requires at least: 7.4 + * Requires at least: 6.4 + * Requires PHP: 7.4 * Text Domain: facebook-for-woocommerce * Requires Plugins: woocommerce * Tested up to: 6.6 From e356a5a997266face82e5f2fd633a99bbdb1dbf2 Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Thu, 11 Jul 2024 15:33:47 +0200 Subject: [PATCH 06/10] Updated Requires at least to 5.6 on readme and plugin file. --- facebook-for-woocommerce.php | 2 +- readme.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/facebook-for-woocommerce.php b/facebook-for-woocommerce.php index 269ece243..00db069fc 100644 --- a/facebook-for-woocommerce.php +++ b/facebook-for-woocommerce.php @@ -12,7 +12,7 @@ * Author: Facebook * Author URI: https://www.facebook.com/ * Version: 3.2.5 - * Requires at least: 6.4 + * Requires at least: 5.6 * Requires PHP: 7.4 * Text Domain: facebook-for-woocommerce * Requires Plugins: woocommerce diff --git a/readme.txt b/readme.txt index 9a2726802..07831d4be 100644 --- a/readme.txt +++ b/readme.txt @@ -1,7 +1,7 @@ === Facebook for WooCommerce === Contributors: facebook, automattic, woothemes Tags: facebook, woocommerce, marketing, product catalog feed, pixel -Requires at least: 4.4 +Requires at least: 5.6 Tested up to: 6.6 Stable tag: 3.2.5 Requires PHP: 7.4 From 270d469243c0bd87e4cae1fded2f640ab0e0d471 Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Wed, 17 Jul 2024 14:42:19 +0200 Subject: [PATCH 07/10] Remove deprecated FILTER_SANITIZE_STRING usage. --- includes/Framework/Helper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/Framework/Helper.php b/includes/Framework/Helper.php index 2b2f6071f..f27ba381a 100644 --- a/includes/Framework/Helper.php +++ b/includes/Framework/Helper.php @@ -137,10 +137,10 @@ public static function str_truncate( $string, $length, $omission = '...' ) { * @return string */ public static function str_to_ascii( $string ) { - // strip ASCII chars 32 and under - $string = filter_var( $string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW ); - // strip ASCII chars 127 and higher - return filter_var( $string, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH ); + // Strip ASCII chars 32 and under + $string = preg_replace('/[\x00-\x1F]/', '', $string); + // Strip ASCII chars 127 and higher + return preg_replace('/[\x7F-\xFF]/', '', $string); } From 611157fd2d67e9f03bb69f7b7479ad0c7a506c6a Mon Sep 17 00:00:00 2001 From: Rodrigue Tusse Date: Wed, 17 Jul 2024 15:04:05 +0200 Subject: [PATCH 08/10] Coding style minor fix. --- includes/Framework/Helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Framework/Helper.php b/includes/Framework/Helper.php index f27ba381a..a17a9416f 100644 --- a/includes/Framework/Helper.php +++ b/includes/Framework/Helper.php @@ -138,9 +138,9 @@ public static function str_truncate( $string, $length, $omission = '...' ) { */ public static function str_to_ascii( $string ) { // Strip ASCII chars 32 and under - $string = preg_replace('/[\x00-\x1F]/', '', $string); + $string = preg_replace( '/[\x00-\x1F]/', '', $string ); // Strip ASCII chars 127 and higher - return preg_replace('/[\x7F-\xFF]/', '', $string); + return preg_replace( '/[\x7F-\xFF]/', '', $string ); } From 8e76f14055784c0a9ea575faf3f2a131bfaf723b Mon Sep 17 00:00:00 2001 From: Justin Palmer <228780+layoutd@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:04:32 -0400 Subject: [PATCH 09/10] woorelease: Product version bump update --- facebook-for-woocommerce.php | 4 ++-- includes/fbproduct.php | 2 +- package-lock.json | 2 +- package.json | 2 +- readme.txt | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/facebook-for-woocommerce.php b/facebook-for-woocommerce.php index 00db069fc..f71c4a5e8 100644 --- a/facebook-for-woocommerce.php +++ b/facebook-for-woocommerce.php @@ -11,7 +11,7 @@ * Description: Grow your business on Facebook! Use this official plugin to help sell more of your products using Facebook. After completing the setup, you'll be ready to create ads that promote your products and you can also create a shop section on your Page where customers can browse your products on Facebook. * Author: Facebook * Author URI: https://www.facebook.com/ - * Version: 3.2.5 + * Version: 3.2.6 * Requires at least: 5.6 * Requires PHP: 7.4 * Text Domain: facebook-for-woocommerce @@ -49,7 +49,7 @@ class WC_Facebook_Loader { /** * @var string the plugin version. This must be in the main plugin file to be automatically bumped by Woorelease. */ - const PLUGIN_VERSION = '3.2.5'; // WRCS: DEFINED_VERSION. + const PLUGIN_VERSION = '3.2.6'; // WRCS: DEFINED_VERSION. // Minimum PHP version required by this plugin. const MINIMUM_PHP_VERSION = '7.4.0'; diff --git a/includes/fbproduct.php b/includes/fbproduct.php index eba52b958..0c9f30963 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -439,7 +439,7 @@ public function get_fb_description() { /** * Filters the FB product description. * - * @since x.x.x + * @since 3.2.6 * * @param string $description Facebook product description. * @param int $id WooCommerce Product ID. diff --git a/package-lock.json b/package-lock.json index b4ebcab16..9958d9791 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "facebook-for-woocommerce", - "version": "3.2.5", + "version": "3.2.6", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/package.json b/package.json index c3aac04b0..2523b89eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "facebook-for-woocommerce", - "version": "3.2.5", + "version": "3.2.6", "author": "Facebook", "homepage": "https://woocommerce.com/products/facebook/", "license": "GPL-2.0", diff --git a/readme.txt b/readme.txt index 07831d4be..e9562168b 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: facebook, automattic, woothemes Tags: facebook, woocommerce, marketing, product catalog feed, pixel Requires at least: 5.6 Tested up to: 6.6 -Stable tag: 3.2.5 +Stable tag: 3.2.6 Requires PHP: 7.4 MySQL: 5.6 or greater License: GPLv2 or later From 60562354cd8bdbe155c140b08d15a67e37f9a7f9 Mon Sep 17 00:00:00 2001 From: Justin Palmer <228780+layoutd@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:05:20 -0400 Subject: [PATCH 10/10] woorelease: Changelog update --- changelog.txt | 5 +++++ readme.txt | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/changelog.txt b/changelog.txt index f534dbf52..86229fea9 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,10 @@ *** Facebook for WooCommerce Changelog *** += 3.2.6 - 2024-07-23 = +* Add - Filter facebook_for_woocommerce_fb_product_description. +* Fix - Remove deprecated FILTER_SANITIZE_STRING usage. +* Tweak - Align PHP require version to composer.json. + = 3.2.5 - 2024-07-10 = * Tweak - WC 9.1 compatibility. * Tweak - WP 6.6 compatibility. diff --git a/readme.txt b/readme.txt index e9562168b..6eb4d8150 100644 --- a/readme.txt +++ b/readme.txt @@ -40,6 +40,11 @@ When opening a bug on GitHub, please give us as many details as possible. == Changelog == += 3.2.6 - 2024-07-23 = +* Add - Filter facebook_for_woocommerce_fb_product_description. +* Fix - Remove deprecated FILTER_SANITIZE_STRING usage. +* Tweak - Align PHP require version to composer.json. + = 3.2.5 - 2024-07-10 = * Tweak - WC 9.1 compatibility. * Tweak - WP 6.6 compatibility.