|
|
|
@ -2,8 +2,8 @@
|
|
|
|
|
/*
|
|
|
|
|
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.11
|
|
|
|
|
Description: 使用阿里云对象存储 OSS 作为附件存储空间。(This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.)
|
|
|
|
|
Version: 1.5.1
|
|
|
|
|
Author: 沈唁
|
|
|
|
|
Author URI: https://qq52o.me
|
|
|
|
|
License: Apache2.0
|
|
|
|
@ -18,12 +18,17 @@ use OSS\OssClient;
|
|
|
|
|
use OSS\Credentials\CredentialsProvider;
|
|
|
|
|
use AlibabaCloud\Credentials\Credential;
|
|
|
|
|
use OSS\Credentials\StaticCredentialsProvider;
|
|
|
|
|
use OSS\Core\OssException;
|
|
|
|
|
|
|
|
|
|
define('OSS_VERSION', '1.4.11');
|
|
|
|
|
define('OSS_VERSION', '1.5.1');
|
|
|
|
|
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
|
|
|
|
@ -49,12 +54,12 @@ class OSSCredentialsWrapper implements CredentialsProvider
|
|
|
|
|
|
|
|
|
|
// 初始化选项
|
|
|
|
|
register_activation_hook(__FILE__, 'oss_set_options');
|
|
|
|
|
// 初始化选项
|
|
|
|
|
function oss_set_options()
|
|
|
|
|
function oss_get_default_options()
|
|
|
|
|
{
|
|
|
|
|
$options = [
|
|
|
|
|
return [
|
|
|
|
|
'bucket' => '',
|
|
|
|
|
'regional' => 'oss-cn-shanghai',
|
|
|
|
|
'origin_region' => '',
|
|
|
|
|
'accessKeyId' => '',
|
|
|
|
|
'accessKeySecret' => '',
|
|
|
|
|
'is_internal' => 'false',
|
|
|
|
@ -63,50 +68,79 @@ function oss_set_options()
|
|
|
|
|
'upload_url_path' => '', // URL前缀
|
|
|
|
|
'style' => '', // 图片处理
|
|
|
|
|
'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()
|
|
|
|
|
{
|
|
|
|
|
$oss_opt = get_option('oss_options', true);
|
|
|
|
|
$roleName = esc_attr($oss_opt['role_name'] ?? '');
|
|
|
|
|
$endpoint = oss_get_bucket_endpoint($oss_opt);
|
|
|
|
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
|
|
|
|
$role_name = esc_attr($oss_options['role_name'] ?? '');
|
|
|
|
|
$endpoint = oss_get_bucket_endpoint($oss_options);
|
|
|
|
|
|
|
|
|
|
if (!empty($roleName)) {
|
|
|
|
|
if (!empty($role_name)) {
|
|
|
|
|
$ecsRamRole = new Credential([
|
|
|
|
|
// 填写Credential类型,固定值为ecs_ram_role。
|
|
|
|
|
'type' => 'ecs_ram_role',
|
|
|
|
|
// 填写角色名称。
|
|
|
|
|
'role_name' => $roleName,
|
|
|
|
|
'role_name' => $role_name,
|
|
|
|
|
]);
|
|
|
|
|
$providerWrapper = new OSSCredentialsWrapper($ecsRamRole);
|
|
|
|
|
$provider = $providerWrapper->getCredentials();
|
|
|
|
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
|
|
|
|
|
$config = ['provider' => $provider, 'endpoint'=> $protocol . $endpoint];
|
|
|
|
|
return new OssClient($config);
|
|
|
|
|
} else {
|
|
|
|
|
$provider = new StaticCredentialsProvider(esc_attr($oss_options['accessKeyId']), esc_attr($oss_options['accessKeySecret']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$accessKeyId = esc_attr($oss_opt['accessKeyId']);
|
|
|
|
|
$accessKeySecret = esc_attr($oss_opt['accessKeySecret']);
|
|
|
|
|
return new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
|
|
|
|
$region = !empty($oss_options['origin_region']) ? esc_attr($oss_options['origin_region']) : esc_attr($oss_options['regional']);
|
|
|
|
|
if (in_array($region, ['oss-accelerate', 'oss-accelerate-overseas']) && empty($oss_options['origin_region'])) {
|
|
|
|
|
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']);
|
|
|
|
|
if ($oss_option['is_internal'] == 'true') {
|
|
|
|
|
return "{$oss_regional}-internal.aliyuncs.com";
|
|
|
|
|
$regional = esc_attr($oss_options['regional']);
|
|
|
|
|
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https' : 'http';
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
$oss_opt = get_option('oss_options', true);
|
|
|
|
|
return $oss_opt['bucket'];
|
|
|
|
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
|
|
|
|
return esc_attr($oss_options['bucket']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -120,9 +154,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];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -131,6 +163,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)
|
|
|
|
|
{
|
|
|
|
@ -142,13 +175,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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -159,14 +193,14 @@ function oss_file_upload($object, $file, $no_local_file = false)
|
|
|
|
|
*/
|
|
|
|
|
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';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除本地文件
|
|
|
|
|
*
|
|
|
|
|
* @param $file
|
|
|
|
|
* @param string $file
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function oss_delete_local_file($file)
|
|
|
|
@ -200,9 +234,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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -222,9 +254,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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -236,7 +266,7 @@ function oss_delete_oss_files(array $files)
|
|
|
|
|
*/
|
|
|
|
|
function oss_upload_attachments($metadata)
|
|
|
|
|
{
|
|
|
|
|
$mime_types = get_allowed_mime_types();
|
|
|
|
|
$mime_types = wp_get_mime_types();
|
|
|
|
|
$image_mime_types = [
|
|
|
|
|
$mime_types['jpg|jpeg|jpe'],
|
|
|
|
|
$mime_types['gif'],
|
|
|
|
@ -245,7 +275,6 @@ function oss_upload_attachments($metadata)
|
|
|
|
|
$mime_types['tiff|tif'],
|
|
|
|
|
$mime_types['webp'],
|
|
|
|
|
$mime_types['ico'],
|
|
|
|
|
$mime_types['heic'],
|
|
|
|
|
];
|
|
|
|
|
// 例如mp4等格式 上传后根据配置选择是否删除 删除后媒体库会显示默认图片 点开内容是正常的
|
|
|
|
|
// 图片在缩略图处理
|
|
|
|
@ -283,7 +312,7 @@ function oss_upload_thumbs($metadata)
|
|
|
|
|
$upload_path = oss_get_option('upload_path');
|
|
|
|
|
|
|
|
|
|
//获取oss插件的配置信息
|
|
|
|
|
$oss_options = get_option('oss_options', true);
|
|
|
|
|
$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';
|
|
|
|
|
|
|
|
|
@ -366,12 +395,11 @@ if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
|
|
|
|
|
*/
|
|
|
|
|
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 = wp_get_attachment_metadata($post_id);
|
|
|
|
|
$upload_path = oss_get_option('upload_path');
|
|
|
|
|
if ($upload_path == '') {
|
|
|
|
|
$upload_path = 'wp-content/uploads';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!empty($meta['file'])) {
|
|
|
|
|
$deleteObjects = [];
|
|
|
|
@ -394,6 +422,13 @@ function oss_delete_remote_attachment($post_id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
oss_delete_oss_files($deleteObjects);
|
|
|
|
|
} else {
|
|
|
|
|
// 获取链接删除
|
|
|
|
@ -405,7 +440,7 @@ function oss_delete_remote_attachment($post_id)
|
|
|
|
|
oss_delete_oss_file($upload_path . end($file_info));
|
|
|
|
|
}
|
|
|
|
|
} 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']);
|
|
|
|
|
$file_info = explode($oss_upload_url, $link);
|
|
|
|
|
if (count($file_info) >= 2) {
|
|
|
|
@ -418,19 +453,19 @@ function oss_delete_remote_attachment($post_id)
|
|
|
|
|
add_action('delete_attachment', 'oss_delete_remote_attachment');
|
|
|
|
|
|
|
|
|
|
// 当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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
$oss_options = get_option('oss_options');
|
|
|
|
|
$oss_options = get_option('oss_options', oss_get_default_options());
|
|
|
|
|
switch ($oss_options['update_file_name']) {
|
|
|
|
|
case 'md5':
|
|
|
|
|
return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
|
|
|
|
@ -456,7 +491,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;
|
|
|
|
@ -486,7 +521,7 @@ function oss_plugin_action_links($links, $file)
|
|
|
|
|
{
|
|
|
|
|
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="https://qq52o.me/sponsor.html" target="_blank">赞赏</a>';
|
|
|
|
|
$links[] = '<a href="https://donate.qq52o.me" target="_blank">Donate</a>';
|
|
|
|
|
}
|
|
|
|
|
return $links;
|
|
|
|
|
}
|
|
|
|
@ -494,7 +529,7 @@ 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');
|
|
|
|
|
$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)) {
|
|
|
|
@ -502,6 +537,9 @@ function oss_custom_image_srcset($sources, $size_array, $image_src, $image_meta,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
@ -532,7 +570,7 @@ function oss_wp_prepare_attachment_for_js($response)
|
|
|
|
|
add_filter('the_content', 'oss_setting_content_style');
|
|
|
|
|
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'])) {
|
|
|
|
|
$style = esc_attr($option['style']);
|
|
|
|
@ -540,6 +578,9 @@ function oss_setting_content_style($content)
|
|
|
|
|
if (!empty($images) && isset($images[1])) {
|
|
|
|
|
$images[1] = array_unique($images[1]);
|
|
|
|
|
foreach ($images[1] as $item) {
|
|
|
|
|
if (!oss_is_image_type($item)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strpos($item, $upload_url_path) !== false && strpos($item, $style) === false) {
|
|
|
|
|
$content = str_replace($item, $item . $style, $content);
|
|
|
|
|
}
|
|
|
|
@ -554,7 +595,7 @@ function oss_setting_content_style($content)
|
|
|
|
|
add_filter('post_thumbnail_html', 'oss_setting_post_thumbnail_style', 10, 3);
|
|
|
|
|
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()) {
|
|
|
|
|
$style = esc_attr($option['style']);
|
|
|
|
@ -562,6 +603,9 @@ function oss_setting_post_thumbnail_style($html, $post_id, $post_image_id)
|
|
|
|
|
if (!empty($images) && isset($images[1])) {
|
|
|
|
|
$images[1] = array_unique($images[1]);
|
|
|
|
|
foreach ($images[1] as $item) {
|
|
|
|
|
if (!oss_is_image_type($item)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (strpos($item, $upload_url_path) !== false && strpos($item, $style) === false) {
|
|
|
|
|
$html = str_replace($item, $item . $style, $html);
|
|
|
|
|
}
|
|
|
|
@ -573,6 +617,11 @@ function oss_setting_post_thumbnail_style($html, $post_id, $post_image_id)
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @link https://help.aliyun.com/zh/oss/user-guide/regions-and-endpoints
|
|
|
|
|
* @param string $regional
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
function oss_get_regional($regional)
|
|
|
|
|
{
|
|
|
|
|
$options = [
|
|
|
|
@ -616,7 +665,7 @@ function oss_get_regional($regional)
|
|
|
|
|
|
|
|
|
|
foreach ($options as $value => $text) {
|
|
|
|
|
$selected = ($regional == $value) ? 'selected="selected"' : '';
|
|
|
|
|
echo "<option value=\"{$value}\" {$selected}>{$text}</option>";
|
|
|
|
|
echo "<option value='{$value}' {$selected}>{$text}</option>";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -625,6 +674,91 @@ function oss_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()
|
|
|
|
|
{
|
|
|
|
@ -639,13 +773,15 @@ function oss_setting_page()
|
|
|
|
|
if (!current_user_can('manage_options')) {
|
|
|
|
|
wp_die('Insufficient privileges!');
|
|
|
|
|
}
|
|
|
|
|
$options = [];
|
|
|
|
|
if (!empty($_POST) and $_POST['type'] == 'oss_set') {
|
|
|
|
|
$nonce = $_POST['update_oss_config-nonce'] ?? '';
|
|
|
|
|
if (empty($nonce) || !wp_verify_nonce($nonce, 'update_oss_config')) {
|
|
|
|
|
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 = [];
|
|
|
|
|
if (!empty($_POST) && $_POST['type'] == 'aliyun_oss_set') {
|
|
|
|
|
$options['bucket'] = isset($_POST['bucket']) ? sanitize_text_field($_POST['bucket']) : '';
|
|
|
|
|
$options['regional'] = isset($_POST['regional']) ? sanitize_text_field($_POST['regional']) : '';
|
|
|
|
|
$options['role_name'] = isset($_POST['role_name']) ? sanitize_text_field($_POST['role_name']) : '';
|
|
|
|
@ -654,10 +790,12 @@ function oss_setting_page()
|
|
|
|
|
$options['is_internal'] = isset($_POST['is_internal']) ? 'true' : 'false';
|
|
|
|
|
$options['nothumb'] = isset($_POST['nothumb']) ? '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['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['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') {
|
|
|
|
|
echo '<div class="error"><p><strong>无地域属性不支持内网,请重新填写配置!</strong></p></div>';
|
|
|
|
@ -665,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'));
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
oss_file_upload($file['key'], $file['filepath']);
|
|
|
|
@ -674,16 +812,11 @@ function oss_setting_page()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 替换数据库链接
|
|
|
|
|
if(!empty($_POST) and $_POST['type'] == 'aliyun_oss_replace') {
|
|
|
|
|
$nonce = $_POST['update_oss_replace-nonce'] ?? '';
|
|
|
|
|
if (empty($nonce) || !wp_verify_nonce($nonce, 'update_oss_replace')) {
|
|
|
|
|
wp_die('Illegal requests!');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!empty($_POST) && $_POST['type'] == 'aliyun_oss_replace') {
|
|
|
|
|
$old_url = esc_url_raw($_POST['old_url']);
|
|
|
|
|
$new_url = esc_url_raw($_POST['new_url']);
|
|
|
|
|
|
|
|
|
|
if (!empty($old_url) && !empty($new_url)) {
|
|
|
|
|
if (!empty($old_url)) {
|
|
|
|
|
global $wpdb;
|
|
|
|
|
$posts_name = $wpdb->prefix . 'posts';
|
|
|
|
|
// 文章内容
|
|
|
|
@ -712,19 +845,16 @@ function oss_setting_page()
|
|
|
|
|
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_is_internal = esc_attr($oss_options['is_internal']);
|
|
|
|
|
$oss_is_internal = $oss_is_internal == 'true';
|
|
|
|
|
$oss_is_internal = esc_attr($oss_options['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_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://';
|
|
|
|
|
?>
|
|
|
|
@ -739,7 +869,7 @@ function oss_setting_page()
|
|
|
|
|
<legend>Bucket名称</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<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>
|
|
|
|
|
</td>
|
|
|
|
@ -768,14 +898,14 @@ function oss_setting_page()
|
|
|
|
|
<th>
|
|
|
|
|
<legend>AccessKeyId</legend>
|
|
|
|
|
</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>
|
|
|
|
|
<th>
|
|
|
|
|
<legend>AccessKeySecret</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<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>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
@ -801,8 +931,7 @@ function oss_setting_page()
|
|
|
|
|
<legend>不在本地保留备份</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<td>
|
|
|
|
|
<input type="checkbox"
|
|
|
|
|
name="nolocalsaving" <?php echo $oss_nolocalsaving ? 'checked="checked"' : ''; ?> />
|
|
|
|
|
<input type="checkbox" name="nolocalsaving" <?php echo $oss_nolocalsaving ? 'checked="checked"' : ''; ?> />
|
|
|
|
|
<p>建议不勾选</p>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
@ -823,7 +952,7 @@ function oss_setting_page()
|
|
|
|
|
<legend>本地文件夹</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<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>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
@ -832,7 +961,7 @@ function oss_setting_page()
|
|
|
|
|
<legend>URL前缀</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<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>
|
|
|
|
|
|
|
|
|
@ -863,13 +992,25 @@ function oss_setting_page()
|
|
|
|
|
<p>则填写为 <code>!stylename</code></p>
|
|
|
|
|
</td>
|
|
|
|
|
</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>
|
|
|
|
|
<th><legend>保存/更新选项</legend></th>
|
|
|
|
|
<td><input type="submit" class="button button-primary" value="保存更改"/></td>
|
|
|
|
|
<?php wp_nonce_field('update_oss_config', 'update_oss_config-nonce'); ?>
|
|
|
|
|
</tr>
|
|
|
|
|
</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 method="post">
|
|
|
|
|
<table class="form-table">
|
|
|
|
@ -878,9 +1019,10 @@ function oss_setting_page()
|
|
|
|
|
<legend>同步历史附件</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<input type="hidden" name="type" value="aliyun_oss_all">
|
|
|
|
|
<?php wp_nonce_field('aliyun_oss_all', 'aliyun_oss_all-nonce'); ?>
|
|
|
|
|
<td>
|
|
|
|
|
<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>
|
|
|
|
|
</tr>
|
|
|
|
|
</table>
|
|
|
|
@ -893,7 +1035,7 @@ function oss_setting_page()
|
|
|
|
|
<legend>数据库原链接替换</legend>
|
|
|
|
|
</th>
|
|
|
|
|
<td>
|
|
|
|
|
<input type="text" name="old_url" size="50" placeholder="请输入要替换的旧域名"/>
|
|
|
|
|
<input type="text" name="old_url" required size="50" placeholder="请输入要替换的旧域名"/>
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
<tr>
|
|
|
|
@ -909,7 +1051,7 @@ function oss_setting_page()
|
|
|
|
|
<legend></legend>
|
|
|
|
|
</th>
|
|
|
|
|
<input type="hidden" name="type" value="aliyun_oss_replace">
|
|
|
|
|
<?php wp_nonce_field('update_oss_replace', 'update_oss_replace-nonce'); ?>
|
|
|
|
|
<?php wp_nonce_field('aliyun_oss_replace', 'aliyun_oss_replace-nonce'); ?>
|
|
|
|
|
<td>
|
|
|
|
|
<input type="submit" class="button button-secondary" value="开始替换"/>
|
|
|
|
|
<p><b>注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接</b></p>
|
|
|
|
|