From 5702c9e533cf3cf60bef3da53b65ef33c70775c4 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 5 Jun 2024 12:06:56 +0300 Subject: [PATCH 1/6] fix: featured available on in some free plugins References: Codeinwp/themeisle#1652 --- src/Modules/Featured_plugins.php | 17 ++++++++++++++++- tests/featured-plugins-test.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/Modules/Featured_plugins.php b/src/Modules/Featured_plugins.php index bf3315c1..2bf1af8a 100644 --- a/src/Modules/Featured_plugins.php +++ b/src/Modules/Featured_plugins.php @@ -32,6 +32,21 @@ class Featured_Plugins extends Abstract_Module { */ private $transient_key = 'themeisle_sdk_featured_plugins_'; + /** + * Check if the slug contains "pro" word as part of slug. + * + * @param string $slug The product slug. + * + * @return bool + */ + private function is_pro_slug( $slug ) { + if ( ! is_string( $slug ) || empty( $slug ) ) { + return false; + } + // Match "pro" as a whole word exclude '_' from the word boundary. + return (bool) preg_match( '/(?:\b|_\K)pro(?=\b|_)/', $slug ); + } + /** * Check if the module can be loaded. * @@ -46,7 +61,7 @@ public function can_load( $product ) { $slug = $product->get_slug(); // only load for products that contain "pro" in the slug. - if ( strpos( $slug, 'pro' ) === false ) { + if ( $this->is_pro_slug( $slug ) === false ) { return false; } diff --git a/tests/featured-plugins-test.php b/tests/featured-plugins-test.php index cf61fd93..cf610d90 100644 --- a/tests/featured-plugins-test.php +++ b/tests/featured-plugins-test.php @@ -97,6 +97,23 @@ public static function wpTearDownAfterClass() { self::delete_user( self::$admin_id ); } + /** + * Utility method to change the value of a protected property. + * + * @param mixed $object The object. + * @param string $property The property name. + * @param mixed $new_value The new value. + * + * @return void + * @throws ReflectionException Throws an exception if the property does not exist. + */ + private function set_protected_property( $object, $property, $new_value ) { + $reflection = new ReflectionClass( $object ); + $property = $reflection->getProperty( $property ); + $property->setAccessible( true ); + $property->setValue( $object, $new_value ); + } + /** * Test plugin not loading without config. */ @@ -117,6 +134,18 @@ public function test_plugin_loading_for_pro() { $this->assertTrue( ( new \ThemeisleSDK\Modules\Featured_Plugins() )->can_load( $plugin_product ) ); } + /** + * Test plugin not loading for slugs that contain pro as part of a word. Eg. Product. + */ + public function test_plugin_loading_for_words_w_pro() { + $plugin = dirname( __FILE__ ) . '/sample_products/sample_pro_plugin/plugin_file.php'; + $plugin_product = new \ThemeisleSDK\Product( $plugin ); + + $this->set_protected_property( $plugin_product, 'slug', 'woocommerce-product-addon' ); + + $this->assertFalse( ( new \ThemeisleSDK\Modules\Featured_Plugins() )->can_load( $plugin_product ) ); + } + /** * Test plugin not loading for pro if disabled. */ From 0115420abdf6077aaf5a3be7e63fef7ddfa69125 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 5 Jun 2024 15:24:34 +0300 Subject: [PATCH 2/6] chore: use plugin headers to check pro --- src/Modules/Featured_plugins.php | 26 +++++++++++++++++--------- tests/featured-plugins-test.php | 4 +++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Modules/Featured_plugins.php b/src/Modules/Featured_plugins.php index 2bf1af8a..79ff63cc 100644 --- a/src/Modules/Featured_plugins.php +++ b/src/Modules/Featured_plugins.php @@ -13,6 +13,7 @@ namespace ThemeisleSDK\Modules; use ThemeisleSDK\Common\Abstract_Module; +use ThemeisleSDK\Loader; use ThemeisleSDK\Product; // Exit if accessed directly. @@ -33,18 +34,27 @@ class Featured_Plugins extends Abstract_Module { private $transient_key = 'themeisle_sdk_featured_plugins_'; /** - * Check if the slug contains "pro" word as part of slug. + * Check if the product is pro or if a pro version is available. * - * @param string $slug The product slug. + * @param Product $product Product data. * * @return bool */ - private function is_pro_slug( $slug ) { - if ( ! is_string( $slug ) || empty( $slug ) ) { + private function is_pro_available( $product ) { + if ( ! $product->is_wordpress_available() ) { + return true; + } + + $pro_slug = $product->get_pro_slug(); + if ( empty( $pro_slug ) ) { return false; } - // Match "pro" as a whole word exclude '_' from the word boundary. - return (bool) preg_match( '/(?:\b|_\K)pro(?=\b|_)/', $slug ); + + $all_products = Loader::get_products(); + if ( isset( $all_products[ $pro_slug ] ) ) { + return true; + } + return false; } /** @@ -59,9 +69,7 @@ public function can_load( $product ) { return false; } - $slug = $product->get_slug(); - // only load for products that contain "pro" in the slug. - if ( $this->is_pro_slug( $slug ) === false ) { + if ( ! $this->is_pro_available( $product ) ) { return false; } diff --git a/tests/featured-plugins-test.php b/tests/featured-plugins-test.php index cf610d90..456e5172 100644 --- a/tests/featured-plugins-test.php +++ b/tests/featured-plugins-test.php @@ -121,6 +121,8 @@ public function test_plugin_not_loading_if_not_pro() { $plugin = dirname( __FILE__ ) . '/sample_products/sample_plugin/plugin_file.php'; $plugin_product = new \ThemeisleSDK\Product( $plugin ); + $this->set_protected_property( $plugin_product, 'wordpress_available', true ); + $this->assertFalse( ( new \ThemeisleSDK\Modules\Featured_Plugins() )->can_load( $plugin_product ) ); } @@ -141,7 +143,7 @@ public function test_plugin_loading_for_words_w_pro() { $plugin = dirname( __FILE__ ) . '/sample_products/sample_pro_plugin/plugin_file.php'; $plugin_product = new \ThemeisleSDK\Product( $plugin ); - $this->set_protected_property( $plugin_product, 'slug', 'woocommerce-product-addon' ); + $this->set_protected_property( $plugin_product, 'wordpress_available', true ); $this->assertFalse( ( new \ThemeisleSDK\Modules\Featured_Plugins() )->can_load( $plugin_product ) ); } From 8d04e49cbf28f030eb56fdf822e9ad95edcd5768 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 5 Jun 2024 15:41:58 +0300 Subject: [PATCH 3/6] chore: simplified check for pro --- src/Modules/Featured_plugins.php | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/Modules/Featured_plugins.php b/src/Modules/Featured_plugins.php index 79ff63cc..b9652007 100644 --- a/src/Modules/Featured_plugins.php +++ b/src/Modules/Featured_plugins.php @@ -33,30 +33,6 @@ class Featured_Plugins extends Abstract_Module { */ private $transient_key = 'themeisle_sdk_featured_plugins_'; - /** - * Check if the product is pro or if a pro version is available. - * - * @param Product $product Product data. - * - * @return bool - */ - private function is_pro_available( $product ) { - if ( ! $product->is_wordpress_available() ) { - return true; - } - - $pro_slug = $product->get_pro_slug(); - if ( empty( $pro_slug ) ) { - return false; - } - - $all_products = Loader::get_products(); - if ( isset( $all_products[ $pro_slug ] ) ) { - return true; - } - return false; - } - /** * Check if the module can be loaded. * @@ -69,7 +45,7 @@ public function can_load( $product ) { return false; } - if ( ! $this->is_pro_available( $product ) ) { + if ( $product->is_wordpress_available() ) { return false; } From 480c7047a8c8f1e41991bcd615443b6d9ea71cdf Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Wed, 5 Jun 2024 15:44:58 +0300 Subject: [PATCH 4/6] chore: removed unused dep --- src/Modules/Featured_plugins.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Modules/Featured_plugins.php b/src/Modules/Featured_plugins.php index b9652007..326ac5e0 100644 --- a/src/Modules/Featured_plugins.php +++ b/src/Modules/Featured_plugins.php @@ -13,7 +13,6 @@ namespace ThemeisleSDK\Modules; use ThemeisleSDK\Common\Abstract_Module; -use ThemeisleSDK\Loader; use ThemeisleSDK\Product; // Exit if accessed directly. From cbfe43cce96899f6dee61ac7e9cb270881b53940 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Tue, 25 Jun 2024 12:12:25 +0300 Subject: [PATCH 5/6] chore: small code style change --- tests/featured-plugins-test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/featured-plugins-test.php b/tests/featured-plugins-test.php index 456e5172..79a2ec63 100644 --- a/tests/featured-plugins-test.php +++ b/tests/featured-plugins-test.php @@ -100,7 +100,7 @@ public static function wpTearDownAfterClass() { /** * Utility method to change the value of a protected property. * - * @param mixed $object The object. + * @param \ThemeisleSDK\Product $object The object. * @param string $property The property name. * @param mixed $new_value The new value. * From 9bc06887bb460ea2b71e1453470c76d9980a169a Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Tue, 25 Jun 2024 12:13:55 +0300 Subject: [PATCH 6/6] chore: code style --- tests/featured-plugins-test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/featured-plugins-test.php b/tests/featured-plugins-test.php index 79a2ec63..3a35c63a 100644 --- a/tests/featured-plugins-test.php +++ b/tests/featured-plugins-test.php @@ -100,9 +100,9 @@ public static function wpTearDownAfterClass() { /** * Utility method to change the value of a protected property. * - * @param \ThemeisleSDK\Product $object The object. - * @param string $property The property name. - * @param mixed $new_value The new value. + * @param \ThemeisleSDK\Product $object The object. + * @param string $property The property name. + * @param mixed $new_value The new value. * * @return void * @throws ReflectionException Throws an exception if the property does not exist.