settings = Plugin::get_settings( 'media' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['clean_upload_file_names'] ) ) { return; } add_filter( 'wp_handle_upload_prefilter', [ $this, 'filter_upload' ] ); add_filter( 'wp_handle_sideload_prefilter', [ $this, 'filter_upload' ] ); } /** * Adds the settings field. * * @param array $fields The existing settings fields. * * @return array */ public function adminease_settings_fields( array $fields ): array { $fields['media']['fields'][] = [ 'type' => 'switch', 'id' => 'clean-upload-file-names', 'name' => 'adminease[media][clean_upload_file_names]', 'value' => $this->settings['clean_upload_file_names'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Clean Upload File Names', 'adminease' ), 'risk' => 'safe', 'description' => __( 'Save new uploads with a clean file name: lowercase, accents removed, spaces and odd characters turned into dashes. Photo Final (2).JPG is saved as photo-final-2.jpg. This avoids broken image links on some servers, CDNs and in emails. Files already in the media library are not renamed. Letters of other alphabets, such as Hebrew, are kept.', 'adminease' ), ]; return $fields; } /** * Cleans the name of the file about to be uploaded. * * @param mixed $file One entry of $_FILES. * * @return mixed */ public function filter_upload( $file ) { if( is_array( $file ) && isset( $file['name'] ) && is_string( $file['name'] ) ) { $file['name'] = self::clean( $file['name'] ); } return $file; } /** * Cleans a file name. * * Only the name before the last dot changes, plus the extension in lowercase. If nothing readable is * left of the name, the original is returned so the upload still works. * * @param string $name The file name as uploaded. * * @return string */ public static function clean( string $name ): string { $extension = pathinfo( $name, PATHINFO_EXTENSION ); $base = '' === $extension ? $name : substr( $name, 0, -( strlen( $extension ) + 1 ) ); if( class_exists( 'Normalizer' ) ) { $normalized = \Normalizer::normalize( $base, \Normalizer::FORM_C ); $base = is_string( $normalized ) ? $normalized : $base; } $clean = remove_accents( $base ); $clean = function_exists( 'mb_strtolower' ) ? mb_strtolower( $clean, 'UTF-8' ) : strtolower( $clean ); $clean = preg_replace( '/[^\p{L}\p{M}\p{N}._-]+/u', '-', $clean ); $clean = is_string( $clean ) ? preg_replace( '/-{2,}/', '-', $clean ) : ''; $clean = is_string( $clean ) ? preg_replace( '/-*\.-*/', '.', $clean ) : ''; $clean = is_string( $clean ) ? trim( $clean, "-. \t\n\r\0\x0B" ) : ''; if( '' === $clean ) { return $name; } $extension = preg_match( '/^[A-Za-z0-9]+$/', $extension ) ? strtolower( $extension ) : $extension; $result = '' === $extension ? $clean : $clean . '.' . $extension; /** * Filters the cleaned upload file name. * * @param string $result The cleaned name. * @param string $name The name as uploaded. */ $filtered = apply_filters( 'adminease_clean_upload_file_name', $result, $name ); return is_string( $filtered ) && '' !== $filtered ? $filtered : $result; } }