Skip to content

Commit 03168e3

Browse files
authored
beta: Improve error handling when installing mu-plugins (#46115)
If the `WP_Filesystem` instance doesn't report any errors on a failure (e.g. `WP_Filesystem_Direct` doesn't record any at all), check PHP's `error_get_last()` before reporting "unknown error".
1 parent 578076a commit 03168e3

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Check `error_get_last()` if the `WP_Filesystem` doesn't report any error on mu-plugin install failure.

projects/plugins/beta/src/class-plugin.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,17 @@ private function update_mu_plugin_loader( $which ) {
581581
}
582582
$contents .= " */\n";
583583
} else {
584+
error_clear_last();
584585
$contents = $wp_filesystem->get_contents( $file );
585586
if ( $contents === false ) {
586-
return is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ? $wp_filesystem->errors : new WP_Error( 'fs_error', "Unknown error while reading {$this->plugin_slug()}-loader.php" );
587+
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
588+
return $wp_filesystem->errors;
589+
}
590+
$err = error_get_last();
591+
if ( ! empty( $err['message'] ) ) {
592+
return new WP_Error( 'fs_error', "PHP error while reading {$this->plugin_slug()}-loader.php: " . esc_html( $err['message'] ) );
593+
}
594+
return new WP_Error( 'fs_error', "Unknown error while reading {$this->plugin_slug()}-loader.php" );
587595
}
588596
if ( ! str_starts_with( $contents, '<?php' ) ) {
589597
return new WP_Error( 'bad_mu_loader', "Mu-plugin loader {$this->plugin_slug()}-loader.php is unparseable" );
@@ -600,16 +608,25 @@ private function update_mu_plugin_loader( $which ) {
600608
// Write the file, or delete it if it's our auto-generated file and turns out to be empty.
601609
if ( preg_match( '#^<\?php\s*/\*\*\n \* Loader generated by Jetpack Beta plugin\n\n(?: \* Plugin Name: .*\n)?(?: \* Description: .*\n)? \*/\s*$#', $new_contents ) ) {
602610
$what = 'deleting';
603-
$ok = $wp_filesystem->delete( $file );
611+
error_clear_last();
612+
$ok = $wp_filesystem->delete( $file );
604613
} elseif ( $contents !== $new_contents ) {
605614
$what = 'updating';
606-
$ok = $wp_filesystem->put_contents( $file, $new_contents );
615+
error_clear_last();
616+
$ok = $wp_filesystem->put_contents( $file, $new_contents );
607617
} else {
608618
$what = 'nothing';
609619
$ok = true;
610620
}
611621
if ( ! $ok ) {
612-
return is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ? $wp_filesystem->errors : new WP_Error( 'fs_error', "Unknown error while $what {$this->plugin_slug()}-loader.php" );
622+
if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
623+
return $wp_filesystem->errors;
624+
}
625+
$err = error_get_last();
626+
if ( ! empty( $err['message'] ) ) {
627+
return new WP_Error( 'fs_error', "PHP error while $what {$this->plugin_slug()}-loader.php: " . esc_html( $err['message'] ) );
628+
}
629+
return new WP_Error( 'fs_error', "Unknown error while $what {$this->plugin_slug()}-loader.php" );
613630
}
614631
return null;
615632
}

0 commit comments

Comments
 (0)