Skip to content

Commit

Permalink
fix: featured available on in some free plugins
Browse files Browse the repository at this point in the history
References: Codeinwp/themeisle#1652
  • Loading branch information
preda-bogdan committed Jun 5, 2024
1 parent e32205e commit 5702c9e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Modules/Featured_plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/featured-plugins-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand Down

0 comments on commit 5702c9e

Please sign in to comment.