Skip to content

Commit

Permalink
Fix sub-site failure to delete remote images (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored Nov 23, 2024
1 parent 01d2990 commit 0948726
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [x] 支持数据监控
- [x] 支持使用 `wp-cli` 命令上传/删除文件
- [x] 支持上传文件到存储桶子目录
- [x] 支持多站点

## 安装

Expand Down
6 changes: 4 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 =

Expand Down
67 changes: 36 additions & 31 deletions sync-qcloud-cos.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__));

Expand All @@ -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' => '',
Expand All @@ -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');
}

/**
Expand All @@ -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']),
Expand All @@ -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']);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
}
Expand All @@ -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');
Expand Down Expand Up @@ -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';
}

Expand Down Expand Up @@ -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'])) {
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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'] ?? '';
Expand All @@ -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);

Expand Down Expand Up @@ -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']);
Expand All @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit 0948726

Please sign in to comment.