Fix sub-site failure to delete remote images (#46)

pull/47/head v1.4.19
Luffy 2024-11-22 11:06:33 +08:00 committed by GitHub
parent f9598e9d71
commit bc9387d3ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 25 deletions

View File

@ -25,6 +25,8 @@
- [x] 支持使用 ECS 的 RAM 操作 - [x] 支持使用 ECS 的 RAM 操作
- [x] 支持原图保护 - [x] 支持原图保护
- [x] 支持 `wp-cli` 命令上传/删除文件 - [x] 支持 `wp-cli` 命令上传/删除文件
- [x] 支持多站点
- [x] 支持图片裁剪编辑等操作后的上传
## 安装 ## 安装

View File

@ -3,7 +3,7 @@
Plugin Name: OSS Aliyun Plugin Name: OSS Aliyun
Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress
Description: 使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving. Description: 使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.
Version: 1.4.18 Version: 1.4.19
Author: 沈唁 Author: 沈唁
Author URI: https://qq52o.me Author URI: https://qq52o.me
License: Apache2.0 License: Apache2.0
@ -19,7 +19,7 @@ use OSS\Credentials\CredentialsProvider;
use AlibabaCloud\Credentials\Credential; use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider; use OSS\Credentials\StaticCredentialsProvider;
define('OSS_VERSION', '1.4.18'); define('OSS_VERSION', '1.4.19');
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__))); define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
if (!function_exists('get_home_path')) { if (!function_exists('get_home_path')) {
@ -53,10 +53,9 @@ class OSSCredentialsWrapper implements CredentialsProvider
// 初始化选项 // 初始化选项
register_activation_hook(__FILE__, 'oss_set_options'); register_activation_hook(__FILE__, 'oss_set_options');
// 初始化选项 function oss_get_default_options()
function oss_set_options()
{ {
$options = [ return [
'bucket' => '', 'bucket' => '',
'regional' => 'oss-cn-shanghai', 'regional' => 'oss-cn-shanghai',
'accessKeyId' => '', 'accessKeyId' => '',
@ -70,12 +69,17 @@ function oss_set_options()
'role_name' => '', // 角色名称 'role_name' => '', // 角色名称
'origin_protect' => '', 'origin_protect' => '',
]; ];
add_option('oss_options', $options, '', 'yes'); }
// 初始化选项
function oss_set_options()
{
add_option('oss_options', oss_get_default_options(), '', 'yes');
} }
function oss_get_client() function oss_get_client()
{ {
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
$role_name = esc_attr($oss_options['role_name'] ?? ''); $role_name = esc_attr($oss_options['role_name'] ?? '');
$endpoint = oss_get_bucket_endpoint($oss_options); $endpoint = oss_get_bucket_endpoint($oss_options);
@ -113,7 +117,7 @@ function oss_get_bucket_endpoint($oss_options)
function oss_get_bucket_name() function oss_get_bucket_name()
{ {
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
return $oss_options['bucket']; return $oss_options['bucket'];
} }
@ -167,7 +171,7 @@ function oss_file_upload($object, $file, $no_local_file = false)
*/ */
function oss_is_delete_local_file() function oss_is_delete_local_file()
{ {
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
return esc_attr($oss_options['nolocalsaving']) == 'true'; return esc_attr($oss_options['nolocalsaving']) == 'true';
} }
@ -286,7 +290,7 @@ function oss_upload_thumbs($metadata)
$upload_path = oss_get_option('upload_path'); $upload_path = oss_get_option('upload_path');
//获取oss插件的配置信息 //获取oss插件的配置信息
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
$no_local_file = esc_attr($oss_options['nolocalsaving']) == 'true'; $no_local_file = esc_attr($oss_options['nolocalsaving']) == 'true';
$no_thumb = esc_attr($oss_options['nothumb']) == 'true'; $no_thumb = esc_attr($oss_options['nothumb']) == 'true';
@ -369,12 +373,11 @@ if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
*/ */
function oss_delete_remote_attachment($post_id) function oss_delete_remote_attachment($post_id)
{ {
$wp_uploads = wp_upload_dir();
$basedir = $wp_uploads['basedir'];
$upload_path = str_replace(get_home_path(), '', $basedir);
// 获取图片类附件的meta信息 // 获取图片类附件的meta信息
$meta = wp_get_attachment_metadata($post_id); $meta = wp_get_attachment_metadata($post_id);
$upload_path = oss_get_option('upload_path');
if ($upload_path == '') {
$upload_path = 'wp-content/uploads';
}
if (!empty($meta['file'])) { if (!empty($meta['file'])) {
$deleteObjects = []; $deleteObjects = [];
@ -397,6 +400,13 @@ function oss_delete_remote_attachment($post_id)
} }
} }
$backup_sizes = get_post_meta($post_id, '_wp_attachment_backup_sizes', true);
if (is_array($backup_sizes)) {
foreach ($backup_sizes as $size) {
$deleteObjects[] = $dirname . $size['file'];
}
}
oss_delete_oss_files($deleteObjects); oss_delete_oss_files($deleteObjects);
} else { } else {
// 获取链接删除 // 获取链接删除
@ -408,7 +418,7 @@ function oss_delete_remote_attachment($post_id)
oss_delete_oss_file($upload_path . end($file_info)); oss_delete_oss_file($upload_path . end($file_info));
} }
} else { } else {
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
$oss_upload_url = esc_attr($oss_options['upload_url_path']); $oss_upload_url = esc_attr($oss_options['upload_url_path']);
$file_info = explode($oss_upload_url, $link); $file_info = explode($oss_upload_url, $link);
if (count($file_info) >= 2) { if (count($file_info) >= 2) {
@ -433,7 +443,7 @@ if (oss_get_option('upload_path') == '.') {
function oss_sanitize_file_name($filename) function oss_sanitize_file_name($filename)
{ {
$oss_options = get_option('oss_options'); $oss_options = get_option('oss_options', oss_get_default_options());
switch ($oss_options['update_file_name']) { switch ($oss_options['update_file_name']) {
case 'md5': case 'md5':
return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION); return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
@ -497,7 +507,7 @@ add_filter('plugin_action_links', 'oss_plugin_action_links', 10, 2);
function oss_custom_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) function oss_custom_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
{ {
$option = get_option('oss_options'); $option = get_option('oss_options', oss_get_default_options());
$style = !empty($option['style']) ? esc_attr($option['style']) : ''; $style = !empty($option['style']) ? esc_attr($option['style']) : '';
$upload_url_path = esc_attr($option['upload_url_path']); $upload_url_path = esc_attr($option['upload_url_path']);
if (empty($style)) { if (empty($style)) {
@ -538,7 +548,7 @@ function oss_wp_prepare_attachment_for_js($response)
add_filter('the_content', 'oss_setting_content_style'); add_filter('the_content', 'oss_setting_content_style');
function oss_setting_content_style($content) function oss_setting_content_style($content)
{ {
$option = get_option('oss_options'); $option = get_option('oss_options', oss_get_default_options());
$upload_url_path = esc_attr($option['upload_url_path']); $upload_url_path = esc_attr($option['upload_url_path']);
if (!empty($option['style'])) { if (!empty($option['style'])) {
$style = esc_attr($option['style']); $style = esc_attr($option['style']);
@ -563,7 +573,7 @@ function oss_setting_content_style($content)
add_filter('post_thumbnail_html', 'oss_setting_post_thumbnail_style', 10, 3); add_filter('post_thumbnail_html', 'oss_setting_post_thumbnail_style', 10, 3);
function oss_setting_post_thumbnail_style($html, $post_id, $post_image_id) function oss_setting_post_thumbnail_style($html, $post_id, $post_image_id)
{ {
$option = get_option('oss_options'); $option = get_option('oss_options', oss_get_default_options());
$upload_url_path = esc_attr($option['upload_url_path']); $upload_url_path = esc_attr($option['upload_url_path']);
if (!empty($option['style']) && has_post_thumbnail()) { if (!empty($option['style']) && has_post_thumbnail()) {
$style = esc_attr($option['style']); $style = esc_attr($option['style']);
@ -633,7 +643,7 @@ function oss_get_regional($regional)
foreach ($options as $value => $text) { foreach ($options as $value => $text) {
$selected = ($regional == $value) ? 'selected="selected"' : ''; $selected = ($regional == $value) ? 'selected="selected"' : '';
echo "<option value=\"{$value}\" {$selected}>{$text}</option>"; echo "<option value='{$value}' {$selected}>{$text}</option>";
} }
} }
@ -642,7 +652,7 @@ function oss_get_option($key)
return esc_attr(get_option($key)); return esc_attr(get_option($key));
} }
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
if (!empty($oss_options['origin_protect']) && esc_attr($oss_options['origin_protect']) === 'on' && !empty(esc_attr($oss_options['style']))) { if (!empty($oss_options['origin_protect']) && esc_attr($oss_options['origin_protect']) === 'on' && !empty(esc_attr($oss_options['style']))) {
add_filter('wp_get_attachment_url', 'oss_add_suffix_to_attachment_url', 10, 2); add_filter('wp_get_attachment_url', 'oss_add_suffix_to_attachment_url', 10, 2);
add_filter('wp_get_attachment_thumb_url', 'oss_add_suffix_to_attachment_url', 10, 2); add_filter('wp_get_attachment_thumb_url', 'oss_add_suffix_to_attachment_url', 10, 2);
@ -722,7 +732,7 @@ function oss_is_image_type($url)
*/ */
function oss_get_image_style() function oss_get_image_style()
{ {
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
return esc_attr($oss_options['style']); return esc_attr($oss_options['style']);
} }
@ -815,7 +825,7 @@ function oss_setting_page()
echo '<div class="updated"><p><strong>设置已保存!</strong></p></div>'; echo '<div class="updated"><p><strong>设置已保存!</strong></p></div>';
} }
$oss_options = get_option('oss_options', true); $oss_options = get_option('oss_options', oss_get_default_options());
$oss_regional = esc_attr($oss_options['regional']); $oss_regional = esc_attr($oss_options['regional']);

View File

@ -5,7 +5,7 @@ Tags: oss, 阿里云, 对象存储, aliyun
Requires at least: 4.6 Requires at least: 4.6
Tested up to: 6.7 Tested up to: 6.7
Requires PHP: 7.1 Requires PHP: 7.1
Stable tag: 1.4.18 Stable tag: 1.4.19
License: Apache2.0 License: Apache2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0.html License URI: http://www.apache.org/licenses/LICENSE-2.0.html
@ -30,7 +30,9 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
9. 支持使用 ECS 的 RAM 操作 9. 支持使用 ECS 的 RAM 操作
10. 支持原图保护 10. 支持原图保护
11. 支持 `wp-cli` 命令上传/删除文件 11. 支持 `wp-cli` 命令上传/删除文件
12. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) 12. 支持多站点
13. 支持图片裁剪编辑等操作后的上传
14. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress)
## 其他插件 ## 其他插件
@ -76,6 +78,12 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
== Changelog == == Changelog ==
= 1.4.19 =
- Fix sub-site failure to delete remote images
- Fix `get_option` default value error
- Fix missing delete backup images
= 1.4.18 = = 1.4.18 =
- Images processing ignore gif format - Images processing ignore gif format