Skip to content

Commit 1d599d3

Browse files
fix: fatal error on servers with broken FTP filesystem configuration (#1258)
On some server setups (e.g. nginx with FTP configured but no active connection), WordPress initializes its FTP filesystem driver but fails when it tries to use it, throwing a PHP fatal error that crashes the page. Wrap the filesystem calls in a try/catch so any such failure falls back to the bundled customization.js gracefully.
1 parent 5e9cea6 commit 1d599d3

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

classes/Visualizer/Module.php

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -532,50 +532,53 @@ protected function get_user_customization_js() {
532532
return $default;
533533
}
534534

535-
require_once( ABSPATH . 'wp-admin/includes/file.php' );
536-
WP_Filesystem();
537-
global $wp_filesystem;
538-
if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base' ) ) {
539-
$creds = request_filesystem_credentials( site_url() );
540-
wp_filesystem( $creds );
541-
}
535+
try {
536+
require_once( ABSPATH . 'wp-admin/includes/file.php' );
537+
WP_Filesystem();
538+
global $wp_filesystem;
539+
if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base' ) ) {
540+
return $default;
541+
}
542542

543-
$multisite_arg = '/';
544-
if ( is_multisite() && ! is_main_site() ) {
545-
$multisite_arg = '/sites/' . get_current_blog_id() . '/';
546-
}
543+
$multisite_arg = '/';
544+
if ( is_multisite() && ! is_main_site() ) {
545+
$multisite_arg = '/sites/' . get_current_blog_id() . '/';
546+
}
547547

548-
$dir = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer';
549-
$file = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer/customization.js';
548+
$dir = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer';
549+
$file = $wp_filesystem->wp_content_dir() . 'uploads' . $multisite_arg . 'visualizer/customization.js';
550550

551-
if ( $wp_filesystem->is_readable( $file ) ) {
552-
return $specific;
553-
}
554-
555-
if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) {
556-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ );
557-
return $default;
558-
}
551+
if ( $wp_filesystem->is_readable( $file ) ) {
552+
return $specific;
553+
}
559554

560-
if ( ! $wp_filesystem->exists( $dir ) ) {
561-
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
562-
if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
563-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
555+
if ( $wp_filesystem->exists( $file ) && ! $wp_filesystem->is_readable( $file ) ) {
556+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to read file %s', $file ), 'error', __FILE__, __LINE__ );
564557
return $default;
565558
}
566-
}
567559

568-
// if file does not exist, copy.
569-
if ( ! $wp_filesystem->exists( $file ) ) {
570-
$src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
571-
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
572-
if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
573-
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
574-
return $default;
560+
if ( ! $wp_filesystem->exists( $dir ) ) {
561+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
562+
if ( ( $done = $wp_filesystem->mkdir( $dir ) ) === false ) {
563+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to create directory %s', $dir ), 'error', __FILE__, __LINE__ );
564+
return $default;
565+
}
566+
}
567+
568+
// if file does not exist, copy.
569+
if ( ! $wp_filesystem->exists( $file ) ) {
570+
$src = str_replace( ABSPATH, $wp_filesystem->abspath(), VISUALIZER_ABSPATH . '/js/customization.js' );
571+
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
572+
if ( ( $done = $wp_filesystem->copy( $src, $file ) ) === false ) {
573+
do_action( 'themeisle_log_event', Visualizer_Plugin::NAME, sprintf( 'Unable to copy file %s to %s', $src, $file ), 'error', __FILE__, __LINE__ );
574+
return $default;
575+
}
575576
}
576-
}
577577

578-
return $specific;
578+
return $specific;
579+
} catch ( \Throwable $e ) {
580+
return $default;
581+
}
579582
}
580583

581584
/**

0 commit comments

Comments
 (0)