Add From Server - Version 3.4.3

Version Description

Download this release

Release Info

Developer dd32-githubsync
Plugin Icon wp plugin Add From Server
Version 3.4.3
Comparing to
See all releases

Code changes from version 3.4.2 to 3.4.3

Files changed (4) hide show
  1. add-from-server.php +3 -3
  2. changelog.txt +5 -0
  3. class.add-from-server.php +54 -15
  4. readme.md +7 -2
add-from-server.php CHANGED
@@ -2,9 +2,9 @@
2
  namespace dd32\WordPress\AddFromServer;
3
  /*
4
  * Plugin Name: Add From Server
5
- * Version: 3.4.2
6
  * Plugin URI: https://dd32.id.au/wordpress-plugins/add-from-server/
7
- * Description: Plugin to allow the Media Manager to add files from the webservers filesystem. <strong>Note:</strong> All files are copied to the uploads directory.
8
  * Author: Dion Hulse
9
  * Author URI: https://dd32.id.au/
10
  * Text Domain: add-from-server
@@ -16,7 +16,7 @@ if ( !is_admin() ) {
16
 
17
  const MIN_WP = '5.4';
18
  const MIN_PHP = '7.0';
19
- const VERSION = '3.4.2';
20
 
21
  // Dynamic constants must be define()'d.
22
  define( __NAMESPACE__ . '\PLUGIN', plugin_basename( __FILE__ ) );
2
  namespace dd32\WordPress\AddFromServer;
3
  /*
4
  * Plugin Name: Add From Server
5
+ * Version: 3.4.3
6
  * Plugin URI: https://dd32.id.au/wordpress-plugins/add-from-server/
7
+ * Description: Plugin to allow the Media Manager to add files from the webservers filesystem.
8
  * Author: Dion Hulse
9
  * Author URI: https://dd32.id.au/
10
  * Text Domain: add-from-server
16
 
17
  const MIN_WP = '5.4';
18
  const MIN_PHP = '7.0';
19
+ const VERSION = '3.4.3';
20
 
21
  // Dynamic constants must be define()'d.
22
  define( __NAMESPACE__ . '\PLUGIN', plugin_basename( __FILE__ ) );
changelog.txt CHANGED
@@ -1,3 +1,8 @@
 
 
 
 
 
1
  = 3.4.2 =
2
  * Restore case insensitive alphabetical sorting
3
 
1
+ = 3.4.3 =
2
+ * Better handling for `/` as the root path
3
+ * Better compatibility with certain WordPress docker images
4
+ * Better handling for some empty folders
5
+
6
  = 3.4.2 =
7
  * Restore case insensitive alphabetical sorting
8
 
class.add-from-server.php CHANGED
@@ -323,7 +323,7 @@ class Plugin {
323
  protected function get_default_dir() {
324
  $root = $this->get_root();
325
 
326
- if ( false !== str_starts_with( WP_CONTENT_DIR, $root ) ) {
327
  return WP_CONTENT_DIR;
328
  }
329
 
@@ -353,15 +353,13 @@ class Plugin {
353
  $cwd_relative = substr( $cwd, strlen( $root ) );
354
 
355
  // Make a list of the directories the user can enter.
356
- $dirparts = [
357
- esc_html( trailingslashit( dirname( $root ) ) ),
358
- '<a href="' . esc_url( add_query_arg( 'path', '/', $url ) ) . '">' . esc_html( basename( $root ) ) . '</a>'
359
- ];
360
 
361
  $dir_path = '';
362
- foreach ( explode( '/', $cwd_relative ) as $dir ) {
363
  $dir_path .= '/' . $dir;
364
- $dirparts[] = '<a href="' . esc_url( add_query_arg( 'path', rawurlencode( $dir_path ), $url ) ) . '">' . esc_html( $dir ) . '/</a> ';
365
  }
366
 
367
  $dirparts = implode( '', $dirparts );
@@ -388,6 +386,27 @@ class Plugin {
388
  return false;
389
  }
390
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
  if ( 1 === count( $files ) && is_dir( $files[0] ) ) {
392
  return $get_import_root( $files[0] );
393
  }
@@ -395,28 +414,46 @@ class Plugin {
395
  return $path;
396
  };
397
 
398
- array_walk( $directories, function( &$data, $path ) use( $root, $cwd_relative, $get_import_root ) {
 
 
 
 
 
 
 
 
 
399
  $import_root = $get_import_root( $path );
 
 
 
 
 
400
 
401
  $data = [
402
  'text' => substr(
403
- substr( $import_root, strlen( $root ) + 1 ),
404
  strlen( $cwd_relative )
405
  ) . '/',
406
- 'path' => substr( $import_root, strlen( $root ) + 1 )
407
  ];
 
 
408
  } );
409
 
 
 
410
  // Sort the directories case insensitively.
411
  uasort( $directories, $sort_by_text );
412
 
413
  // Prefix the parent directory.
414
- if ( str_starts_with( dirname( $cwd ), $root ) ) {
415
  $directories = array_merge(
416
  [
417
  dirname( $cwd ) => [
418
  'text' => __( 'Parent Folder', 'add-from-server' ),
419
- 'path' => substr( dirname( $cwd ), strlen( $root ) + 1 ) ?: '/',
420
  ]
421
  ],
422
  $directories
@@ -426,13 +463,13 @@ class Plugin {
426
  $files = array_flip( array_filter( $nodes, function( $node ) {
427
  return is_file( $node );
428
  } ) );
429
- array_walk( $files, function( &$data, $path ) use( $root ) {
430
  $importable = ( false !== wp_check_filetype( $path )['type'] || current_user_can( 'unfiltered_upload' ) );
431
  $readable = is_readable( $path );
432
 
433
  $data = [
434
  'text' => basename( $path ),
435
- 'file' => substr( $path, strlen( $root ) + 1 ),
436
  'importable' => $importable,
437
  'readable' => $readable,
438
  'error' => (
@@ -476,7 +513,7 @@ class Plugin {
476
  <td>&nbsp;</td>
477
  <td><a href="%s">%s</a></td>
478
  </tr>',
479
- esc_url( add_query_arg( 'path', $dir['path'], $url ) ),
480
  esc_html( $dir['text'] )
481
  );
482
  }
@@ -565,6 +602,8 @@ Thanks! Dion.', 'add-from-server' );
565
  $old_root = get_option( 'frmsvr_root', '' );
566
 
567
  if (
 
 
568
  str_contains( $old_root, '%' )
569
  &&
570
  ! defined( 'ADD_FROM_SERVER' )
323
  protected function get_default_dir() {
324
  $root = $this->get_root();
325
 
326
+ if ( str_starts_with( WP_CONTENT_DIR, $root ) ) {
327
  return WP_CONTENT_DIR;
328
  }
329
 
353
  $cwd_relative = substr( $cwd, strlen( $root ) );
354
 
355
  // Make a list of the directories the user can enter.
356
+ $dirparts = [];
357
+ $dirparts[] = '<a href="' . esc_url( add_query_arg( 'path', rawurlencode( '/' ), $url ) ) . '">' . esc_html( trailingslashit( $root ) ) . '</a> ';
 
 
358
 
359
  $dir_path = '';
360
+ foreach ( array_filter( explode( '/', $cwd_relative ) ) as $dir ) {
361
  $dir_path .= '/' . $dir;
362
+ $dirparts[] = '<a href="' . esc_url( add_query_arg( 'path', rawurlencode( $dir_path ), $url ) ) . '">' . esc_html( $dir ?: basename( $root ) ) . '/</a> ';
363
  }
364
 
365
  $dirparts = implode( '', $dirparts );
386
  return false;
387
  }
388
 
389
+ $has_files = false;
390
+ foreach ( $files as $i => $file ) {
391
+ if ( is_file( $file ) ) {
392
+ $has_files = true;
393
+ break;
394
+ } else {
395
+ if ( $get_import_root( $file ) ) {
396
+ $has_files = true;
397
+ break;
398
+ } else {
399
+ unset( $files[ $i ] );
400
+ }
401
+ }
402
+ }
403
+ if ( ! $has_files ) {
404
+ return false;
405
+ }
406
+
407
+ // Rekey the array incase anything was removed.
408
+ $files = array_values( $files );
409
+
410
  if ( 1 === count( $files ) && is_dir( $files[0] ) ) {
411
  return $get_import_root( $files[0] );
412
  }
414
  return $path;
415
  };
416
 
417
+ $get_root_relative_path = function( $path ) use( $root ) {
418
+ $root_offset = strlen( $root );
419
+ if ( '/' !== $root ) {
420
+ $root_offset += 1;
421
+ }
422
+
423
+ return substr( $path, $root_offset );
424
+ };
425
+
426
+ array_walk( $directories, function( &$data, $path ) use( $root, $cwd_relative, $get_import_root, $get_root_relative_path ) {
427
  $import_root = $get_import_root( $path );
428
+ if ( ! $import_root ) {
429
+ // Unreadable, etc.
430
+ $data = false;
431
+ return;
432
+ }
433
 
434
  $data = [
435
  'text' => substr(
436
+ $get_root_relative_path( $import_root ),
437
  strlen( $cwd_relative )
438
  ) . '/',
439
+ 'path' => $get_root_relative_path( $import_root )
440
  ];
441
+
442
+ $data['text'] = ltrim( $data['text'], '/' );
443
  } );
444
 
445
+ $directories = array_filter( $directories );
446
+
447
  // Sort the directories case insensitively.
448
  uasort( $directories, $sort_by_text );
449
 
450
  // Prefix the parent directory.
451
+ if ( str_starts_with( dirname( $cwd ), $root ) && dirname( $cwd ) != $cwd ) {
452
  $directories = array_merge(
453
  [
454
  dirname( $cwd ) => [
455
  'text' => __( 'Parent Folder', 'add-from-server' ),
456
+ 'path' => $get_root_relative_path( dirname( $cwd ) ) ?: '/',
457
  ]
458
  ],
459
  $directories
463
  $files = array_flip( array_filter( $nodes, function( $node ) {
464
  return is_file( $node );
465
  } ) );
466
+ array_walk( $files, function( &$data, $path ) use( $root, $get_root_relative_path ) {
467
  $importable = ( false !== wp_check_filetype( $path )['type'] || current_user_can( 'unfiltered_upload' ) );
468
  $readable = is_readable( $path );
469
 
470
  $data = [
471
  'text' => basename( $path ),
472
+ 'file' => $get_root_relative_path( $path ),
473
  'importable' => $importable,
474
  'readable' => $readable,
475
  'error' => (
513
  <td>&nbsp;</td>
514
  <td><a href="%s">%s</a></td>
515
  </tr>',
516
+ esc_url( add_query_arg( 'path', rawurlencode( $dir['path'] ), $url ) ),
517
  esc_html( $dir['text'] )
518
  );
519
  }
602
  $old_root = get_option( 'frmsvr_root', '' );
603
 
604
  if (
605
+ $old_root
606
+ &&
607
  str_contains( $old_root, '%' )
608
  &&
609
  ! defined( 'ADD_FROM_SERVER' )
readme.md CHANGED
@@ -5,13 +5,13 @@ Add From Server
5
  * Requires at least: 5.4
6
  * Tested up to: 5.5
7
  * Requires PHP: 7.0
8
- * Stable tag: 3.4.2
9
 
10
  Add From Server is designed to help ease the pain of bad web hosts, allowing you to upload files via FTP or SSH and later import them into WordPress.
11
 
12
  ## Description
13
 
14
- This plugin is officially unsupported. Please do not expect new features or bugfixes. Features may be removed at any time.
15
 
16
  Add From Server is designed to help ease the pain of bad web hosts, allowing you to upload files via FTP or SSH and later import them into WordPress.
17
 
@@ -32,6 +32,11 @@ https://developer.wordpress.org/cli/commands/media/import/
32
 
33
  ## Changelog
34
 
 
 
 
 
 
35
  ### 3.4.2
36
  * Restore case insensitive alphabetical sorting
37
 
5
  * Requires at least: 5.4
6
  * Tested up to: 5.5
7
  * Requires PHP: 7.0
8
+ * Stable tag: 3.4.3
9
 
10
  Add From Server is designed to help ease the pain of bad web hosts, allowing you to upload files via FTP or SSH and later import them into WordPress.
11
 
12
  ## Description
13
 
14
+ This plugin offers limited support. Please do not expect new features or too many bugfixes. Features may be removed at any time.
15
 
16
  Add From Server is designed to help ease the pain of bad web hosts, allowing you to upload files via FTP or SSH and later import them into WordPress.
17
 
32
 
33
  ## Changelog
34
 
35
+ ### 3.4.3
36
+ * Better handling for `/` as the root path
37
+ * Better compatibility with certain WordPress docker images
38
+ * Better handling for some empty folders
39
+
40
  ### 3.4.2
41
  * Restore case insensitive alphabetical sorting
42