Skip to content

Commit

Permalink
Hotfix: improved the error messages displayed when the backup directo…
Browse files Browse the repository at this point in the history
…ry doesn't exist or is not writable.

Also:
- actually use `imagify_backup_dir_is_writable()` in `_imagify_warning_backup_folder_not_writable_notice()`, it was created for this use :|
- added a `$bypass_error` parameter to `get_imagify_upload_basedir()` and `get_imagify_backup_dir_path()`, so we can get the path even if there is an error. This prevents displaying an empty path when the uploads folder is not writable.
  • Loading branch information
Screenfeed committed Jul 26, 2017
1 parent 10a762c commit acd99a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
22 changes: 13 additions & 9 deletions inc/admin/ui/notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,25 +353,29 @@ function _imagify_warning_backup_folder_not_writable_notice() {
return;
}

$filesystem = imagify_get_filesystem();
$has_backup_dir = wp_mkdir_p( get_imagify_backup_dir_path() );

if ( $has_backup_dir && $filesystem->is_writable( get_imagify_backup_dir_path() ) ) {
if ( imagify_backup_dir_is_writable() ) {
return;
}

$backup_path = imagify_make_file_path_replative( get_imagify_backup_dir_path() );
$filesystem = imagify_get_filesystem();

if ( $filesystem->exists( get_imagify_backup_dir_path() ) ) {
/* translators: %s is a file path. */
$message = __( 'The backup folder %s is not writable by the server, original images cannot be saved!', 'imagify' );
} else {
/* translators: %s is a file path. */
$message = __( 'The backup folder %s cannot be created. Is its parent directory writable by the server? Original images cannot be saved!', 'imagify' );
}

$backup_path = imagify_make_file_path_replative( get_imagify_backup_dir_path( true ) );
?>
<div class="clear"></div>
<div class="imagify-notice error below-h2">
<div class="imagify-notice-logo">
<img class="imagify-logo" src="<?php echo IMAGIFY_ASSETS_IMG_URL; ?>imagify-logo.png" width="138" height="16" alt="Imagify" />
</div>
<div class="imagify-notice-content">
<p><?php
/* translators: %s is a file path. */
printf( __( 'The backup folder %s can\'t be created, original images can\'t be saved!', 'imagify' ), "<code>$backup_path</code>" );
?></p>
<p><?php printf( $message, "<code>$backup_path</code>" ); ?></p>
</div>
</div>
<?php
Expand Down
4 changes: 2 additions & 2 deletions inc/admin/ui/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ function _imagify_display_options_page() {

<br/><strong id="backup-dir-is-writable" class="imagify-error<?php echo $backup_error_class; ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'imagify_check_backup_dir_is_writable' ) ); ?>">
<?php
$backup_path = imagify_make_file_path_replative( get_imagify_backup_dir_path() );
$backup_path = imagify_make_file_path_replative( get_imagify_backup_dir_path( true ) );
/* translators: %s is a file path. */
printf( __( 'The backup folder %s can\'t be created, original images can\'t be saved!', 'imagify' ), "<code>$backup_path</code>" );
printf( __( 'The backup folder %s cannot be created or is not writable by the server, original images cannot be saved!', 'imagify' ), "<code>$backup_path</code>" );
?>
</strong>
</td>
Expand Down
28 changes: 16 additions & 12 deletions inc/functions/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ function imagify_is_attachment_mime_type_supported( $attachment_id ) {
* @since 1.6.8
* @author Grégory Viguier
*
* @return string|bool Path to the backups directory. False on failure.
* @param bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
* @return string|bool Path to the backups directory. False on failure.
*/
function get_imagify_backup_dir_path() {
function get_imagify_backup_dir_path( $bypass_error = false ) {
static $backup_dir;

if ( isset( $backup_dir ) ) {
return $backup_dir;
}

$upload_basedir = get_imagify_upload_basedir();
$upload_basedir = get_imagify_upload_basedir( $bypass_error );

if ( ! $upload_basedir ) {
return false;
Expand Down Expand Up @@ -223,27 +224,30 @@ function get_imagify_thumbnail_sizes() {
* A simple helper to get the upload basedir.
*
* @since 1.6.7
* @since 1.6.8 Added the $bypass_error parameter.
* @author Grégory Viguier
*
* @return string|bool The path. False on failure.
* @param bool $bypass_error True to return the path even if there is an error. This is used when we want to display this path in a message for example.
* @return string|bool The path. False on failure.
*/
function get_imagify_upload_basedir() {
function get_imagify_upload_basedir( $bypass_error = false ) {
static $upload_basedir;
static $upload_basedir_or_error;

if ( isset( $upload_basedir ) ) {
return $upload_basedir;
return $bypass_error ? $upload_basedir : $upload_basedir_or_error;
}

$uploads = wp_upload_dir();
$uploads = wp_upload_dir();
$upload_basedir = trailingslashit( wp_normalize_path( $uploads['basedir'] ) );

if ( false !== $uploads['error'] ) {
$upload_basedir = false;
return $upload_basedir;
$upload_basedir_or_error = false;
} else {
$upload_basedir_or_error = $upload_basedir;
}

$upload_basedir = trailingslashit( wp_normalize_path( $uploads['basedir'] ) );

return $upload_basedir;
return $bypass_error ? $upload_basedir : $upload_basedir_or_error;
}

/**
Expand Down

0 comments on commit acd99a4

Please sign in to comment.