Plugin Directory

Changeset 3418466


Ignore:
Timestamp:
12/12/2025 06:03:16 PM (2 months ago)
Author:
pipdig
Message:

Optimise image import process

Location:
blogger-importer-extended/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • blogger-importer-extended/trunk/bootstrap.php

    r3341752 r3418466  
    33Plugin Name: Blogger Importer Extended
    44Plugin URI: https://wordpress.org/plugins/blogger-importer-extended/
    5 Description: The only plugin you need to move from Blogger to WordPress. Import all your content and setup 301 redirects automatically.
     5Description: The only plugin you need to move from Blogger to WordPress. Import all content and setup 301 redirects automatically.
    66Author: pipdig
    77Version: 3.2.7
     
    3434define('BIE_PATH', plugin_dir_url(__FILE__));
    3535
    36  // Wait time between import batches
     36// Wait time between import batches
    3737if (!defined('BIE_WAIT_TIME')) {
    3838    define('BIE_WAIT_TIME', 1500);
     39}
     40
     41// Import the first 15 images from post content to avoid timeout
     42if (!defined('BIE_IMAGE_IMPORT_LIMIT')) {
     43    define('BIE_IMAGE_IMPORT_LIMIT', 15);
    3944}
    4045
     
    499504            $.post(ajaxurl, data, function(response) {
    500505               
    501                 //console.log(response);
     506                console.log(response);
    502507               
    503508                if (!checkIsJsonString(response)) {
     
    798803        foreach ($response->items as $item) {
    799804           
    800             $exists = (int) $wpdb->get_var( $wpdb->prepare('SELECT post_id FROM '.$wpdb->prefix.'bie_redirects WHERE blogger_post_id = %s', $item->id) );
     805            $exists = (int) $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM {$wpdb->prefix}bie_redirects WHERE blogger_post_id = %s LIMIT 1", $item->id) );
    801806           
    802807            if ($exists !== 0) {
     
    900905                foreach ($response->items as $item) {
    901906                   
    902                     $exists = (int) $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = %s AND meta_key = 'blogger_post_id'", $item->id) );
     907                    $exists = (int) $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value = %s AND meta_key = 'blogger_post_id' LIMIT 1", $item->id) );
    903908                   
    904909                    if ($exists !== 0) {
     
    10431048       
    10441049        $query_args['page_query'] = $response->nextPageToken; // request next page, keep other args
    1045                
     1050       
    10461051        $response = pipdig_blogger_get_response($query_args);
    10471052       
     
    11451150        }
    11461151       
     1152        $images = array_unique($images); // remove dupes
     1153       
    11471154        if (!empty($images)) {
    11481155           
    1149             $x = 0;
    1150            
    1151             foreach ($images as $found_image) {
    1152                
    1153                 // skip if returns 404
    1154                 $headers = get_headers($found_image, 1);
    1155                 if (isset($headers[0]) && strpos($headers[0], '404') !== false) {
    1156                     continue;
     1156            foreach ($images as $i => $found_image) {
     1157               
     1158                if ($i >= BIE_IMAGE_IMPORT_LIMIT) {
     1159                    break;
    11571160                }
    11581161               
    11591162                $found_image_original = $found_image; // keep original for later, we need it for str_replace in content
    11601163               
    1161                 /*
    1162                 preg_match('/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $found_image, $matches);
    1163                
    1164                 if (empty($matches[0])) {
    1165                     continue;
    1166                 }
    1167                 */
    1168                
    1169                 // check mime
    1170                 $file_size = getimagesize($found_image);
    1171                
    1172                 $lets_go = false;
    1173                
    1174                 if (!empty($file_size['mime'])) {
    1175                    
    1176                     $mime = strtolower($file_size['mime']);
    1177                    
    1178                     $allowed_types = ['jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp'];
    1179                    
    1180                     foreach ($allowed_types as $allowed_type) {
    1181                        
    1182                         if (strpos($mime, $allowed_type) !== false) {
    1183                             $file_ext = '.'.$allowed_type;
    1184                             $lets_go = true;
    1185                             break;
    1186                         }
    1187                        
    1188                     }
    1189                    
    1190                 }
    1191                
    1192                 if (!$lets_go) {
    1193                     continue;
    1194                 }
    1195                
    11961164                // urldecode twice to better rename imported Chinese characters. See support ticket #47809 for more info
    1197                 $name = urldecode(urldecode(wp_basename($found_image)));
    1198                
    1199                 // Are Chinese characters in $name?
    1200                 if (preg_match("/\p{Han}+/u", $name)) {
    1201                    
    1202                     // Chinese chars found, so urldecode twice for better filename
    1203                     $found_image = urldecode(urldecode($found_image)); // needed to overcome long filenames.
    1204                    
    1205                 } else {
    1206                     // No Chinese characters found, so revert back to standard filename.
    1207                     // TODO - This might not be necessary if it is safe to double urldecode any filenames. Needs more testing to confirm.
    1208                     $name = wp_basename($found_image);
    1209                 }
     1165                $name = wp_basename(urldecode(urldecode($found_image)));
     1166                $found_image = urldecode(urldecode($found_image));
    12101167               
    12111168                if (empty($name)) {
    12121169                    continue;
    12131170                }
     1171               
     1172                $tmp = download_url($found_image);
     1173                if (is_wp_error($tmp)) {
     1174                    continue;
     1175                }
     1176
     1177                $info = getimagesize($tmp);
     1178                if (!$info || empty($info['mime'])) {
     1179                    @unlink($tmp);
     1180                    continue;
     1181                }
     1182               
     1183                $ext_map = [
     1184                    'image/jpeg' => '.jpg',
     1185                    'image/png'  => '.png',
     1186                    'image/gif'  => '.gif',
     1187                    'image/webp' => '.webp',
     1188                ];
     1189
     1190                $file_ext = isset($ext_map[$info['mime']]) ? $ext_map[$info['mime']] : '.jpg';
    12141191               
    12151192                // Some filesystems can't handle long filenames. So fallback to post slug.
     
    12301207                $file = array(
    12311208                    'name' => $name,
    1232                     'tmp_name' => download_url($found_image),
     1209                    'tmp_name' => $tmp,
    12331210                );
    12341211               
    1235                 if (is_wp_error($file['tmp_name'])) {
    1236                     @unlink($file['tmp_name']);
    1237                     continue;
    1238                 }
    1239                
    1240                 // remove extension if it's ther. E.g. rtim .jpeg first, then re-add it after. Needed in case there isn't an extension already set
    1241                 $filename = rtrim($file['name'], $file_ext).$file_ext;
     1212                // remove extension if it's there. E.g. rtim .jpeg first, then re-add it after. Needed in case there isn't an extension already set
     1213                $filename = pathinfo($file['name'], PATHINFO_FILENAME).$file_ext;
    12421214               
    12431215                $image_id = media_handle_sideload($file, $post_id, $filename, array('post_date' => $post_date, 'post_author' => $author_id));
     
    12601232                   
    12611233                    // if this is the first image, include it in the return as the featured_image_id
    1262                     if ($x === 0) {
     1234                    if ($i === 0) {
    12631235                       
    12641236                        $featured_image_id = $image_id;
     
    12711243                    }
    12721244                   
    1273                     $x++;
    1274                    
    12751245                }
    12761246               
  • blogger-importer-extended/trunk/redirects.php

    r3018368 r3418466  
    9898       
    9999    }
    100        
     100   
    101101    if (!$redirect) {
    102102        if (isset($options['redirect_404s']) && $options['redirect_404s']) {
     
    195195    jQuery(document).ready(function($) {
    196196       
    197         var hrefBlogger301Redirect = $('tr[data-slug="blogger-301-redirect"] .deactivate a').attr('href');
     197        let hrefBlogger301Redirect = $('tr[data-slug="blogger-301-redirect"] .deactivate a').attr('href');
    198198        if (hrefBlogger301Redirect) {
    199199            $('#showDeactivateBlogger301RedirectText').show();
     
    201201        $('#deactivateBlogger301RedirectPlugin').attr('href', hrefBlogger301Redirect);
    202202       
    203         var hrefBloggerToWordPress = $('tr[data-slug="blogger-to-wordpress-redirection"] .deactivate a').attr('href');
     203        let hrefBloggerToWordPress = $('tr[data-slug="blogger-to-wordpress-redirection"] .deactivate a').attr('href');
    204204        if (hrefBloggerToWordPress) {
    205205            $('#showDeactivateBloggerToWordPressText').show();
    206206        }
    207207        $('#deactivateBloggerToWordPress').attr('href', hrefBloggerToWordPress);
     208       
    208209    });
    209210    </script>
  • blogger-importer-extended/trunk/settings.php

    r3147993 r3418466  
    3131    #bieNoticeOk {
    3232        display: block !important;
    33         /* max-width: 691px; */
    3433    }
    3534    .card {
     
    350349   
    351350    die;
     351   
    352352});
  • blogger-importer-extended/trunk/uninstall.php

    r2566267 r3418466  
    1010
    1111global $wpdb;
    12 $results = $wpdb->get_results("SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'bie_page_token_%'");
     12$results = $wpdb->get_results("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'bie_page_token_%'");
    1313foreach ($results as $result) {
    1414    delete_option($result->option_name);
     
    1616
    1717/*
    18 // Do we want to delete the redirects on uninstall?
    19 // Todo - add option to settings page
     18// Do we want to delete the redirects on uninstall? Maybe not
    2019global $wpdb;
    2120$table_name = $wpdb->prefix.'bie_redirects';
Note: See TracChangeset for help on using the changeset viewer.