diff --git a/.wordpress-org/screenshot-7.png b/.wordpress-org/screenshot-7.png new file mode 100644 index 0000000..f2d104f Binary files /dev/null and b/.wordpress-org/screenshot-7.png differ diff --git a/README.md b/README.md index 957f8fb..3eeaa22 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ - [x] 支持文件预览 - [x] 支持文本内容审核 - [x] 支持原图保护 +- [x] 支持数据监控 +- [x] 支持 `wp-cli` 命令上传文件 ## 安装 @@ -51,6 +53,31 @@ GitHub 下载节点:[https://github.com/sy-records/sync-qcloud-cos/releases/la - 方法二:在 WordPress 后台管理左侧导航栏`设置`下`腾讯云 COS`,点击进入设置页面 - 方法三:在 WordPress 后台管理左侧一级导航栏`腾讯云 COS`,点击进入设置页面 +### 常量定义 + +数据监控中默认根据配置项判断是否展示`图片处理`、`文档处理`和`文本审核`的图表,如果需要强制展示或关闭,可以在`wp-config.php`中定义常量: + +| 常量 | 说明 | +| :----------------------- | :------------------- | +| `COS_DISABLE_CHARTS` | 禁用所有图表 | +| `COS_ENABLE_STYLE_CHART` | 强制展示图片处理图表 | +| `COS_ENABLE_PREVIEW_CHART` | 强制展示文档处理图表 | +| `COS_ENABLE_TEXT_CHART` | 强制展示文本审核图表 | + +```php +// 禁用所有图表 +define('COS_DISABLE_CHARTS', true); + +// 强制展示图片处理图表 +define('COS_ENABLE_STYLE_CHART', true); + +// 强制展示文档处理图表 +define('COS_ENABLE_PREVIEW_CHART', true); + +// 强制展示文本审核图表 +define('COS_ENABLE_TEXT_CHART', true); +``` + ## Contributors This project exists thanks to all the people who contribute. [[Contributors](https://github.com/sy-records/sync-qcloud-cos/graphs/contributors)]. @@ -136,6 +163,10 @@ curl.cainfo=/path/to/cacert.pem ![文档处理](.wordpress-org/screenshot-5.png) +![文本审核](.wordpress-org/screenshot-6.png) + +![内置 wp-cli 命令](.wordpress-org/screenshot-7.png) + ## 更新记录 [CHANGELOG](https://github.com/sy-records/sync-qcloud-cos/blob/master/CHANGELOG.md) diff --git a/cos-commands.php b/cos-commands.php new file mode 100644 index 0000000..7762905 --- /dev/null +++ b/cos-commands.php @@ -0,0 +1,101 @@ + + * : 要同步的文件夹 + * + * ## EXAMPLES + * + * wp cos upload wp-content/uploads + * + * @when after_wp_load + */ + public function upload($args, $assoc_args) + { + [$path] = $args; + $dir = ABSPATH . $path; + if (!is_dir($dir)) { + WP_CLI::error("Directory not found: [{$dir}]"); + } + + WP_CLI::line("Uploading files from [{$dir}] to COS..."); + + $files = cos_read_dir_queue(ABSPATH, $path); + if (empty($files)) { + WP_CLI::success('No files to upload.'); + return; + } + + foreach ($files as $file) { + $status = cos_file_upload($file['key'], $file['filepath']); + if ($status) { + WP_CLI::line("Uploaded: {$file['key']}"); + } else { + WP_CLI::line("Failed: {$file['key']}"); + } + } + + $total = count($files); + WP_CLI::success("Uploaded {$total} files."); + } + + /** + * 同步文件到 COS + * + * ## OPTIONS + * + * + * : 要同步的文件 + * + * [--delete] + * : 如果设置,上传后会删除本地文件 + * [--key=] + * : 指定上传到 COS 的 key,默认和文件路径一致 + * + * ## EXAMPLES + * + * wp cos upload-file wp-content/uploads/2021/01/1.jpg + * wp cos upload-file wp-content/uploads/2021/01/1.jpg --delete + * wp cos upload-file wp-content/uploads/2021/01/1.jpg --key=2021/01/1.jpg + * + * @when after_wp_load + * @subcommand upload-file + */ + public function upload_file($args, $assoc_args) + { + [$path] = $args; + $file = ABSPATH . $path; + if (!is_file($file)) { + WP_CLI::error("File not found: {$file}"); + } + + $delete = false; + if (isset($assoc_args['delete'])) { + $delete = true; + } + + $key = isset($assoc_args['key']) ? $assoc_args['key'] : $path; + + WP_CLI::line("Uploading file [{$file}] to COS with key [$key]..."); + + $status = cos_file_upload("/{$key}", $file, $delete); + if ($status) { + WP_CLI::success("Uploaded: {$path}"); + } else { + WP_CLI::error("Failed: {$path}"); + } + } +} + +WP_CLI::add_command('cos', 'COS_CLI_Commands', ['shortdesc' => 'Commands used to operate COS.']); diff --git a/readme.txt b/readme.txt index f45eedb..7cce70b 100644 --- a/readme.txt +++ b/readme.txt @@ -5,7 +5,7 @@ Tags: COS, 腾讯云, 对象存储, Tencent, Qcloud Requires at least: 4.6 Tested up to: 6.5 Requires PHP: 7.2 -Stable tag: 2.5.5 +Stable tag: 2.5.6 License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html @@ -34,6 +34,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 12. 支持文件预览 13. 支持文本内容审核 14. 支持原图保护 +15. 支持数据监控 +16. 支持 `wp-cli` 命令上传文件 插件更多详细介绍和安装:[https://github.com/sy-records/sync-qcloud-cos](https://github.com/sy-records/sync-qcloud-cos) @@ -57,6 +59,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 4. 数据监控 5. 文档处理 6. 文本内容审核:评论审核 +7. 内置的 wp-cli 命令 == Frequently Asked Questions == @@ -100,7 +103,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html = Stable = -- Optimize wpdb query +- Optimize chart display, you can set constants to be turned off. +- Support wp-cli commands to upload files. = Other = diff --git a/sync-qcloud-cos.php b/sync-qcloud-cos.php index b4488be..d3648c3 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.5.5 +Version: 2.5.6 Author: 沈唁 Author URI: https://qq52o.me License: Apache2.0 @@ -27,12 +27,16 @@ use SyncQcloudCos\Monitor\DataPoints; use SyncQcloudCos\Object\Head; -define('COS_VERSION', '2.5.5'); +define('COS_VERSION', '2.5.6'); define('COS_PLUGIN_SLUG', 'sync-qcloud-cos'); define('COS_PLUGIN_PAGE', plugin_basename(dirname(__FILE__)) . '%2F' . basename(__FILE__)); if (!function_exists('get_home_path')) { - require_once(ABSPATH . 'wp-admin/includes/file.php'); + require_once ABSPATH . 'wp-admin/includes/file.php'; +} + +if (defined('WP_CLI') && WP_CLI) { + require_once plugin_dir_path(__FILE__) . 'cos-commands.php'; } // 初始化选项 @@ -73,15 +77,16 @@ function cos_get_client($cos_options = null) if ($cos_options === null) { $cos_options = get_option('cos_options', true); } - return new Client([ - 'region' => esc_attr($cos_options['regional']), - 'scheme' => cos_get_url_scheme(''), - 'credentials' => [ - 'secretId' => esc_attr($cos_options['secret_id']), - 'secretKey' => esc_attr($cos_options['secret_key']) - ], - 'userAgent' => 'WordPress v' . $GLOBALS['wp_version'] . '; SyncQCloudCOS v' . COS_VERSION . '; SDK v' . Client::VERSION, - ]); + $config = [ + 'region' => esc_attr($cos_options['regional']), + 'scheme' => cos_get_url_scheme(''), + 'credentials' => [ + 'secretId' => esc_attr($cos_options['secret_id']), + 'secretKey' => esc_attr($cos_options['secret_key']) + ], + 'userAgent' => 'WordPress v' . $GLOBALS['wp_version'] . '; SyncQCloudCOS v' . COS_VERSION . '; SDK v' . Client::VERSION, + ]; + return new Client($config); } function cos_get_bucket_name($cos_options = null) @@ -239,10 +244,10 @@ function cos_get_image_style() } /** - * @param $object - * @param $filename + * @param string $object + * @param string $filename * @param bool $no_local_file - * @return false|void + * @return bool */ function cos_file_upload($object, $filename, $no_local_file = false) { @@ -264,12 +269,14 @@ function cos_file_upload($object, $filename, $no_local_file = false) if ($no_local_file) { cos_delete_local_file($filename); } + + return true; } } catch (\Throwable $e) { - if (WP_DEBUG) { - exit(json_encode(['errorMessage' => $e->getMessage(), 'statusCode' => $e->getStatusCode(), 'requestId' => $e->getRequestId()])); - } + error_log($e->getMessage()); } + + return false; } /** @@ -1781,7 +1788,20 @@ function cos_setting_page()
colors); echo Charts::storage($monitor->getStorage()); @@ -1789,16 +1809,16 @@ function cos_setting_page() echo Charts::requests($monitor->getRequests()); echo Charts::traffic($monitor->getTraffic()); - if (!empty($cos_options['ci_style'])) { + if ($styleChart) { echo Charts::ciStyle($monitor->getImageBasicsRequests()); echo Charts::ciTraffic($monitor->getCITraffic()); } - if (!empty($cos_options['attachment_preview']) && $cos_options['attachment_preview'] == 'on') { + if ($previewChart) { echo Charts::ciDocumentHtml($monitor->getDocumentHtmlRequests()); } - if (!empty($cos_options['ci_text_comments']) && $cos_options['ci_text_comments'] == 'on') { + if ($textChart) { echo Charts::ciTextAuditing($monitor->getTextAuditing()); } }