Plugin Directory

Changeset 1302725


Ignore:
Timestamp:
12/08/2015 05:39:45 AM (10 years ago)
Author:
webdeveric
Message:

Works with WP 4.4

Location:
clean-media-library-file-names/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • clean-media-library-file-names/trunk/clean-media-library-file-names.php

    r911360 r1302725  
    44Plugin Group: Media Library
    55Plugin URI: http://phplug.in/
    6 Version: 0.2
     6Version: 0.3.1
    77Description: This plugin cleans uploaded file names to remove special characters and spaces.
    88Author: Eric King
     
    2323*********************************************/
    2424
    25 defined('ABSPATH') || die('What are you doing? You\'re not allowed back here!');
     25defined('ABSPATH') || exit;
    2626
    27 function wde_clean_media_library_file_names(array $data, $file, $filename, $mimes)
     27function wde_get_file_info( $file )
    2828{
    29     if (isset($data['ext'])) {
    30         $data['proper_filename'] = sanitize_title_with_dashes(
    31             substr($filename, 0, strlen($filename) - strlen($data['ext']) - 1),
    32             '',
    33             'save'
    34         ) . '.' . $data['ext'];
     29    if ( ! function_exists('finfo_file') ) {
     30        return false;
    3531    }
     32
     33    $finfo = finfo_open( FILEINFO_MIME_TYPE );
     34    $mime  = finfo_file( $finfo, $file );
     35    finfo_close( $finfo );
     36
     37    $extensions = array_search( $mime, wp_get_mime_types() );
     38
     39    if ( $extensions === false ) {
     40        return false;
     41    }
     42
     43    $ext = explode('|', $extensions);
     44
     45    return array(
     46        'ext'  => reset( $ext ),
     47        'type' => $mime
     48    );
     49}
     50
     51function wde_clean_media_library_file_names( array $data, $file, $filename, $mimes )
     52{
     53    $clean_name = sanitize_file_name( $filename );
     54
     55    if ( $filename === $clean_name ) {
     56        return $data;
     57    }
     58
     59    if ( $data['ext'] === false || $data['type'] === false ) {
     60        $info = wde_get_file_info( $file );
     61
     62        if ( $info !== false ) {
     63            $data = array_merge( $data, $info );
     64        }
     65    }
     66
     67    if ( $clean_name == '' || $clean_name == $data['ext'] ) {
     68
     69        if ( $data['ext'] !== false ) {
     70
     71            $clean_name = sprintf('%s-%s.%s', str_replace('/', '-', $data['type'] ), uniqid(), $data['ext'] );
     72
     73        } else {
     74
     75            $clean_name = sprintf('%s-%s', str_replace('/', '-', $data['type'] ), uniqid() );
     76
     77        }
     78
     79    }
     80
     81    $data['proper_filename'] = $clean_name;
     82
    3683    return $data;
    3784}
    38 add_filter('wp_check_filetype_and_ext', 'wde_clean_media_library_file_names', 10, 4);
     85
     86add_filter( 'wp_check_filetype_and_ext', 'wde_clean_media_library_file_names', 10, 4 );
  • clean-media-library-file-names/trunk/readme.txt

    r911667 r1302725  
    11=== Clean Media Library File Names ===
    2 Plugin Name: Clean Media Library File Names
    32Contributors: webdeveric
    4 Author URI: http://webdeveric.com/
    5 Plugin URI: http://phplug.in/
    63Tags: media library, filename, sanitize, special characters
    74Requires at least: 3.0.0
    8 Tested up to: 3.9.1
    9 Stable tag: 0.2
     5Tested up to: 4.4.0
     6Stable tag: 0.3.1
    107
    118This plugin cleans uploaded file names to remove special characters and spaces.
     
    1714== Installation ==
    1815
    19 1. Upload `clean-media-library-file-names` folder to the `/wp-content/plugins/` directory
     161. Upload `clean-media-library-filenames` folder to the `/wp-content/plugins/` directory
    20171. Activate the plugin through the 'Plugins' menu in WordPress
    21181. You're done
    2219
    2320== Changelog ==
     21
     22= 0.3.1 =
     23* Works with WordPress 4.4
     24
     25= 0.3 =
     26* Added default file name when the sanitized file name is blank. This could happen when the file name contains only special characters.
    2427
    2528= 0.2 =
Note: See TracChangeset for help on using the changeset viewer.