Force Regenerate Thumbnails - Version 2.0.2

Version Description

  • New style for results (thanks @justonarnar)
  • Automatic update "wp_attachment_metadata" and "_wp_attachedfile" (thanks @norecipes)
Download this release

Release Info

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

Code changes from version 2.0.1 to 2.0.2

Files changed (3) hide show
  1. force-regenerate-thumbnails.php +145 -100
  2. readme.txt +4 -3
  3. style.css +12 -0
force-regenerate-thumbnails.php CHANGED
@@ -3,12 +3,17 @@
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.1
7
  Author: Pedro Elsner
8
  Author URI: http://www.pedroelsner.com/
9
  */
10
 
11
 
 
 
 
 
 
12
  /**
13
  * Force GD for Image handle (WordPress 3.5 or better)
14
  * Thanks (@nikcree)
@@ -62,7 +67,7 @@ class ForceRegenerateThumbnails {
62
  function ForceRegenerateThumbnails() {
63
 
64
  load_plugin_textdomain('force-regenerate-thumbnails', false, '/force-regenerate-thumbnails/localization');
65
-
66
  add_action('admin_menu', array(&$this, 'add_admin_menu'));
67
  add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueues'));
68
  add_action('wp_ajax_regeneratethumbnail', array(&$this, 'ajax_process_image'));
@@ -107,6 +112,7 @@ class ForceRegenerateThumbnails {
107
 
108
  wp_enqueue_script('jquery-ui-progressbar', plugins_url('jquery-ui/jquery.ui.progressbar.min.1.7.2.js', __FILE__), array('jquery-ui-core'), '1.7.2');
109
  wp_enqueue_style('jquery-ui-regenthumbs', plugins_url('jquery-ui/redmond/jquery-ui-1.7.2.custom.css', __FILE__), array(), '1.7.2');
 
110
  }
111
 
112
  /**
@@ -426,7 +432,10 @@ class ForceRegenerateThumbnails {
426
  $id = (int) $_REQUEST['id'];
427
 
428
  try {
429
-
 
 
 
430
  header('Content-type: application/json');
431
  $image = get_post($id);
432
 
@@ -441,66 +450,80 @@ class ForceRegenerateThumbnails {
441
  if (!current_user_can($this->capability)) {
442
  throw new Exception(__('Your user account does not have permission to regenerate images.', 'force-regenerate-thumbnails'));
443
  }
444
-
445
- // Get original image
446
- $image_fullpath = get_attached_file($image->ID);
447
-
448
- if (false === $image_fullpath) {
449
- throw new Exception(__('The originally uploaded image file cannot be found.', 'force-regenerate-thumbnails'));
450
- }
451
-
452
- if (strlen($image_fullpath) == 0) {
453
- throw new Exception(__('The originally uploaded image file cannot be found.', 'force-regenerate-thumbnails'));
454
- }
455
-
456
-
457
- /**
458
  * Fix for get_option('upload_path')
459
  * Thanks (@DavidLingren)
460
  *
461
  * @since 2.0.1
462
  */
463
  $upload_dir = wp_upload_dir();
464
- $upload_dir['basedir'];
465
-
466
- if ((strrpos($image_fullpath, $upload_dir['basedir']) === false)) {
467
- $image_fullpath = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $image_fullpath;
468
- }
469
-
470
- if (realpath($image_fullpath) === false) {
471
- throw new Exception(sprintf(__('The originally uploaded image file cannot be found at "%s".', 'force-regenerate-thumbnails'), esc_html((string) $image_fullpath)));
 
 
 
 
 
 
 
 
 
 
472
  }
473
-
474
- if (!file_exists($image_fullpath)) {
475
- throw new Exception(sprintf(__('The originally uploaded image file cannot be found at "%s".', 'force-regenerate-thumbnails'), esc_html((string) $image_fullpath)));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  }
 
 
 
 
 
 
 
 
 
477
 
478
- // 5 minutes per image should be PLENTY
479
- set_time_limit(900);
480
 
481
- $deleted_success = array();
482
- $deleted_error = array();
 
 
483
 
484
- /**
485
- * New CORE
486
- *
487
- * @since 1.6
488
- */
489
- $thumbnails = array();
490
  $file_info = pathinfo($image_fullpath);
 
491
 
492
 
493
- /*
494
- * Verify thumbnail in POSTS
495
- $posts = array();
496
- $guid = explode($file_info['filename'], $image->guid);
497
- $guid = $guid[0];
498
- */
499
-
500
- // Hack to find thumbnail
501
- $file_info['filename'] .= '-';
502
  $files = array();
503
-
504
  $path = opendir($file_info['dirname']);
505
  while (false !== ($thumb = readdir($path))) {
506
  if (!(strrpos($thumb, $file_info['filename']) === false)) {
@@ -519,80 +542,102 @@ class ForceRegenerateThumbnails {
519
  if (is_numeric($dimension_thumb[0]) && is_numeric($dimension_thumb[1])) {
520
  unlink($thumb_fullpath);
521
  if (!file_exists($thumb_fullpath)) {
522
- $deleted_success[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
523
  } else {
524
- $deleted_error[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
525
  }
526
  }
527
  }
528
  }
529
  }
530
 
531
-
532
- /**
533
- * Regenerate
534
  */
535
  $metadata = wp_generate_attachment_metadata($image->ID, $image_fullpath);
536
-
537
  if (is_wp_error($metadata)) {
538
- $this->die_json_error_msg($image->ID, $metadata->get_error_message());
539
- exit;
540
  }
541
-
542
  if (empty($metadata)) {
543
  throw new Exception(__('Unknown failure reason.', 'force-regenerate-thumbnails'));
544
  }
545
-
546
  wp_update_attachment_metadata($image->ID, $metadata);
547
- $this->die_json_end_with_success($image->ID, $image_fullpath, $deleted_success, $deleted_error);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548
 
549
  } catch (Exception $e) {
550
- $this->die_json_failure_msg($id, '<b><span style="color: #FF3366;">' . $e->getMessage() . '</span></b>');
551
  }
552
 
553
  exit;
554
  }
555
 
556
- /**
557
- * Helper to make a JSON success message
558
- *
559
- * @param integer $id
560
- * @param string $image_fullpath
561
- * @param array $deleted_success
562
- * @access public
563
- * @since 2.0.0
564
- */
565
- function die_json_end_with_success($id, $image_fullpath, $deleted_success, $deleted_error) {
566
- $message = sprintf(__('<b>&quot;%s&quot; (ID %s)</b>', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $id);
567
- $message .= sprintf(__('<br />Original image: %s', 'force-regenerate-thumbnails'), $image_fullpath);
568
- if (is_array($deleted_success)) {
569
- if (count($deleted_success) > 0) {
570
- $message .= sprintf(__('<br />Deleted success: %s', 'force-regenerate-thumbnails'), implode(', ', $deleted_success));
571
- }
572
- }
573
- if (is_array($deleted_error)) {
574
- if (count($deleted_error) > 0) {
575
- $upload_dir = wp_upload_dir();
576
- $message .= sprintf(__('<br /><b><span style="color: #FF3366;">Deleted error: %s</span></b>', 'force-regenerate-thumbnails'), implode(', ', $deleted_error));
577
- $message .= sprintf(__('<br /><span style="color: #FF3366;">Please, check the folder permission (chmod 777): %s</span>', 'force-regenerate-thumbnails'), $upload_dir['basedir']);
578
- }
579
- }
580
- $message .= sprintf(__('<br /><b>All thumbnails was successfully regenerated in %s seconds</b>', 'force-regenerate-thumbnails'), timer_stop());
581
- die(json_encode(array('success' => $message)));
582
- }
583
-
584
-
585
- /**
586
- * Helper to make a JSON error message
587
- *
588
- * @param integer $id
589
- * @param string #message
590
- * @access public
591
- * @since 1.0
592
- */
593
- function die_json_error_msg($id, $message) {
594
- die(json_encode(array('error' => sprintf(__('&quot;%s&quot; (ID %s)<br />%s', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $id, $message))));
595
- }
596
 
597
  /**
598
  * Helper to make a JSON failure message
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.2
7
  Author: Pedro Elsner
8
  Author URI: http://www.pedroelsner.com/
9
  */
10
 
11
 
12
+ /*
13
+ Bebug URL
14
+ wp-admin/admin-ajax.php?action=regeneratethumbnail&id=
15
+ */
16
+
17
  /**
18
  * Force GD for Image handle (WordPress 3.5 or better)
19
  * Thanks (@nikcree)
67
  function ForceRegenerateThumbnails() {
68
 
69
  load_plugin_textdomain('force-regenerate-thumbnails', false, '/force-regenerate-thumbnails/localization');
70
+
71
  add_action('admin_menu', array(&$this, 'add_admin_menu'));
72
  add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueues'));
73
  add_action('wp_ajax_regeneratethumbnail', array(&$this, 'ajax_process_image'));
112
 
113
  wp_enqueue_script('jquery-ui-progressbar', plugins_url('jquery-ui/jquery.ui.progressbar.min.1.7.2.js', __FILE__), array('jquery-ui-core'), '1.7.2');
114
  wp_enqueue_style('jquery-ui-regenthumbs', plugins_url('jquery-ui/redmond/jquery-ui-1.7.2.custom.css', __FILE__), array(), '1.7.2');
115
+ wp_enqueue_style('plugin-custom-style', plugins_url('style.css', __FILE__), array(), '2.0.1');
116
  }
117
 
118
  /**
432
  $id = (int) $_REQUEST['id'];
433
 
434
  try {
435
+
436
+ // 5 minutes per image should be PLENTY
437
+ set_time_limit(900);
438
+
439
  header('Content-type: application/json');
440
  $image = get_post($id);
441
 
450
  if (!current_user_can($this->capability)) {
451
  throw new Exception(__('Your user account does not have permission to regenerate images.', 'force-regenerate-thumbnails'));
452
  }
453
+
454
+
455
+ /**
 
 
 
 
 
 
 
 
 
 
 
456
  * Fix for get_option('upload_path')
457
  * Thanks (@DavidLingren)
458
  *
459
  * @since 2.0.1
460
  */
461
  $upload_dir = wp_upload_dir();
462
+
463
+ // Get original image
464
+ $image_fullpath = get_attached_file($image->ID);
465
+
466
+
467
+ // Can't get image path
468
+ if (false === $image_fullpath || strlen($image_fullpath) == 0) {
469
+
470
+ // Try get image path from url
471
+ if ((strrpos($image->guid, $upload_dir['baseurl']) !== false)) {
472
+ $image_fullpath = realpath($upload_dir['basedir'] . DIRECTORY_SEPARATOR . substr($image->guid, strlen($upload_dir['baseurl']), strlen($image->guid)));
473
+ if (realpath($image_fullpath) === false) {
474
+ throw new Exception(sprintf(__('The originally uploaded image file cannot be found at &quot;%s&quot;.', 'force-regenerate-thumbnails'), esc_html((string) $image_fullpath)));
475
+ }
476
+ } else {
477
+ throw new Exception(__('The originally uploaded image file cannot be found.', 'force-regenerate-thumbnails'));
478
+ }
479
+
480
  }
481
+
482
+ // Image path incomplete
483
+ if ((strrpos($image_fullpath, $upload_dir['basedir']) === false)) {
484
+ $image_fullpath = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $image_fullpath;
485
+ }
486
+
487
+ // Image don't exists
488
+ if (!file_exists($image_fullpath) || realpath($image_fullpath) === false) {
489
+
490
+ // Try get image path from url
491
+ if ((strrpos($image->guid, $upload_dir['baseurl']) !== false)) {
492
+ $image_fullpath = realpath($upload_dir['basedir'] . DIRECTORY_SEPARATOR . substr($image->guid, strlen($upload_dir['baseurl']), strlen($image->guid)));
493
+ if (realpath($image_fullpath) === false) {
494
+ throw new Exception(sprintf(__('The originally uploaded image file cannot be found at &quot;%s&quot;.', 'force-regenerate-thumbnails'), esc_html((string) $image_fullpath)));
495
+ }
496
+ } else {
497
+ throw new Exception(sprintf(__('The originally uploaded image file cannot be found at &quot;%s&quot;.', 'force-regenerate-thumbnails'), esc_html((string) $image_fullpath)));
498
+ }
499
+
500
  }
501
+
502
+
503
+ /**
504
+ * Update META POST
505
+ * Thanks (@norecipes)
506
+ *
507
+ * @since 2.0.2
508
+ */
509
+ update_attached_file($image->ID, realpath($image_fullpath));
510
 
 
 
511
 
512
+ // Results
513
+ $thumb_deleted = array();
514
+ $thumb_error = array();
515
+ $thumb_regenerate = array();
516
 
517
+
518
+ // Hack to find thumbnail
 
 
 
 
519
  $file_info = pathinfo($image_fullpath);
520
+ $file_info['filename'] .= '-';
521
 
522
 
523
+ /**
524
+ * Try delete all thumbnails
525
+ */
 
 
 
 
 
 
526
  $files = array();
 
527
  $path = opendir($file_info['dirname']);
528
  while (false !== ($thumb = readdir($path))) {
529
  if (!(strrpos($thumb, $file_info['filename']) === false)) {
542
  if (is_numeric($dimension_thumb[0]) && is_numeric($dimension_thumb[1])) {
543
  unlink($thumb_fullpath);
544
  if (!file_exists($thumb_fullpath)) {
545
+ $thumb_deleted[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
546
  } else {
547
+ $thumb_error[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
548
  }
549
  }
550
  }
551
  }
552
  }
553
 
554
+
555
+ /**
556
+ * Regenerate all thumbnails
557
  */
558
  $metadata = wp_generate_attachment_metadata($image->ID, $image_fullpath);
 
559
  if (is_wp_error($metadata)) {
560
+ throw new Exception($metadata->get_error_message());
 
561
  }
 
562
  if (empty($metadata)) {
563
  throw new Exception(__('Unknown failure reason.', 'force-regenerate-thumbnails'));
564
  }
 
565
  wp_update_attachment_metadata($image->ID, $metadata);
566
+
567
+
568
+ /**
569
+ * Verify results (deleted, errors, success)
570
+ */
571
+ $files = array();
572
+ $path = opendir($file_info['dirname']);
573
+ while (false !== ($thumb = readdir($path))) {
574
+ if (!(strrpos($thumb, $file_info['filename']) === false)) {
575
+ $files[] = $thumb;
576
+ }
577
+ }
578
+ closedir($path);
579
+ sort($files);
580
+ foreach ($files as $thumb) {
581
+ $thumb_fullpath = $file_info['dirname'] . DIRECTORY_SEPARATOR . $thumb;
582
+ $thumb_info = pathinfo($thumb_fullpath);
583
+ $valid_thumb = explode($file_info['filename'], $thumb_info['filename']);
584
+ if ($valid_thumb[0] == "") {
585
+ $dimension_thumb = explode('x', $valid_thumb[1]);
586
+ if (count($dimension_thumb) == 2) {
587
+ if (is_numeric($dimension_thumb[0]) && is_numeric($dimension_thumb[1])) {
588
+ $thumb_regenerate[] = sprintf("%sx%s", $dimension_thumb[0], $dimension_thumb[1]);
589
+ }
590
+ }
591
+ }
592
+ }
593
+
594
+
595
+ // Remove success if has in error list
596
+ foreach ($thumb_regenerate as $key => $regenerate) {
597
+ if (in_array($regenerate, $thumb_error))
598
+ unset($thumb_regenerate[$key]);
599
+ }
600
+
601
+ // Remove deleted if has in success list
602
+ foreach ($thumb_deleted as $key => $deleted) {
603
+ if (in_array($deleted, $thumb_regenerate))
604
+ unset($thumb_deleted[$key]);
605
+ }
606
+
607
+
608
+ /**
609
+ * Display results
610
+ */
611
+ $message = sprintf(__('<b>&quot;%s&quot; (ID %s)</b>', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $image->ID);
612
+ $message .= sprintf(__('<br />Original image: %s', 'force-regenerate-thumbnails'), $image_fullpath);
613
+ if (count($thumb_deleted) > 0) {
614
+ $message .= sprintf(__('<br />Deleted: %s', 'force-regenerate-thumbnails'), implode(', ', $thumb_deleted));
615
+ }
616
+ if (count($thumb_error) > 0) {
617
+ $message .= sprintf(__('<br /><b><span style="color: #DD3D36;">Deleted error: %s</span></b>', 'force-regenerate-thumbnails'), implode(', ', $thumb_error));
618
+ $message .= sprintf(__('<br /><span style="color: #DD3D36;">Please, check the folder permission (chmod 777): %s</span>', 'force-regenerate-thumbnails'), $upload_dir['basedir']);
619
+ }
620
+ if (count($thumb_regenerate) > 0) {
621
+ $message .= sprintf(__('<br />Regenerate: %s</span>', 'force-regenerate-thumbnails'), implode(', ', $thumb_regenerate));
622
+ if (count($thumb_error) <= 0) {
623
+ $message .= sprintf(__('<br />Successfully regenerated in %s seconds', 'force-regenerate-thumbnails'), timer_stop());
624
+ }
625
+ }
626
+
627
+ if (count($thumb_error) > 0) {
628
+ die(json_encode(array('error' => '<div id="message" class="error fade"><p>' . $message . '</p></div>')));
629
+ } else {
630
+ die(json_encode(array('success' => '<div id="message" class="updated fade"><p>' . $message . '</p></div>')));
631
+ }
632
+
633
 
634
  } catch (Exception $e) {
635
+ $this->die_json_failure_msg($id, '<b><span style="color: #DD3D36;">' . $e->getMessage() . '</span></b>');
636
  }
637
 
638
  exit;
639
  }
640
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
641
 
642
  /**
643
  * Helper to make a JSON failure message
readme.txt CHANGED
@@ -13,9 +13,6 @@ Force Regenerate Thumbnails allows you to delete all old images size and REALLY
13
 
14
  See the [screenshots tab](http://wordpress.org/extend/plugins/force-regenerate-thumbnails/screenshots/) for more details.
15
 
16
- * Regenerate Thumbnails improved - All credits and thanks to Viper007Bond
17
-
18
-
19
  == Installation ==
20
 
21
  1. Go to your admin area and select Plugins -> Add new from the menu.
@@ -30,6 +27,10 @@ See the [screenshots tab](http://wordpress.org/extend/plugins/force-regenerate-t
30
 
31
  == ChangeLog ==
32
 
 
 
 
 
33
  = 2.0.1 =
34
  * Fix issue with get_option('upload_path') in Wordpress 3.5+ (thanks @DavidLingren)
35
 
13
 
14
  See the [screenshots tab](http://wordpress.org/extend/plugins/force-regenerate-thumbnails/screenshots/) for more details.
15
 
 
 
 
16
  == Installation ==
17
 
18
  1. Go to your admin area and select Plugins -> Add new from the menu.
27
 
28
  == ChangeLog ==
29
 
30
+ = 2.0.2 =
31
+ * New style for results (thanks @justonarnar)
32
+ * Automatic update "_wp_attachment_metadata" and "_wp_attached_file" (thanks @norecipes)
33
+
34
  = 2.0.1 =
35
  * Fix issue with get_option('upload_path') in Wordpress 3.5+ (thanks @DavidLingren)
36
 
style.css ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #regenthumbs-debuglist {
3
+ overflow: auto;
4
+ height: 400px;
5
+ margin-left: 0;
6
+ padding: 1em 1.2em;
7
+ list-style-position: inside;
8
+ background: #fff;
9
+ -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1);
10
+ box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1);
11
+ }
12
+