Make WordPress Core

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:

Description

Introduced in [52837] as part of #49412, the wp_filesize function is a wrapper around filesize. It has some improves on this function, like casting to int and filters. The filesize function is call throughout core functions. This function may be useful in more contexts.

Attachments (1)

55376.diff​ (4.3 KB ) - added by nihar007 4 years ago.
Hi,

Download all attachments as: .zip

Change History (20)

#1 @spacedmonkey
5 years ago

CC @flixos90

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!

#4 follow-up: @spacedmonkey
5 years ago

I would consider using this function in the following places.

โ€‹https://github.com/WordPress/wordpress-develop/blob/8697aee5f9d834c1d7135524c7f13dc3c96f4d85/src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php#L87

โ€‹https://github.com/WordPress/wordpress-develop/blob/2648a5f984b8abf06872151898e3a61d3458a628/src/wp-includes/ms-files.php#L45

โ€‹https://github.com/WordPress/wordpress-develop/blob/ba518c2d86e62ed63b24e9a720d42aee3ab951fb/src/wp-admin/includes/class-wp-filesystem-direct.php#L490

โ€‹https://github.com/WordPress/wordpress-develop/blob/33048b933379ae48723f617588ed8aa996ca5d14/src/wp-admin/includes/class-wp-filesystem-ssh2.php#L649

โ€‹https://github.com/WordPress/wordpress-develop/blob/ba518c2d86e62ed63b24e9a720d42aee3ab951fb/src/wp-admin/includes/ms.php#L33

โ€‹https://github.com/WordPress/wordpress-develop/blob/ecf1d6a158c70dd7f10cc42c4b26ecb0fb22bb59/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L1287

โ€‹https://github.com/WordPress/wordpress-develop/blob/2d707c45127d204936eef48b3c9e133b60596b33/src/wp-admin/includes/update-core.php#L1256

โ€‹https://github.com/WordPress/wordpress-develop/blob/26bb0d7479e1dd08dd982513ee6bee94f4c6f13d/src/wp-admin/includes/plugin.php#L401

โ€‹https://github.com/WordPress/wordpress-develop/blob/6adff3d989319e633cfdcdb7aff4dc906087c230/src/wp-includes/script-loader.php#L2736

โ€‹https://github.com/WordPress/wordpress-develop/blob/8db1549c0f1af05fbb919ad2eeb7f7bff5a63da9/src/wp-includes/class-wp-customize-manager.php#L6035

โ€‹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 @acoulombe
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.

Last edited 5 years ago by acoulombe (previous) (diff)

#7 @spacedmonkey
5 years ago

  • Milestone Awaiting Review โ†’ Future Release

#9 @nihar007
4 years ago

Thanks, @spacedmonkey for your advice, I followed your instruction and changed the filesize() function in the rest of the files.

#10 @spacedmonkey
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

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 @desrosj
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.php file, which has been deprecated since WP 3.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.php
  • src/wp-admin/includes/ms.php
  • src/wp-admin/includes/plugin.php
  • src/wp-includes/class-wp-customize-manager.php
  • src/wp-includes/ms-files.php
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
  • src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php

Please review the PR at your convenience. Thank You ๐Ÿ™Œ

#17 @ankitkumarshah
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.

Last edited 18 months ago by ankitkumarshah (previous) (diff)

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 check
  • src/wp-admin/includes/ms.php โ€” check_upload_size() file size check
  • src/wp-admin/includes/plugin.php โ€” get_mu_plugins() index.php size check
  • src/wp-admin/theme-editor.php โ€” file size checks for theme file reading
  • src/wp-includes/class-wp-customize-manager.php โ€” _validate_header_video() video size validation
  • src/wp-includes/functions.php โ€” recurse_dirsize() directory size calculation
  • src/wp-includes/ms-files.php โ€” Content-Length header for multisite file serving
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php โ€” upload space check
  • src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php โ€” Content-Length header 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 PHP filesize()
  • 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 @binsaifullah
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().

  1. Tools > Theme File Editor, open style.css: loads the full file (3009 of 3009 bytes). Logged from theme-editor.php:165 and :167. โœ…
  2. mu-plugins with a 28-byte "Silence is golden" index.php: index.php is still hidden from the Must-Use list. Logged from plugin.php:406. โœ…
  3. Site Editor export (GET /wp-block-editor/v1/export): Content-Length header (8181559) matches the bytes received. Logged from class-wp-rest-edit-site-export-controller.php:88. โœ…
  4. recurse_dirsize( WP_CONTENT_DIR . '/themes' ) returns 46709125, which matches the on-disk total. Logged from functions.php (recurse_dirsize). โœ…
  5. Raw-body REST media upload (POST /wp/v2/media, goes through wp_handle_sideload()): a 166-byte PNG uploads fine and media_details.filesize is 166. โœ…
  6. php -l passes 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.

Note: See TracTickets for help on using tickets.