Force Regenerate Thumbnails - Version 2.0.0

Version Description

Download this release

Release Info

Developer Pedro Elsner
Plugin Icon 128x128 Force Regenerate Thumbnails
Version 2.0.0
Comparing to
See all releases

Code changes from version 1.8 to 2.0.0

Files changed (1) hide show
  1. force-regenerate-thumbnails.php +43 -25
force-regenerate-thumbnails.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Force Regenerate Thumbnails
4
  Plugin URI: http://pedroelsner.com/2012/08/forcando-a-atualizacao-de-thumbnails-no-wordpress
5
  Description: Delete and REALLY force the regenerate thumbnail.
6
- Version: 1.8
7
  Author: Pedro Elsner
8
  Author URI: http://www.pedroelsner.com/
9
  */
@@ -456,9 +456,9 @@ class ForceRegenerateThumbnails {
456
  exit;
457
  }
458
 
459
- $upload_dir = get_option('upload_path');
460
- if ((strrpos($image_fullpath, $upload_dir) === false)) {
461
- $image_fullpath = realpath($upload_dir . DIRECTORY_SEPARATOR . $image_fullpath);
462
  }
463
 
464
  if (!file_exists($image_fullpath)) {
@@ -470,7 +470,8 @@ class ForceRegenerateThumbnails {
470
  // 5 minutes per image should be PLENTY
471
  set_time_limit(900);
472
 
473
- $thumbnails_deleted = array();
 
474
 
475
  /**
476
  * New CORE 1.6 version
@@ -481,25 +482,35 @@ class ForceRegenerateThumbnails {
481
 
482
  // Hack to find thumbnail
483
  $file_info['filename'] .= '-';
 
484
 
485
  $path = opendir($file_info['dirname']);
486
  while (false !== ($thumb = readdir($path))) {
487
  if (!(strrpos($thumb, $file_info['filename']) === false)) {
488
- $thumb_fullpath = $file_info['dirname'] . DIRECTORY_SEPARATOR . $thumb;
489
- $thumb_info = pathinfo($thumb_fullpath);
490
- $valid_thumb = explode($file_info['filename'], $thumb_info['filename']);
491
- if ($valid_thumb[0] == "") {
492
- $dimension_thumb = explode('x', $valid_thumb[1]);
493
- if (count($dimension_thumb) == 2) {
494
- if (is_numeric($dimension_thumb[0]) && is_numeric($dimension_thumb[1])) {
495
- $thumbnails_deleted[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
496
- unlink(realpath($thumb_fullpath));
497
- }
498
- }
499
- }
500
  }
501
  }
502
- closedir($path);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
503
 
504
 
505
  /**
@@ -518,7 +529,7 @@ class ForceRegenerateThumbnails {
518
  }
519
 
520
  wp_update_attachment_metadata($image->ID, $metadata);
521
- $this->die_json_success_msg($image->ID, $image_fullpath, $thumbnails_deleted);
522
  exit;
523
 
524
  } catch (Exception $e) {
@@ -531,16 +542,23 @@ class ForceRegenerateThumbnails {
531
  * Helper to make a JSON success message
532
  *
533
  * @param integer $id
534
- * @param array $thumbnails_deleted
 
535
  * @access public
536
- * @since 1.8
537
  */
538
- function die_json_success_msg($id, $image_fullpath, $thumbnails_deleted) {
539
  $message = sprintf(__('<b>&quot;%s&quot; (ID %s)</b>', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $id);
540
  $message .= sprintf(__('<br />Original image: %s', 'force-regenerate-thumbnails'), $image_fullpath);
541
- if (is_array($thumbnails_deleted)) {
542
- if (count($thumbnails_deleted) > 0) {
543
- $message .= sprintf(__('<br />Deleted thumbnails: %s', 'force-regenerate-thumbnails'), implode(', ', $thumbnails_deleted));
 
 
 
 
 
 
544
  }
545
  }
546
  $message .= sprintf(__('<br /><b>All thumbnails was successfully regenerated in %s seconds</b>', 'force-regenerate-thumbnails'), timer_stop());
3
  Plugin Name: Force Regenerate Thumbnails
4
  Plugin URI: http://pedroelsner.com/2012/08/forcando-a-atualizacao-de-thumbnails-no-wordpress
5
  Description: Delete and REALLY force the regenerate thumbnail.
6
+ Version: 2.0.0
7
  Author: Pedro Elsner
8
  Author URI: http://www.pedroelsner.com/
9
  */
456
  exit;
457
  }
458
 
459
+
460
+ if ((strrpos($image_fullpath, get_option('upload_path')) === false)) {
461
+ $image_fullpath = realpath(get_option('upload_path') . DIRECTORY_SEPARATOR . $image_fullpath);
462
  }
463
 
464
  if (!file_exists($image_fullpath)) {
470
  // 5 minutes per image should be PLENTY
471
  set_time_limit(900);
472
 
473
+ $deleted_success = array();
474
+ $deleted_error = array();
475
 
476
  /**
477
  * New CORE 1.6 version
482
 
483
  // Hack to find thumbnail
484
  $file_info['filename'] .= '-';
485
+ $files = array();
486
 
487
  $path = opendir($file_info['dirname']);
488
  while (false !== ($thumb = readdir($path))) {
489
  if (!(strrpos($thumb, $file_info['filename']) === false)) {
490
+ $files[] = $thumb;
 
 
 
 
 
 
 
 
 
 
 
491
  }
492
  }
493
+ closedir($path);
494
+ sort($files);
495
+ foreach ($files as $thumb) {
496
+ $thumb_fullpath = $file_info['dirname'] . DIRECTORY_SEPARATOR . $thumb;
497
+ $thumb_info = pathinfo($thumb_fullpath);
498
+ $valid_thumb = explode($file_info['filename'], $thumb_info['filename']);
499
+ if ($valid_thumb[0] == "") {
500
+ $dimension_thumb = explode('x', $valid_thumb[1]);
501
+ if (count($dimension_thumb) == 2) {
502
+ if (is_numeric($dimension_thumb[0]) && is_numeric($dimension_thumb[1])) {
503
+ unlink(realpath($thumb_fullpath));
504
+ if (!file_exists(realpath($thumb_fullpath))) {
505
+ $deleted_success[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
506
+ } else {
507
+ $deleted_error[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
508
+ }
509
+ }
510
+ }
511
+ }
512
+ }
513
+
514
 
515
 
516
  /**
529
  }
530
 
531
  wp_update_attachment_metadata($image->ID, $metadata);
532
+ $this->die_json_end_with_success($image->ID, $image_fullpath, $deleted_success, $deleted_error);
533
  exit;
534
 
535
  } catch (Exception $e) {
542
  * Helper to make a JSON success message
543
  *
544
  * @param integer $id
545
+ * @param string $image_fullpath
546
+ * @param array $deleted_success
547
  * @access public
548
+ * @since 2.0.0
549
  */
550
+ function die_json_end_with_success($id, $image_fullpath, $deleted_success, $deleted_error) {
551
  $message = sprintf(__('<b>&quot;%s&quot; (ID %s)</b>', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $id);
552
  $message .= sprintf(__('<br />Original image: %s', 'force-regenerate-thumbnails'), $image_fullpath);
553
+ if (is_array($deleted_success)) {
554
+ if (count($deleted_success) > 0) {
555
+ $message .= sprintf(__('<br />Deleted success: %s', 'force-regenerate-thumbnails'), implode(', ', $deleted_success));
556
+ }
557
+ }
558
+ if (is_array($deleted_error)) {
559
+ if (count($deleted_error) > 0) {
560
+ $message .= sprintf(__('<br /><b><span style="color: #FF3366;">Deleted error: %s</span></b>', 'force-regenerate-thumbnails'), implode(', ', $deleted_error));
561
+ $message .= sprintf(__('<br /><span style="color: #FF3366;">Please, check the folder permission (chmod 777): %s</span>', 'force-regenerate-thumbnails'), get_option('upload_path'));
562
  }
563
  }
564
  $message .= sprintf(__('<br /><b>All thumbnails was successfully regenerated in %s seconds</b>', 'force-regenerate-thumbnails'), timer_stop());