Skip to content

Commit 2ddd437

Browse files
authored
Merge pull request #33 from stellarwp/feature/add_version_filter
2 parents 3da0472 + cc1b420 commit 2ddd437

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Assets/Asset.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,16 @@ public function get_version(): string {
11231123
return (string) $asset_file_contents['version'];
11241124
}
11251125

1126-
return $this->version;
1126+
$hook_prefix = Config::get_hook_prefix();
1127+
1128+
/**
1129+
* Filters the asset version when it doesn't come from an asset file.
1130+
*
1131+
* @param string $version The asset version.
1132+
* @param string $slug The asset slug.
1133+
* @param Asset $asset The Asset object.
1134+
*/
1135+
return (string) apply_filters( "stellarwp/assets/{$hook_prefix}/version", $this->version, $this->slug, $this );
11271136
}
11281137

11291138
/**

tests/wpunit/AssetTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,31 @@ public function test_add_to_group_path_changes_resolution_path_to_group(): void
2929

3030
$this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() );
3131
}
32+
33+
public function test_can_filter_asset_version(): void {
34+
$prefix = 'bork';
35+
36+
Config::reset();
37+
Config::set_hook_prefix( $prefix );
38+
Config::set_version( '1.1.0' );
39+
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
40+
41+
$asset = new Asset( 'test-script', 'fake.js', '1.0.0', codecept_data_dir() );
42+
43+
// The asset version should be the same as the one passed to the constructor.
44+
$this->assertEquals( '1.0.0', $asset->get_version() );
45+
46+
// Filter the asset version.
47+
$filter = function () {
48+
return '2.0.0';
49+
};
50+
51+
add_filter( "stellarwp/assets/{$prefix}/version", $filter );
52+
53+
// After filtering, the asset version should be updated.
54+
$this->assertEquals( '2.0.0', $asset->get_version() );
55+
56+
// Cleanup the filter.
57+
remove_filter( "stellarwp/assets/{$prefix}/version", $filter );
58+
}
3259
}

0 commit comments

Comments
 (0)