在"媒体设置"中更改了图像尺寸后,请修复图像尺寸。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
11 KiB

  1. <?php
  2. if ( ! class_exists( 'Acme_Fix_Images' ) ){
  3. /**
  4. * Class for Acme Fix Images Menu and Setting
  5. *
  6. * @package AcmeThemes
  7. * @subpackage Acme Fix Images
  8. * @since 1.0
  9. */
  10. class Acme_Fix_Images{
  11. /*Basic variables for class*/
  12. /**
  13. * Acme_Fix_Images instance.
  14. *
  15. * @see acme_get_instance()
  16. * @var object
  17. * @access protected
  18. * @since 1.0
  19. *
  20. */
  21. protected static $acme_instance = NULL;
  22. /**
  23. * Access Acme Fix Images working acme_instance
  24. *
  25. * @access public
  26. * @since 1.0.0
  27. * @return object of this class
  28. */
  29. public static function acme_get_instance() {
  30. NULL === self::$acme_instance and self::$acme_instance = new self;
  31. return self::$acme_instance;
  32. }
  33. /**
  34. * Used for regular plugin work.
  35. *
  36. * @access public
  37. * @since 1.0
  38. *
  39. * @return void
  40. *
  41. */
  42. public function acme_admin_menu_init() {
  43. /*Hook before any function of class start */
  44. do_action( 'acme_admin_menu_before');
  45. /*Adding menu page*/
  46. add_action( 'admin_menu', array($this,'acme_admin_submenu') ,12 );
  47. /*see more here https://codex.wordpress.org/Plugin_API/Filter_Reference/attachment_fields_to_edit*/
  48. add_filter( 'attachment_fields_to_edit', array(&$this, 'fix_image_single'), 10, 2 );
  49. /*Hook before any function of class end */
  50. do_action( 'acme_admin_menu_after');
  51. }
  52. /**
  53. * Constructor. Intentionally left empty and public.
  54. *
  55. * @access public
  56. * @since 1.0.0
  57. *
  58. */
  59. public function __construct(){ }
  60. /**
  61. * Add submenu in general options
  62. *
  63. * @access public
  64. * @since 1.0.0
  65. *
  66. * @return void
  67. *
  68. */
  69. public function acme_admin_submenu() {
  70. add_submenu_page(
  71. "themes.php",
  72. __('Acme Fix Images','acme-fix-images'),
  73. __('Acme Fix Images','acme-fix-images'),
  74. 'manage_options',
  75. 'acme-fix-images-setting',
  76. array($this, 'acme_submenu_page' )
  77. );
  78. /*add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );*/
  79. }
  80. /**
  81. * Add button to the media page
  82. *
  83. * @param array $fields
  84. * @param object $post
  85. * @return array
  86. */
  87. function fix_image_single($fields, $post) {
  88. $thumbnails = array();
  89. foreach ( acme_fix_images_get_image_sizes() as $s )
  90. $thumbnails[] = 'thumbnails[]='.$s['name'];
  91. $thumbnails = '&'.implode('&', $thumbnails);
  92. ob_start();
  93. ?>
  94. <script>
  95. function setMessage(msg) {
  96. jQuery("#update-msg").html(msg);
  97. jQuery("#update-msg").show();
  98. }
  99. function regenerate() {
  100. jQuery("#acme_fix_images").prop("disabled", true);
  101. setMessage("<?php _e('Reading attachments...', 'acme-fix-images') ?>");
  102. thumbnails = '<?php echo $thumbnails ?>';
  103. jQuery.ajax({
  104. url: "<?php echo admin_url('admin-ajax.php'); ?>",
  105. type: "POST",
  106. data: "action=acme_fix_images&do=regen&id=<?php echo $post->ID ?>" + thumbnails,
  107. success: function(result) {
  108. if (result != '-1') {
  109. setMessage("<?php _e('Done.', 'acme-fix-images') ?>");
  110. }
  111. },
  112. error: function(request, status, error) {
  113. setMessage("<?php _e('Error', 'acme-fix-images') ?>" + request.status);
  114. },
  115. complete: function() {
  116. jQuery("#acme_fix_images").prop("disabled", false);
  117. }
  118. });
  119. }
  120. </script>
  121. <input type='button' onclick='javascript:regenerate();' class='button' name='acme_fix_images' id='acme_fix_images' value='Fix Images'>
  122. <span id="update-msg" class="updated fade" style="clear:both;display:none;line-height:28px;padding-left:10px;"></span>
  123. <?php
  124. $html = ob_get_clean();
  125. $fields["acme-fix-images"] = array(
  126. "label" => __('Fix Images', 'acme-fix-images'),
  127. "input" => "html",
  128. "html" => $html
  129. );
  130. return $fields;
  131. }
  132. /**
  133. * Add form fields in Acme Fix Images Menu
  134. *
  135. * @access public
  136. * @since 1.0
  137. *
  138. * @return void
  139. *
  140. */
  141. public function acme_submenu_page() {
  142. ?>
  143. <!--form value-->
  144. <div id="message" class="updated fade" style="display:none"></div>
  145. <script type="text/javascript">
  146. // <![CDATA[
  147. function setMessage(msg) {
  148. jQuery("#message").html(msg);
  149. jQuery("#message").show();
  150. }
  151. function regenerate() {
  152. jQuery("#acme_fix_images").prop("disabled", true);
  153. setMessage("<p><?php _e('Reading attachments...', 'acme-fix-images') ?></p>");
  154. inputs = jQuery( 'input:checked' );
  155. var thumbnails= '';
  156. if( inputs.length != jQuery( 'input[type=checkbox]' ).length ){
  157. inputs.each( function(){
  158. thumbnails += '&thumbnails[]='+jQuery(this).val();
  159. } );
  160. }
  161. var featuredimgonly = jQuery("#featuredimgonly").prop('checked') ? 1 : 0;
  162. jQuery.ajax({
  163. url: "<?php echo admin_url('admin-ajax.php'); ?>",
  164. type: "POST",
  165. data: "action=acme_fix_images&do=getimglists&featuredimgonly="+featuredimgonly,
  166. success: function(result) {
  167. var list = eval(result);
  168. var curr = 0;
  169. if (!list) {
  170. setMessage("<?php _e('No attachments found.', 'acme-fix-images')?>");
  171. jQuery("#acme_fix_images").prop("disabled", false);
  172. return;
  173. }
  174. function regenItem() {
  175. if (curr >= list.length) {
  176. jQuery("#acme_fix_images").prop("disabled", false);
  177. setMessage("<?php _e('Done.', 'acme-fix-images') ?>");
  178. jQuery("#last-image").html("<?php _e('Completed', 'acme-fix-images')?>");
  179. return;
  180. }
  181. setMessage(<?php printf( __('"Fixing " + %s + " of " + %s + " (" + %s + ")..."', 'acme-fix-images'), "(curr+1)", "list.length", "list[curr].title"); ?>);
  182. jQuery.ajax({
  183. url: "<?php echo admin_url('admin-ajax.php'); ?>",
  184. type: "POST",
  185. data: "action=acme_fix_images&do=regen&id=" + list[curr].id + thumbnails,
  186. success: function(result) {
  187. curr = curr + 1;
  188. if (result != '-1') {
  189. jQuery(".thumb").show();
  190. jQuery("#thumb-img").attr("src",result);
  191. }
  192. regenItem();
  193. }
  194. });
  195. }
  196. regenItem();
  197. },
  198. error: function(request, status, error) {
  199. setMessage("<?php _e('Error', 'acme-fix-images') ?>" + request.status);
  200. }
  201. });
  202. }
  203. jQuery(document).ready(function() {
  204. jQuery('#size-toggle').click(function() {
  205. jQuery("#sizeselect").find("input[type=checkbox]").each(function() {
  206. jQuery(this).prop("checked", !jQuery(this).prop("checked"));
  207. });
  208. });
  209. });
  210. // ]]>
  211. </script>
  212. <form method="post" action="" style="width: 100%">
  213. <h2><?php _e('Select which thumbnails you want to fix', 'acme-fix-images'); ?>:</h2>
  214. <a href="javascript:void(0);" id="size-toggle"><?php _e('Toggle all', 'acme-fix-images'); ?></a>
  215. <div id="sizeselect">
  216. <?php
  217. foreach ( acme_fix_images_get_image_sizes() as $s ):
  218. ?>
  219. <label>
  220. <input type="checkbox" name="thumbnails[]" id="sizeselect" checked="checked" value="<?php echo $s['name'] ?>" />
  221. <em><?php echo $s['name'] ?></em>
  222. &nbsp;(<?php echo $s['width'] ?>x<?php echo $s['height'] ?>
  223. <?php if ($s['crop']) _e('cropped', 'acme-fix-images'); ?>)
  224. </label>
  225. <br/>
  226. <?php endforeach;?>
  227. </div>
  228. <p>
  229. <label>
  230. <input type="checkbox" id="featuredimgonly" name="featuredimgonly" />
  231. <?php _e('Only fix featured images', 'acme-fix-images'); ?>
  232. </label>
  233. </p>
  234. <p>
  235. <?php
  236. _e("Note: If you've changed the dimensions of your thumbnails, existing thumbnail images will not be deleted.",
  237. 'acme-fix-images'); ?>
  238. </p>
  239. <input type="button" onClick="javascript:regenerate();" class="button" name="acme_fix_images" id="acme_fix_images" value="<?php _e( 'Fix All Images', 'acme-fix-images' ) ?>" />
  240. <br />
  241. </form>
  242. <div class="thumb" style="display:none;"><h4 id="last-image"><?php _e('Processing', 'acme-fix-images'); ?>...</h4><img id="thumb-img" /></div>
  243. <?php
  244. }
  245. } /*END class Acme_Fix_Images*/
  246. /*Initialize class after init*/
  247. add_action( 'init', array ( Acme_Fix_Images::acme_get_instance(), 'acme_admin_menu_init' ));
  248. }/*END if(!class_exists('Acme_Fix_Images'))*/