-
Notifications
You must be signed in to change notification settings - Fork 83
Attachments: avoid fatal errors when WP filesystem isn't available #2728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
05e0a04 to
d55fa5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds defensive checks to prevent fatal errors when the WordPress filesystem (WP_Filesystem) isn't available, particularly on hosts using FTP-based filesystem configurations. The changes ensure graceful failure handling instead of fatal errors when filesystem operations cannot be performed.
- Adds checks for
WP_Filesystem()initialization success before using the global$wp_filesystemobject - Returns appropriate error messages or silently fails for delete operations when filesystem is unavailable
- Ensures proper cleanup of temporary files even when filesystem initialization fails
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| includes/class-attachments.php | Added WP_Filesystem initialization checks in four methods: delete_ap_posts_directory, save_attachment, save_file, and delete_actors_directory to prevent fatal errors when filesystem operations fail |
| .github/changelog/fix-wp-filesystem-null-check | Added changelog entry documenting the patch-level fix for WP_Filesystem initialization checks |
Comments suppressed due to low confidence (4)
includes/class-attachments.php:80
- The second check for
$wp_filesystembeing falsy is redundant. IfWP_Filesystem()returns true (passes the first check on line 72), then$wp_filesystemglobal will be properly initialized. This redundant check adds unnecessary complexity without providing additional safety.
if ( $wp_filesystem->is_dir( $activitypub_dir ) ) {
$wp_filesystem->delete( $activitypub_dir, true );
includes/class-attachments.php:473
- The second check for
$wp_filesystembeing falsy is redundant. IfWP_Filesystem()returns true (passes the first check on line 465), then$wp_filesystemglobal will be properly initialized. This redundant check adds unnecessary complexity without providing additional safety.
/* translators: %s: file path */
return new \WP_Error( 'file_not_found', sprintf( \__( 'File not found: %s', 'activitypub' ), $attachment_data['url'] ) );
}
includes/class-attachments.php:616
- The second check for
$wp_filesystembeing falsy is redundant. IfWP_Filesystem()returns true (passes the first check on line 606), then$wp_filesystemglobal will be properly initialized. This redundant check adds unnecessary complexity without providing additional safety.
}
// Move file to destination.
if ( ! $wp_filesystem->move( $tmp_file, $file_path, true ) ) {
includes/class-attachments.php:1068
- The second check for
$wp_filesystembeing falsy is redundant. IfWP_Filesystem()returns true (passes the first check on line 1060), then$wp_filesystemglobal will be properly initialized. This redundant check adds unnecessary complexity without providing additional safety.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d55fa5c to
d4b7058
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Is this still working, because it now inits the filesystem after setting the global!? |
Fixes #2718
Proposed changes:
Let's introduce some failsafes when WP_Filesystem isn't fully available.
Other information:
Testing instructions:
I'm not sure how to best test this. It may be an option to set a custom
FS_METHODconstant in wp-config.php to override the default behavior and break things.Changelog entry
Changelog Entry Details
Significance
Type
Message