Skip to content

Commit

Permalink
Support disable charts and wp-cli commands (#68)
Browse files Browse the repository at this point in the history
* Support disable charts and wp-cli commands

* update readme

* Add upload-file command

* format code

* add screenshot
  • Loading branch information
sy-records authored May 29, 2024
1 parent 59625d3 commit e2f12e7
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 24 deletions.
Binary file added .wordpress-org/screenshot-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- [x] 支持文件预览
- [x] 支持文本内容审核
- [x] 支持原图保护
- [x] 支持数据监控
- [x] 支持 `wp-cli` 命令上传文件

## 安装

Expand All @@ -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)].
Expand Down Expand Up @@ -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)
101 changes: 101 additions & 0 deletions cos-commands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

if (!class_exists('WP_CLI')) {
return;
}

class COS_CLI_Commands
{

/**
* 同步文件夹到 COS
*
* ## OPTIONS
*
* <path>
* : 要同步的文件夹
*
* ## 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
*
* <path>
* : 要同步的文件
*
* [--delete]
* : 如果设置,上传后会删除本地文件
* [--key=<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.']);
8 changes: 6 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.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

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

Expand All @@ -57,6 +59,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
4. 数据监控
5. 文档处理
6. 文本内容审核:评论审核
7. 内置的 wp-cli 命令

== Frequently Asked Questions ==

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

Expand Down
64 changes: 42 additions & 22 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.5.5
Version: 2.5.6
Author: 沈唁
Author URI: https://qq52o.me
License: Apache2.0
Expand All @@ -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';
}

// 初始化选项
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -1781,24 +1788,37 @@ function cos_setting_page()
<div class="charts-container">
<?php
$bucket = cos_get_bucket_name($cos_options);
if (!empty($bucket)) {
$disableCharts = defined('COS_DISABLE_CHARTS') && COS_DISABLE_CHARTS;
if (!empty($bucket) && !$disableCharts) {
$styleChart = !empty($cos_options['ci_style']);
$previewChart = !empty($cos_options['attachment_preview']) && $cos_options['attachment_preview'] == 'on';
$textChart = !empty($cos_options['ci_text_comments']) && $cos_options['ci_text_comments'] == 'on';
if (defined('COS_ENABLE_STYLE_CHART')) {
$styleChart = COS_ENABLE_STYLE_CHART;
}
if (defined('COS_ENABLE_PREVIEW_CHART')) {
$previewChart = COS_ENABLE_PREVIEW_CHART;
}
if (defined('COS_ENABLE_TEXT_CHART')) {
$textChart = COS_ENABLE_TEXT_CHART;
}
$monitor = new DataPoints($bucket, $cos_options);
Charts::setColors($color_scheme->colors);
echo Charts::storage($monitor->getStorage());
echo Charts::objectNumber($monitor->getObjectNumber());
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());
}
}
Expand Down

0 comments on commit e2f12e7

Please sign in to comment.