update<first>

main
赤月未咲 10 months ago
parent 4c5444b973
commit 2d1833a37a

@ -1,3 +1,11 @@
# boheimg-wordpress-plugin # boheimg-wordpress-plugin
薄荷图床的wordpress插件 薄荷图床的wordpress插件
# install
直接上传到wordpress的插件中
# use
进入插件设置页面正确配置你的相关信息

@ -0,0 +1,36 @@
<?php
/*
Plugin Name: 薄荷图床上传(Beta)
Plugin URI:
Description: 方便上传图片到薄荷图床,如有问题请点击反馈发送邮件
Version: 0.0.1
Author: 紗夕里
*/
if (!defined('ABSPATH')) {
die;
}
define('BohePro_WordPress_FILE', __FILE__);
define('BohePro_WordPress_DIRNAME', dirname(__FILE__));
define('BohePro_WordPress_Plugin', plugins_url('',__FILE__));
require BohePro_WordPress_DIRNAME . '/includes/bohe-config.php';
require BohePro_WordPress_DIRNAME . '/upload/bohe-img.php';
add_action( 'admin_init', 'bohe_upload_admin_init' );
function bohe_upload_admin_init() {
add_filter( 'plugin_action_links', 'bohe_upload_add_link', 10, 2 );
}
function bohe_upload_add_link( $actions, $plugin_file ) { //添加设置按钮
static $plugin;
if (!isset($plugin))
$plugin = plugin_basename(__FILE__);
if ($plugin == $plugin_file) {
$settings = array('settings' => '<a href="admin.php?page=boheupload_options">' . __('Settings') . '</a>');
$site_link = array('contact' => '<a href="mailto:mirai@lolicon.team" target="_blank">反馈</a>','bohe' => '<a href="https://lolicon.team/" target="_blank">博客</a>');
$actions = array_merge($settings, $actions);
$actions = array_merge($site_link, $actions);
}
return $actions;
}
?>

@ -0,0 +1,111 @@
.com-hd {
margin: 20px 0 15px
}
.com-hd b {
font-size: 16px;
color: #333;
font-family: 'Microsoft Yahei'
}
.tb-set {
border-collapse: collapse
}
.tb-set tr td {
padding: 10px 5px;
border: 1px solid #e0e0e0
}
.actived {
color: red
}
.tb-set tr td input,
.tb-set tr td img {
margin: 0 auto;
vertical-align: middle
}
.tb-set tr td b {
color: #000
}
.tb-set tr td .sel {
height: 17px;
border: 1px solid #ccc;
border-radius: 5px;
outline: 0;
color: #333;
padding: 3px 1px
}
.tb-set tr td .sel select {
border: 0;
outline: 0;
width: auto
}
.tb-set tr td .txt {
width: 80%;
height: 28px;
border: 1px solid #ccc;
line-height: 20px;
outline: 0;
color: #333;
padding: 2px 0 2px 5px;
margin: -2px 0 0 5px
}
.tb-set tr td .sec {
width: 30px;
height: 20px;
border: 1px solid #ccc;
line-height: 20px;
border-radius: 5px;
outline: 0;
color: #333;
padding: 2px 0 2px 5px;
margin: -2px 0 0 5px
}
.tb-set tr td .dy {
width: 15%;
height: 20px;
border: 1px solid #ccc;
line-height: 20px;
border-radius: 5px;
outline: 0;
vertical-align: middle;
font-weight: bold;
color: #0c0;
padding: 2px 0 2px 5px;
margin: -2px 0 0 5px
}
.tb-set tr td .txt-lar {
width: 450px;
height: 100px;
border: 1px solid #ccc;
line-height: 15px
}
.tb-set tr td .txt-sho {
width: 150px
}
.message {
margin: 1em 0;
padding: .5em;
height: 30px;
line-height: 30px;
background: #fff;
}
.success {
border-left: 3px solid #0c0;
}
.error {
border-left: 3px solid #c00;
}

@ -0,0 +1,72 @@
<?php
add_action('admin_menu', 'bohe_upload_menu_page');
function bohe_upload_menu_page(){
add_menu_page('薄荷图床设置', '薄荷图床设置', 'administrator', 'boheupload_options', 'boheupload_options', 'dashicons-format-image', 99);
}
//设置页面
function boheupload_options(){?>
<link href="<?php echo plugin_dir_url(__FILE__ ); ?>assets/style.css" type="text/css" rel="stylesheet" />
<h2>薄荷图床(BohePro)上传设置</h2>
<?php update_boheupload_options() ?>
<form method="post">
<table class="tb-set" width="100%" style='padding:0;margin:0;' cellspacing='0' cellpadding='0'>
<tr>
<td align="right"><b>API网址设置</b><br />(填写要对接图床的域名必须带有http://或https://)</td>
<td colspan="3"><input type="url" class="txt txt-sho" style="width:300px;font-weight:bold;" name="domain" value="<?php if(get_option('domain')==''){echo '';}else{echo get_option('domain');} ?>" />例如https://www.bohe.pro</td>
</tr>
<tr>
<td align="right" width="30%"><b>图床Tokens</b><br />(填写<b>图床后台获取的Tokens</b>)</td>
<td colspan="3"><input type="text" class="txt txt-sho" style="width:300px;font-weight:bold;" name="tokens" value="<?php if(get_option('tokens')==''){echo '';}else{echo get_option('tokens');} ?>" />例如1|1bJbwlqBfnggmOMEZqXT5XusaIwqiZjCDs7r1Ob5</td>
</tr>
<tr>
<td align="right" width="30%"><b>协议类型</b><br />(填写http或https不填写默认为https)</td>
<td colspan="3"><input type="text" class="txt txt-sho" style="width:300px;font-weight:bold;" name="protocol_type" value="<?php if(get_option('protocol_type')==''){echo 'https';}else{echo get_option('protocol_type');} ?>" /></td>
</tr>
<tr>
<td align="right"><b>命名方式</b><br />(可选值1, 2, 3。1自动重命名2保留原文件名3自动重命名短链接模式。不填默认为自动重命名)</td>
<td colspan="3"><input type="number" class="txt txt-sho" style="width:300px;font-weight:bold;" name="mode" value="<?php if(get_option('mode')==''){echo '';}else{echo get_option('mode');} ?>" /></td>
</tr>
<tr>
<td align="right"><b>上传目录</b><br />(要上传的目录,不填默认为 年/月/日 目录。多级目录以 / 分隔,首目录无需使用/)</td>
<td colspan="3"><input type="text" class="txt txt-sho" style="width:300px;font-weight:bold;" name="uploadPath" value="<?php if(get_option('uploadPath')==''){echo '';}else{echo get_option('uploadPath');} ?>" /> 例如img/picgo</td>
</tr>
<tr>
<td align="right"><b>是否开启水印</b><br />(可取值1, 0。1开启水印0关闭水印。不填默认为关闭水印)</td>
<td colspan="3"><input type="number" class="txt txt-sho" style="width:300px;font-weight:bold;" name="watermark" value="<?php if(get_option('watermark')==''){echo '';}else{echo get_option('watermark');} ?>" /></td>
</tr>
<tr>
<td align="right"><b>是否开启图片无损压缩</b><br />(可取值1, 0。1开启无损压缩0关闭无损压缩。不填默认为关闭无损压缩)</td>
<td colspan="3"><input type="number" class="txt txt-sho" style="width:300px;font-weight:bold;" name="image_compress" value="<?php if(get_option('image_compress')==''){echo '';}else{echo get_option('image_compress');} ?>" /></td>
</tr>
<tr>
<td align="right"><b>压缩质量</b><br />(可取值0到100之间的任意整数数值越大质量越高数值越小质量越低。如果上一个参数【image_compress】取值为1则必须填写这个参数否则报【801参数不足】错误)</td>
<td colspan="3"><input type="number" class="txt txt-sho" style="width:300px;font-weight:bold;" name="image_compress_level" value="<?php if(get_option('image_compress_level')==''){echo '';}else{echo get_option('image_compress_level');} ?>" /></td>
</tr>
<tr>
<td align="right"></td>
<td colspan="4"><input onclick="load();" type="submit" class="button" name="submit" id="submit" value="保存设置" /></td>
</tr>
</table>
</form>
<script>
function load() {document.getElementById("submit").value="保存中,请稍候...";setTimeout(function() {document.getElementById("submit").disabled="false";}, 1)}</script>
<?php }
function update_boheupload_options(){
if(isset($_POST['submit'])){
$updated = true;
update_option('domain',$_POST['domain']);
update_option('tokens',$_POST['tokens']);
update_option('protocol_type',$_POST['protocol_type']);
update_option('mode',$_POST['mode']);
update_option('uploadPath',$_POST['uploadPath']);
update_option('watermark',$_POST['watermark']);
update_option('image_compress',$_POST['image_compress']);
update_option('image_compress_level',$_POST['image_compress_level']);
if($updated){
echo '<div class="message success">设置成功!</div>';
}else{
echo '<div class="message error">保存失败!请检查网络或设置!</div>';
}
}
}
?>

@ -0,0 +1,97 @@
.bohe-upload {
padding: 4px 10px;
height: 20px;
line-height: 20px;
position: relative;
cursor: pointer;
color: #888;
background: #fafafa;
border: 1px solid #ddd;
border-radius: 4px;
overflow: hidden;
display: inline-block;
zoom: 1;
}
.bohe-upload input {
position: absolute;
right: 0;
top: 0;
opacity: 0;
cursor: pointer;
}
.bohe-upload:hover {
color: #444;
background: #eee;
border-color: #ccc;
text-decoration: none;
}
#bohe-tip {
margin-left: 10px;
padding: 0 15px;
display: inline-block;
height: 30px;
position: absolute;
text-align: center;
line-height: 30px;
background: #fff;
color: #c00;
font-size: 12px;
border-radius: 6px;
box-shadow: 0px 0px 12px 4px rgba(0, 0, 0, .05);
}
#bohe-upload-box {
width: auto;
height: 100px;
border: 3px dashed silver;
line-height: 100px;
text-align: center;
font-size: 20px;
color: #d3d3d3;
cursor: pointer;
}
#bohe-upload-box-input {
position: absolute;
display: block;
top: 0;
left: 0;
bottom: 0;
right: 0;
opacity: 0;
-moz-opacity: 0;
filter: alpha(opacity=0);
cursor: pointer;
}
#result {
position: relative;
width: 100%;
margin-top: 15px;
border-radius: 4px;
text-align: center;
font-size: 20px;
color: #d3d3d3;
}
#result input {
width: 100%;
height: 40px;
outline-style: none;
box-shadow: none;
}
#result input:not(:last-child) {
margin-bottom: 8px;
}
#result input:focus {
border: 1px solid #425AEF;
}

@ -0,0 +1,160 @@
<?php
add_action('media_buttons', 'add_bohe_upload_button');
function add_bohe_upload_button() {
if(!empty(wp_get_current_user()->roles) && in_array('administrator', wp_get_current_user()->roles)){
$getdataDoMain = get_option('domain');
$getdataTokens = get_option('tokens');
$getdataProtocolType = get_option('protocol_type');
$getdataMode = get_option('mode');
$getdataUploadPath = get_option('uploadPath');
$getdataWatermark = get_option('watermark');
$getdataImageCompress = get_option('image_compress');
$getdataImageCompressLevel = get_option('image_compress_level');
}
else return 0;
?>
<link href="<?php echo plugin_dir_url(__FILE__ ); ?>assets/style.css" type="text/css" rel="stylesheet" />
<a href="javascript:;" class="bohe-upload"><input type="file" id="input-bohe-upload" multiple />上传图片</a>
<span id="tip">上传图片过程中请耐心等待!请勿刷新页面,如果等待时间过长没有上传成功可以刷新页面重试或检查插件设置</span>
<script src="//lib.baomitu.com/axios/0.27.2/axios.min.js"></script>
<script>
const bohe_upload = document.getElementById('input-bohe-upload');
bohe_upload.addEventListener("change", () => { //监听上传按钮
const getdataDoMain = "<?php echo get_option('domain'); ?>";
const getdataTokens = "<?php echo get_option('tokens'); ?>";
const getdataProtocolType = "<?php echo get_option('protocol_type'); ?>"; //协议类型
const getdataMode = "<?php echo get_option('mode'); ?>";
const getdataUploadPath = "<?php echo get_option('uploadPath'); ?>";
const getdataWatermark = "<?php echo get_option('watermark'); ?>";
const getdataImageCompress = "<?php echo get_option('image_compress'); ?>";
const getdataImageCompressLevel = "<?php echo get_option('image_compress_level'); ?>";
const files = bohe_upload.files;
for(let i = 0; i < files.length; i++) {
const file = files[i];
const formData = new FormData();
//输出调试信息
formData.append("api_token", getdataTokens);
formData.append('uploadedFile', file);
// 只在协议类型不为空时添加参数
if(getdataProtocolType !== ''){
formData.append('protocol_type', '<?php echo get_option('protocol_type'); ?>');
}
if(getdataMode !== ''){
formData.append('mode', getdataMode);
}
if(getdataImageCompress !== ''){
formData.append('image_compress', '1');
formData.append('image_compress_level', '<?php echo get_option('image_compress_level'); ?>');
}
formData.append('uploadPath', '<?php echo get_option('uploadPath'); ?>');
// 添加水印和压缩参数判断
if(getdataWatermark === '1'){
formData.append('watermark', '1');
}
document.getElementById('input-bohe-upload').disabled = true;
document.getElementById('tip').innerHTML = '正在上传中...';
axios.defaults.crossDomain = true;
axios({
method:'post',
url: '<?php echo get_option('domain'); ?>/api/upload/',
data:formData
}).then(response => {
const data = response.data;
if (data.status) {
const url = data.url;
const name = data.originFileName;
wp.media.editor.insert('<a href="'+ url +'"><img src="'+ url +'" alt="'+ name +'" /></a>');
document.getElementById('input-bohe-upload').disabled = false;
document.getElementById('tip').innerHTML = '上传图片过程中请耐心等待!请勿刷新页面,如果等待时间过长没有上传成功可以刷新页面重试或检查插件设置';
}
}).catch(error => {
console.log(error);
document.getElementById('input-bohe-upload').disabled = false;
document.getElementById('tip').innerHTML = '上传图片过程中请耐心等待!请勿刷新页面,若等待时间过长可以尝试刷新页面重试或检查插件设置';
});
}
});
</script>
<?php }
add_action('add_meta_boxes', 'add_bohe_upload_box');
function add_bohe_upload_box() {
if(!empty(wp_get_current_user()->roles) && in_array('administrator', wp_get_current_user()->roles)){
add_meta_box('bohe_upload_box_tmp', '薄荷图床(BohePro)上传', 'bohe_upload_box', 'post', 'side', 'high');
}
else return 0;
}
function bohe_upload_box() {?>
<link href="<?php echo plugin_dir_url(__FILE__ ); ?>assets/style.css" type="text/css" rel="stylesheet" />
<div id="bohe-upload-box">点击此区域上传图片</div>
<input type="file" multiple id="bohe-upload-box-input" />
<div id="result"></div>
<script src="//lib.baomitu.com/axios/0.27.2/axios.min.js"></script>
<script>
const bohe_upload_input = document.getElementById('bohe-upload-box-input');
bohe_upload_input.addEventListener("change", () => {
const getdataDoMain = "<?php echo get_option('domain'); ?>";
const getdataTokens = "<?php echo get_option('tokens'); ?>";
const getdataProtocolType = "<?php echo get_option('protocol_type'); ?>"; //协议类型
const getdataMode = "<?php echo get_option('mode'); ?>";
const getdataUploadPath = "<?php echo get_option('uploadPath'); ?>";
const getdataWatermark = "<?php echo get_option('watermark'); ?>";
const getdataImageCompress = "<?php echo get_option('image_compress'); ?>";
const getdataImageCompressLevel = "<?php echo get_option('image_compress_level'); ?>";
const files = bohe_upload_input.files;
for(let i = 0; i < files.length; i++) {
const file = files[i];
const formData = new FormData();
formData.append("api_token", getdataTokens);
formData.append('uploadedFile', file);
// 只在协议类型不为空时添加参数
if(getdataProtocolType !== ''){
formData.append('protocol_type', '<?php echo get_option('protocol_type'); ?>');
}
if(getdataMode !== ''){
formData.append('mode', getdataMode);
}
if(getdataImageCompress !== ''){
formData.append('image_compress', '1');
formData.append('image_compress_level', '<?php echo get_option('image_compress_level'); ?>');
}
formData.append('uploadPath', '<?php echo get_option('uploadPath'); ?>');
// 添加水印和压缩参数判断
if(getdataWatermark === '1'){
formData.append('watermark', '1');
}
console.log(formData);
document.getElementById('bohe-upload-box-input').disabled = true;
document.getElementById('bohe-upload-box').innerHTML = '正在上传中...';
axios({
method:'post',
url: '<?php echo get_option('domain'); ?>/api/upload/',
data:formData
}).then(response => {
const data = response.data;
if (data.status) {
const url = data.url;
const name = data.originFileName;
const input = document.createElement('input');
input.type = 'text';
input.value = url;
document.getElementById('result').appendChild(input);
input.addEventListener('click', function() {
input.select();
document.execCommand('copy');
});
document.getElementById('bohe-upload-box-input').disabled = false;
document.getElementById('bohe-upload-box').innerHTML = '点击此区域上传图片';
}
}).catch(error => {
console.log(error);
document.getElementById('bohe-upload-box-input').disabled = false;
document.getElementById('bohe-upload-box').innerHTML = '点击此区域上传图片';
});
}
});
</script>
<?php } ?>
Loading…
Cancel
Save