Skip to content

Commit 2f477ea

Browse files
authored
Merge pull request #376 from WordPress/add/wp_filesize-fuction
Use `wp_filesize` instead of `filesize` if available
2 parents 11bf31c + bf18dba commit 2f477ea

8 files changed

Lines changed: 66 additions & 10 deletions

File tree

‎load.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,6 @@ function perflab_load_active_modules() {
197197
if ( is_admin() ) {
198198
require_once plugin_dir_path( __FILE__ ) . 'admin/load.php';
199199
}
200+
201+
// Polyfills.
202+
require_once plugin_dir_path( __FILE__ ) . 'polyfills.php';

‎modules/images/webp-uploads/helper.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
9999
'file' => $image['file'],
100100
'filesize' => array_key_exists( 'filesize', $image )
101101
? $image['filesize']
102-
: filesize( $image['path'] ),
102+
: wp_filesize( $image['path'] ),
103103
);
104104
}
105105

@@ -153,7 +153,7 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
153153

154154
return array(
155155
'file' => $image['file'],
156-
'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0,
156+
'filesize' => isset( $image['path'] ) ? wp_filesize( $image['path'] ) : 0,
157157
);
158158
}
159159

‎modules/images/webp-uploads/image-edit.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_i
2525
// Add sources to original image metadata.
2626
$metadata['sources'][ $targeted_mime ] = array(
2727
'file' => $main_images[ $targeted_mime ]['file'],
28-
'filesize' => filesize( $main_images[ $targeted_mime ]['path'] ),
28+
'filesize' => wp_filesize( $main_images[ $targeted_mime ]['path'] ),
2929
);
3030
$image_directory = pathinfo( $main_images[ $targeted_mime ]['path'], PATHINFO_DIRNAME );
3131
}
@@ -68,7 +68,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_i
6868

6969
$metadata['sizes'][ $size_name ]['sources'][ $targeted_mime ] = array(
7070
'file' => $subsized_images[ $targeted_mime ][ $size_name ]['file'],
71-
'filesize' => filesize( $subsize_path ),
71+
'filesize' => wp_filesize( $subsize_path ),
7272
);
7373
}
7474
}

‎modules/images/webp-uploads/load.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )
6161
) {
6262
$metadata['sources'][ $mime_type ] = array(
6363
'file' => wp_basename( $file ),
64-
'filesize' => filesize( $file ),
64+
'filesize' => wp_filesize( $file ),
6565
);
6666
wp_update_attachment_metadata( $attachment_id, $metadata );
6767
}
@@ -137,7 +137,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )
137137
// Set the filesize from the current mime image.
138138
$file_location = path_join( $original_directory, $properties['file'] );
139139
if ( file_exists( $file_location ) ) {
140-
$properties['sources'][ $current_mime ]['filesize'] = filesize( $file_location );
140+
$properties['sources'][ $current_mime ]['filesize'] = wp_filesize( $file_location );
141141
}
142142
$metadata['sizes'][ $size_name ] = $properties;
143143
wp_update_attachment_metadata( $attachment_id, $metadata );

‎modules/site-health/audit-enqueued-assets/helper.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ function perflab_aea_get_path_from_resource_url( $resource_url ) {
115115
* @return int Returns size if file exists, 0 if it doesn't.
116116
*/
117117
function perflab_aea_get_resource_file_size( $file_src ) {
118-
return file_exists( $file_src ) ? filesize( $file_src ) : 0;
118+
return wp_filesize( $file_src );
119119
}
120120

‎polyfills.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* These functions are either introduced in WP Core in latest version or
4+
* available in different versions. For simplicity for future, we can remove them
5+
* if they are supported in WP Core versions which this plugin supports.
6+
*
7+
* @package performance-lab
8+
* @since n.e.x.t
9+
*/
10+
11+
// WP Filesize function.
12+
if ( ! function_exists( 'wp_filesize' ) ) {
13+
/**
14+
* Wrapper for PHP filesize with filters and casting the result as an integer.
15+
*
16+
* This function was introduced in WP 6.0, for backward compatibility the
17+
* function is added as backup here.
18+
*
19+
* @param string $path Path to the file.
20+
*
21+
* @return int The size of the file in bytes, or 0 in the event of an error.
22+
* @since n.e.x.t
23+
*
24+
* @link https://www.php.net/manual/en/function.filesize.php
25+
*/
26+
function wp_filesize( $path ) {
27+
/**
28+
* Filters the result of wp_filesize before the PHP function is run.
29+
*
30+
* @param null|int $size The unfiltered value. Returning an int from the callback bypasses the filesize call.
31+
* @param string $path Path to the file.
32+
*
33+
* @since n.e.x.t
34+
*/
35+
$size = apply_filters( 'pre_wp_filesize', null, $path );
36+
37+
if ( is_int( $size ) ) {
38+
return $size;
39+
}
40+
41+
$size = file_exists( $path ) ? (int) filesize( $path ) : 0;
42+
43+
/**
44+
* Filters the size of the file.
45+
*
46+
* @param int $size The result of PHP filesize on the file.
47+
* @param string $path Path to the file.
48+
*
49+
* @since n.e.x.t
50+
*/
51+
return (int) apply_filters( 'wp_filesize', $size, $path );
52+
}
53+
}

‎tests/modules/images/webp-uploads/load-tests.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ public function it_should_create_a_webp_version_with_all_the_required_properties
141141
$this->assertImageHasSource( $attachment_id, 'image/jpeg' );
142142
$this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $file );
143143
$this->assertFileExists( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) );
144-
$this->assertSame( $metadata['sources']['image/jpeg']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ) );
144+
$this->assertSame( $metadata['sources']['image/jpeg']['filesize'], wp_filesize( path_join( $dirname, $metadata['sources']['image/jpeg']['file'] ) ) );
145145

146146
$this->assertImageHasSource( $attachment_id, 'image/webp' );
147147
$this->assertFileExists( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) );
148-
$this->assertSame( $metadata['sources']['image/webp']['filesize'], filesize( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ) );
148+
$this->assertSame( $metadata['sources']['image/webp']['filesize'], wp_filesize( path_join( $dirname, $metadata['sources']['image/webp']['file'] ) ) );
149149

150150
$this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' );
151151
$this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' );

‎tests/modules/site-health/audit-enqueued-assets/audit-enqueued-assets-helper-test.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function test_perflab_aea_get_resource_file_size() {
119119
$filename = __FUNCTION__ . '.css';
120120
$contents = __FUNCTION__ . '_contents';
121121
$file = wp_upload_bits( $filename, null, $contents );
122-
$this->assertEquals( filesize( $file['file'] ), perflab_aea_get_resource_file_size( $file['file'] ) );
122+
$this->assertEquals( wp_filesize( $file['file'] ), perflab_aea_get_resource_file_size( $file['file'] ) );
123123
}
124124

125125
}

0 commit comments

Comments
 (0)