Arkadaşlar aşağıdaki eklentinin kodlarında bir hata var, geliştirici çözmüyor ve bu durum benim canımı sıkıyor zira aktif olarak işime yarıyor. Sorunu yazmadan önce eklentinin ne işe yaradığını açıklayayım; Wordpress siteye yüklenen görsellerin isimlerini düzeltiyor. Sorun işe Yüklenen görselin başında
Türkçe karakter varsa o karakteri düzelmek yerine siliyor. Mesela "Çanakkale fotoğrafı.jpg" dosyasını "canakkale-fotografi.jpg" yapması gerekirken Ç'yi silip "
anakkale-fotografi.jpg" olarak yüklüyor. Bulan biri çıkarsa çok mutlu olacağım. /**
* Clean Image Filenames.
*/
class CleanImageFilenames {
/**
* Plugin settings.
*
* @since 1.1
*
* @var array $plugin_settings Plugin settings for version, default mime types.
*/
public $plugin_settings = array(
'version' => '1.3',
'default_mime_types' => array(
'image/gif',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/tiff',
),
);
/**
* Sets up hooks, actions and filters that the plugin responds to.
*
* @since 1.0
*/
public function __construct() {
register_activation_hook( __FILE__, array( $this, 'plugin_activation' ) );
add_action( 'wp_handle_upload_prefilter', array( $this, 'upload_filter' ) );
add_action( 'add_attachment', array( $this, 'update_attachment_title' ) );
}
/**
* Checks whether or not the current file should be cleaned.
*
* This function runs when files are being uploaded to the WordPress media
* library. The function checks if the clean_image_filenames_mime_types filter
* has been used and overrides other settings if it has. Otherwise, the plugin
* settings are used.
*
* If a file shall be cleaned or not is checked by comparing the current file's
* mime type to the list of mime types to be cleaned.
*
* @since 1.1 Added more complex checks and moved the actual cleaning to clean_filename().
* @since 1.0
*
* @param array $file The file information including the filename in $file['name'].
* @return array $file The file information with the cleaned or original filename.
*/
public function upload_filter( $file ) {
// Save original filename in transient and add to attachment post later.
$original_filename = pathinfo( $file['name'] );
set_transient( '_clean_image_filenames_original_filename', $original_filename['filename'], 60 );
// Get mime type settings.
$mime_types_setting = get_option( 'clean_image_filenames_mime_types' );
$default_mime_types = $this->plugin_settings['default_mime_types'];
$valid_mime_types = apply_filters( 'clean_image_filenames_mime_types', $default_mime_types );
if ( $valid_mime_types !== $default_mime_types ) {
// Use mime types defined with filter.
if ( in_array( $file['type'], $valid_mime_types, true ) ) {
$file = $this->clean_filename( $file );
}
} else {
// Use mime types as defined in settings.
if ( 'all' === $mime_types_setting ) {
$file = $this->clean_filename( $file );
} elseif ( 'images' === $mime_types_setting && in_array( $file['type'], $default_mime_types, true ) ) {
$file = $this->clean_filename( $file );
}
}
// Return cleaned file, or input file file type is not set to be cleaned.
return $file;
}
/**
* Clean filename.
*
* Performs the cleaning of filenames. It replaces whitespaces, some specific
* chacters and uses WordPress' remove_accents() function for the rest. It
* also converts filenames to lowercase.
*
* @since 1.3 Rewrote filename cleaning to better handle specific characters.
* @since 1.1 Added function.
*
* @param array $file Uploaded file details.
* @return array $file Uploaded file details with cleaned filename.
*/
public function clean_filename( $file ) {
// Get file details.
$file_pathinfo = pathinfo( $file['name'] );
// Replace whitespaces with dashes.
$cleaned_filename = str_replace( ' ', '-', $file_pathinfo['filename'] );
// Specific replacements not handled at all or not handled well by remove_accents().
$specific_replacements = array(
'Ğ' => 'g',
'ğ' => 'g',
'Ü' => 'u',
'ü' => 'u',
'Ş' => 's',
'ş' => 's',
'İ' => 'i',
'ı' => 'i',
'Ö' => 'o',
'ö' => 'o',
'Ç' => 'c',
'ç' => 'c',
'_' => '-',
'%20' => '-',
);
// Replace specific characters.
$cleaned_filename = str_replace( array_keys( $specific_replacements ), array_values( $specific_replacements ), $cleaned_filename );
// Convert characters to ASCII equivalents.
$cleaned_filename = remove_accents( $cleaned_filename );
// Convert filename to lowercase.
$cleaned_filename = strtolower( $cleaned_filename );
// Remove characters that are not a-z, 0-9, or - (dash).
$cleaned_filename = preg_replace( '/[^a-z0-9-]/', '', $cleaned_filename );
// Remove multiple dashes in a row.
$cleaned_filename = preg_replace( '/-+/', '-', $cleaned_filename );
// Trim potential leftover dashes at each end of filename.
$cleaned_filename = trim( $cleaned_filename, '-' );
// Replace original filename with cleaned filename.
$file['name'] = $cleaned_filename . '.' . $file_pathinfo['extension'];
return $file;
}
/**
* Save original filename to attachment title.
*
* The original, un-cleaned filename is saved as a transient called
* _clean_image_filenames_original_filename just before the filename is cleaned
* and saved. When WordPress adds the attachment to the database, this function
* picks up the original filename from the transient and saves it as the
* attachment title.
*
* @since 1.2
*
* @param int $attachment_id Attachment post ID.
*/
public function update_attachment_title( $attachment_id ) {
$original_filename = get_transient( '_clean_image_filenames_original_filename' );
if ( $original_filename ) {
// Update attachment post.
wp_update_post(
array(
'ID' => $attachment_id,
'post_title' => $original_filename,
)
);
// Delete transient.
delete_transient( '_clean_image_filenames_original_filename' );
}
}
}
new CleanImageFilenames();