From fe8a0b6988db40cd2c814a3ecd212b7ee841202b Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 21 Nov 2024 13:12:55 +0800 Subject: [PATCH] Fix sub-site failure to delete remote images --- README.md | 1 + readme.txt | 6 ++-- sync-qcloud-cos.php | 67 ++++++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index eba7247..8345303 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ - [x] 支持数据监控 - [x] 支持使用 `wp-cli` 命令上传/删除文件 - [x] 支持上传文件到存储桶子目录 +- [x] 支持多站点 ## 安装 diff --git a/readme.txt b/readme.txt index 478debb..95d78ee 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: COS, 腾讯云, 对象存储, Tencent, Qcloud Requires at least: 4.6 Tested up to: 6.7 Requires PHP: 7.2 -Stable tag: 2.6.0 +Stable tag: 2.6.1 License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html @@ -37,6 +37,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 15. 支持数据监控 16. 支持使用 `wp-cli` 命令上传/删除文件 17. 支持上传文件到存储桶子目录 +18. 支持多站点 插件更多详细介绍和安装:[https://github.com/sy-records/sync-qcloud-cos](https://github.com/sy-records/sync-qcloud-cos) @@ -104,7 +105,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html = Stable = -- Support upload to subdirectories. +- Fix sub-site failure to delete remote images +- Fix `get_option` default value error = Other = diff --git a/sync-qcloud-cos.php b/sync-qcloud-cos.php index b7105bc..3661c4a 100644 --- a/sync-qcloud-cos.php +++ b/sync-qcloud-cos.php @@ -3,7 +3,7 @@ Plugin Name: Sync QCloud COS Plugin URI: https://qq52o.me/2518.html Description: 使用腾讯云对象存储服务 COS 作为附件存储空间。(Using Tencent Cloud Object Storage Service COS as Attachment Storage Space.) -Version: 2.6.0 +Version: 2.6.1 Author: 沈唁 Author URI: https://qq52o.me License: Apache2.0 @@ -27,7 +27,7 @@ use SyncQcloudCos\Monitor\DataPoints; use SyncQcloudCos\Object\Head; -define('COS_VERSION', '2.6.0'); +define('COS_VERSION', '2.6.1'); define('COS_PLUGIN_SLUG', 'sync-qcloud-cos'); define('COS_PLUGIN_PAGE', plugin_basename(dirname(__FILE__)) . '%2F' . basename(__FILE__)); @@ -41,9 +41,9 @@ // 初始化选项 register_activation_hook(__FILE__, 'cos_set_options'); -function cos_set_options() +function cos_get_default_options() { - $options = [ + return [ 'bucket' => '', 'regional' => 'ap-beijing', 'app_id' => '', @@ -66,7 +66,10 @@ function cos_set_options() 'ci_text_comments_check_roles' => '', 'origin_protect' => 'off', ]; - add_option('cos_options', $options, '', 'yes'); +} +function cos_set_options() +{ + add_option('cos_options', cos_get_default_options(), '', 'yes'); } /** @@ -76,7 +79,7 @@ function cos_set_options() function cos_get_client($cos_options = null) { if ($cos_options === null) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); } $config = [ 'region' => esc_attr($cos_options['regional']), @@ -93,7 +96,7 @@ function cos_get_client($cos_options = null) function cos_get_bucket_name($cos_options = null) { if ($cos_options === null) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); } $cos_bucket = esc_attr($cos_options['bucket']); $cos_app_id = esc_attr($cos_options['app_id']); @@ -159,7 +162,7 @@ function cos_replace_client_region($client, $bucket) return $client; } -$cos_options = get_option('cos_options', true); +$cos_options = get_option('cos_options', cos_get_default_options()); if (!empty($cos_options['origin_protect']) && esc_attr($cos_options['origin_protect']) === 'on' && !empty(esc_attr($cos_options['ci_style']))) { add_filter('wp_get_attachment_url', 'cos_add_suffix_to_attachment_url', 10, 2); add_filter('wp_get_attachment_thumb_url', 'cos_add_suffix_to_attachment_url', 10, 2); @@ -239,7 +242,7 @@ function cos_is_image_type($url) */ function cos_get_image_style() { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); return esc_attr($cos_options['ci_style']); } @@ -256,7 +259,7 @@ function cos_file_upload($object, $filename, $no_local_file = false) if (!@file_exists($filename)) { return false; } - $options = get_option('cos_options', true); + $options = get_option('cos_options', cos_get_default_options()); $bucket = cos_get_bucket_name($options); try { $file = fopen($filename, 'rb'); @@ -291,7 +294,7 @@ function cos_file_upload($object, $filename, $no_local_file = false) */ function cos_is_delete_local_file() { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); return esc_attr($cos_options['nolocalsaving']) == 'true'; } @@ -326,7 +329,7 @@ function cos_delete_local_file($file) */ function cos_delete_cos_file($file) { - $options = get_option('cos_options', true); + $options = get_option('cos_options', cos_get_default_options()); $bucket = cos_get_bucket_name($options); $cosClient = cos_get_client($options); if (!empty($options['upload_subdirectory'])) { @@ -341,7 +344,7 @@ function cos_delete_cos_file($file) */ function cos_delete_cos_files(array $files) { - $options = get_option('cos_options', true); + $options = get_option('cos_options', cos_get_default_options()); $subdirectory = !empty($options['upload_subdirectory']) ? esc_attr($options['upload_subdirectory']) . '/' : ''; $deleteObjects = []; @@ -419,7 +422,7 @@ function cos_upload_thumbs($metadata) $basedir = $wp_uploads['basedir']; $upload_path = cos_get_option('upload_path'); - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); $no_local_file = esc_attr($cos_options['nolocalsaving']) == 'true'; $no_thumb = esc_attr($cos_options['nothumb']) == 'true'; @@ -496,9 +499,11 @@ function cos_image_editor_file_do($metadata) */ function cos_delete_remote_attachment($post_id) { + $wp_uploads = wp_upload_dir(); + $basedir = $wp_uploads['basedir']; + $upload_path = str_replace(get_home_path(), '', $basedir); // 获取图片类附件的meta信息 $meta = wp_get_attachment_metadata($post_id); - $upload_path = cos_get_option('upload_path'); if (!empty($meta['file'])) { $deleteObjects = []; @@ -533,7 +538,7 @@ function cos_delete_remote_attachment($post_id) // 获取链接删除 $link = wp_get_attachment_url($post_id); if ($link) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); $subdirectory = !empty($cos_options['upload_subdirectory']) ? '/' . esc_attr($cos_options['upload_subdirectory']) : ''; if ($upload_path != '.') { $file_info = explode($upload_path, $link); @@ -568,7 +573,7 @@ function cos_modefiy_img_url($url, $post_id) function cos_sanitize_file_name($filename) { - $cos_options = get_option('cos_options'); + $cos_options = get_option('cos_options', cos_get_default_options()); switch ($cos_options['update_file_name']) { case 'md5': return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION); @@ -639,7 +644,7 @@ function cos_plugin_action_links($links, $file) function cos_wp_prepare_attachment_for_js($response) { if (empty($response['filesizeInBytes']) || empty($response['filesizeHumanReadable'])) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); $upload_url_path = esc_attr($cos_options['upload_url_path']); $upload_path = get_option('upload_path'); $object = str_replace($upload_url_path, $upload_path, $response['url']); @@ -655,7 +660,7 @@ function cos_wp_prepare_attachment_for_js($response) function cos_custom_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) { - $option = get_option('cos_options'); + $option = get_option('cos_options', cos_get_default_options()); $style = !empty($option['ci_style']) ? esc_attr($option['ci_style']) : ''; $upload_url_path = esc_attr($option['upload_url_path']); if (empty($style)) { @@ -673,7 +678,7 @@ function cos_custom_image_srcset($sources, $size_array, $image_src, $image_meta, function cos_setting_content_ci($content) { - $option = get_option('cos_options'); + $option = get_option('cos_options', cos_get_default_options()); $style = esc_attr($option['ci_style']); $upload_url_path = esc_attr($option['upload_url_path']); if (!empty($style)) { @@ -714,7 +719,7 @@ function cos_setting_content_ci($content) function cos_setting_post_thumbnail_ci($html, $post_id, $post_image_id) { - $option = get_option('cos_options'); + $option = get_option('cos_options', cos_get_default_options()); $style = esc_attr($option['ci_style']); $upload_url_path = esc_attr($option['upload_url_path']); if (!empty($style) && has_post_thumbnail()) { @@ -739,7 +744,7 @@ function cos_setting_post_thumbnail_ci($html, $post_id, $post_image_id) */ function cos_append_options($options) { - $cos_options = get_option('cos_options'); + $cos_options = get_option('cos_options', cos_get_default_options()); $options['ci_image_slim'] = $cos_options['ci_image_slim'] ?? 'off'; $options['ci_image_slim_mode'] = $cos_options['ci_image_slim_mode'] ?? ''; @@ -760,7 +765,7 @@ function cos_append_options($options) */ function cos_update_config_parameters($parametersToUpdate, $currentOptions = null) { - $currentOptions = $currentOptions ?: get_option('cos_options'); + $currentOptions = $currentOptions ?: get_option('cos_options', cos_get_default_options()); $options = array_merge($currentOptions, $parametersToUpdate); @@ -792,7 +797,7 @@ function cos_sync_image_slim_config($slimConfigData, $currentOptions) */ function cos_append_ci_style($url, $options = null) { - if (empty($options)) $options = get_option('cos_options'); + if (empty($options)) $options = get_option('cos_options', cos_get_default_options()); if (!empty($options['ci_style']) && !empty($options['upload_url_path']) && strpos($url, esc_attr($options['upload_url_path'])) !== false) { $url .= esc_attr($options['ci_style']); @@ -808,7 +813,7 @@ function cos_append_ci_style($url, $options = null) */ function cos_local2remote($url, $options = null) { - if (empty($options)) $options = get_option('cos_options'); + if (empty($options)) $options = get_option('cos_options', cos_get_default_options()); $upload_path = get_option('upload_path'); @@ -947,7 +952,7 @@ function cos_sync_setting_form($cos_options) */ function cos_ci_image_slim_setting($content) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); if (!cos_validate_configuration($cos_options)) { return false; @@ -1199,7 +1204,7 @@ function cos_ci_text_page($options) function cos_ci_text_setting($content) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); if (!cos_validate_configuration($cos_options)) { return false; } @@ -1259,7 +1264,7 @@ function cos_contact_page() function cos_process_comments($comment_data) { - $options = get_option('cos_options', true); + $options = get_option('cos_options', cos_get_default_options()); // If CI text for comments is not enabled if (($options['ci_text_comments'] ?? 'off') !== 'on') { @@ -1315,7 +1320,7 @@ function cos_request_txt_check($options, $comment) */ function cos_ci_attachment_preview_setting($content) { - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); if (!cos_validate_configuration($cos_options)) { return false; } @@ -1501,7 +1506,7 @@ function cos_setting_page() } if (!empty($_POST) and $_POST['type'] == 'qcloud_cos_all') { - if (cos_validate_configuration(get_option('cos_options', true))) { + if (cos_validate_configuration(get_option('cos_options', cos_get_default_options()))) { $files = cos_read_dir_queue(get_home_path(), cos_get_option('upload_path')); foreach ($files as $file) { cos_file_upload($file['key'], $file['filepath']); @@ -1575,7 +1580,7 @@ function cos_setting_page() } } - $cos_options = get_option('cos_options', true); + $cos_options = get_option('cos_options', cos_get_default_options()); $cos_regional = esc_attr($cos_options['regional']); $cos_nothumb = esc_attr($cos_options['nothumb']);