-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support disable charts and wp-cli commands (#68)
* Support disable charts and wp-cli commands * update readme * Add upload-file command * format code * add screenshot
- Loading branch information
1 parent
59625d3
commit e2f12e7
Showing
5 changed files
with
180 additions
and
24 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters