Skip to content

Commit

Permalink
Merge pull request #220 from Codeinwp/fix/script-loader
Browse files Browse the repository at this point in the history
fix: script loading when multiple products are loaded
  • Loading branch information
Soare-Robert-Daniel authored Mar 29, 2024
2 parents b4813b6 + ab8a457 commit 75d7372
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Modules/Script_loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ class Script_Loader extends Abstract_Module {
* @return bool Should we load ?
*/
public function can_load( $product ) {
if ( apply_filters( 'themeisle_sdk_ran_promos', false ) === true ) {
return false;
}

if ( $this->is_from_partner( $product ) ) {
return false;
}
Expand All @@ -56,11 +52,18 @@ public function load( $product ) {
}

/**
* Setup actions.
* Setup actions. Once for all products.
*/
private function setup_actions() {

if ( apply_filters( 'themeisle_sdk_script_setup', false ) ) {
return;
}

add_filter( 'themeisle_sdk_dependency_script_handler', [ $this, 'get_script_handler' ], 10, 1 );
add_action( 'themeisle_sdk_dependency_enqueue_script', [ $this, 'enqueue_script' ], 10, 1 );

add_filter( 'themeisle_sdk_script_setup', '__return_true' );
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/script-loader-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ public function test_script_loader_filters_check() {
$this->assertEquals( has_action( 'themeisle_sdk_dependency_enqueue_script', [ $module, 'enqueue_script' ] ), 10 );
}

public function test_multiple_script_loading() {
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';

$product = new \ThemeisleSDK\Product( $file );

/**
* When multiple products are loaded, the script loader hooks registration should not be triggered multiple times.
*/
( new \ThemeisleSDK\Modules\Script_Loader() )->load( $product );
( new \ThemeisleSDK\Modules\Script_Loader() )->load( $product );
( new \ThemeisleSDK\Modules\Script_Loader() )->load( $product );

// Load survey script.
$handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'survey' );
$this->assertNotEmpty( $handler );
$this->assertTrue( 'themeisle_sdk_survey_script' === $handler );
do_action( 'themeisle_sdk_dependency_enqueue_script', 'survey' );
$this->assertTrue( wp_script_is( $handler, 'enqueued' ) );

// Load tracking script.
$handler = apply_filters( 'themeisle_sdk_dependency_script_handler', 'tracking' );
$this->assertNotEmpty( $handler );
$this->assertTrue( 'themeisle_sdk_tracking_script' === $handler );
do_action( 'themeisle_sdk_dependency_enqueue_script', 'tracking' );
$this->assertTrue( wp_script_is( $handler, 'enqueued' ) );

$this->assertTrue( has_filter( 'themeisle_sdk_script_setup' ) );
}

public function test_script_loader_handler_check() {
$file = dirname( __FILE__ ) . '/sample_products/sample_theme/style.css';

Expand Down

0 comments on commit 75d7372

Please sign in to comment.