## 修 Prism is not defined
首页控制台报 `Uncaught ReferenceError: Prism is not defined`,来源是
wp-editormd 插件(不是本主题):它在 Prism 脚本加载之前就无条件执行了
`Prism.plugins.autoloader.languages_path = "..."`。
线上页面里的真实顺序:
第 668 行 该赋值语句
第 695 行 prism-core.min.js
第 696 行 prism-autoloader.min.js
在 `wp_head` 优先级 0 垫一个最小空壳 `window.Prism`(含 plugins.autoloader
这条访问链路),让那句赋值有地方可写。已核对 prism-core.min.js 源码:它开头
只读 `u.Prism.manual` 与 `u.Prism.disableWorkerMessageHandler`,结尾
`global.Prism = Prism` 整体覆盖 —— 空壳必定被真实对象替换,不承担持久职责,
只为吸收那条过早的赋值。已用 jsdom 验证:去掉空壳重现原始报错,加上后消失,
且之后 `Prism.highlightAll` 仍可正常调用。
## 项目地址统一到 code.yangyq.net
原作者署名、原主题地址、README、语言包、JS 里的控制台横幅全部换成自建仓库地址。
LICENSE 文件保持原样不动(MIT 要求保留原始版权声明)。
## CDN 分支改成拿不到就回退本地
原来的 `g_cdn` 分支指向 `cdn.jsdelivr.net/gh/vtrois/kratos`,本仓库在自建
Gitea 上,jsDelivr 只认 github/npm/wordpress,实测该地址 404。现改为仅在
`YANG_THEME_CDN_REPO` 常量存在时才走 CDN,否则一律用
`get_template_directory_uri()`。留一个 404 的地址比留空更危险:页面能开、
样式全丢,极难排查。
## 顺带
- `.gitattributes` 补 `*.mo binary`:原来是靠"含 NUL"启发式判定,不是契约
- `update.json` 的版本号跟进到 3.2.5
- `kratos.min.js` / `kratos.min.css` 用压缩器重新生成,不手工改产物
151 lines
5.5 KiB
PHP
151 lines
5.5 KiB
PHP
<?php
|
|
/**
|
|
* dogecloud 对象存储
|
|
* @author Yongquan Yang <yangyq.net>
|
|
* @license MIT License
|
|
* @version 2020.03.17
|
|
*/
|
|
|
|
if (kratos_option('g_cos', false)) {
|
|
function dogcloud_upload($object, $file, $mime){
|
|
if (!@file_exists($file)) {
|
|
return false;
|
|
}
|
|
if (@file_exists($file)) {
|
|
$accessKey = kratos_option('g_cos_accesskey');
|
|
$secretKey = kratos_option('g_cos_secretkey');
|
|
$bucket = kratos_option('g_cos_bucketname');
|
|
|
|
$filesize = fileSize($file);
|
|
$file = fopen($file, 'rb');
|
|
|
|
$signStr = "/oss/upload/put.json?bucket=$bucket&key=$object" . "\n" . "";
|
|
$sign = hash_hmac('sha1', $signStr, $secretKey);
|
|
$authorization = "TOKEN " . $accessKey . ":" . $sign;
|
|
|
|
$url = "https://api.dogecloud.com/oss/upload/put.json?bucket=$bucket&key=$object";
|
|
$headers = array("Host: api.dogecloud.com", "Content-Type: $mime", "Authorization: $authorization");
|
|
|
|
$curl = curl_init();
|
|
curl_setopt_array($curl, array(
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_PUT => true,
|
|
CURLOPT_TIMEOUT => 0,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
|
CURLOPT_CUSTOMREQUEST => "PUT",
|
|
CURLOPT_INFILE => $file,
|
|
CURLOPT_INFILESIZE => $filesize,
|
|
CURLOPT_HTTPHEADER => $headers,
|
|
));
|
|
$response = curl_exec($curl);
|
|
curl_close($curl);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 上传附件
|
|
function dogecloud_upload_attachments($metadata)
|
|
{
|
|
if (get_option('upload_path') == '.') {
|
|
$metadata['file'] = str_replace("./", '', $metadata['file']);
|
|
}
|
|
|
|
$object = str_replace("\\", '/', $metadata['file']);
|
|
$object = str_replace(get_home_path(), '', $object);
|
|
$file = get_home_path() . $object;
|
|
$object = str_replace("wp-content/uploads/", '', $object);
|
|
$mime = $metadata['type'];
|
|
|
|
dogcloud_upload('/' . $object, $file, $mime);
|
|
|
|
return $metadata;
|
|
}
|
|
|
|
if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
|
|
add_filter('wp_handle_upload', 'dogecloud_upload_attachments', 50);
|
|
}
|
|
|
|
// 上传缩略图
|
|
function dogecloud_upload_thumbs($metadata)
|
|
{
|
|
if (isset($metadata['sizes']) && count($metadata['sizes']) > 0) {
|
|
$wp_uploads = wp_upload_dir();
|
|
$basedir = $wp_uploads['basedir'];
|
|
$file_dir = $metadata['file'];
|
|
$file_path = $basedir . '/' . dirname($file_dir) . '/';
|
|
if (get_option('upload_path') == '.') {
|
|
$file_path = str_replace("\\", '/', $file_path);
|
|
$file_path = str_replace(get_home_path() . "./", '', $file_path);
|
|
} else {
|
|
$file_path = str_replace("\\", '/', $file_path);
|
|
}
|
|
$object_path = str_replace(get_home_path(), '', $file_path);
|
|
foreach ($metadata['sizes'] as $val) {
|
|
$object = '/' . $object_path . $val['file'];
|
|
$object = str_replace("wp-content/uploads/", '', $object);
|
|
$file = $file_path . $val['file'];
|
|
$mime = $metadata['type'];
|
|
|
|
dogcloud_upload('/' . $object, $file, $mime);
|
|
}
|
|
}
|
|
return $metadata;
|
|
}
|
|
|
|
if (substr_count($_SERVER['REQUEST_URI'], '/update.php') <= 0) {
|
|
add_filter('wp_generate_attachment_metadata', 'dogecloud_upload_thumbs', 100);
|
|
}
|
|
|
|
// 删除文件
|
|
function dogecloud_delete_remote_file($file)
|
|
{
|
|
$accessKey = kratos_option('g_cos_accesskey');
|
|
$secretKey = kratos_option('g_cos_secretkey');
|
|
$bucket = kratos_option('g_cos_bucketname');
|
|
|
|
$file = str_replace("\\", '/', $file);
|
|
$file = str_replace(get_home_path(), '', $file);
|
|
$del_file_path = str_replace("wp-content/uploads/", '', $file);
|
|
$del_file_body = "[\"$del_file_path\"]";
|
|
|
|
$signStr = "/oss/file/delete.json?bucket=$bucket" . "\n" . $del_file_body;
|
|
$sign = hash_hmac('sha1', $signStr, $secretKey);
|
|
$authorization = "TOKEN " . $accessKey . ":" . $sign;
|
|
|
|
$url = "https://api.dogecloud.com/oss/file/delete.json?bucket=$bucket";
|
|
$headers = array('Host' => 'api.dogecloud.com', 'Content-Type' => 'application/json', 'Authorization' => $authorization);
|
|
|
|
$request = new WP_Http;
|
|
$result = $request->request($url, array('method' => 'POST', 'body' => $del_file_body, 'headers' => $headers));
|
|
|
|
return $file;
|
|
}
|
|
add_action('wp_delete_file', 'dogecloud_delete_remote_file', 100);
|
|
|
|
// 修改图片地址
|
|
function custom_upload_dir($uploads)
|
|
{
|
|
$upload_path = '';
|
|
$upload_url_path = kratos_option('g_cos_url');
|
|
|
|
if (empty($upload_path) || 'wp-content/uploads' == $upload_path) {
|
|
$uploads['basedir'] = WP_CONTENT_DIR . '/uploads';
|
|
} elseif (0 !== strpos($upload_path, ABSPATH)) {
|
|
$uploads['basedir'] = path_join(ABSPATH, $upload_path);
|
|
} else {
|
|
$uploads['basedir'] = $upload_path;
|
|
}
|
|
|
|
$uploads['path'] = $uploads['basedir'] . $uploads['subdir'];
|
|
|
|
if ($upload_url_path) {
|
|
$uploads['baseurl'] = $upload_url_path;
|
|
$uploads['url'] = $uploads['baseurl'] . $uploads['subdir'];
|
|
}
|
|
return $uploads;
|
|
}
|
|
add_filter('upload_dir', 'custom_upload_dir');
|
|
} |