diff --git a/.wordpress-org/screenshot-3.png b/.wordpress-org/screenshot-3.png new file mode 100644 index 0000000..af02ccc Binary files /dev/null and b/.wordpress-org/screenshot-3.png differ diff --git a/README.md b/README.md index 8b937e3..79da5fd 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - [x] 支持上传文件自动重命名 - [x] 支持使用 RAM 操作 - [x] 支持原图保护 +- [x] 支持 `wp-cli` 命令上传文件 ## 安装 diff --git a/aliyun-oss-wordpress.php b/aliyun-oss-wordpress.php index df8f5d7..9961fd7 100644 --- a/aliyun-oss-wordpress.php +++ b/aliyun-oss-wordpress.php @@ -3,7 +3,7 @@ Plugin Name: OSS Aliyun Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress Description: 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.) -Version: 1.4.12 +Version: 1.4.13 Author: 沈唁 Author URI: https://qq52o.me License: Apache2.0 @@ -19,11 +19,15 @@ use OSS\Credentials\CredentialsProvider; use AlibabaCloud\Credentials\Credential; use OSS\Credentials\StaticCredentialsProvider; -define('OSS_VERSION', '1.4.12'); +define('OSS_VERSION', '1.4.13'); define('OSS_BASEFOLDER', plugin_basename(dirname(__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__) . 'oss-commands.php'; } class OSSCredentialsWrapper implements CredentialsProvider @@ -121,9 +125,7 @@ function oss_get_file_meta($object) $bucket = oss_get_bucket_name(); return $ossClient->getObjectMeta($bucket, $object); } catch (\Throwable $e) { - if (WP_DEBUG) { - echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode(); - } + error_log($e->getMessage()); return ['content-length' => 0]; } } @@ -132,6 +134,7 @@ function oss_get_file_meta($object) * @param string $object * @param string $file * @param bool $no_local_file + * @return bool */ function oss_file_upload($object, $file, $no_local_file = false) { @@ -143,13 +146,14 @@ function oss_file_upload($object, $file, $no_local_file = false) $ossClient = oss_get_client(); try { $ossClient->uploadFile($bucket, ltrim($object, '/'), $file); - } catch (\Throwable $e) { - if (WP_DEBUG) { - echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode(); + if ($no_local_file) { + oss_delete_local_file($file); } - } - if ($no_local_file) { - oss_delete_local_file($file); + + return true; + } catch (\Throwable $e) { + error_log($e->getMessage()); + return false; } } @@ -167,7 +171,7 @@ function oss_is_delete_local_file() /** * 删除本地文件 * - * @param $file + * @param string $file * @return bool */ function oss_delete_local_file($file) @@ -201,9 +205,7 @@ function oss_delete_oss_file($file) $ossClient = oss_get_client(); $ossClient->deleteObject($bucket, $file); } catch (\Throwable $e) { - if (WP_DEBUG) { - echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode(); - } + error_log($e->getMessage()); } } @@ -223,9 +225,7 @@ function oss_delete_oss_files(array $files) $ossClient = oss_get_client(); $ossClient->deleteObjects($bucket, $deleteObjects); } catch (\Throwable $e) { - if (WP_DEBUG) { - echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode(); - } + error_log($e->getMessage()); } } @@ -457,7 +457,7 @@ function oss_read_dir_queue($homePath, $uploadPath) $foundFiles = []; while (!$dirsToProcess->isEmpty()) { - list($currentDir, $relativeDir) = $dirsToProcess->dequeue(); + [$currentDir, $relativeDir] = $dirsToProcess->dequeue(); foreach (new DirectoryIterator($currentDir) as $fileInfo) { if ($fileInfo->isDot()) continue; diff --git a/oss-commands.php b/oss-commands.php new file mode 100644 index 0000000..1fad8e5 --- /dev/null +++ b/oss-commands.php @@ -0,0 +1,101 @@ + + * : 要同步的文件夹 + * + * ## EXAMPLES + * + * wp oss 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 OSS..."); + + $files = oss_read_dir_queue(ABSPATH, $path); + if (empty($files)) { + WP_CLI::success('No files to upload.'); + return; + } + + foreach ($files as $file) { + $status = oss_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."); + } + + /** + * 同步文件到 OSS + * + * ## OPTIONS + * + * + * : 要同步的文件 + * + * [--delete] + * : 如果设置,上传后会删除本地文件 + * [--key=] + * : 指定上传到 OSS 的 key,默认和文件路径一致 + * + * ## EXAMPLES + * + * wp oss upload-file wp-content/uploads/2021/01/1.jpg + * wp oss upload-file wp-content/uploads/2021/01/1.jpg --delete + * wp oss 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 OSS with key [$key]..."); + + $status = oss_file_upload("/{$key}", $file, $delete); + if ($status) { + WP_CLI::success("Uploaded: {$path}"); + } else { + WP_CLI::error("Failed: {$path}"); + } + } +} + +WP_CLI::add_command('oss', 'OSS_CLI_Commands', ['shortdesc' => 'Commands used to operate OSS.']); diff --git a/readme.txt b/readme.txt index b966f66..153bf88 100644 --- a/readme.txt +++ b/readme.txt @@ -4,8 +4,8 @@ Donate link: https://qq52o.me/sponsor.html Tags: oss, 阿里云, 对象存储, aliyun Requires at least: 4.6 Tested up to: 6.5 -Requires PHP: 7.0 -Stable tag: 1.4.12 +Requires PHP: 7.1 +Stable tag: 1.4.13 License: Apache2.0 License URI: http://www.apache.org/licenses/LICENSE-2.0.html @@ -29,7 +29,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 8. 支持上传文件自动重命名 9. 支持使用 RAM 操作 10. 支持原图保护 -11. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) +11. 支持 `wp-cli` 命令上传文件 +12. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress) ## 其他插件 @@ -54,6 +55,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html 1. 设置页面 2. 数据库同步 +3. 内置的 wp-cli 命令 == Frequently Asked Questions == @@ -74,6 +76,10 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html == Changelog == += 1.4.13 = + +- 支持 `wp-cli` 命令上传文件 + = 1.4.12 = - 支持原图保护 diff --git a/uninstall.php b/uninstall.php index 0539ba3..3e26537 100644 --- a/uninstall.php +++ b/uninstall.php @@ -1,11 +1,11 @@