开发的 wordpress下载插件 去了替换和删除功能!

开发的 wordpress下载插件 去了替换和删除功能!

 

download-images-to-local

 

download-images-to-local.php

<?php
/*
Plugin Name: Download Images to Local (Final)
Description: Full functional version with debug support
Version: 4.1
Author: heping
*/

// 注册编辑器按钮
add_filter('mce_buttons', function($buttons) {
array_push($buttons, 'separator', 'download_images');
return $buttons;
});

// 加载TinyMCE插件
add_filter('mce_external_plugins', function($plugin_array) {
$plugin_array['download_images'] = plugins_url('download-images.js', __FILE__) . '?v=' . time();
return $plugin_array;
});

// 加载JSZip库
add_action('admin_enqueue_scripts', function() {
wp_enqueue_script('jszip', 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js', [], '3.7.1', true);
});

 

 

=======================

 

download-images.js

 

(function() {
tinymce.PluginManager.add('download_images', function(editor, url) {
const DEBUG_PREFIX = '[IMG DOWNLOADER]';

editor.addButton('download_images', {
text: 'Download Images to Local',
icon: false,
onclick: async function() {
try {
console.debug(DEBUG_PREFIX, 'Button clicked');

// ===== 阶段1:提取图片地址 =====
const originalContent = editor.getContent();
const imageUrls = extractImageUrls(originalContent);

if (imageUrls.length === 0) {
alert('No images found!');
return;
}
console.debug(DEBUG_PREFIX, 'Extracted URLs:', imageUrls);

// ===== 阶段2:打包下载 =====
await downloadImages(imageUrls);
console.debug(DEBUG_PREFIX, 'Download completed');

} catch (error) {
console.error(DEBUG_PREFIX, 'Process failed:', error);
alert('Operation failed: ' + error.message);
}
}
});

// ================= 工具函数 =================
function extractImageUrls(content) {
return Array.from(content.matchAll(/<img[^>]+src="(https?:\/\/[^"]+)"/g))
.map(match => match[1])
.filter(url => {
const ext = getFileExtension(url).toLowerCase();
return ext === 'jpg' || ext === 'gif' || ext === 'jpeg' || ext === 'png';
});
}

async function downloadImages(urls) {
const zip = new JSZip();
const dateStr = new Date().toISOString().slice(0,10).replace(/-/g, '');

await Promise.all(urls.map(async (url, index) => {
const ext = getFileExtension(url);
const filename = `${dateStr}-${String(index+1).padStart(3, '0')}.${ext}`;

try {
const response = await fetch(url);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
zip.file(filename, await response.blob());
} catch (error) {
console.warn(DEBUG_PREFIX, `Failed to download ${url}:`, error);
}
}));

const zipBlob = await zip.generateAsync({type: 'blob'});
triggerDownload(zipBlob, `${dateStr}_images.zip`);
}

function getFileExtension(url) {
return url.split('.').pop().split(/[?#]/)[0].toLowerCase();
}

function triggerDownload(blob, filename) {
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
document.body.appendChild(link);
link.click();
setTimeout(() => document.body.removeChild(link), 1000);
}
});
})();

 

 

==========

 

 

学习资料见知识星球。

以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。

快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利​​​​!

更多技巧, www.excelbook.cn

欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;

Excelbook.cn Excel技巧 SQL技巧 Python 学习!

你将获得:

1、价值上万元的专业的PPT报告模板。

2、专业案例分析和解读笔记。

3、实用的Excel、Word、PPT技巧。

4、VIP讨论群,共享资源。

5、优惠的会员商品。

6、一次付费只需129元,即可下载本站文章涉及的文件和软件。

文章版权声明 1、本网站名称:Excelbook
2、本站永久网址:http://www.excelbook.cn
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长王小琥进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报。
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。

THE END
分享
二维码
< <上一篇
下一篇>>