Opened 5 years ago
Last modified 7 weeks ago
#55376 new enhancement
Use wp_filesize in core
| Reported by: | spacedmonkey | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | good-first-bug has-patch changes-requested |
| Cc: | Focuses: |
Attachments (1)
Change History (20)
This ticket was mentioned in โPR #2402 on โWordPress/wordpress-develop by โChargnn.
5 years ago
#2
- Keywords has-patch added; needs-patch removed
Replaced usage of filesize functions to wp_filesize in wp-admin/theme-editor.php
Trac ticket: https://core.trac.wordpress.org/ticket/55376
โmauriac commented on โPR #2402:
5 years ago
#3
LGTM!
โspacedmonkey commented on โPR #2402:
5 years ago
#5
Thanks @Chargnn for the patch. I have added some links other places where this function can be used.
#6
in reply to: ↑ 5
@
5 years ago
Replying to spacedmonkey:
comment
1067905355
}}}
โspacedmonkey commented on โPR #2402:
I would consider using this function in the following places.
Thank you! Looking into this right now.
#8
in reply to: ↑ 4
@
5 years ago
Replying to spacedmonkey:
I would suggest not to use wp_filesize() in WP_Filesystem_* classes or other mission-critical parts like the update_core() function, due to backward compatibiltity concerns. See the discussion in comment:2:ticket:55688.
Using wp_filesize() in other instances seems fine at a glance.
#9
@
4 years ago
Thanks, @spacedmonkey for your advice, I followed your instruction and changed the filesize() function in the rest of the files.
#10
@
4 years ago
- Keywords needs-patch needs-refresh added; has-patch removed
@nihar007 This patch needs a refresh.
This ticket was mentioned in โPR #4361 on โWordPress/wordpress-develop by โ@spacedmonkey.
4 years ago
#11
- Keywords has-patch added; needs-patch needs-refresh removed
Trac ticket: https://core.trac.wordpress.org/ticket/55376
โ@spacedmonkey commented on โPR #4361:
3 years ago
#12
Fixed
This ticket was mentioned in โPR #7818 on โWordPress/wordpress-develop by โ@ankitkumarshah.
2 years ago
#13
Trac ticket: https://core.trac.wordpress.org/ticket/55376
This PR replaces the use of filesize() with wp_filesize() in file.php and class-snoopy.php. The wp_filesize() function provides better handling for file size operations. This ensures improved compatibility and consistency with WordPress core standards.
#14
@
18 months ago
- Keywords needs-refresh changes-requested added
I reviewed @ankitkumarshah's PR on GitHub. To summarize here:
- The PR does not address all occurrences of
filesize()(minus the file system/update related ones noted by @SergeyBiryukov above). - Changes an instance within the
class-snoopy.phpfile, which has been deprecated since WP3.0. There's on benefit to updating this file.
โ@ankitkumarshah commented on โPR #7818:
18 months ago
#15
Thank you for the feedback, @desrosj! Iโll make the suggested updates and refresh the PR shortly. ๐
โ@ankitkumarshah commented on โPR #7818:
18 months ago
#16
Hi @desrosj,
Thank you for the feedback! Iโve made the changes noted by @spacedmonkey in his comment on the Trac ticket. The following files were updated:
src/wp-admin/includes/file.phpsrc/wp-admin/includes/ms.phpsrc/wp-admin/includes/plugin.phpsrc/wp-includes/class-wp-customize-manager.phpsrc/wp-includes/ms-files.phpsrc/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.phpsrc/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php
Please review the PR at your convenience. Thank You ๐
#17
@
18 months ago
Hi @desrosj,
Thank you for the feedback. Iโve made the necessary changes as requested. Please let me know if thereโs anything else youโd like me to address.
This ticket was mentioned in โPR #11990 on โWordPress/wordpress-develop by โ@orionaselite.
6 months ago
#18
- Keywords needs-refresh removed
This PR replaces direct filesize() calls with wp_filesize() in core WordPress files where the wrapper function is appropriate. The wp_filesize() function (introduced in 5.7 via r52837) provides int casting and filter support, making it the preferred API for getting file sizes in WordPress.
## Files changed
src/wp-admin/includes/file.phpโ_wp_handle_upload()file size checksrc/wp-admin/includes/ms.phpโcheck_upload_size()file size checksrc/wp-admin/includes/plugin.phpโget_mu_plugins()index.php size checksrc/wp-admin/theme-editor.phpโ file size checks for theme file readingsrc/wp-includes/class-wp-customize-manager.phpโ_validate_header_video()video size validationsrc/wp-includes/functions.phpโrecurse_dirsize()directory size calculationsrc/wp-includes/ms-files.phpโContent-Lengthheader for multisite file servingsrc/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.phpโ upload space checksrc/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.phpโContent-Lengthheader for export
## Excluded (per reviewer feedback)
Per comment:8 by @SergeyBiryukov, filesystem abstraction and update/auto-update code should not be changed:
class-wp-filesystem-*โ filesystem abstraction classes should use raw PHPfilesize()update-core.phpโ update/auto-update code excluded- Third-party libraries (Snoopy, pclzip, getid3, sodium_compat, ftp) โ not WordPress core code
This addresses the feedback from @desrosj on โPR #7818 requesting the additional instances noted by @spacedmonkey in comment:4.
#19
@
7 weeks ago
Test Report
Patch tested: โhttps://github.com/WordPress/wordpress-develop/pull/11990
Environment
- WordPress: 7.2-alpha (trunk @ b24d8fbf90)
- PHP: 8.3.32 (PHP.wasm)
- Server: WordPress Contributor Toolkit (Playground-based)
- Browser: Chrome
- OS: macOS
- Theme: Twenty Twenty-Five
Coverage check
Grepped src/ on trunk for raw filesize() calls. The PR converts all of them except the ones intentionally excluded per comment:8 (WP_Filesystem_Direct, WP_Filesystem_SSH2, update_core()), the call inside wp_filesize() itself, and the deprecated class-snoopy.php. The earlier review feedback on โPR 7818 (missing instances, Snoopy change) looks addressed.
Steps / Results
I added a temporary mu-plugin hooked to the wp_filesize filter that logged the calling file:line, to confirm each changed path now goes through wp_filesize().
- Tools > Theme File Editor, open
style.css: loads the full file (3009 of 3009 bytes). Logged fromtheme-editor.php:165and:167. โ - mu-plugins with a 28-byte "Silence is golden"
index.php:index.phpis still hidden from the Must-Use list. Logged fromplugin.php:406. โ - Site Editor export (
GET /wp-block-editor/v1/export):Content-Lengthheader (8181559) matches the bytes received. Logged fromclass-wp-rest-edit-site-export-controller.php:88. โ recurse_dirsize( WP_CONTENT_DIR . '/themes' )returns 46709125, which matches the on-disk total. Logged fromfunctions.php(recurse_dirsize). โ- Raw-body REST media upload (
POST /wp/v2/media, goes throughwp_handle_sideload()): a 166-byte PNG uploads fine andmedia_details.filesizeis 166. โ php -lpasses on all changed files. โ
Not tested: the multisite-only paths (check_upload_size() in ms.php and in the attachments controller, ms-files.php) and the Customizer header video size validation. ms-files.php runs with SHORTINIT, but functions.php and plugin.php load before the SHORTINIT return, so wp_filesize() is available there.
Review note
In theme-editor.php the value is now also used as the fread() length. If a plugin filters wp_filesize to a value smaller than the real size, the editor would load truncated content and saving would truncate the file. Would it be safer to keep filesize() for the fread() length, or switch to file_get_contents()?
Otherwise the PR looks good to me. Once a committer confirms, changes-requested can probably be removed.
CC @flixos90