Force Regenerate Thumbnails - Version 1.8

Version Description

  • New function to display ajax results
Download this release

Release Info

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

Code changes from version 1.7 to 1.8

Files changed (2) hide show
  1. force-regenerate-thumbnails.php +76 -33
  2. readme.txt +3 -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: 1.7
7
  Author: Pedro Elsner
8
  Author URI: http://www.pedroelsner.com/
9
  */
@@ -347,6 +347,7 @@ class ForceRegenerateThumbnails {
347
  function RegenThumbs(id) {
348
  $.ajax({
349
  type: 'POST',
 
350
  url: ajaxurl,
351
  data: { action: "regeneratethumbnail", id: id },
352
  success: function(response) {
@@ -418,48 +419,58 @@ class ForceRegenerateThumbnails {
418
  function ajax_process_image() {
419
 
420
  // Don't break the JSON result
421
- @error_reporting(0);
422
-
423
 
424
  try {
425
 
426
  header('Content-type: application/json');
427
-
428
- $id = (int) $_REQUEST['id'];
429
  $image = get_post($id);
 
 
 
 
 
430
 
431
- if (!$image || 'attachment' != $image->post_type || 'image/' != substr($image->post_mime_type, 0, 6)) {
432
- die(json_encode(array('error' => sprintf(__('<span style="color: #FF3366;">Failed: %s is an invalid image ID.</span>', 'force-regenerate-thumbnails'), esc_html($_REQUEST['id'])))));
433
- return;
434
  }
435
 
436
  if (!current_user_can($this->capability)) {
437
- $this->die_json_error_msg($image->ID, __('<span style="color: #FF3366;">Your user account does not have permission to regenerate images</span>', 'force-regenerate-thumbnails'));
438
- return;
439
  }
440
 
 
 
441
  $image_fullpath = get_attached_file($image->ID);
 
 
 
 
 
 
 
 
 
 
 
442
  $upload_dir = get_option('upload_path');
443
  if ((strrpos($image_fullpath, $upload_dir) === false)) {
444
  $image_fullpath = realpath($upload_dir . DIRECTORY_SEPARATOR . $image_fullpath);
445
  }
446
 
447
- if (false === $image_fullpath || !file_exists($image_fullpath)) {
448
- $this->die_json_error_msg($image->ID, sprintf( __('<span style="color: #FF3366;">The originally uploaded image file cannot be found at %s</span>', 'force-regenerate-thumbnails'), '<code>' . esc_html($image_fullpath) . '</code>'));
449
- return;
450
  }
451
 
 
452
  // 5 minutes per image should be PLENTY
453
- @set_time_limit(900);
454
-
455
-
456
-
457
- /**
458
- * Grant image deleted
459
- * @since 1.1
460
- */
461
- $message = '';
462
 
 
463
 
464
  /**
465
  * New CORE 1.6 version
@@ -467,7 +478,6 @@ class ForceRegenerateThumbnails {
467
  $thumbnails = array();
468
  $file_info = pathinfo($image_fullpath);
469
 
470
- $message = '<br /> - Original file: ' . realpath($image_fullpath);
471
 
472
  // Hack to find thumbnail
473
  $file_info['filename'] .= '-';
@@ -482,8 +492,8 @@ class ForceRegenerateThumbnails {
482
  $dimension_thumb = explode('x', $valid_thumb[1]);
483
  if (count($dimension_thumb) == 2) {
484
  if (is_numeric($dimension_thumb[0]) && is_numeric($dimension_thumb[1])) {
485
- $message .= sprintf('<br /> - ' . __("Thumbnail: %sx%s was deleted.", 'force-regenerate-thumbnails'), $dimension_thumb[0], $dimension_thumb[1]);
486
- @unlink(realpath($thumb_fullpath));
487
  }
488
  }
489
  }
@@ -499,24 +509,45 @@ class ForceRegenerateThumbnails {
499
 
500
  if (is_wp_error($metadata)) {
501
  $this->die_json_error_msg($image->ID, $metadata->get_error_message());
502
- return;
503
  }
504
 
505
  if (empty($metadata)) {
506
- $this->die_json_error_msg($image->ID, __('<span style="color: #FF3366;">Unknown failure reason.</span>', 'force-regenerate-thumbnails'));
507
- return;
508
  }
509
 
510
  wp_update_attachment_metadata($image->ID, $metadata);
511
-
512
- $message = sprintf(__('<b>&quot;%1$s&quot; (ID %2$s): All thumbnails was successfully regenerated in %3$s seconds.</b>', 'force-regenerate-thumbnails'), esc_html(get_the_title($image->ID)), $image->ID, timer_stop()) . $message;
513
- die(json_encode(array('success' => $message)));
514
 
515
  } catch (Exception $e) {
516
- die(json_encode(array('error' => "Fatal Error: " . $e->getMessage())));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  }
 
 
518
  }
519
 
 
520
  /**
521
  * Helper to make a JSON error message
522
  *
@@ -526,7 +557,19 @@ class ForceRegenerateThumbnails {
526
  * @since 1.0
527
  */
528
  function die_json_error_msg($id, $message) {
529
- die(json_encode(array('error' => sprintf(__('&quot;%1$s&quot; (ID %2$s) failed to resize. The error message was: %3$s', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $id, $message))));
 
 
 
 
 
 
 
 
 
 
 
 
530
  }
531
 
532
  /**
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
  */
347
  function RegenThumbs(id) {
348
  $.ajax({
349
  type: 'POST',
350
+ cache: false,
351
  url: ajaxurl,
352
  data: { action: "regeneratethumbnail", id: id },
353
  success: function(response) {
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
  $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)) {
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);
 
 
 
 
 
 
 
 
472
 
473
+ $thumbnails_deleted = array();
474
 
475
  /**
476
  * New CORE 1.6 version
478
  $thumbnails = array();
479
  $file_info = pathinfo($image_fullpath);
480
 
 
481
 
482
  // Hack to find thumbnail
483
  $file_info['filename'] .= '-';
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
  }
509
 
510
  if (is_wp_error($metadata)) {
511
  $this->die_json_error_msg($image->ID, $metadata->get_error_message());
512
+ exit;
513
  }
514
 
515
  if (empty($metadata)) {
516
+ $this->die_json_error_msg($image->ID, __('<b><span style="color: #FF3366;">Unknown failure reason.</span></b>', 'force-regenerate-thumbnails'));
517
+ exit;
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) {
525
+ $this->die_json_failure_msg($id, '<b><span style="color: #FF3366;">Fatal Error: ' . $e->getMessage() . '</span></b>');
526
+ exit;
527
+ }
528
+ }
529
+
530
+ /**
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());
547
+ die(json_encode(array('success' => $message)));
548
  }
549
 
550
+
551
  /**
552
  * Helper to make a JSON error message
553
  *
557
  * @since 1.0
558
  */
559
  function die_json_error_msg($id, $message) {
560
+ die(json_encode(array('error' => sprintf(__('&quot;%s&quot; (ID %s)<br />%s', 'force-regenerate-thumbnails'), esc_html(get_the_title($id)), $id, $message))));
561
+ }
562
+
563
+ /**
564
+ * Helper to make a JSON failure message
565
+ *
566
+ * @param integer $id
567
+ * @param string #message
568
+ * @access public
569
+ * @since 1.8
570
+ */
571
+ function die_json_failure_msg($id, $message) {
572
+ die(json_encode(array('error' => sprintf(__('(ID %s)<br />%s', 'force-regenerate-thumbnails'), $id, $message))));
573
  }
574
 
575
  /**
readme.txt CHANGED
@@ -30,6 +30,9 @@ See the [screenshots tab](http://wordpress.org/extend/plugins/force-regenerate-t
30
 
31
  == ChangeLog ==
32
 
 
 
 
33
  = 1.7 =
34
  * Fix issue with getters path in Linux/Windows/Unix servers
35
 
30
 
31
  == ChangeLog ==
32
 
33
+ = 1.8 =
34
+ * New function to display ajax results
35
+
36
  = 1.7 =
37
  * Fix issue with getters path in Linux/Windows/Unix servers
38