├── .distignore ├── .github └── workflows │ ├── asset-update.yaml │ └── deploy.yml ├── .wordpress-org ├── banner-772x250.png ├── blueprints │ └── blueprint.json ├── icon-128x128.png ├── screenshot-1.png └── screenshot-2.png ├── LICENSE ├── README.md ├── readme.txt ├── sdk └── composer.json ├── uninstall.php └── upyun-uss-wordpress.php /.distignore: -------------------------------------------------------------------------------- 1 | *.github 2 | *.gitignore 3 | *.gitattributes 4 | *.yml 5 | *examples 6 | *tests 7 | /.git 8 | /.distignore 9 | /sdk/composer.lock 10 | /.wordpress-org 11 | -------------------------------------------------------------------------------- /.github/workflows/asset-update.yaml: -------------------------------------------------------------------------------- 1 | name: Plugin asset/readme update 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - master 7 | jobs: 8 | update: 9 | uses: sy-records/.github/.github/workflows/wordpress-plugin-asset-update.yaml@main 10 | with: 11 | skip_assets: false 12 | secrets: inherit 13 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy to WordPress.org 2 | on: 3 | pull_request: 4 | release: 5 | types: [ published ] 6 | jobs: 7 | deploy: 8 | uses: sy-records/.github/.github/workflows/wordpress-plugin-deploy.yaml@main 9 | with: 10 | build: true 11 | secrets: inherit 12 | -------------------------------------------------------------------------------- /.wordpress-org/banner-772x250.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/upyun-uss-wordpress/8de9637ff35b5067adba59aa09f35cd861bada4b/.wordpress-org/banner-772x250.png -------------------------------------------------------------------------------- /.wordpress-org/blueprints/blueprint.json: -------------------------------------------------------------------------------- 1 | { 2 | "landingPage": "/wp-admin/admin.php?page=uss-upyun%2Fupyun-uss-wordpress.php", 3 | "preferredVersions": { 4 | "php": "latest", 5 | "wp": "latest" 6 | }, 7 | "phpExtensionBundles": [ 8 | "kitchen-sink" 9 | ], 10 | "steps": [ 11 | { 12 | "step": "login", 13 | "username": "admin", 14 | "password": "password" 15 | }, 16 | { 17 | "step": "installPlugin", 18 | "pluginZipFile": { 19 | "resource": "wordpress.org/plugins", 20 | "slug": "uss-upyun" 21 | }, 22 | "options": { 23 | "activate": true 24 | } 25 | } 26 | ] 27 | } -------------------------------------------------------------------------------- /.wordpress-org/icon-128x128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/upyun-uss-wordpress/8de9637ff35b5067adba59aa09f35cd861bada4b/.wordpress-org/icon-128x128.png -------------------------------------------------------------------------------- /.wordpress-org/screenshot-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/upyun-uss-wordpress/8de9637ff35b5067adba59aa09f35cd861bada4b/.wordpress-org/screenshot-1.png -------------------------------------------------------------------------------- /.wordpress-org/screenshot-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sy-records/upyun-uss-wordpress/8de9637ff35b5067adba59aa09f35cd861bada4b/.wordpress-org/screenshot-2.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # USS Upyun 2 | 3 | 使用又拍云云存储 USS 作为附件存储空间的 WordPress 插件 4 | 5 | [](https://github.com/sy-records/upyun-uss-wordpress/releases) 6 | [](LICENSE) 7 | [](https://qq52o.me) 8 | [](https://github.com/sy-records/upyun-uss-wordpress/graphs/contributors) 9 | [](https://wordpress.org/plugins/uss-upyun/) 10 | [](https://wordpress.org/plugins/uss-upyun/) 11 | [](https://github.com/sy-records/upyun-uss-wordpress/actions/workflows/deploy.yml) 12 | 13 | > 使用 WordPress Playground 在线预览 [USS Upyun](https://wordpress.org/plugins/uss-upyun/?preview=1) 插件 14 | 15 | ## 插件特色 16 | 17 | * [x] 可配置是否上传缩略图和是否保留本地备份 18 | * [x] 本地删除可同步删除又拍云云存储USS中的文件 19 | * [x] 支持又拍云云存储USS绑定的个性域名 20 | * [x] 支持替换数据库中旧的资源链接地址 21 | * [x] 支持又拍云云存储USS完整地域使用 22 | * [x] 支持同步历史附件到又拍云云存储USS 23 | * [x] 支持上传时自动重命名文件(MD5或时间戳+随机数两种方式) 24 | * [x] 支持设置图片处理 25 | * [x] 支持多站点 26 | 27 | ## 安装 28 | 29 | ### 后台安装(推荐使用) 30 | 31 | WordPress 后台安装插件页面搜索`USS Upyun`,点击安装 32 | 33 | ### 下载源码 34 | 35 | 从 GitHub 下载源码,通过 WordPress 后台上传安装,或者直接将源码上传到 WordPress 插件目录`wp-content/plugins`,然后在后台启用 36 | 37 | GitHub 下载节点:[https://github.com/sy-records/upyun-uss-wordpress/releases/latest](https://github.com/sy-records/upyun-uss-wordpress/releases/latest) 38 | 39 | ## 修改配置 40 | 41 | * 方法一:在 WordPress 插件管理页面有设置按钮,进行设置 42 | * 方法二:在 WordPress 后台管理左侧导航栏`设置`下`又拍云USS设置`,点击进入设置页面 43 | 44 | ## 插件截图 45 | 46 |  47 | 48 |  49 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | === USS Upyun === 2 | Contributors: shenyanzhi 3 | Donate link: https://qq52o.me/sponsor.html 4 | Tags: USS, 又拍云, 对象存储, upyun, 云存储 5 | Requires at least: 4.6 6 | Tested up to: 6.8 7 | Requires PHP: 7.0.0 8 | Stable tag: 1.5.0 9 | License: Apache 2.0 10 | License URI: http://www.apache.org/licenses/LICENSE-2.0.html 11 | 12 | 使用又拍云云存储USS作为附件存储空间。(This is a plugin that uses UPYUN Storage Service for attachments remote saving.) 13 | 14 | == Description == 15 | 16 | 使用又拍云云存储USS作为附件存储空间。(This is a plugin that uses UPYUN Storage Service for attachments remote saving.) 17 | 18 | * 依赖又拍云云存储USS服务:https://www.upyun.com/products/file-storage 19 | 20 | ## 插件特点 21 | 22 | 1. 可配置是否上传缩略图和是否保留本地备份 23 | 2. 本地删除可同步删除又拍云云存储USS中的文件 24 | 3. 支持又拍云云存储USS绑定的个性域名 25 | 4. 支持替换数据库中旧的资源链接地址 26 | 5. 支持又拍云云存储USS完整地域使用 27 | 6. 支持同步历史附件到又拍云云存储USS 28 | 7. 支持上传时自动重命名文件(MD5或时间戳+随机数两种方式) 29 | 8. 支持设置图片处理 30 | 9. 支持多站点 31 | 32 | 插件更多详细介绍和安装:[https://github.com/sy-records/upyun-uss-wordpress](https://github.com/sy-records/upyun-uss-wordpress) 33 | 34 | ## 其他插件 35 | 36 | 腾讯云COS:[GitHub](https://github.com/sy-records/wordpress-qcloud-cos),[WordPress Plugins](https://wordpress.org/plugins/sync-qcloud-cos) 37 | 华为云OBS:[GitHub](https://github.com/sy-records/huaweicloud-obs-wordpress),[WordPress Plugins](https://wordpress.org/plugins/obs-huaweicloud) 38 | 七牛云KODO:[GitHub](https://github.com/sy-records/qiniu-kodo-wordpress),[WordPress Plugins](https://wordpress.org/plugins/kodo-qiniu) 39 | 阿里云OSS:[GitHub](https://github.com/sy-records/aliyun-oss-wordpress),[WordPress Plugins](https://wordpress.org/plugins/oss-aliyun) 40 | 41 | ## 作者博客 42 | 43 | [沈唁志](https://qq52o.me "沈唁志") 44 | 45 | QQ交流群:887595381 46 | 47 | == Installation == 48 | 49 | 1. Upload the folder `upyun-uss-wordpress` or `uss-upyun` to the `/wp-content/plugins/` directory 50 | 2. Activate the plugin through the 'Plugins' menu in WordPress 51 | 3. That's all 52 | 53 | == Screenshots == 54 | 55 | 1. 设置页面 56 | 2. 更新数据库链接 57 | 58 | == Frequently Asked Questions == 59 | 60 | = 怎么替换文章中之前的旧资源地址链接 = 61 | 62 | 这个插件已经加上了替换数据库中之前的旧资源地址链接功能,只需要填好对应的链接即可 63 | 64 | == Changelog == 65 | 66 | = 1.5.0 = 67 | * 优化卸载逻辑 68 | * 修复更新数据库链接错误 69 | * 优化部分代码 70 | 71 | = 1.4.0 = 72 | * 优化代码 73 | * 支持 webp、heic 格式图片上传 74 | * 优化图片处理 75 | 76 | = 1.3.0 = 77 | * 支持 WordPress 6.3 版本 78 | 79 | = 1.2.5 = 80 | * 修复替换url错误 81 | 82 | = 1.2.3 = 83 | * 支持WordPress 5.7 84 | * 添加 get_home_path 方法判断 85 | * 优化 delete_remote_attachment 86 | 87 | = 1.2.2 = 88 | * 支持WordPress 5.6 89 | * 优化缩略图删除逻辑 90 | 91 | = 1.2.1 = 92 | * 升级sdk,可能会有问题,有问题及时反馈。 93 | 94 | = 1.2.0= 95 | * 优化同步文件逻辑 96 | * 修复多站点上传原图失败,缩略图正常问题 97 | * 升级sdk至3.5.0版本 升不了 https://github.com/sy-records/upyun-uss-wordpress/commit/b899c35df6796fe282bf8c840a5e1244a6cffe30 98 | 99 | = 1.1.3 = 100 | * 修复删除本地文件失败 101 | 102 | = 1.1.2 = 103 | * 增加图片处理功能 104 | * 替换数据库链接增加题图 105 | 106 | = 1.1.1 = 107 | * 修复设置页面版本号问题 108 | * 增加上传时自动重命名处理,支持MD5或时间戳+随机数两种方式 109 | 110 | = 1.1.0 = 111 | * 修复勾选不在本地保存图片后媒体库显示默认图片问题 112 | * 修改删除为异步删除 113 | * 修复本地文件夹为根目录时路径错误 114 | 115 | = 1.0.1 = 116 | * 修复勾选不在本地保存图片后媒体库显示默认图片问题 117 | 118 | = 1.0.0 = 119 | * First version 120 | -------------------------------------------------------------------------------- /sdk/composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "upyun/sdk": "^3.4" 4 | } 5 | } -------------------------------------------------------------------------------- /uninstall.php: -------------------------------------------------------------------------------- 1 | '', 29 | 'OperatorName' => '', 30 | 'OperatorPwd' => '', 31 | 'nothumb' => 'false', // 是否上传缩略图 32 | 'nolocalsaving' => 'false', // 是否保留本地备份 33 | 'upload_url_path' => '', // URL前缀 34 | 'update_file_name' => 'false', // 是否重命名文件名 35 | 'image_process' => '', // 图片处理 https://console.upyun.com/services/process/ 36 | ]; 37 | } 38 | register_activation_hook(__FILE__, 'uss_set_options'); 39 | function uss_set_options() 40 | { 41 | add_option('uss_options', uss_get_default_options(), '', 'yes'); 42 | } 43 | 44 | function uss_get_client() 45 | { 46 | $uss_opt = get_option('uss_options', uss_get_default_options()); 47 | $bucket = esc_attr($uss_opt['bucket']); 48 | $OperatorName = esc_attr($uss_opt['OperatorName']); 49 | $OperatorPwd = esc_attr($uss_opt['OperatorPwd']); 50 | $serviceConfig = new Config($bucket, $OperatorName, $OperatorPwd); 51 | return new Upyun($serviceConfig); 52 | } 53 | 54 | /** 55 | * 上传 56 | * 57 | * @param string $object 58 | * @param string $file 59 | * @param bool $no_local_file 60 | * @return false|void 61 | * @throws Exception 62 | */ 63 | function uss_file_upload($object, $file, $no_local_file = false) 64 | { 65 | //如果文件不存在,直接返回false 66 | if (!@file_exists($file)) { 67 | return false; 68 | } 69 | 70 | $file_resource = fopen($file, 'rb'); 71 | if (!$file_resource) { 72 | return false; 73 | } 74 | 75 | $client = uss_get_client(); 76 | $client->write($object, $file_resource); 77 | if (is_resource($file_resource)) { 78 | fclose($file_resource); 79 | } 80 | if ($no_local_file) { 81 | uss_delete_local_file($file); 82 | } 83 | } 84 | 85 | function uss_sanitize_file_name($filename) 86 | { 87 | $uss_options = get_option('uss_options', uss_get_default_options()); 88 | switch ($uss_options['update_file_name']) { 89 | case 'md5': 90 | return md5($filename) . '.' . pathinfo($filename, PATHINFO_EXTENSION); 91 | case 'time': 92 | return date('YmdHis', current_time('timestamp')) . mt_rand(100, 999) . '.' . pathinfo($filename, PATHINFO_EXTENSION); 93 | default: 94 | return $filename; 95 | } 96 | } 97 | 98 | add_filter('sanitize_file_name', 'uss_sanitize_file_name', 10, 1); 99 | 100 | /** 101 | * 是否需要删除本地文件 102 | * 103 | * @return bool 104 | */ 105 | function uss_is_delete_local_file() 106 | { 107 | $uss_options = get_option('uss_options', uss_get_default_options()); 108 | return esc_attr($uss_options['nolocalsaving']) == 'true'; 109 | } 110 | 111 | /** 112 | * 删除本地文件 113 | * 114 | * @param $file 115 | * @return bool 116 | */ 117 | function uss_delete_local_file($file) 118 | { 119 | try { 120 | //文件不存在 121 | if (!@file_exists($file)) { 122 | return true; 123 | } 124 | 125 | //删除文件 126 | if (!@unlink($file)) { 127 | return false; 128 | } 129 | 130 | return true; 131 | } catch (\Exception $ex) { 132 | return false; 133 | } 134 | } 135 | 136 | /** 137 | * 删除uss中的文件 138 | * @param $file 139 | * @return void 140 | */ 141 | function uss_delete_uss_file($file) 142 | { 143 | $client = uss_get_client(); 144 | $client->delete($file, true); 145 | } 146 | 147 | /** 148 | * 批量删除uss中的文件 149 | * 150 | * @param array $files 151 | * @return void 152 | */ 153 | function uss_delete_uss_files($files) 154 | { 155 | $client = uss_get_client(); 156 | foreach ($files as $file) { 157 | $client->delete(str_replace(["\\", './'], ['/', ''], $file), true); 158 | } 159 | } 160 | 161 | /** 162 | * 上传附件(包括图片的原图) 163 | * 164 | * @param array $metadata 165 | * @return array 166 | */ 167 | function uss_upload_attachments($metadata) 168 | { 169 | $mime_types = wp_get_mime_types(); 170 | $image_mime_types = [ 171 | $mime_types['jpg|jpeg|jpe'], 172 | $mime_types['gif'], 173 | $mime_types['png'], 174 | $mime_types['bmp'], 175 | $mime_types['tiff|tif'], 176 | $mime_types['webp'], 177 | $mime_types['ico'], 178 | ]; 179 | 180 | // 例如mp4等格式 上传后根据配置选择是否删除 删除后媒体库会显示默认图片 点开内容是正常的 181 | // 图片在缩略图处理 182 | if (!in_array($metadata['type'], $image_mime_types)) { 183 | //生成object在uss中的存储路径 184 | if (get_option('upload_path') == '.') { 185 | $metadata['file'] = str_replace("./", '', $metadata['file']); 186 | } 187 | $object = str_replace("\\", '/', $metadata['file']); 188 | $home_path = get_home_path(); 189 | $object = str_replace($home_path, '', $object); 190 | 191 | //在本地的存储路径 192 | $file = $home_path . $object; //向上兼容,较早的WordPress版本上$metadata['file']存放的是相对路径 193 | 194 | //执行上传操作 195 | uss_file_upload('/' . $object, $file, uss_is_delete_local_file()); 196 | } 197 | 198 | return $metadata; 199 | } 200 | 201 | /** 202 | * 上传图片的缩略图 203 | */ 204 | function uss_upload_thumbs($metadata) 205 | { 206 | if (empty($metadata['file'])) { 207 | return $metadata; 208 | } 209 | 210 | //获取上传路径 211 | $wp_uploads = wp_upload_dir(); 212 | $basedir = $wp_uploads['basedir']; 213 | $upload_path = get_option('upload_path'); 214 | //获取uss插件的配置信息 215 | $uss_options = get_option('uss_options', uss_get_default_options()); 216 | $no_local_file = esc_attr($uss_options['nolocalsaving']) == 'true'; 217 | $no_thumb = esc_attr($uss_options['nothumb']) == 'true'; 218 | 219 | // Maybe there is a problem with the old version 220 | // Fix multi-site problems 221 | $file = $basedir . '/' . $metadata['file']; 222 | if ($upload_path != '.') { 223 | $path_array = explode($upload_path, $file); 224 | if (count($path_array) >= 2) { 225 | $object = '/' . $upload_path . end($path_array); 226 | } 227 | } else { 228 | $object = '/' . $metadata['file']; 229 | $file = str_replace('./', '', $file); 230 | } 231 | 232 | uss_file_upload($object, $file, $no_local_file); 233 | 234 | //得到本地文件夹和远端文件夹 235 | $dirname = dirname($metadata['file']); 236 | $file_path = $dirname != '.' ? "{$basedir}/{$dirname}/" : "{$basedir}/"; 237 | $file_path = str_replace("\\", '/', $file_path); 238 | if ($upload_path == '.') { 239 | $file_path = str_replace('./', '', $file_path); 240 | } 241 | $object_path = str_replace(get_home_path(), '', $file_path); 242 | 243 | if (!empty($metadata['original_image'])) { 244 | uss_file_upload("/{$object_path}{$metadata['original_image']}", "{$file_path}{$metadata['original_image']}", $no_local_file); 245 | } 246 | 247 | //如果禁止上传缩略图,就不用继续执行了 248 | if ($no_thumb) { 249 | return $metadata; 250 | } 251 | 252 | //上传所有缩略图 253 | if (!empty($metadata['sizes'])) { 254 | foreach ($metadata['sizes'] as $val) { 255 | $object = '/' . $object_path . $val['file']; 256 | $file = $file_path . $val['file']; 257 | 258 | uss_file_upload($object, $file, $no_local_file); 259 | } 260 | } 261 | 262 | return $metadata; 263 | } 264 | 265 | /** 266 | * @param $override 267 | * @return mixed 268 | */ 269 | function uss_save_image_editor_file($override) 270 | { 271 | add_filter('wp_update_attachment_metadata', 'uss_image_editor_file_do'); 272 | return $override; 273 | } 274 | 275 | /** 276 | * @param $metadata 277 | * @return mixed 278 | */ 279 | function uss_image_editor_file_do($metadata) 280 | { 281 | return uss_upload_thumbs($metadata); 282 | } 283 | 284 | //避免上传插件/主题时出现同步到uss的情况 285 | if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) { 286 | add_filter('wp_handle_upload', 'uss_upload_attachments', 50); 287 | add_filter('wp_generate_attachment_metadata', 'uss_upload_thumbs', 100); 288 | add_filter('wp_save_image_editor_file', 'uss_save_image_editor_file', 101); 289 | } 290 | 291 | /** 292 | * 删除远端文件,删除文件时触发 293 | * @param $post_id 294 | */ 295 | function uss_delete_remote_attachment($post_id) 296 | { 297 | $wp_uploads = wp_upload_dir(); 298 | $basedir = $wp_uploads['basedir']; 299 | $upload_path = str_replace(get_home_path(), '', $basedir); 300 | // 获取图片类附件的meta信息 301 | $meta = wp_get_attachment_metadata($post_id); 302 | 303 | if (!empty($meta['file'])) { 304 | $deleteObjects = []; 305 | 306 | // meta['file']的格式为 "2020/01/wp-bg.png" 307 | $file_path = $upload_path . '/' . $meta['file']; 308 | $dirname = dirname($file_path) . '/'; 309 | 310 | $deleteObjects[] = $file_path; 311 | 312 | // 超大图原图 313 | if (!empty($meta['original_image'])) { 314 | $deleteObjects[] = $dirname . $meta['original_image']; 315 | } 316 | 317 | // 删除缩略图 318 | if (!empty($meta['sizes'])) { 319 | foreach ($meta['sizes'] as $val) { 320 | $deleteObjects[] = $dirname . $val['file']; 321 | } 322 | } 323 | 324 | $backup_sizes = get_post_meta($post_id, '_wp_attachment_backup_sizes', true); 325 | if (is_array($backup_sizes)) { 326 | foreach ($backup_sizes as $size) { 327 | $deleteObjects[] = $dirname . $size['file']; 328 | } 329 | } 330 | 331 | uss_delete_uss_files($deleteObjects); 332 | } else { 333 | // 获取链接删除 334 | $link = wp_get_attachment_url($post_id); 335 | if ($link) { 336 | if ($upload_path != '.') { 337 | $file_info = explode($upload_path, $link); 338 | if (count($file_info) >= 2) { 339 | $file = $upload_path . end($file_info); 340 | } 341 | } else { 342 | $uss_options = get_option('uss_options', uss_get_default_options()); 343 | $uss_upload_url = esc_attr($uss_options['upload_url_path']); 344 | $file_info = explode($uss_upload_url, $link); 345 | if (count($file_info) >= 2) { 346 | $file = end($file_info); 347 | } 348 | } 349 | 350 | uss_delete_uss_file($file); 351 | } 352 | } 353 | } 354 | 355 | add_action('delete_attachment', 'uss_delete_remote_attachment'); 356 | 357 | // 当upload_path为根目录时,需要移除URL中出现的“绝对路径” 358 | function uss_modefiy_img_url($url, $post_id) 359 | { 360 | // 移除 ./ 和 项目根路径 361 | return str_replace(['./', get_home_path()], '', $url); 362 | } 363 | 364 | if (get_option('upload_path') == '.') { 365 | add_filter('wp_get_attachment_url', 'uss_modefiy_img_url', 30, 2); 366 | } 367 | 368 | /** 369 | * @param string $homePath 370 | * @param string $uploadPath 371 | * @return array 372 | */ 373 | function uss_read_dir_queue($homePath, $uploadPath) 374 | { 375 | $dir = $homePath . $uploadPath; 376 | $dirsToProcess = new SplQueue(); 377 | $dirsToProcess->enqueue([$dir, '']); 378 | $foundFiles = []; 379 | 380 | while (!$dirsToProcess->isEmpty()) { 381 | list($currentDir, $relativeDir) = $dirsToProcess->dequeue(); 382 | 383 | foreach (new DirectoryIterator($currentDir) as $fileInfo) { 384 | if ($fileInfo->isDot()) continue; 385 | 386 | $filepath = $fileInfo->getRealPath(); 387 | 388 | // Compute the relative path of the file/directory with respect to upload path 389 | $currentRelativeDir = "{$relativeDir}/{$fileInfo->getFilename()}"; 390 | 391 | if ($fileInfo->isDir()) { 392 | $dirsToProcess->enqueue([$filepath, $currentRelativeDir]); 393 | } else { 394 | // Add file path and key to the result array 395 | $foundFiles[] = [ 396 | 'filepath' => $filepath, 397 | 'key' => '/' . $uploadPath . $currentRelativeDir 398 | ]; 399 | } 400 | } 401 | } 402 | 403 | return $foundFiles; 404 | } 405 | 406 | add_filter('the_content', 'uss_setting_content_img_process'); 407 | add_filter('post_thumbnail_html', 'uss_setting_post_thumbnail_img_process', 10, 3); 408 | add_filter('wp_calculate_image_srcset', 'uss_custom_image_srcset', 10, 5); 409 | 410 | function uss_custom_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) 411 | { 412 | $option = get_option('uss_options', uss_get_default_options()); 413 | $style = !empty($option['image_process']) ? esc_attr($option['image_process']) : ''; 414 | $upload_url_path = esc_attr($option['upload_url_path']); 415 | if (empty($style)) { 416 | return $sources; 417 | } 418 | 419 | foreach ($sources as $index => $source) { 420 | if (strpos($source['url'], $upload_url_path) !== false && strpos($source['url'], $style) === false) { 421 | $sources[$index]['url'] .= $style; 422 | } 423 | } 424 | 425 | return $sources; 426 | } 427 | 428 | function uss_setting_content_img_process($content) 429 | { 430 | $option = get_option('uss_options', uss_get_default_options()); 431 | $style = esc_attr($option['image_process']); 432 | $upload_url_path = esc_attr($option['upload_url_path']); 433 | if (!empty($style)) { 434 | preg_match_all('//sim', $content, $images); 435 | if (!empty($images) && isset($images[1])) { 436 | $images[1] = array_unique($images[1]); 437 | foreach ($images[1] as $item) { 438 | if (strpos($item, $upload_url_path) !== false && strpos($item, $style) === false) { 439 | $content = str_replace($item, $item . $style, $content); 440 | } 441 | } 442 | 443 | $content = str_replace($style . $style, $style, $content); 444 | } 445 | } 446 | 447 | return $content; 448 | } 449 | 450 | function uss_setting_post_thumbnail_img_process($html, $post_id, $post_image_id) 451 | { 452 | $option = get_option('uss_options', uss_get_default_options()); 453 | $style = esc_attr($option['image_process']); 454 | $upload_url_path = esc_attr($option['upload_url_path']); 455 | if (!empty($style) && has_post_thumbnail()) { 456 | preg_match_all('//sim', $html, $images); 457 | if (!empty($images) && isset($images[1])) { 458 | $images[1] = array_unique($images[1]); 459 | foreach ($images[1] as $item) { 460 | if (strpos($item, $upload_url_path) !== false && strpos($item, $style) === false) { 461 | $html = str_replace($item, $item . $style, $html); 462 | } 463 | } 464 | 465 | $html = str_replace($style . $style, $style, $html); 466 | } 467 | } 468 | 469 | return $html; 470 | } 471 | 472 | // 在插件列表页添加设置按钮 473 | function uss_plugin_action_links($links, $file) 474 | { 475 | if ($file == plugin_basename(dirname(__FILE__) . '/upyun-uss-wordpress.php')) { 476 | $links[] = '设置'; 477 | $links[] = '赞赏'; 478 | } 479 | return $links; 480 | } 481 | 482 | add_filter('plugin_action_links', 'uss_plugin_action_links', 10, 2); 483 | 484 | // 在导航栏“设置”中添加条目 485 | function uss_add_setting_page() 486 | { 487 | add_options_page('又拍云 USS', '又拍云 USS', 'manage_options', __FILE__, 'uss_setting_page'); 488 | } 489 | 490 | add_action('admin_menu', 'uss_add_setting_page'); 491 | 492 | // 插件设置页面 493 | function uss_setting_page() 494 | { 495 | if (!current_user_can('manage_options')) { 496 | wp_die('Insufficient privileges!'); 497 | } 498 | $options = []; 499 | if (!empty($_POST) and $_POST['type'] == 'uss_set') { 500 | $options['bucket'] = isset($_POST['bucket']) ? sanitize_text_field($_POST['bucket']) : ''; 501 | $options['OperatorName'] = isset($_POST['OperatorName']) ? sanitize_text_field($_POST['OperatorName']) : ''; 502 | $options['OperatorPwd'] = isset($_POST['OperatorPwd']) ? sanitize_text_field($_POST['OperatorPwd']) : ''; 503 | $options['nothumb'] = isset($_POST['nothumb']) ? 'true' : 'false'; 504 | $options['nolocalsaving'] = isset($_POST['nolocalsaving']) ? 'true' : 'false'; 505 | $options['image_process'] = isset($_POST['image_process']) ? sanitize_text_field($_POST['image_process']) : ''; 506 | //仅用于插件卸载时比较使用 507 | $options['upload_url_path'] = isset($_POST['upload_url_path']) ? sanitize_text_field(stripslashes($_POST['upload_url_path'])) : ''; 508 | $options['update_file_name'] = isset($_POST['update_file_name']) ? sanitize_text_field($_POST['update_file_name']) : 'false'; 509 | } 510 | 511 | if (!empty($_POST) and $_POST['type'] == 'upyun_uss_all') { 512 | $files = uss_read_dir_queue(get_home_path(), get_option('upload_path')); 513 | foreach ($files as $file) { 514 | uss_file_upload($file['key'], $file['filepath']); 515 | } 516 | echo '本次操作成功同步' . count($files) . '个文件'; 517 | } 518 | 519 | // 替换数据库链接 520 | if (!empty($_POST) and $_POST['type'] == 'upyun_uss_replace') { 521 | $nonce = $_POST['upyun_uss_replace-nonce'] ?? ''; 522 | if (empty($nonce) || !wp_verify_nonce($nonce, 'upyun_uss_replace')) { 523 | wp_die('Illegal requests!'); 524 | } 525 | 526 | $old_url = esc_url_raw($_POST['old_url']); 527 | $new_url = esc_url_raw($_POST['new_url']); 528 | 529 | if (!empty($old_url) && !empty($new_url)) { 530 | global $wpdb; 531 | // 文章内容 532 | $posts_name = $wpdb->prefix . 'posts'; 533 | $posts_result = $wpdb->query($wpdb->prepare("UPDATE $posts_name SET post_content = REPLACE(post_content, '%s', '%s')", [$old_url, $new_url])); 534 | 535 | // 修改题图之类的 536 | $postmeta_name = $wpdb->prefix . 'postmeta'; 537 | $postmeta_result = $wpdb->query($wpdb->prepare("UPDATE $postmeta_name SET meta_value = REPLACE(meta_value, '%s', '%s')", [$old_url, $new_url])); 538 | 539 | echo '替换成功!共替换文章内链' . $posts_result . '条、题图链接' . $postmeta_result . '条!'; 540 | } else { 541 | echo '请填写资源链接URL地址!'; 542 | } 543 | } 544 | 545 | // 若$options不为空数组,则更新数据 546 | if ($options !== []) { 547 | //更新数据库 548 | update_option('uss_options', $options); 549 | 550 | $upload_path = sanitize_text_field(trim(stripslashes($_POST['upload_path']), '/')); 551 | $upload_path = $upload_path == '' ? 'wp-content/uploads' : $upload_path; 552 | update_option('upload_path', $upload_path); 553 | $upload_url_path = sanitize_text_field(trim(stripslashes($_POST['upload_url_path']), '/')); 554 | update_option('upload_url_path', $upload_url_path); 555 | echo '设置已保存!'; 556 | } 557 | 558 | $uss_options = get_option('uss_options', uss_get_default_options()); 559 | $upload_path = get_option('upload_path'); 560 | $upload_url_path = get_option('upload_url_path'); 561 | 562 | $uss_bucket = esc_attr($uss_options['bucket']); 563 | $uss_OperatorName = esc_attr($uss_options['OperatorName']); 564 | $uss_OperatorPwd = esc_attr($uss_options['OperatorPwd']); 565 | 566 | $uss_nothumb = esc_attr($uss_options['nothumb']); 567 | $check_nothumb = $uss_nothumb == 'true' ? 'checked="checked"' : ''; 568 | 569 | $uss_nolocalsaving = esc_attr($uss_options['nolocalsaving']); 570 | $check_nolocalsaving = $uss_nolocalsaving == 'true' ? 'checked="checked"' : ''; 571 | 572 | $uss_update_file_name = esc_attr($uss_options['update_file_name']); 573 | 574 | $uss_image_process = esc_attr($uss_options['image_process']); 575 | 576 | $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://'; 577 | 578 | $nonce = wp_nonce_field('upyun_uss_replace', 'upyun_uss_replace-nonce', true, false); 579 | ?> 580 | 581 | 又拍云 USS 当前版本: 582 | 活动推荐:点我注册并完成实名认证,赠送 61 元免费代金券 583 | 如果觉得此插件对你有所帮助,不妨到 GitHub 上点个Star,Watch关注更新;欢迎加入云存储插件交流群,QQ群号:887595381; 584 | 585 | 586 | 587 | 588 | 589 | 服务名称 590 | 591 | 592 | 593 | 请先访问 又拍云控制台 创建云存储服务,再填写以上内容。 594 | 595 | 596 | 597 | 598 | 操作员 599 | 600 | 601 | 602 | 603 | 604 | 密码 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 不上传缩略图 613 | 614 | 615 | /> 616 | 建议不勾选 617 | 618 | 619 | 620 | 621 | 不在本地保留备份 622 | 623 | 624 | /> 625 | 建议不勾选 626 | 627 | 628 | 629 | 630 | 自动重命名文件 631 | 632 | 633 | 634 | value="false">不处理 635 | value="md5">MD5 636 | value="time">时间戳+随机数 637 | 638 | 639 | 640 | 641 | 642 | 本地文件夹 643 | 644 | 645 | 646 | 附件在服务器上的存储位置,例如:wp-content/uploads (注意不要以“/”开头和结尾),根目录请输入.。 647 | 648 | 649 | 650 | 651 | URL前缀 652 | 653 | 654 | 655 | 656 | 注意: 657 | 658 | 1)URL前缀的格式为 {加速域名}/{本地文件夹} 659 | ,“本地文件夹”务必与上面保持一致(结尾无/ ),或者“本地文件夹”为 . 时 660 | {加速域名} 。 661 | 662 | 663 | 2)URL前缀中的协议头http://和https://请根据实际情况填写,提示的协议头默认和源站相同。 664 | 3)操作员需要有可读取、可写入、可删除权限。 665 | 666 | 667 | 668 | 669 | 图片处理 670 | 671 | 672 | 673 | 674 | 版本名称: 675 | 1)在 云存储服务管理列表 中对应服务的 图片处理 处添加。具体处理参数设置参考又拍云文档。 676 | 2)填写时需要将间隔标识符和对应的版本名称或 处理参数进行拼接,例如: 677 | 间隔标识符为!(感叹号),版本名称为blog,处理参数为/watermark/text/VVBZVU4g5Y+I5ouN5LqR/align/center/color/52acd9/flip/left-right 678 | 则填写为 !blog 或 !/watermark/text/VVBZVU4g5Y+I5ouN5LqR/align/center/color/52acd9/flip/left-right 679 | 680 | 681 | 682 | 683 | 保存/更新选项 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 同步历史附件 695 | 696 | 697 | 698 | 699 | 注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。 所以,建议那些几千上万附件的用户考虑官方的 同步工具 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 数据库原链接替换 710 | 711 | 712 | 713 | 如:https://qq52o.me 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 如:USS加速域名http://uss-sy-records.test.upcdn.net或自定义域名https://resources.qq52o.me 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接 734 | 735 | 736 | 737 | 738 | 739 | --------------------------------------------------------------------------------
本次操作成功同步' . count($files) . '个文件
替换成功!共替换文章内链' . $posts_result . '条、题图链接' . $postmeta_result . '条!
请填写资源链接URL地址!
设置已保存!
活动推荐:点我注册并完成实名认证,赠送 61 元免费代金券
如果觉得此插件对你有所帮助,不妨到 GitHub 上点个Star,Watch关注更新;欢迎加入云存储插件交流群,QQ群号:887595381;
Star
Watch
请先访问 又拍云控制台 创建云存储服务,再填写以上内容。
云存储服务
建议不勾选
附件在服务器上的存储位置,例如:wp-content/uploads (注意不要以“/”开头和结尾),根目录请输入.。
wp-content/uploads
.
注意:
1)URL前缀的格式为 {加速域名}/{本地文件夹} 659 | ,“本地文件夹”务必与上面保持一致(结尾无/ ),或者“本地文件夹”为 . 时 660 | {加速域名} 。 661 |
{加速域名}/{本地文件夹}
/
{加速域名}
2)URL前缀中的协议头http://和https://请根据实际情况填写,提示的协议头默认和源站相同。
http://
https://
3)操作员需要有可读取、可写入、可删除权限。
可读取
可写入
可删除
版本名称:
1)在 云存储服务管理列表 中对应服务的 图片处理 处添加。具体处理参数设置参考又拍云文档。
图片处理
2)填写时需要将间隔标识符和对应的版本名称或 处理参数进行拼接,例如:
间隔标识符
版本名称
处理参数
间隔标识符为!(感叹号),版本名称为blog,处理参数为/watermark/text/VVBZVU4g5Y+I5ouN5LqR/align/center/color/52acd9/flip/left-right
!
blog
/watermark/text/VVBZVU4g5Y+I5ouN5LqR/align/center/color/52acd9/flip/left-right
则填写为 !blog 或 !/watermark/text/VVBZVU4g5Y+I5ouN5LqR/align/center/color/52acd9/flip/left-right
!blog
!/watermark/text/VVBZVU4g5Y+I5ouN5LqR/align/center/color/52acd9/flip/left-right
注意:如果是首次同步,执行时间将会十分十分长(根据你的历史附件数量),有可能会因执行时间过长,页面显示超时或者报错。 所以,建议那些几千上万附件的用户考虑官方的 同步工具
如:https://qq52o.me
https://qq52o.me
如:USS加速域名http://uss-sy-records.test.upcdn.net或自定义域名https://resources.qq52o.me
http://uss-sy-records.test.upcdn.net
https://resources.qq52o.me
注意:如果是首次替换,请注意备份!此功能会替换文章以及设置的特色图片(题图)等使用的资源链接