Skip to content

Commit 8e09adf

Browse files
authored
Merge pull request #36 from stellarwp/feat/add-full-path-api-method
Adds a full path API method
2 parents 2ddd437 + 3539721 commit 8e09adf

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

.github/workflows/tests-php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
id: get-composer-cache-dir
3333
run: |
3434
echo "::set-output name=dir::$(composer config cache-files-dir)"
35-
- uses: actions/cache@v2
35+
- uses: actions/cache@v4
3636
id: composer-cache
3737
with:
3838
path: ${{ steps.get-composer-cache-dir.outputs.dir }}

src/Assets/Asset.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,39 @@ public function get_url( bool $use_min_if_available = true ): string {
11111111
return $this->url;
11121112
}
11131113

1114+
/**
1115+
* Get the asset's full path - considering if minified exists.
1116+
*
1117+
* @since 1.4.6
1118+
*
1119+
* @param bool $use_min_if_available
1120+
*
1121+
* @return string
1122+
*/
1123+
public function get_full_resource_path( bool $use_min_if_available = true ): string {
1124+
$resource_path_data = $this->build_resource_path_data();
1125+
$resource = $resource_path_data['resource'];
1126+
$resource_path = $resource_path_data['resource_path'];
1127+
1128+
$root_path = $this->get_root_path();
1129+
1130+
$path = wp_normalize_path( $root_path . $resource_path . $resource );
1131+
1132+
if ( ! $use_min_if_available ) {
1133+
return $path;
1134+
}
1135+
1136+
if ( strstr( $path, '.min.' . $this->get_type() ) ) {
1137+
return $path;
1138+
}
1139+
1140+
$min_relative_path = $this->get_min_path();
1141+
$min_path = $min_relative_path === $this->get_path() ? preg_replace( '#(.*).(js|css)#', '$1.min.$2', $path ) : $root_path . $min_relative_path . $resource;
1142+
$min_path = wp_normalize_path( $min_path );
1143+
1144+
return file_exists( $min_path ) ? $min_path : $path;
1145+
}
1146+
11141147
/**
11151148
* Get the asset version.
11161149
*

tests/wpunit/AssetsTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,9 @@ protected function existence_assertions( $test_slug_prefix ) {
10201020
protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false, $id = '', $add_to_path_group = '', $group_path_path = '', $wont_figure_out_min_vs_unmin = false, $should_prefix = true ) {
10211021
$asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) );
10221022

1023-
$url = plugins_url( ( $group_path_path ? $group_path_path : '/assets/tests/_data/' ) . ( $should_prefix ? ( $is_js ? 'js' : 'css' ) : '' ) . '/' . $slug_prefix );
1023+
$url = plugins_url( ( $group_path_path ? $group_path_path : '/assets/tests/_data/' ) . ( $should_prefix ? ( $is_js ? 'js/' : 'css/' ) : '' ) . $slug_prefix );
10241024

1025+
$path = wp_normalize_path( WP_PLUGIN_DIR . ( $group_path_path ? $group_path_path : '/assets/tests/_data/' ) . ( $should_prefix ? ( $is_js ? 'js/' : 'css/' ) : '' ) . $slug_prefix );
10251026
$urls = [];
10261027

10271028
$this->set_const_value( 'SCRIPT_DEBUG', false );
@@ -1031,12 +1032,17 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min
10311032
if ( $has_only_min ) {
10321033
$urls[] = $url . '.min' . ( $is_js ? '.js' : '.css' );
10331034
$urls[] = $url . '.min' . ( $is_js ? '.js' : '.css' );
1035+
$temp_path = $path . '.min' . ( $is_js ? '.js' : '.css' );
1036+
$path = file_exists( $temp_path ) ? $temp_path : $path . ( $is_js ? '.js' : '.css' );
10341037
} elseif ( $has_min ) {
10351038
$urls[] = $url . ( $is_js ? '.min.js' : '.min.css' );
10361039
$urls[] = $url . ( $is_js ? '.js' : '.css' );
1040+
$temp_path = $path . '.min' . ( $is_js ? '.js' : '.css' );
1041+
$path = file_exists( $temp_path ) ? $temp_path : $path . ( $is_js ? '.js' : '.css' );
10371042
} else {
10381043
$urls[] = $url . ( $is_js ? '.js' : '.css' );
10391044
$urls[] = $url . ( $is_js ? '.js' : '.css' );
1045+
$path .= $is_js ? '.js' : '.css';
10401046
}
10411047

10421048
$plugins_path = str_replace( constant( 'WP_CONTENT_DIR' ), '', constant( 'WP_PLUGIN_DIR' ) );
@@ -1076,6 +1082,8 @@ static function ( $url ) {
10761082
$asset->get_url(),
10771083
$id
10781084
);
1085+
1086+
$this->assertEquals( $path, $asset->get_full_resource_path() );
10791087
}
10801088

10811089
/**

0 commit comments

Comments
 (0)