WordPress.org

Make WordPress Core

Ticket #22157: readymadeweb-filetype-HEAD.patch

File readymadeweb-filetype-HEAD.patch, 4.1 KB (added by ReadyMadeWeb, 6 years ago)

Patch to WordPress Importer

  • wordpress-importer.php

     
    838838                wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
    839839
    840840                // remap resized image URLs, works by stripping the extension and remapping the URL stub.
    841                 if ( preg_match( '!^image/!', $info['type'] ) ) {
     841                // Note that we don't do this in the case that the original upload didn't
     842                // have a valid extension, but we were able to determine one by looking at
     843                // the relevant HTTP headers (as denoted by the
     844                // 'force_extension_in_replacement' flag)
     845                if ( preg_match( '!^image/!', $info['type'] ) && empty($upload['force_extension_in_replacement'])) {
    842846                        $parts = pathinfo( $url );
    843847                        $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
    844848
     
    859863         * @return array|WP_Error Local file location details on success, WP_Error otherwise
    860864         */
    861865        function fetch_remote_file( $url, $post ) {
     866
    862867                // extract the file name and extension from the url
    863868                $file_name = basename( $url );
    864869
     870                // Some systems, like moveable-type / typepad, advertise files w/o
     871                // extension information attached.      We can get around this limitation
     872                // by grabbing information about the file from the webserver
     873                $file_type = wp_check_filetype($file_name);
     874
     875                // If there is a situation where we're adding a file extension
     876                // onto an upload that didn't originally have one, we
     877                // can tell the remapper to not strip off the extension/
     878                $force_extension_in_replacement = false;
     879
     880                if ( empty( $file_type['ext'] ) ) {
     881
     882                        $retured_file_info = $this->fetch_attachment_info_from_server( $url );
     883
     884                        if ( $retured_file_info ) {
     885                                $file_name = $retured_file_info['filename'];
     886                                $force_extension_in_replacement = true;
     887                        }
     888                }
     889
    865890                // get placeholder file in the upload dir with a unique, sanitized filename
    866891                $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
    867892                if ( $upload['error'] )
     
    907932                if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
    908933                        $this->url_remap[$headers['x-final-location']] = $upload['url'];
    909934
     935                if ($force_extension_in_replacement)
     936                        $upload['force_extension_in_replacement'] = true;
    910937                return $upload;
    911938        }
    912939
     
    10811108        function cmpr_strlen( $a, $b ) {
    10821109                return strlen($b) - strlen($a);
    10831110        }
     1111
     1112        /**
     1113         * Attempts to grab information about a file from a URL.  This
     1114         * call just requests the header information from the server (the
     1115         * body of the resource isn't fetched) so the call is relativly lite.
     1116         *
     1117         * @param string $url
     1118         *   A valid url to request mime info from
     1119         *
     1120         * @return array|bool
     1121         *   If the server didn't return a valid header, false is returned.  Otherwise,
     1122         *   an array with the following keys is returned:
     1123         *     'ext' (string):  The extension of the file, if avaiable
     1124         *     'type' (string): The advertised mime type of the resource, if available
     1125         *     'filename' (string):
     1126         *                      The suggested name of the file, if available
     1127         */
     1128        function fetch_attachment_info_from_server($url) {
     1129
     1130                $results = array(
     1131                        'ext' => '',
     1132                        'type' => '',
     1133                        'filename' => '',
     1134                );
     1135
     1136                $header_info = wp_remote_head( $url );
     1137
     1138                if ( is_wp_error( $header_info ) ) {
     1139                        return false;
     1140                }
     1141
     1142                $content_type = wp_remote_retrieve_header( $header_info, 'content-type' );
     1143                if ( $content_type ) {
     1144                        $results['type'] = trim( $content_type );
     1145                }
     1146
     1147                $display_disposition = wp_remote_retrieve_header( $header_info, 'content-disposition' );
     1148                $file_name_pattern = '/filename=([^\s]+)/i';
     1149                $file_name_matches = array();
     1150                if ( preg_match( $file_name_pattern, $display_disposition, $file_name_matches ) ) {
     1151                        $results['filename'] = trim( $file_name_matches[1] );
     1152
     1153                        if ( ( $index = strripos( $results['filename'], '.' ) ) !== false ) {
     1154                                $results['ext'] = substr( $results['filename'], $index + 1 );
     1155                        }
     1156                }
     1157
     1158                return $results;
     1159        }
    10841160}
    10851161
    10861162} // class_exists( 'WP_Importer' )