Compare commits
31 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
96d6861184 | |
![]() |
fd39ea37af | |
![]() |
2d7be1839d | |
![]() |
04023f72eb | |
![]() |
f291f253aa | |
![]() |
fe38540a5b | |
![]() |
bc9387d3ad | |
![]() |
f9598e9d71 | |
![]() |
c8981d2ced | |
![]() |
28df5b05f4 | |
![]() |
69626aa636 | |
![]() |
eda8511bda | |
![]() |
c32790061d | |
![]() |
1892fd78b9 | |
![]() |
f8976d8915 | |
![]() |
53934e9d4b | |
![]() |
b192c22585 | |
![]() |
52aa553d9e | |
![]() |
30781999db | |
![]() |
0ef8ef14bd | |
![]() |
30e7e6eb17 | |
![]() |
ce9cfa44e1 | |
![]() |
530ddf967d | |
![]() |
7096900318 | |
![]() |
59c57fe976 | |
![]() |
9852fff6c2 | |
![]() |
9a9882fd96 | |
![]() |
dd04d774bf | |
![]() |
6c09f4057e | |
![]() |
48df76994b | |
![]() |
cb26713b32 |
|
@ -1,6 +1,10 @@
|
||||||
/.git
|
/.git
|
||||||
/.github
|
/.github
|
||||||
/.distignore
|
/.distignore
|
||||||
|
/.wordpress-org
|
||||||
/sdk/composer.lock
|
/sdk/composer.lock
|
||||||
/sdk/vendor/aliyuncs/oss-sdk-php/samples/
|
/sdk/vendor/aliyuncs/oss-sdk-php/samples/
|
||||||
/sdk/vendor/aliyuncs/oss-sdk-php/tests/
|
/sdk/vendor/aliyuncs/oss-sdk-php/tests/
|
||||||
|
/sdk/vendor/aliyuncs/oss-sdk-php/.coveralls.yml
|
||||||
|
*.github
|
||||||
|
*.php_cs*
|
|
@ -7,4 +7,6 @@ on:
|
||||||
jobs:
|
jobs:
|
||||||
update:
|
update:
|
||||||
uses: sy-records/.github/.github/workflows/wordpress-plugin-asset-update.yaml@main
|
uses: sy-records/.github/.github/workflows/wordpress-plugin-asset-update.yaml@main
|
||||||
|
with:
|
||||||
|
skip_assets: false
|
||||||
secrets: inherit
|
secrets: inherit
|
||||||
|
|
After Width: | Height: | Size: 22 KiB |
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"landingPage": "/wp-admin/admin.php?page=oss-aliyun%2Faliyun-oss-wordpress.php",
|
||||||
|
"preferredVersions": {
|
||||||
|
"php": "latest",
|
||||||
|
"wp": "latest"
|
||||||
|
},
|
||||||
|
"phpExtensionBundles": [
|
||||||
|
"kitchen-sink"
|
||||||
|
],
|
||||||
|
"steps": [
|
||||||
|
{
|
||||||
|
"step": "login",
|
||||||
|
"username": "admin",
|
||||||
|
"password": "password"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"step": "installPlugin",
|
||||||
|
"pluginZipFile": {
|
||||||
|
"resource": "wordpress.org/plugins",
|
||||||
|
"slug": "oss-aliyun"
|
||||||
|
},
|
||||||
|
"options": {
|
||||||
|
"activate": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
After Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 385 KiB After Width: | Height: | Size: 385 KiB |
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
After Width: | Height: | Size: 97 KiB |
40
README.md
|
@ -10,17 +10,23 @@
|
||||||
[](https://wordpress.org/plugins/oss-aliyun/)
|
[](https://wordpress.org/plugins/oss-aliyun/)
|
||||||
[](https://github.com/sy-records/aliyun-oss-wordpress/actions/workflows/deploy.yml)
|
[](https://github.com/sy-records/aliyun-oss-wordpress/actions/workflows/deploy.yml)
|
||||||
|
|
||||||
|
> 使用 WordPress Playground 在线预览 [OSS Aliyun](https://wordpress.org/plugins/oss-aliyun/?preview=1) 插件
|
||||||
|
|
||||||
## 插件特色
|
## 插件特色
|
||||||
|
|
||||||
* [x] 可配置是否上传缩略图和是否保留本地备份
|
- [x] 可配置是否上传缩略图和是否保留本地备份
|
||||||
* [x] 本地删除可同步删除阿里云对象存储OSS中的文件
|
- [x] 本地删除可同步删除阿里云对象存储 OSS 中的文件
|
||||||
* [x] 支持阿里云对象存储OSS绑定的用户域名
|
- [x] 支持阿里云对象存储 OSS 绑定的用户域名
|
||||||
* [x] 支持替换数据库中旧的资源链接地址
|
- [x] 支持替换数据库中旧的资源链接地址
|
||||||
* [x] 支持阿里云对象存储OSS完整地域使用
|
- [x] 支持阿里云对象存储 OSS 完整地域使用
|
||||||
* [x] 支持同步历史附件到阿里云对象存储OSS
|
- [x] 支持同步历史附件到阿里云对象存储 OSS
|
||||||
* [x] 支持阿里云OSS图片处理
|
- [x] 支持阿里云 OSS 图片处理
|
||||||
* [x] 支持上传文件自动重命名
|
- [x] 支持上传文件自动重命名
|
||||||
* [x] 支持使用RAM操作
|
- [x] 支持使用 ECS 的 RAM 操作
|
||||||
|
- [x] 支持原图保护
|
||||||
|
- [x] 支持 `wp-cli` 命令上传/删除文件
|
||||||
|
- [x] 支持多站点
|
||||||
|
- [x] 支持图片裁剪编辑等操作后的上传
|
||||||
|
|
||||||
## 安装
|
## 安装
|
||||||
|
|
||||||
|
@ -36,24 +42,24 @@ GitHub 下载节点:[https://github.com/sy-records/aliyun-oss-wordpress/releas
|
||||||
|
|
||||||
## 修改配置
|
## 修改配置
|
||||||
|
|
||||||
* 方法一:在 WordPress 插件管理页面有设置按钮,进行设置
|
- 方法一:在 WordPress 插件管理页面有设置按钮,进行设置
|
||||||
* 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面
|
- 方法二:在 WordPress 后台管理左侧导航栏`设置`下`阿里云OSS设置`,点击进入设置页面
|
||||||
|
|
||||||
## 常见问题
|
## 常见问题
|
||||||
|
|
||||||
* 通过文件URL访问图片无法预览而是以附件形式下载?
|
- 通过文件 URL 访问图片无法预览而是以附件形式下载?
|
||||||
|
|
||||||
因为Bucket是2019年9月23日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
|
因为 Bucket 是 2019 年 9 月 23 日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
|
||||||
|
|
||||||
* 如果存在第三方插件或者主题自带上传功能,内容上传到`本地文件夹`(即默认`wp-content/uploads`)中,怎么上传到oss中?
|
- 如果存在第三方插件或者主题自带上传功能,内容上传到`本地文件夹`(即默认`wp-content/uploads`)中,怎么上传到 oss 中?
|
||||||
|
|
||||||
解决方案有两种,推荐使用第二种。
|
解决方案有两种,推荐使用第二种。
|
||||||
|
|
||||||
一是修改第三方插件或者主题的上传功能,调用插件的`oss_file_upload`方法(不推荐,一般人不会修改)
|
一是修改第三方插件或者主题的上传功能,调用插件的`oss_file_upload`方法(不推荐,一般人不会修改)
|
||||||
二是使用对象存储OSS提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行GET操作时,OSS会向回源地址请求这个文件,返回给用户,同时会将该文件存入OSS。这样就达到了上传到OSS的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html)
|
二是使用对象存储 OSS 提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行 GET 操作时,OSS 会向回源地址请求这个文件,返回给用户,同时会将该文件存入 OSS。这样就达到了上传到 OSS 的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html)
|
||||||
|
|
||||||
## 插件截图
|
## 插件截图
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||

|

|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
/*
|
/*
|
||||||
Plugin Name: OSS Aliyun
|
Plugin Name: OSS Aliyun
|
||||||
Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress
|
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.)
|
Description: 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.)
|
||||||
Version: 1.4.3
|
Version: 1.5.1
|
||||||
Author: 沈唁
|
Author: 沈唁
|
||||||
Author URI: https://qq52o.me
|
Author URI: https://qq52o.me
|
||||||
License: Apache 2.0
|
License: Apache2.0
|
||||||
*/
|
*/
|
||||||
if (!defined('ABSPATH')) {
|
if (!defined('ABSPATH')) {
|
||||||
exit;
|
exit;
|
||||||
|
@ -15,16 +15,20 @@ if (!defined('ABSPATH')) {
|
||||||
require_once 'sdk/vendor/autoload.php';
|
require_once 'sdk/vendor/autoload.php';
|
||||||
|
|
||||||
use OSS\OssClient;
|
use OSS\OssClient;
|
||||||
use OSS\Core\OssException;
|
|
||||||
use OSS\Credentials\CredentialsProvider;
|
use OSS\Credentials\CredentialsProvider;
|
||||||
use AlibabaCloud\Credentials\Credential;
|
use AlibabaCloud\Credentials\Credential;
|
||||||
use OSS\Credentials\StaticCredentialsProvider;
|
use OSS\Credentials\StaticCredentialsProvider;
|
||||||
|
use OSS\Core\OssException;
|
||||||
|
|
||||||
define('OSS_VERSION', '1.4.3');
|
define('OSS_VERSION', '1.5.1');
|
||||||
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
|
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
|
||||||
|
|
||||||
if (!function_exists('get_home_path')) {
|
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
|
class OSSCredentialsWrapper implements CredentialsProvider
|
||||||
|
@ -50,12 +54,12 @@ class OSSCredentialsWrapper implements CredentialsProvider
|
||||||
|
|
||||||
// 初始化选项
|
// 初始化选项
|
||||||
register_activation_hook(__FILE__, 'oss_set_options');
|
register_activation_hook(__FILE__, 'oss_set_options');
|
||||||
// 初始化选项
|
function oss_get_default_options()
|
||||||
function oss_set_options()
|
|
||||||
{
|
{
|
||||||
$options = [
|
return [
|
||||||
'bucket' => '',
|
'bucket' => '',
|
||||||
'regional' => 'oss-cn-shanghai',
|
'regional' => 'oss-cn-shanghai',
|
||||||
|
'origin_region' => '',
|
||||||
'accessKeyId' => '',
|
'accessKeyId' => '',
|
||||||
'accessKeySecret' => '',
|
'accessKeySecret' => '',
|
||||||
'is_internal' => 'false',
|
'is_internal' => 'false',
|
||||||
|
@ -64,56 +68,102 @@ function oss_set_options()
|
||||||
'upload_url_path' => '', // URL前缀
|
'upload_url_path' => '', // URL前缀
|
||||||
'style' => '', // 图片处理
|
'style' => '', // 图片处理
|
||||||
'update_file_name' => 'false', // 是否重命名文件名
|
'update_file_name' => 'false', // 是否重命名文件名
|
||||||
'role_name' => '' // 角色名称
|
'role_name' => '', // 角色名称
|
||||||
|
'origin_protect' => 'off',
|
||||||
];
|
];
|
||||||
add_option('oss_options', $options, '', 'yes');
|
}
|
||||||
|
|
||||||
|
// 初始化选项
|
||||||
|
function oss_set_options()
|
||||||
|
{
|
||||||
|
add_option('oss_options', oss_get_default_options(), '', 'yes');
|
||||||
}
|
}
|
||||||
|
|
||||||
function oss_get_client()
|
function oss_get_client()
|
||||||
{
|
{
|
||||||
$oss_opt = get_option('oss_options', true);
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
$roleName = esc_attr($oss_opt['role_name'] ?? '');
|
$role_name = esc_attr($oss_options['role_name'] ?? '');
|
||||||
$endpoint = oss_get_bucket_endpoint($oss_opt);
|
$endpoint = oss_get_bucket_endpoint($oss_options);
|
||||||
|
|
||||||
if (!empty($roleName)) {
|
if (!empty($role_name)) {
|
||||||
$ecsRamRole = new Credential([
|
$ecsRamRole = new Credential([
|
||||||
// 填写Credential类型,固定值为ecs_ram_role。
|
// 填写Credential类型,固定值为ecs_ram_role。
|
||||||
'type' => 'ecs_ram_role',
|
'type' => 'ecs_ram_role',
|
||||||
// 填写角色名称。
|
// 填写角色名称。
|
||||||
'role_name' => $roleName,
|
'role_name' => $role_name,
|
||||||
]);
|
]);
|
||||||
$providerWrapper = new OSSCredentialsWrapper($ecsRamRole);
|
$providerWrapper = new OSSCredentialsWrapper($ecsRamRole);
|
||||||
$provider = $providerWrapper->getCredentials();
|
$provider = $providerWrapper->getCredentials();
|
||||||
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
|
} else {
|
||||||
$config = ['provider' => $provider, 'endpoint'=> $protocol . $endpoint];
|
$provider = new StaticCredentialsProvider(esc_attr($oss_options['accessKeyId']), esc_attr($oss_options['accessKeySecret']));
|
||||||
return new OssClient($config);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$accessKeyId = esc_attr($oss_opt['accessKeyId']);
|
$region = !empty($oss_options['origin_region']) ? esc_attr($oss_options['origin_region']) : esc_attr($oss_options['regional']);
|
||||||
$accessKeySecret = esc_attr($oss_opt['accessKeySecret']);
|
if (in_array($region, ['oss-accelerate', 'oss-accelerate-overseas']) && empty($oss_options['origin_region'])) {
|
||||||
return new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
try {
|
||||||
|
$config = [
|
||||||
|
'provider' => $provider,
|
||||||
|
'endpoint' => $region == 'oss-accelerate' ? 'https://oss-cn-hangzhou.aliyuncs.com' : 'https://oss-cn-hongkong.aliyuncs.com',
|
||||||
|
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
|
||||||
|
'region' => $region == 'oss-accelerate' ? 'cn-hangzhou' : 'cn-hongkong',
|
||||||
|
];
|
||||||
|
$region = (new OssClient($config))->getBucketInfo(esc_attr($oss_options['bucket']))->getLocation();
|
||||||
|
$oss_options['origin_region'] = sanitize_text_field($region);
|
||||||
|
update_option('oss_options', $oss_options);
|
||||||
|
} catch (OssException $e) {
|
||||||
|
error_log("get bucket location failed: {$e->getMessage()}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$region = str_replace('oss-', '', $region);
|
||||||
|
|
||||||
|
$config = [
|
||||||
|
'provider' => $provider,
|
||||||
|
'endpoint' => $endpoint,
|
||||||
|
'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4,
|
||||||
|
'region' => $region,
|
||||||
|
];
|
||||||
|
return new OssClient($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
function oss_get_bucket_endpoint($oss_option)
|
function oss_get_bucket_endpoint($oss_options)
|
||||||
{
|
{
|
||||||
$oss_regional = esc_attr($oss_option['regional']);
|
$regional = esc_attr($oss_options['regional']);
|
||||||
if ($oss_option['is_internal'] == 'true') {
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
|
||||||
return "{$oss_regional}-internal.aliyuncs.com";
|
if ($oss_options['is_internal'] == 'true') {
|
||||||
|
return "{$protocol}://{$regional}-internal.aliyuncs.com";
|
||||||
}
|
}
|
||||||
|
|
||||||
return "{$oss_regional}.aliyuncs.com";
|
return "{$protocol}://{$regional}.aliyuncs.com";
|
||||||
}
|
}
|
||||||
|
|
||||||
function oss_get_bucket_name()
|
function oss_get_bucket_name()
|
||||||
{
|
{
|
||||||
$oss_opt = get_option('oss_options', true);
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
return $oss_opt['bucket'];
|
return esc_attr($oss_options['bucket']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $object
|
* @param string $object
|
||||||
* @param $file
|
* @return array
|
||||||
* @param false $no_local_file
|
*/
|
||||||
|
function oss_get_file_meta($object)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$ossClient = oss_get_client();
|
||||||
|
$bucket = oss_get_bucket_name();
|
||||||
|
return $ossClient->getObjectMeta($bucket, $object);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
error_log($e->getMessage());
|
||||||
|
return ['content-length' => 0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $object
|
||||||
|
* @param string $file
|
||||||
|
* @param bool $no_local_file
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function oss_file_upload($object, $file, $no_local_file = false)
|
function oss_file_upload($object, $file, $no_local_file = false)
|
||||||
{
|
{
|
||||||
|
@ -123,15 +173,16 @@ function oss_file_upload($object, $file, $no_local_file = false)
|
||||||
}
|
}
|
||||||
$bucket = oss_get_bucket_name();
|
$bucket = oss_get_bucket_name();
|
||||||
$ossClient = oss_get_client();
|
$ossClient = oss_get_client();
|
||||||
try{
|
try {
|
||||||
$ossClient->uploadFile($bucket, ltrim($object, "/"), $file);
|
$ossClient->uploadFile($bucket, ltrim($object, '/'), $file);
|
||||||
} catch (OssException $e) {
|
if ($no_local_file) {
|
||||||
if (WP_DEBUG) {
|
oss_delete_local_file($file);
|
||||||
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if ($no_local_file) {
|
return true;
|
||||||
oss_delete_local_file($file);
|
} catch (\Throwable $e) {
|
||||||
|
error_log($e->getMessage());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,14 +193,14 @@ function oss_file_upload($object, $file, $no_local_file = false)
|
||||||
*/
|
*/
|
||||||
function oss_is_delete_local_file()
|
function oss_is_delete_local_file()
|
||||||
{
|
{
|
||||||
$oss_options = get_option('oss_options', true);
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
return esc_attr($oss_options['nolocalsaving']) == 'true';
|
return esc_attr($oss_options['nolocalsaving']) == 'true';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除本地文件
|
* 删除本地文件
|
||||||
*
|
*
|
||||||
* @param $file
|
* @param string $file
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function oss_delete_local_file($file)
|
function oss_delete_local_file($file)
|
||||||
|
@ -183,9 +234,7 @@ function oss_delete_oss_file($file)
|
||||||
$ossClient = oss_get_client();
|
$ossClient = oss_get_client();
|
||||||
$ossClient->deleteObject($bucket, $file);
|
$ossClient->deleteObject($bucket, $file);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
if (WP_DEBUG) {
|
error_log($e->getMessage());
|
||||||
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,14 +244,17 @@ function oss_delete_oss_file($file)
|
||||||
*/
|
*/
|
||||||
function oss_delete_oss_files(array $files)
|
function oss_delete_oss_files(array $files)
|
||||||
{
|
{
|
||||||
|
$deleteObjects = [];
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$deleteObjects[] = str_replace(["\\", './'], ['/', ''], $file);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$bucket = oss_get_bucket_name();
|
$bucket = oss_get_bucket_name();
|
||||||
$ossClient = oss_get_client();
|
$ossClient = oss_get_client();
|
||||||
$ossClient->deleteObjects($bucket, $files);
|
$ossClient->deleteObjects($bucket, $deleteObjects);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
if (WP_DEBUG) {
|
error_log($e->getMessage());
|
||||||
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,7 +266,7 @@ function oss_delete_oss_files(array $files)
|
||||||
*/
|
*/
|
||||||
function oss_upload_attachments($metadata)
|
function oss_upload_attachments($metadata)
|
||||||
{
|
{
|
||||||
$mime_types = get_allowed_mime_types();
|
$mime_types = wp_get_mime_types();
|
||||||
$image_mime_types = [
|
$image_mime_types = [
|
||||||
$mime_types['jpg|jpeg|jpe'],
|
$mime_types['jpg|jpeg|jpe'],
|
||||||
$mime_types['gif'],
|
$mime_types['gif'],
|
||||||
|
@ -223,7 +275,6 @@ function oss_upload_attachments($metadata)
|
||||||
$mime_types['tiff|tif'],
|
$mime_types['tiff|tif'],
|
||||||
$mime_types['webp'],
|
$mime_types['webp'],
|
||||||
$mime_types['ico'],
|
$mime_types['ico'],
|
||||||
$mime_types['heic'],
|
|
||||||
];
|
];
|
||||||
// 例如mp4等格式 上传后根据配置选择是否删除 删除后媒体库会显示默认图片 点开内容是正常的
|
// 例如mp4等格式 上传后根据配置选择是否删除 删除后媒体库会显示默认图片 点开内容是正常的
|
||||||
// 图片在缩略图处理
|
// 图片在缩略图处理
|
||||||
|
@ -251,45 +302,53 @@ function oss_upload_attachments($metadata)
|
||||||
*/
|
*/
|
||||||
function oss_upload_thumbs($metadata)
|
function oss_upload_thumbs($metadata)
|
||||||
{
|
{
|
||||||
|
if (empty($metadata['file'])) {
|
||||||
|
return $metadata;
|
||||||
|
}
|
||||||
|
|
||||||
//获取上传路径
|
//获取上传路径
|
||||||
$wp_uploads = wp_upload_dir();
|
$wp_uploads = wp_upload_dir();
|
||||||
$basedir = $wp_uploads['basedir'];
|
$basedir = $wp_uploads['basedir'];
|
||||||
//获取oss插件的配置信息
|
$upload_path = oss_get_option('upload_path');
|
||||||
$oss_options = get_option('oss_options', true);
|
|
||||||
if (!empty($metadata['file'])) {
|
|
||||||
// Maybe there is a problem with the old version
|
|
||||||
$file = $basedir . '/' . $metadata['file'];
|
|
||||||
$upload_path = oss_get_option('upload_path');
|
|
||||||
if ($upload_path != '.') {
|
|
||||||
$path_array = explode($upload_path, $file);
|
|
||||||
if (count($path_array) >= 2) {
|
|
||||||
$object = '/' . $upload_path . end($path_array);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$object = '/' . $metadata['file'];
|
|
||||||
$file = str_replace('./', '', $file);
|
|
||||||
}
|
|
||||||
|
|
||||||
oss_file_upload($object, $file, esc_attr($oss_options['nolocalsaving']) == 'true');
|
//获取oss插件的配置信息
|
||||||
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
|
$no_local_file = esc_attr($oss_options['nolocalsaving']) == 'true';
|
||||||
|
$no_thumb = esc_attr($oss_options['nothumb']) == 'true';
|
||||||
|
|
||||||
|
// Maybe there is a problem with the old version
|
||||||
|
$file = $basedir . '/' . $metadata['file'];
|
||||||
|
if ($upload_path != '.') {
|
||||||
|
$path_array = explode($upload_path, $file);
|
||||||
|
if (count($path_array) >= 2) {
|
||||||
|
$object = '/' . $upload_path . end($path_array);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$object = '/' . $metadata['file'];
|
||||||
|
$file = str_replace('./', '', $file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oss_file_upload($object, $file, $no_local_file);
|
||||||
|
|
||||||
|
//得到本地文件夹和远端文件夹
|
||||||
|
$dirname = dirname($metadata['file']);
|
||||||
|
$file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/";
|
||||||
|
$file_path = str_replace("\\", '/', $file_path);
|
||||||
|
if ($upload_path == '.') {
|
||||||
|
$file_path = str_replace('./', '', $file_path);
|
||||||
|
}
|
||||||
|
$object_path = str_replace(get_home_path(), '', $file_path);
|
||||||
|
|
||||||
|
if (!empty($metadata['original_image'])) {
|
||||||
|
oss_file_upload("/{$object_path}{$metadata['original_image']}", "{$file_path}{$metadata['original_image']}", $no_local_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($no_thumb) {
|
||||||
|
return $metadata;
|
||||||
|
}
|
||||||
|
|
||||||
//上传所有缩略图
|
//上传所有缩略图
|
||||||
if (!empty($metadata['sizes'])) {
|
if (!empty($metadata['sizes'])) {
|
||||||
//是否需要上传缩略图
|
|
||||||
$nothumb = esc_attr($oss_options['nothumb']) == 'true';
|
|
||||||
//如果禁止上传缩略图,就不用继续执行了
|
|
||||||
if ($nothumb) {
|
|
||||||
return $metadata;
|
|
||||||
}
|
|
||||||
//得到本地文件夹和远端文件夹
|
|
||||||
$dirname = dirname($metadata['file']);
|
|
||||||
$file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/";
|
|
||||||
$file_path = str_replace("\\", '/', $file_path);
|
|
||||||
if ($upload_path == '.') {
|
|
||||||
$file_path = str_replace('./', '', $file_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$object_path = str_replace(get_home_path(), '', $file_path);
|
|
||||||
|
|
||||||
//there may be duplicated filenames,so ....
|
//there may be duplicated filenames,so ....
|
||||||
foreach ($metadata['sizes'] as $val) {
|
foreach ($metadata['sizes'] as $val) {
|
||||||
//生成object在oss中的存储路径
|
//生成object在oss中的存储路径
|
||||||
|
@ -298,7 +357,7 @@ function oss_upload_thumbs($metadata)
|
||||||
$file = $file_path . $val['file'];
|
$file = $file_path . $val['file'];
|
||||||
|
|
||||||
//执行上传操作
|
//执行上传操作
|
||||||
oss_file_upload($object, $file, esc_attr($oss_options['nolocalsaving']) == 'true');
|
oss_file_upload($object, $file, $no_local_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $metadata;
|
return $metadata;
|
||||||
|
@ -336,6 +395,9 @@ if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
|
||||||
*/
|
*/
|
||||||
function oss_delete_remote_attachment($post_id)
|
function oss_delete_remote_attachment($post_id)
|
||||||
{
|
{
|
||||||
|
$wp_uploads = wp_upload_dir();
|
||||||
|
$basedir = $wp_uploads['basedir'];
|
||||||
|
$upload_path = str_replace(get_home_path(), '', $basedir);
|
||||||
// 获取图片类附件的meta信息
|
// 获取图片类附件的meta信息
|
||||||
$meta = wp_get_attachment_metadata($post_id);
|
$meta = wp_get_attachment_metadata($post_id);
|
||||||
|
|
||||||
|
@ -343,19 +405,27 @@ function oss_delete_remote_attachment($post_id)
|
||||||
$deleteObjects = [];
|
$deleteObjects = [];
|
||||||
|
|
||||||
// meta['file']的格式为 "2020/01/wp-bg.png"
|
// meta['file']的格式为 "2020/01/wp-bg.png"
|
||||||
$upload_path = oss_get_option('upload_path');
|
|
||||||
if ($upload_path == '') {
|
|
||||||
$upload_path = 'wp-content/uploads';
|
|
||||||
}
|
|
||||||
$file_path = $upload_path . '/' . $meta['file'];
|
$file_path = $upload_path . '/' . $meta['file'];
|
||||||
|
$dirname = dirname($file_path) . '/';
|
||||||
|
|
||||||
$deleteObjects[] = str_replace("\\", '/', $file_path);
|
$deleteObjects[] = $file_path;
|
||||||
|
|
||||||
|
// 超大图原图
|
||||||
|
if (!empty($meta['original_image'])) {
|
||||||
|
$deleteObjects[] = $dirname . $meta['original_image'];
|
||||||
|
}
|
||||||
|
|
||||||
// 删除缩略图
|
// 删除缩略图
|
||||||
if (!empty($meta['sizes'])) {
|
if (!empty($meta['sizes'])) {
|
||||||
foreach ($meta['sizes'] as $val) {
|
foreach ($meta['sizes'] as $val) {
|
||||||
$size_file = dirname($file_path) . '/' . $val['file'];
|
$deleteObjects[] = $dirname . $val['file'];
|
||||||
$deleteObjects[] = str_replace("\\", '/', $size_file);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$backup_sizes = get_post_meta($post_id, '_wp_attachment_backup_sizes', true);
|
||||||
|
if (is_array($backup_sizes)) {
|
||||||
|
foreach ($backup_sizes as $size) {
|
||||||
|
$deleteObjects[] = $dirname . $size['file'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,14 +434,13 @@ function oss_delete_remote_attachment($post_id)
|
||||||
// 获取链接删除
|
// 获取链接删除
|
||||||
$link = wp_get_attachment_url($post_id);
|
$link = wp_get_attachment_url($post_id);
|
||||||
if ($link) {
|
if ($link) {
|
||||||
$upload_path = oss_get_option('upload_path');
|
|
||||||
if ($upload_path != '.') {
|
if ($upload_path != '.') {
|
||||||
$file_info = explode($upload_path, $link);
|
$file_info = explode($upload_path, $link);
|
||||||
if (count($file_info) >= 2) {
|
if (count($file_info) >= 2) {
|
||||||
oss_delete_oss_file($upload_path . end($file_info));
|
oss_delete_oss_file($upload_path . end($file_info));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$oss_options = get_option('oss_options', true);
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
$oss_upload_url = esc_attr($oss_options['upload_url_path']);
|
$oss_upload_url = esc_attr($oss_options['upload_url_path']);
|
||||||
$file_info = explode($oss_upload_url, $link);
|
$file_info = explode($oss_upload_url, $link);
|
||||||
if (count($file_info) >= 2) {
|
if (count($file_info) >= 2) {
|
||||||
|
@ -384,19 +453,19 @@ function oss_delete_remote_attachment($post_id)
|
||||||
add_action('delete_attachment', 'oss_delete_remote_attachment');
|
add_action('delete_attachment', 'oss_delete_remote_attachment');
|
||||||
|
|
||||||
// 当upload_path为根目录时,需要移除URL中出现的“绝对路径”
|
// 当upload_path为根目录时,需要移除URL中出现的“绝对路径”
|
||||||
function oss_modefiy_img_url($url, $post_id)
|
function oss_modify_img_url($url, $post_id)
|
||||||
{
|
{
|
||||||
// 移除 ./ 和 项目根路径
|
// 移除 ./ 和 项目根路径
|
||||||
return str_replace(['./', get_home_path()], '', $url);
|
return str_replace(['./', get_home_path()], '', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (oss_get_option('upload_path') == '.') {
|
if (oss_get_option('upload_path') == '.') {
|
||||||
add_filter('wp_get_attachment_url', 'oss_modefiy_img_url', 30, 2);
|
add_filter('wp_get_attachment_url', 'oss_modify_img_url', 30, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function oss_sanitize_file_name($filename)
|
function oss_sanitize_file_name($filename)
|
||||||
{
|
{
|
||||||
$oss_options = get_option('oss_options');
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
switch ($oss_options['update_file_name']) {
|
switch ($oss_options['update_file_name']) {
|
||||||
case 'md5':
|
case 'md5':
|
||||||
return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
|
return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
|
||||||
|
@ -452,25 +521,72 @@ function oss_plugin_action_links($links, $file)
|
||||||
{
|
{
|
||||||
if ($file == plugin_basename(dirname(__FILE__) . '/aliyun-oss-wordpress.php')) {
|
if ($file == plugin_basename(dirname(__FILE__) . '/aliyun-oss-wordpress.php')) {
|
||||||
$links[] = '<a href="options-general.php?page=' . OSS_BASEFOLDER . '/aliyun-oss-wordpress.php">设置</a>';
|
$links[] = '<a href="options-general.php?page=' . OSS_BASEFOLDER . '/aliyun-oss-wordpress.php">设置</a>';
|
||||||
$links[] = '<a href="https://qq52o.me/sponsor.html" target="_blank">赞赏</a>';
|
$links[] = '<a href="https://donate.qq52o.me" target="_blank">Donate</a>';
|
||||||
}
|
}
|
||||||
return $links;
|
return $links;
|
||||||
}
|
}
|
||||||
add_filter('plugin_action_links', 'oss_plugin_action_links', 10, 2);
|
add_filter('plugin_action_links', 'oss_plugin_action_links', 10, 2);
|
||||||
|
|
||||||
|
function oss_custom_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
|
||||||
|
{
|
||||||
|
$option = get_option('oss_options', oss_get_default_options());
|
||||||
|
$style = !empty($option['style']) ? esc_attr($option['style']) : '';
|
||||||
|
$upload_url_path = esc_attr($option['upload_url_path']);
|
||||||
|
if (empty($style)) {
|
||||||
|
return $sources;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($sources as $index => $source) {
|
||||||
|
if (!isset($source['url']) || !oss_is_image_type($source['url'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (strpos($source['url'], $upload_url_path) !== false && strpos($source['url'], $style) === false) {
|
||||||
|
$sources[$index]['url'] .= $style;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $sources;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_filter('wp_calculate_image_srcset', 'oss_custom_image_srcset', 10, 5);
|
||||||
|
|
||||||
|
add_filter('wp_prepare_attachment_for_js', 'oss_wp_prepare_attachment_for_js', 10);
|
||||||
|
function oss_wp_prepare_attachment_for_js($response)
|
||||||
|
{
|
||||||
|
if (empty($response['filesizeInBytes']) || empty($response['filesizeHumanReadable'])) {
|
||||||
|
$upload_url_path = get_option('upload_url_path');
|
||||||
|
$upload_path = get_option('upload_path');
|
||||||
|
$object = str_replace($upload_url_path, $upload_path, $response['url']);
|
||||||
|
$meta = oss_get_file_meta($object);
|
||||||
|
if (!empty($meta['content-length'])) {
|
||||||
|
$response['filesizeInBytes'] = $meta['content-length'];
|
||||||
|
$response['filesizeHumanReadable'] = size_format($meta['content-length']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
add_filter('the_content', 'oss_setting_content_style');
|
add_filter('the_content', 'oss_setting_content_style');
|
||||||
function oss_setting_content_style($content)
|
function oss_setting_content_style($content)
|
||||||
{
|
{
|
||||||
$option = get_option('oss_options');
|
$option = get_option('oss_options', oss_get_default_options());
|
||||||
|
$upload_url_path = esc_attr($option['upload_url_path']);
|
||||||
if (!empty($option['style'])) {
|
if (!empty($option['style'])) {
|
||||||
|
$style = esc_attr($option['style']);
|
||||||
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $images);
|
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $images);
|
||||||
if (!empty($images) && isset($images[1])) {
|
if (!empty($images) && isset($images[1])) {
|
||||||
$images[1] = array_unique($images[1]);
|
$images[1] = array_unique($images[1]);
|
||||||
foreach ($images[1] as $item) {
|
foreach ($images[1] as $item) {
|
||||||
if((strpos($item, esc_attr($option['upload_url_path'])) !== false) && (strpos($item, esc_attr($option['style'])) === false)){
|
if (!oss_is_image_type($item)) {
|
||||||
$content = str_replace($item, $item . esc_attr($option['style']), $content);
|
continue;
|
||||||
|
}
|
||||||
|
if (strpos($item, $upload_url_path) !== false && strpos($item, $style) === false) {
|
||||||
|
$content = str_replace($item, $item . $style, $content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$content = str_replace($style . $style, $style, $content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $content;
|
return $content;
|
||||||
|
@ -479,21 +595,33 @@ function oss_setting_content_style($content)
|
||||||
add_filter('post_thumbnail_html', 'oss_setting_post_thumbnail_style', 10, 3);
|
add_filter('post_thumbnail_html', 'oss_setting_post_thumbnail_style', 10, 3);
|
||||||
function oss_setting_post_thumbnail_style($html, $post_id, $post_image_id)
|
function oss_setting_post_thumbnail_style($html, $post_id, $post_image_id)
|
||||||
{
|
{
|
||||||
$option = get_option('oss_options');
|
$option = get_option('oss_options', oss_get_default_options());
|
||||||
|
$upload_url_path = esc_attr($option['upload_url_path']);
|
||||||
if (!empty($option['style']) && has_post_thumbnail()) {
|
if (!empty($option['style']) && has_post_thumbnail()) {
|
||||||
|
$style = esc_attr($option['style']);
|
||||||
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $html, $images);
|
preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $html, $images);
|
||||||
if (!empty($images) && isset($images[1])) {
|
if (!empty($images) && isset($images[1])) {
|
||||||
$images[1] = array_unique($images[1]);
|
$images[1] = array_unique($images[1]);
|
||||||
foreach ($images[1] as $item) {
|
foreach ($images[1] as $item) {
|
||||||
if((strpos($item, esc_attr($option['upload_url_path'])) !== false) && (strpos($item, esc_attr($option['style'])) === false)){
|
if (!oss_is_image_type($item)) {
|
||||||
$html = str_replace($item, $item . esc_attr($option['style']), $html);
|
continue;
|
||||||
|
}
|
||||||
|
if (strpos($item, $upload_url_path) !== false && strpos($item, $style) === false) {
|
||||||
|
$html = str_replace($item, $item . $style, $html);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$html = str_replace($style . $style, $style, $html);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @link https://help.aliyun.com/zh/oss/user-guide/regions-and-endpoints
|
||||||
|
* @param string $regional
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
function oss_get_regional($regional)
|
function oss_get_regional($regional)
|
||||||
{
|
{
|
||||||
$options = [
|
$options = [
|
||||||
|
@ -537,7 +665,7 @@ function oss_get_regional($regional)
|
||||||
|
|
||||||
foreach ($options as $value => $text) {
|
foreach ($options as $value => $text) {
|
||||||
$selected = ($regional == $value) ? 'selected="selected"' : '';
|
$selected = ($regional == $value) ? 'selected="selected"' : '';
|
||||||
echo "<option value=\"{$value}\" {$selected}>{$text}</option>";
|
echo "<option value='{$value}' {$selected}>{$text}</option>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -546,6 +674,91 @@ function oss_get_option($key)
|
||||||
return esc_attr(get_option($key));
|
return esc_attr(get_option($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
|
if (!empty($oss_options['origin_protect']) && esc_attr($oss_options['origin_protect']) === 'on' && !empty(esc_attr($oss_options['style']))) {
|
||||||
|
add_filter('wp_get_attachment_url', 'oss_add_suffix_to_attachment_url', 10, 2);
|
||||||
|
add_filter('wp_get_attachment_thumb_url', 'oss_add_suffix_to_attachment_url', 10, 2);
|
||||||
|
add_filter('wp_get_original_image_url', 'oss_add_suffix_to_attachment_url', 10, 2);
|
||||||
|
add_filter('wp_prepare_attachment_for_js', 'oss_add_suffix_to_attachment', 10, 2);
|
||||||
|
add_filter('image_get_intermediate_size', 'oss_add_suffix_for_media_send_to_editor');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @param int $post_id
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function oss_add_suffix_to_attachment_url($url, $post_id)
|
||||||
|
{
|
||||||
|
if (oss_is_image_type($url)) {
|
||||||
|
$url .= oss_get_image_style();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $response
|
||||||
|
* @param array $attachment
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function oss_add_suffix_to_attachment($response, $attachment)
|
||||||
|
{
|
||||||
|
if ($response['type'] != 'image') {
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
$style = oss_get_image_style();
|
||||||
|
if (!empty($response['sizes'])) {
|
||||||
|
foreach ($response['sizes'] as $size_key => $size_file) {
|
||||||
|
if (oss_is_image_type($size_file['url'])) {
|
||||||
|
$response['sizes'][$size_key]['url'] .= $style;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($response['originalImageURL'])) {
|
||||||
|
if (oss_is_image_type($response['originalImageURL'])) {
|
||||||
|
$response['originalImageURL'] .= $style;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
function oss_add_suffix_for_media_send_to_editor($data)
|
||||||
|
{
|
||||||
|
// https://github.com/WordPress/wordpress-develop/blob/43d2455dc68072fdd43c3c800cc8c32590f23cbe/src/wp-includes/media.php#L239
|
||||||
|
if (oss_is_image_type($data['file'])) {
|
||||||
|
$data['file'] .= oss_get_image_style();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $url
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
function oss_is_image_type($url)
|
||||||
|
{
|
||||||
|
return (bool) preg_match('/\.(jpg|jpeg|jpe|png|bmp|webp|heic|heif|svg)$/i', $url);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
function oss_get_image_style()
|
||||||
|
{
|
||||||
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
|
|
||||||
|
return esc_attr($oss_options['style']);
|
||||||
|
}
|
||||||
|
|
||||||
// 在导航栏“设置”中添加条目
|
// 在导航栏“设置”中添加条目
|
||||||
function oss_add_setting_page()
|
function oss_add_setting_page()
|
||||||
{
|
{
|
||||||
|
@ -560,8 +773,15 @@ function oss_setting_page()
|
||||||
if (!current_user_can('manage_options')) {
|
if (!current_user_can('manage_options')) {
|
||||||
wp_die('Insufficient privileges!');
|
wp_die('Insufficient privileges!');
|
||||||
}
|
}
|
||||||
|
if (!empty($_POST) && !empty($_POST['type'])) {
|
||||||
|
$nonce = $_POST["{$_POST['type']}-nonce"] ?? '';
|
||||||
|
if (empty($nonce) || !wp_verify_nonce($nonce, $_POST['type'])) {
|
||||||
|
wp_die('Illegal requests!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$options = [];
|
$options = [];
|
||||||
if (!empty($_POST) and $_POST['type'] == 'oss_set') {
|
if (!empty($_POST) && $_POST['type'] == 'aliyun_oss_set') {
|
||||||
$options['bucket'] = isset($_POST['bucket']) ? sanitize_text_field($_POST['bucket']) : '';
|
$options['bucket'] = isset($_POST['bucket']) ? sanitize_text_field($_POST['bucket']) : '';
|
||||||
$options['regional'] = isset($_POST['regional']) ? sanitize_text_field($_POST['regional']) : '';
|
$options['regional'] = isset($_POST['regional']) ? sanitize_text_field($_POST['regional']) : '';
|
||||||
$options['role_name'] = isset($_POST['role_name']) ? sanitize_text_field($_POST['role_name']) : '';
|
$options['role_name'] = isset($_POST['role_name']) ? sanitize_text_field($_POST['role_name']) : '';
|
||||||
|
@ -570,10 +790,12 @@ function oss_setting_page()
|
||||||
$options['is_internal'] = isset($_POST['is_internal']) ? 'true' : 'false';
|
$options['is_internal'] = isset($_POST['is_internal']) ? 'true' : 'false';
|
||||||
$options['nothumb'] = isset($_POST['nothumb']) ? 'true' : 'false';
|
$options['nothumb'] = isset($_POST['nothumb']) ? 'true' : 'false';
|
||||||
$options['nolocalsaving'] = isset($_POST['nolocalsaving']) ? 'true' : 'false';
|
$options['nolocalsaving'] = isset($_POST['nolocalsaving']) ? 'true' : 'false';
|
||||||
//仅用于插件卸载时比较使用
|
|
||||||
$options['upload_url_path'] = isset($_POST['upload_url_path']) ? sanitize_text_field(stripslashes($_POST['upload_url_path'])) : '';
|
$options['upload_url_path'] = isset($_POST['upload_url_path']) ? sanitize_text_field(stripslashes($_POST['upload_url_path'])) : '';
|
||||||
$options['style'] = isset($_POST['style']) ? sanitize_text_field($_POST['style']) : '';
|
$options['style'] = isset($_POST['style']) ? sanitize_text_field($_POST['style']) : '';
|
||||||
$options['update_file_name'] = isset($_POST['update_file_name']) ? sanitize_text_field($_POST['update_file_name']) : 'false';
|
$options['update_file_name'] = isset($_POST['update_file_name']) ? sanitize_text_field($_POST['update_file_name']) : 'false';
|
||||||
|
$options['origin_protect'] = isset($_POST['origin_protect']) ? sanitize_text_field($_POST['origin_protect']) : 'off';
|
||||||
|
|
||||||
|
$options['origin_region'] = in_array($options['regional'], ['oss-accelerate', 'oss-accelerate-overseas']) ? '' : $options['regional'];
|
||||||
|
|
||||||
if ($options['regional'] === 'oss-rg-china-mainland' && $options['is_internal'] === 'true') {
|
if ($options['regional'] === 'oss-rg-china-mainland' && $options['is_internal'] === 'true') {
|
||||||
echo '<div class="error"><p><strong>无地域属性不支持内网,请重新填写配置!</strong></p></div>';
|
echo '<div class="error"><p><strong>无地域属性不支持内网,请重新填写配置!</strong></p></div>';
|
||||||
|
@ -581,7 +803,7 @@ function oss_setting_page()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($_POST) and $_POST['type'] == 'aliyun_oss_all') {
|
if (!empty($_POST) && $_POST['type'] == 'aliyun_oss_all') {
|
||||||
$files = oss_read_dir_queue(get_home_path(), oss_get_option('upload_path'));
|
$files = oss_read_dir_queue(get_home_path(), oss_get_option('upload_path'));
|
||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
oss_file_upload($file['key'], $file['filepath']);
|
oss_file_upload($file['key'], $file['filepath']);
|
||||||
|
@ -590,20 +812,24 @@ function oss_setting_page()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 替换数据库链接
|
// 替换数据库链接
|
||||||
if(!empty($_POST) and $_POST['type'] == 'aliyun_oss_replace') {
|
if(!empty($_POST) && $_POST['type'] == 'aliyun_oss_replace') {
|
||||||
$old_url = esc_url_raw($_POST['old_url']);
|
$old_url = esc_url_raw($_POST['old_url']);
|
||||||
$new_url = esc_url_raw($_POST['new_url']);
|
$new_url = esc_url_raw($_POST['new_url']);
|
||||||
|
|
||||||
global $wpdb;
|
if (!empty($old_url)) {
|
||||||
$posts_name = $wpdb->prefix .'posts';
|
global $wpdb;
|
||||||
// 文章内容
|
$posts_name = $wpdb->prefix . 'posts';
|
||||||
$posts_result = $wpdb->query("UPDATE $posts_name SET post_content = REPLACE(post_content, '$old_url', '$new_url')");
|
// 文章内容
|
||||||
|
$posts_result = $wpdb->query($wpdb->prepare("UPDATE $posts_name SET post_content = REPLACE(post_content, '%s', '%s')", [$old_url, $new_url]));
|
||||||
|
|
||||||
// 修改题图之类的
|
// 修改题图之类的
|
||||||
$postmeta_name = $wpdb->prefix .'postmeta';
|
$postmeta_name = $wpdb->prefix . 'postmeta';
|
||||||
$postmeta_result = $wpdb->query("UPDATE $postmeta_name SET meta_value = REPLACE(meta_value, '$old_url', '$new_url')");
|
$postmeta_result = $wpdb->query($wpdb->prepare("UPDATE $postmeta_name SET meta_value = REPLACE(meta_value, '%s', '%s')", [$old_url, $new_url]));
|
||||||
|
|
||||||
echo '<div class="updated"><p><strong>替换成功!共替换文章内链'.$posts_result.'条、题图链接'.$postmeta_result.'条!</strong></p></div>';
|
echo '<div class="updated"><p><strong>替换成功!共替换文章内链'.$posts_result.'条、题图链接'.$postmeta_result.'条!</strong></p></div>';
|
||||||
|
} else {
|
||||||
|
echo '<div class="error"><p><strong>请填写资源链接URL地址!</strong></p></div>';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 若$options不为空数组,则更新数据
|
// 若$options不为空数组,则更新数据
|
||||||
|
@ -619,34 +845,31 @@ function oss_setting_page()
|
||||||
echo '<div class="updated"><p><strong>设置已保存!</strong></p></div>';
|
echo '<div class="updated"><p><strong>设置已保存!</strong></p></div>';
|
||||||
}
|
}
|
||||||
|
|
||||||
$oss_options = get_option('oss_options', true);
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
||||||
|
|
||||||
$oss_regional = esc_attr($oss_options['regional']);
|
$oss_regional = esc_attr($oss_options['regional']);
|
||||||
|
|
||||||
$oss_is_internal = esc_attr($oss_options['is_internal']);
|
$oss_is_internal = esc_attr($oss_options['is_internal']) == 'true';
|
||||||
$oss_is_internal = $oss_is_internal == 'true';
|
$oss_nothumb = esc_attr($oss_options['nothumb']) == 'true';
|
||||||
|
$oss_nolocalsaving = esc_attr($oss_options['nolocalsaving']) == 'true';
|
||||||
|
|
||||||
$oss_nothumb = esc_attr($oss_options['nothumb']);
|
|
||||||
$oss_nothumb = $oss_nothumb == 'true';
|
|
||||||
|
|
||||||
$oss_nolocalsaving = esc_attr($oss_options['nolocalsaving']);
|
|
||||||
$oss_nolocalsaving = $oss_nolocalsaving == 'true';
|
|
||||||
$oss_update_file_name = esc_attr($oss_options['update_file_name']);
|
$oss_update_file_name = esc_attr($oss_options['update_file_name']);
|
||||||
|
$oss_origin_protect = esc_attr($oss_options['origin_protect'] ?? 'off') !== 'off' ? 'checked="checked"' : '';
|
||||||
|
|
||||||
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
|
||||||
?>
|
?>
|
||||||
<div class="wrap" style="margin: 10px;">
|
<div class="wrap" style="margin: 10px;">
|
||||||
<h1>阿里云 OSS <span style="font-size: 13px;">当前版本:<?php echo OSS_VERSION; ?></span></h1>
|
<h1>阿里云 OSS <span style="font-size: 13px;">当前版本:<?php echo OSS_VERSION; ?></span></h1>
|
||||||
<p>如果觉得此插件对你有所帮助,不妨到 <a href="https://github.com/sy-records/aliyun-oss-wordpress" target="_blank">GitHub</a> 上点个<code>Star</code>,<code>Watch</code>关注更新;<a href="https://go.qq52o.me/qm/ccs" target="_blank">欢迎加入云存储插件交流群,QQ群号:887595381</a>;</p>
|
<p>如果觉得此插件对你有所帮助,不妨到 <a href="https://github.com/sy-records/aliyun-oss-wordpress" target="_blank">GitHub</a> 上点个<code>Star</code>,<code>Watch</code>关注更新;<a href="https://go.qq52o.me/qm/ccs" target="_blank">欢迎加入云存储插件交流群,QQ群号:887595381</a>;</p>
|
||||||
<hr/>
|
<hr/>
|
||||||
<form name="form1" method="post">
|
<form method="post">
|
||||||
<table class="form-table">
|
<table class="form-table">
|
||||||
<tr>
|
<tr>
|
||||||
<th>
|
<th>
|
||||||
<legend>Bucket名称</legend>
|
<legend>Bucket名称</legend>
|
||||||
</th>
|
</th>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" name="bucket" value="<?php echo esc_attr($oss_options['bucket']); ?>" size="50" placeholder="请填写Bucket名称"/>
|
<input type="text" name="bucket" required value="<?php echo esc_attr($oss_options['bucket']); ?>" size="50" placeholder="请填写Bucket名称"/>
|
||||||
|
|
||||||
<p>请先访问 <a href="https://oss.console.aliyun.com/bucket" target="_blank">阿里云控制台</a> 创建<code>Bucket</code>,再填写以上内容。</p>
|
<p>请先访问 <a href="https://oss.console.aliyun.com/bucket" target="_blank">阿里云控制台</a> 创建<code>Bucket</code>,再填写以上内容。</p>
|
||||||
</td>
|
</td>
|
||||||
|
@ -675,14 +898,14 @@ function oss_setting_page()
|
||||||
<th>
|
<th>
|
||||||
<legend>AccessKeyId</legend>
|
<legend>AccessKeyId</legend>
|
||||||
</th>
|
</th>
|
||||||
<td><input type="text" name="accessKeyId" value="<?php echo esc_attr($oss_options['accessKeyId']); ?>" size="50" placeholder="AccessKeyId"/></td>
|
<td><input type="text" name="accessKeyId" required value="<?php echo esc_attr($oss_options['accessKeyId']); ?>" size="50" placeholder="AccessKeyId"/></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>
|
<th>
|
||||||
<legend>AccessKeySecret</legend>
|
<legend>AccessKeySecret</legend>
|
||||||
</th>
|
</th>
|
||||||
<td>
|
<td>
|
||||||
<input type="password" name="accessKeySecret" value="<?php echo esc_attr($oss_options['accessKeySecret']); ?>" size="50" placeholder="AccessKeySecret"/>
|
<input type="password" name="accessKeySecret" required value="<?php echo esc_attr($oss_options['accessKeySecret']); ?>" size="50" placeholder="AccessKeySecret"/>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -708,8 +931,7 @@ function oss_setting_page()
|
||||||
<legend>不在本地保留备份</legend>
|
<legend>不在本地保留备份</legend>
|
||||||
</th>
|
</th>
|
||||||
<td>
|
<td>
|
||||||
<input type="checkbox"
|
<input type="checkbox" name="nolocalsaving" <?php echo $oss_nolocalsaving ? 'checked="checked"' : ''; ?> />
|
||||||
name="nolocalsaving" <?php echo $oss_nolocalsaving ? 'checked="checked"' : ''; ?> />
|
|
||||||
<p>建议不勾选</p>
|
<p>建议不勾选</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -730,7 +952,7 @@ function oss_setting_page()
|
||||||
<legend>本地文件夹</legend>
|
<legend>本地文件夹</legend>
|
||||||
</th>
|
</th>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" name="upload_path" value="<?php echo oss_get_option('upload_path'); ?>" size="50" placeholder="请输入上传文件夹"/>
|
<input type="text" name="upload_path" required value="<?php echo oss_get_option('upload_path'); ?>" size="50" placeholder="请输入上传文件夹"/>
|
||||||
<p>附件在服务器上的存储位置,例如: <code>wp-content/uploads</code> (注意不要以“/”开头和结尾),根目录请输入<code>.</code>。</p>
|
<p>附件在服务器上的存储位置,例如: <code>wp-content/uploads</code> (注意不要以“/”开头和结尾),根目录请输入<code>.</code>。</p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -739,7 +961,7 @@ function oss_setting_page()
|
||||||
<legend>URL前缀</legend>
|
<legend>URL前缀</legend>
|
||||||
</th>
|
</th>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" name="upload_url_path" value="<?php echo oss_get_option('upload_url_path'); ?>" size="50" placeholder="请输入URL前缀"/>
|
<input type="text" name="upload_url_path" required value="<?php echo oss_get_option('upload_url_path'); ?>" size="50" placeholder="请输入URL前缀"/>
|
||||||
|
|
||||||
<p><b>注意:</b></p>
|
<p><b>注意:</b></p>
|
||||||
|
|
||||||
|
@ -770,12 +992,25 @@ function oss_setting_page()
|
||||||
<p>则填写为 <code>!stylename</code></p>
|
<p>则填写为 <code>!stylename</code></p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th>
|
||||||
|
<legend>原图保护</legend>
|
||||||
|
</th>
|
||||||
|
<td>
|
||||||
|
<input type="checkbox" name="origin_protect" <?php echo $oss_origin_protect; ?> />
|
||||||
|
|
||||||
|
<p>开启原图保护功能后,存储桶中的图片文件仅能以带样式的 URL 进行访问,能够阻止恶意用户对源文件的请求。</p>
|
||||||
|
<p>使用时请先访问阿里云对象存储控制台<b>开启原图保护</b>并设置<b>图片处理样式</b>!</p>
|
||||||
|
<p>注:此功能为实验性功能,如遇错误或不可用,请关闭后联系作者反馈。</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th><legend>保存/更新选项</legend></th>
|
<th><legend>保存/更新选项</legend></th>
|
||||||
<td><input type="submit" name="submit" class="button button-primary" value="保存更改"/></td>
|
<td><input type="submit" class="button button-primary" value="保存更改"/></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
<input type="hidden" name="type" value="oss_set">
|
<input type="hidden" name="type" value="aliyun_oss_set">
|
||||||
|
<?php wp_nonce_field('aliyun_oss_set', 'aliyun_oss_set-nonce'); ?>
|
||||||
</form>
|
</form>
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<table class="form-table">
|
<table class="form-table">
|
||||||
|
@ -784,9 +1019,10 @@ function oss_setting_page()
|
||||||
<legend>同步历史附件</legend>
|
<legend>同步历史附件</legend>
|
||||||
</th>
|
</th>
|
||||||
<input type="hidden" name="type" value="aliyun_oss_all">
|
<input type="hidden" name="type" value="aliyun_oss_all">
|
||||||
|
<?php wp_nonce_field('aliyun_oss_all', 'aliyun_oss_all-nonce'); ?>
|
||||||
<td>
|
<td>
|
||||||
<input type="submit" name="submit" class="button button-secondary" value="开始同步"/>
|
<input type="submit" class="button button-secondary" value="开始同步"/>
|
||||||
<p><b>注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。<br> 所以,建议那些几千上万附件的大神们,考虑官方的 <a target="_blank" rel="nofollow" href="https://help.aliyun.com/knowledge_detail/39628.html">同步工具</a></b></p>
|
<p><b>注意:如果是首次同步,执行时间将会非常长(根据你的历史附件数量),有可能会因为执行时间过长,导致页面显示超时或者报错。<br> 所以,建议附件数量过多的用户,考虑官方的<a target="_blank" rel="nofollow" href="https://help.aliyun.com/knowledge_detail/39628.html">同步工具</a>或下载 WP-CLI 使用插件内置的命令进行上传。</b></p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
@ -799,7 +1035,7 @@ function oss_setting_page()
|
||||||
<legend>数据库原链接替换</legend>
|
<legend>数据库原链接替换</legend>
|
||||||
</th>
|
</th>
|
||||||
<td>
|
<td>
|
||||||
<input type="text" name="old_url" size="50" placeholder="请输入要替换的旧域名"/>
|
<input type="text" name="old_url" required size="50" placeholder="请输入要替换的旧域名"/>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -815,8 +1051,9 @@ function oss_setting_page()
|
||||||
<legend></legend>
|
<legend></legend>
|
||||||
</th>
|
</th>
|
||||||
<input type="hidden" name="type" value="aliyun_oss_replace">
|
<input type="hidden" name="type" value="aliyun_oss_replace">
|
||||||
|
<?php wp_nonce_field('aliyun_oss_replace', 'aliyun_oss_replace-nonce'); ?>
|
||||||
<td>
|
<td>
|
||||||
<input type="submit" name="submit" class="button button-secondary" value="开始替换"/>
|
<input type="submit" class="button button-secondary" value="开始替换"/>
|
||||||
<p><b>注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接</b></p>
|
<p><b>注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接</b></p>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
|
@ -0,0 +1,123 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
if (!class_exists('WP_CLI')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
class OSS_CLI_Commands
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 同步文件夹到 OSS
|
||||||
|
*
|
||||||
|
* ## OPTIONS
|
||||||
|
*
|
||||||
|
* <path>
|
||||||
|
* : 要同步的文件夹
|
||||||
|
*
|
||||||
|
* ## 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
|
||||||
|
*
|
||||||
|
* <path>
|
||||||
|
* : 要同步的文件
|
||||||
|
*
|
||||||
|
* [--delete]
|
||||||
|
* : 如果设置,上传后会删除本地文件
|
||||||
|
* [--key=<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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除 OSS 中的文件
|
||||||
|
*
|
||||||
|
* ## OPTIONS
|
||||||
|
*
|
||||||
|
* <key>
|
||||||
|
* : 需要删除 OSS 中的文件 key
|
||||||
|
*
|
||||||
|
* ## EXAMPLES
|
||||||
|
*
|
||||||
|
* wp oss delete-file 2021/01/1.jpg
|
||||||
|
*
|
||||||
|
* @when after_wp_load
|
||||||
|
* @subcommand delete-file
|
||||||
|
*/
|
||||||
|
public function delete_file($args, $assoc_args)
|
||||||
|
{
|
||||||
|
[$key] = $args;
|
||||||
|
WP_CLI::line("Deleting file [{$key}] from OSS...");
|
||||||
|
|
||||||
|
oss_delete_oss_file($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WP_CLI::add_command('oss', 'OSS_CLI_Commands', ['shortdesc' => 'Commands used to operate OSS.']);
|
240
readme.txt
|
@ -2,45 +2,50 @@
|
||||||
Contributors: shenyanzhi
|
Contributors: shenyanzhi
|
||||||
Donate link: https://qq52o.me/sponsor.html
|
Donate link: https://qq52o.me/sponsor.html
|
||||||
Tags: oss, 阿里云, 对象存储, aliyun
|
Tags: oss, 阿里云, 对象存储, aliyun
|
||||||
Requires at least: 4.2
|
Requires at least: 4.6
|
||||||
Tested up to: 6.3
|
Tested up to: 6.8
|
||||||
Requires PHP: 7.0.0
|
Requires PHP: 7.1
|
||||||
Stable tag: 1.4.3
|
Stable tag: 1.5.1
|
||||||
License: Apache 2.0
|
License: Apache2.0
|
||||||
License URI: http://www.apache.org/licenses/LICENSE-2.0.html
|
License URI: http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
|
||||||
使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.)
|
使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.)
|
||||||
|
|
||||||
== Description ==
|
== Description ==
|
||||||
|
|
||||||
使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.)
|
使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.)
|
||||||
|
|
||||||
* 依赖阿里云OSS服务:https://www.aliyun.com/product/oss
|
- 依赖阿里云 OSS 服务:https://www.aliyun.com/product/oss
|
||||||
|
|
||||||
## 插件特点
|
## 插件特点
|
||||||
|
|
||||||
1. 可配置是否上传缩略图和是否保留本地备份
|
1. 可配置是否上传缩略图和是否保留本地备份
|
||||||
2. 本地删除可同步删除阿里云对象存储OSS中的文件
|
2. 本地删除可同步删除阿里云对象存储 OSS 中的文件
|
||||||
3. 支持阿里云对象存储OSS绑定的用户域名
|
3. 支持阿里云对象存储 OSS 绑定的用户域名
|
||||||
4. 支持替换数据库中旧的资源链接地址
|
4. 支持替换数据库中旧的资源链接地址
|
||||||
5. 支持阿里云对象存储OSS完整地域使用
|
5. 支持阿里云对象存储 OSS 完整地域使用
|
||||||
6. 支持同步历史附件到阿里云对象存储OSS
|
6. 支持同步历史附件到阿里云对象存储 OSS
|
||||||
7. 支持阿里云OSS图片处理
|
7. 支持阿里云 OSS 图片处理
|
||||||
8. 支持上传文件自动重命名
|
8. 支持上传文件自动重命名
|
||||||
9. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress)
|
9. 支持使用 ECS 的 RAM 操作
|
||||||
|
10. 支持原图保护
|
||||||
|
11. 支持 `wp-cli` 命令上传/删除文件
|
||||||
|
12. 支持多站点
|
||||||
|
13. 支持图片裁剪编辑等操作后的上传
|
||||||
|
14. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress)
|
||||||
|
|
||||||
## 其他插件
|
## 其他插件
|
||||||
|
|
||||||
腾讯云COS:[GitHub](https://github.com/sy-records/wordpress-qcloud-cos),[WordPress Plugins](https://wordpress.org/plugins/sync-qcloud-cos)
|
腾讯云 COS:[GitHub](https://github.com/sy-records/wordpress-qcloud-cos),[WordPress Plugins](https://wordpress.org/plugins/sync-qcloud-cos)
|
||||||
华为云OBS:[GitHub](https://github.com/sy-records/huaweicloud-obs-wordpress),[WordPress Plugins](https://wordpress.org/plugins/obs-huaweicloud)
|
华为云 OBS:[GitHub](https://github.com/sy-records/huaweicloud-obs-wordpress),[WordPress Plugins](https://wordpress.org/plugins/obs-huaweicloud)
|
||||||
七牛云KODO:[GitHub](https://github.com/sy-records/qiniu-kodo-wordpress),[WordPress Plugins](https://wordpress.org/plugins/kodo-qiniu)
|
七牛云 KODO:[GitHub](https://github.com/sy-records/qiniu-kodo-wordpress),[WordPress Plugins](https://wordpress.org/plugins/kodo-qiniu)
|
||||||
又拍云USS:[GitHub](https://github.com/sy-records/upyun-uss-wordpress),[WordPress Plugins](https://wordpress.org/plugins/uss-upyun)
|
又拍云 USS:[GitHub](https://github.com/sy-records/upyun-uss-wordpress),[WordPress Plugins](https://wordpress.org/plugins/uss-upyun)
|
||||||
|
|
||||||
## 作者博客
|
## 作者博客
|
||||||
|
|
||||||
[沈唁志](https://qq52o.me "沈唁志")
|
[沈唁志](https://qq52o.me "沈唁志")
|
||||||
|
|
||||||
欢迎加入沈唁的WordPress云存储全家桶QQ交流群:887595381
|
欢迎加入沈唁的 WordPress 云存储全家桶 QQ 交流群:887595381
|
||||||
|
|
||||||
== Installation ==
|
== Installation ==
|
||||||
|
|
||||||
|
@ -50,8 +55,9 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
|
||||||
== Screenshots ==
|
== Screenshots ==
|
||||||
|
|
||||||
1. screenshot-1.png
|
1. 设置页面
|
||||||
2. screenshot-2.png
|
2. 数据库同步
|
||||||
|
3. 内置的 wp-cli 命令
|
||||||
|
|
||||||
== Frequently Asked Questions ==
|
== Frequently Asked Questions ==
|
||||||
|
|
||||||
|
@ -59,94 +65,200 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
|
|
||||||
这个插件已经加上了替换数据库中之前的旧资源地址链接功能,只需要填好对应的链接即可
|
这个插件已经加上了替换数据库中之前的旧资源地址链接功能,只需要填好对应的链接即可
|
||||||
|
|
||||||
= 通过文件URL访问图片无法预览而是以附件形式下载? =
|
= 通过文件 URL 访问图片无法预览而是以附件形式下载? =
|
||||||
|
|
||||||
因为Bucket是2019年9月23日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
|
因为 Bucket 是 2019 年 9 月 23 日后创建的,使用默认域名时会自动下载,需要绑定自有域名访问。具体参考[阿里云文档](https://help.aliyun.com/document_detail/142631.html)。
|
||||||
|
|
||||||
= 如果存在第三方插件或者主题自带上传功能,内容上传到本地文件夹(即默认wp-content/uploads)中,怎么上传到OSS中? =
|
= 如果存在第三方插件或者主题自带上传功能,内容上传到本地文件夹(即默认 wp-content/uploads)中,怎么上传到 OSS 中? =
|
||||||
|
|
||||||
解决方案有两种,推荐使用第二种。
|
解决方案有两种,推荐使用第二种。
|
||||||
|
|
||||||
一是修改第三方插件或者主题的上传功能,调用插件的`oss_file_upload`方法(不推荐,一般人不会修改)
|
一是修改第三方插件或者主题的上传功能,调用插件的`oss_file_upload`方法(不推荐,一般人不会修改)
|
||||||
二是使用对象存储OSS提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行GET操作时,OSS会向回源地址请求这个文件,返回给用户,同时会将该文件存入OSS。这样就达到了上传到OSS的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html)
|
二是使用对象存储 OSS 提供的回源功能,配置为镜像方式。如果配置了镜像回源,当用户对该存储空间内一个不存在的文件进行 GET 操作时,OSS 会向回源地址请求这个文件,返回给用户,同时会将该文件存入 OSS。这样就达到了上传到 OSS 的需求。具体配置参考阿里云文档[设置回源规则](https://help.aliyun.com/document_detail/31906.html)
|
||||||
|
|
||||||
== Changelog ==
|
== Changelog ==
|
||||||
|
|
||||||
|
= 1.5.1 =
|
||||||
|
|
||||||
|
- Update nonce handling for form submissions
|
||||||
|
|
||||||
|
= 1.5.0 =
|
||||||
|
|
||||||
|
- Enhance region handling for OSS configuration
|
||||||
|
|
||||||
|
= 1.4.21 =
|
||||||
|
|
||||||
|
- Optimize uninstall code
|
||||||
|
|
||||||
|
= 1.4.20 =
|
||||||
|
|
||||||
|
- Fix origin_protect default value error
|
||||||
|
|
||||||
|
= 1.4.19 =
|
||||||
|
|
||||||
|
- Fix sub-site failure to delete remote images
|
||||||
|
- Fix `get_option` default value error
|
||||||
|
- Fix missing delete backup images
|
||||||
|
|
||||||
|
= 1.4.18 =
|
||||||
|
|
||||||
|
- Images processing ignore gif format
|
||||||
|
|
||||||
|
= 1.4.17 =
|
||||||
|
|
||||||
|
- Fix endpoint failed to use https
|
||||||
|
|
||||||
|
= 1.4.16 =
|
||||||
|
|
||||||
|
- 强制 endpoint 使用 https
|
||||||
|
|
||||||
|
= 1.4.15 =
|
||||||
|
|
||||||
|
- 将阿里云V1签名升级为V4签名
|
||||||
|
|
||||||
|
= 1.4.14 =
|
||||||
|
|
||||||
|
- 支持 `wp-cli` 命令删除文件
|
||||||
|
- Use wp_get_mime_types instead of get_allowed_mime_types
|
||||||
|
- 修复 heic 格式图片上传失败问题
|
||||||
|
|
||||||
|
= 1.4.13 =
|
||||||
|
|
||||||
|
- 支持 `wp-cli` 命令上传文件
|
||||||
|
|
||||||
|
= 1.4.12 =
|
||||||
|
|
||||||
|
- 支持原图保护
|
||||||
|
|
||||||
|
= 1.4.11 =
|
||||||
|
|
||||||
|
- 优化数据库数据替换语法
|
||||||
|
|
||||||
|
= 1.4.10 =
|
||||||
|
|
||||||
|
- 修复`不在本地保留备份`时获取不到非图片文件大小
|
||||||
|
|
||||||
|
= 1.4.9 =
|
||||||
|
|
||||||
|
- 升级 SDK
|
||||||
|
- 增加 CSRF 验证
|
||||||
|
|
||||||
|
= 1.4.8 =
|
||||||
|
|
||||||
|
- 修复图片处理参数重复添加
|
||||||
|
|
||||||
|
= 1.4.7 =
|
||||||
|
|
||||||
|
- 修复 `upload_url_path` 设置为 `.` 时删除失败
|
||||||
|
- 优化图片处理参数追加
|
||||||
|
|
||||||
|
= 1.4.6 =
|
||||||
|
|
||||||
|
- 修复 pdf 等文件格式上传时报错
|
||||||
|
|
||||||
|
= 1.4.5 =
|
||||||
|
|
||||||
|
- 兼容 PHP 7.0
|
||||||
|
|
||||||
|
= 1.4.4 =
|
||||||
|
|
||||||
|
- 修复超大文件原图上传和删除
|
||||||
|
|
||||||
= 1.4.3 =
|
= 1.4.3 =
|
||||||
* 修复同步错误
|
|
||||||
* 更新地域
|
- 修复同步错误
|
||||||
|
- 更新地域
|
||||||
|
|
||||||
= 1.4.2 =
|
= 1.4.2 =
|
||||||
* 优化同步代码逻辑
|
|
||||||
* 修复 webp 和 heic 格式图片上传缩略图失败问题
|
- 优化同步代码逻辑
|
||||||
|
- 修复 webp 和 heic 格式图片上传缩略图失败问题
|
||||||
|
|
||||||
= 1.4.1 =
|
= 1.4.1 =
|
||||||
* 支持媒体库编辑图片上传
|
|
||||||
|
- 支持媒体库编辑图片上传
|
||||||
|
|
||||||
= 1.4.0 =
|
= 1.4.0 =
|
||||||
* 支持 WordPress 6.3 版本
|
|
||||||
* 支持 RAM 操作 OSS
|
- 支持 WordPress 6.3 版本
|
||||||
|
- 支持 RAM 操作 OSS
|
||||||
|
|
||||||
= 1.3.2 =
|
= 1.3.2 =
|
||||||
* 添加地域
|
|
||||||
|
- 添加地域
|
||||||
|
|
||||||
= 1.3.1 =
|
= 1.3.1 =
|
||||||
* 优化代码
|
|
||||||
|
- 优化代码
|
||||||
|
|
||||||
= 1.3.0 =
|
= 1.3.0 =
|
||||||
* 增加地域
|
|
||||||
* 优化 isset 判断
|
- 增加地域
|
||||||
* 优化访问权限
|
- 优化 isset 判断
|
||||||
* 修复存在同名 path 时截取错误
|
- 优化访问权限
|
||||||
* 修改 accessKeySecret 类型为 password
|
- 修复存在同名 path 时截取错误
|
||||||
|
- 修改 accessKeySecret 类型为 password
|
||||||
|
|
||||||
= 1.2.8 =
|
= 1.2.8 =
|
||||||
* 支持上传文件自动重命名
|
|
||||||
* 优化图片处理
|
- 支持上传文件自动重命名
|
||||||
|
- 优化图片处理
|
||||||
|
|
||||||
= 1.2.7 =
|
= 1.2.7 =
|
||||||
* 增加地域
|
|
||||||
|
- 增加地域
|
||||||
|
|
||||||
= 1.2.6 =
|
= 1.2.6 =
|
||||||
* 升级 oss sdk
|
|
||||||
* 修复删除文件的request id异常
|
- 升级 oss sdk
|
||||||
* 支持 WordPress 5.8 版本
|
- 修复删除文件的 request id 异常
|
||||||
|
- 支持 WordPress 5.8 版本
|
||||||
|
|
||||||
= 1.2.5 =
|
= 1.2.5 =
|
||||||
* 修复当文章图片重复时导致添加多个样式
|
|
||||||
|
- 修复当文章图片重复时导致添加多个样式
|
||||||
|
|
||||||
= 1.2.4 =
|
= 1.2.4 =
|
||||||
* 添加 get_home_path 方法判断
|
|
||||||
* 支持 WordPress 5.7 版本
|
- 添加 get_home_path 方法判断
|
||||||
|
- 支持 WordPress 5.7 版本
|
||||||
|
|
||||||
= 1.2.3 =
|
= 1.2.3 =
|
||||||
* 支持删除非图片类型文件
|
|
||||||
|
- 支持删除非图片类型文件
|
||||||
|
|
||||||
= 1.2.2 =
|
= 1.2.2 =
|
||||||
* 支持 WordPress 5.6 版本
|
|
||||||
* 升级 OSS SDK
|
- 支持 WordPress 5.6 版本
|
||||||
* 修复勾选不上传缩略图删除时不会删除已存在的缩略图
|
- 升级 OSS SDK
|
||||||
|
- 修复勾选不上传缩略图删除时不会删除已存在的缩略图
|
||||||
|
|
||||||
= 1.2.1 =
|
= 1.2.1 =
|
||||||
* 支持阿里云OSS图片处理
|
|
||||||
|
- 支持阿里云 OSS 图片处理
|
||||||
|
|
||||||
= 1.2.0 =
|
= 1.2.0 =
|
||||||
* 优化同步上传路径获取
|
|
||||||
* 修复多站点上传原图失败,缩略图正常问题
|
- 优化同步上传路径获取
|
||||||
* 优化上传路径获取
|
- 修复多站点上传原图失败,缩略图正常问题
|
||||||
* 增加数据库题图链接替换
|
- 优化上传路径获取
|
||||||
|
- 增加数据库题图链接替换
|
||||||
|
|
||||||
= 1.1.1 =
|
= 1.1.1 =
|
||||||
* 修复本地文件夹为根目录时路径错误
|
|
||||||
* 减少一次获取配置代码...
|
- 修复本地文件夹为根目录时路径错误
|
||||||
* 增加回源说明
|
- 减少一次获取配置代码...
|
||||||
|
- 增加回源说明
|
||||||
|
|
||||||
= 1.1.0 =
|
= 1.1.0 =
|
||||||
* 优化删除文件使用删除多个接口
|
|
||||||
* 修复勾选不在本地保存图片后媒体库显示默认图片问题
|
- 优化删除文件使用删除多个接口
|
||||||
|
- 修复勾选不在本地保存图片后媒体库显示默认图片问题
|
||||||
|
|
||||||
= 1.0.1 =
|
= 1.0.1 =
|
||||||
* 修复勾选不在本地保存图片后媒体库显示默认图片问题
|
|
||||||
|
- 修复勾选不在本地保存图片后媒体库显示默认图片问题
|
||||||
|
|
||||||
= 1.0 =
|
= 1.0 =
|
||||||
* First version
|
|
||||||
|
- First version
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"aliyuncs/oss-sdk-php": "^2.3",
|
"aliyuncs/oss-sdk-php": "^2.7",
|
||||||
"alibabacloud/credentials": "^1.1"
|
"alibabacloud/credentials": "^1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
//防止有人恶意访问此文件,所以在没有 WP_UNINSTALL_PLUGIN 常量的情况下结束程序
|
|
||||||
if (!defined('WP_UNINSTALL_PLUGIN')) {
|
if (!defined('WP_UNINSTALL_PLUGIN')) {
|
||||||
exit();
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$obs_options = get_option('oss_options', true);
|
update_option('upload_url_path', '');
|
||||||
$upload_url_path = get_option('upload_url_path');
|
|
||||||
$oss_upload_url_path = esc_attr($obs_options['upload_url_path']);
|
|
||||||
|
|
||||||
//如果现在使用的是OSS的URL,则恢复原状
|
|
||||||
if ($upload_url_path == $oss_upload_url_path) {
|
|
||||||
update_option('upload_url_path', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
//移除配置
|
|
||||||
delete_option('oss_options');
|
delete_option('oss_options');
|
||||||
|
|