Force Regenerate Thumbnails - Version 2.0.1

Version Description

  • Fix issue with get_option('upload_path') in Wordpress 3.5+ (thanks @DavidLingren)
Download this release

Release Info

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

Code changes from version 2.0.0 to 2.0.1

Files changed (2) hide show
  1. force-regenerate-thumbnails.php +48 -31
  2. readme.txt +6 -0
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: 2.0.0
7
  Author: Pedro Elsner
8
  Author URI: http://www.pedroelsner.com/
9
  */
@@ -12,6 +12,8 @@ Author URI: http://www.pedroelsner.com/
12
  /**
13
  * Force GD for Image handle (WordPress 3.5 or better)
14
  * Thanks (@nikcree)
 
 
15
  */
16
  $version = get_bloginfo('version');
17
  if ($version >= 3.5) {
@@ -410,6 +412,7 @@ class ForceRegenerateThumbnails {
410
  <?php
411
  }
412
 
 
413
  /**
414
  * Process a single image ID (this is an AJAX handler)
415
  *
@@ -419,53 +422,58 @@ class ForceRegenerateThumbnails {
419
  function ajax_process_image() {
420
 
421
  // Don't break the JSON result
422
- error_reporting(0);
423
  $id = (int) $_REQUEST['id'];
424
 
425
  try {
426
 
427
  header('Content-type: application/json');
428
  $image = get_post($id);
429
-
430
  if (is_null($image)) {
431
- $this->die_json_failure_msg($id, sprintf(__('<b><span style="color: #FF3366;">Failed: %s is an invalid image ID.</span></b>', 'force-regenerate-thumbnails'), esc_html($_REQUEST['id'])));
432
- exit;
433
  }
434
 
435
  if ('attachment' != $image->post_type || 'image/' != substr($image->post_mime_type, 0, 6)) {
436
- $this->die_json_failure_msg($id, sprintf(__('<b><span style="color: #FF3366;">Failed: %s is an invalid image ID.</span></b>', 'force-regenerate-thumbnails'), esc_html($_REQUEST['id'])));
437
- exit;
438
  }
439
 
440
  if (!current_user_can($this->capability)) {
441
- $this->die_json_error_msg($image->ID, __('<b><span style="color: #FF3366;">Your user account does not have permission to regenerate images</span></b>', 'force-regenerate-thumbnails'));
442
- exit;
443
  }
444
 
445
-
446
  // Get original image
447
  $image_fullpath = get_attached_file($image->ID);
448
 
449
  if (false === $image_fullpath) {
450
- $this->die_json_error_msg($image->ID, __('<b><span style="color: #FF3366;">The originally uploaded image file cannot be found</span></b>', 'force-regenerate-thumbnails'));
451
- exit;
452
  }
453
 
454
  if (strlen($image_fullpath) == 0) {
455
- $this->die_json_error_msg($image->ID, __('<b><span style="color: #FF3366;">The originally uploaded image file cannot be found</span></b>', 'force-regenerate-thumbnails'));
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)) {
465
- $this->die_json_error_msg($image->ID, sprintf( __('<b><span style="color: #FF3366;">The originally uploaded image file cannot be found at %s</span></b>', 'force-regenerate-thumbnails'), '<code>' . esc_html($image_fullpath) . '</code>'));
466
- exit;
467
  }
468
-
469
 
470
  // 5 minutes per image should be PLENTY
471
  set_time_limit(900);
@@ -474,13 +482,22 @@ class ForceRegenerateThumbnails {
474
  $deleted_error = array();
475
 
476
  /**
477
- * New CORE 1.6 version
 
 
478
  */
479
  $thumbnails = array();
480
  $file_info = pathinfo($image_fullpath);
481
 
482
 
483
- // Hack to find thumbnail
 
 
 
 
 
 
 
484
  $file_info['filename'] .= '-';
485
  $files = array();
486
 
@@ -500,8 +517,8 @@ class ForceRegenerateThumbnails {
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]);
@@ -511,7 +528,6 @@ class ForceRegenerateThumbnails {
511
  }
512
  }
513
 
514
-
515
 
516
  /**
517
  * Regenerate
@@ -524,18 +540,17 @@ class ForceRegenerateThumbnails {
524
  }
525
 
526
  if (empty($metadata)) {
527
- $this->die_json_error_msg($image->ID, __('<b><span style="color: #FF3366;">Unknown failure reason.</span></b>', 'force-regenerate-thumbnails'));
528
- exit;
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) {
536
- $this->die_json_failure_msg($id, '<b><span style="color: #FF3366;">Fatal Error: ' . $e->getMessage() . '</span></b>');
537
- exit;
538
  }
 
 
539
  }
540
 
541
  /**
@@ -557,8 +572,9 @@ class ForceRegenerateThumbnails {
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());
@@ -613,4 +629,5 @@ function ForceRegenerateThumbnails() {
613
  }
614
  add_action('init', 'ForceRegenerateThumbnails');
615
 
 
616
  ?>
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
  */
12
  /**
13
  * Force GD for Image handle (WordPress 3.5 or better)
14
  * Thanks (@nikcree)
15
+ *
16
+ * @since 1.5
17
  */
18
  $version = get_bloginfo('version');
19
  if ($version >= 3.5) {
412
  <?php
413
  }
414
 
415
+
416
  /**
417
  * Process a single image ID (this is an AJAX handler)
418
  *
422
  function ajax_process_image() {
423
 
424
  // Don't break the JSON result
425
+ error_reporting(0);
426
  $id = (int) $_REQUEST['id'];
427
 
428
  try {
429
 
430
  header('Content-type: application/json');
431
  $image = get_post($id);
432
+
433
  if (is_null($image)) {
434
+ throw new Exception(sprintf(__('Failed: %d is an invalid image ID.', 'force-regenerate-thumbnails'), $id));
 
435
  }
436
 
437
  if ('attachment' != $image->post_type || 'image/' != substr($image->post_mime_type, 0, 6)) {
438
+ throw new Exception(sprintf(__('Failed: %d is an invalid image ID.', 'force-regenerate-thumbnails'), $id));
 
439
  }
440
 
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 &quot;%s&quot;.', '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 &quot;%s&quot;.', 'force-regenerate-thumbnails'), esc_html((string) $image_fullpath)));
 
476
  }
 
477
 
478
  // 5 minutes per image should be PLENTY
479
  set_time_limit(900);
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
 
517
  $dimension_thumb = explode('x', $valid_thumb[1]);
518
  if (count($dimension_thumb) == 2) {
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]);
528
  }
529
  }
530
 
 
531
 
532
  /**
533
  * Regenerate
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
  /**
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());
629
  }
630
  add_action('init', 'ForceRegenerateThumbnails');
631
 
632
+
633
  ?>
readme.txt CHANGED
@@ -30,6 +30,12 @@ See the [screenshots tab](http://wordpress.org/extend/plugins/force-regenerate-t
30
 
31
  == ChangeLog ==
32
 
 
 
 
 
 
 
33
  = 1.8 =
34
  * New function to display ajax results
35
 
30
 
31
  == ChangeLog ==
32
 
33
+ = 2.0.1 =
34
+ * Fix issue with get_option('upload_path') in Wordpress 3.5+ (thanks @DavidLingren)
35
+
36
+ = 2.0.0 =
37
+ * Fix error handle
38
+
39
  = 1.8 =
40
  * New function to display ajax results
41