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/facebook-for-woocommerce.php b/facebook-for-woocommerce.php index 4770f033f..f71c4a5e8 100644 --- a/facebook-for-woocommerce.php +++ b/facebook-for-woocommerce.php @@ -11,8 +11,9 @@ * 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 * Requires Plugins: woocommerce * Tested up to: 6.6 @@ -48,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/Framework/Helper.php b/includes/Framework/Helper.php index 2b2f6071f..a17a9416f 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 ); } diff --git a/includes/fbproduct.php b/includes/fbproduct.php index b38dd16a1..0c9f30963 100644 --- a/includes/fbproduct.php +++ b/includes/fbproduct.php @@ -391,56 +391,60 @@ 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; + } - return $description; + if ( empty( $description ) ) { + $description = $post_title; + } + } + /** + * Filters the FB product description. + * + * @since 3.2.6 + * + * @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/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 58b572b7a..6eb4d8150 100644 --- a/readme.txt +++ b/readme.txt @@ -1,10 +1,10 @@ === Facebook for WooCommerce === 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 +Requires at least: 5.6 +Tested up to: 6.6 +Stable tag: 3.2.6 +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,12 @@ When opening a bug on GitHub, please give us as many details as possible. == Changelog == -= 3.2.5 - 2024-07-10 = += 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/tests/Unit/fbproductTest.php b/tests/Unit/fbproductTest.php new file mode 100644 index 000000000..6d8d7b730 --- /dev/null +++ b/tests/Unit/fbproductTest.php @@ -0,0 +1,106 @@ +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 ); + } + + /** + * 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' ); + + } +}