Files
wordpress-kratos-yang/inc/options-framework/includes/class-options-media-uploader.php
T
laoyang 9da5e9e2df 后台代码配色预览不上色 + donate.png 404 一并修复 #14
Bug 1:后台预览的示例代码没有高亮。三层原因叠在一起。
  ① yang_hl_admin_assets() 从未 enqueue hljs 本体,而 yang_hl_enqueue()
     开头 `if (is_admin() || is_feed()) return;` 挡掉后台
     ⇒ 后台 window.hljs 恒 undefined ⇒ recolor() 静默 return。
     卡片的边框/顶栏是 data-kind 驱动的纯 CSS,切深浅配色时照样会变,
     看起来像"预览在工作,只是代码没颜色"。
  ② init() 里 applyTheme(sel.value, true) 传 noRecolor=true ⇒ 首屏永不上色,
     只有手动拨下拉框才有颜色(正是报障的"下面的示例代码并没有高亮")。
  ③ recolor() 缺 hljs 时只是 return,控制台零输出 ⇒ 极难定位。

  修法:
  - inc/theme-highlight.php:后台 enqueue hljs 本体,并作为 preview 脚本的
    硬依赖(不靠 WP 的加载顺序碰运气);前台 yang_hl_enqueue() 仍挡后台。
  - assets/js/highlight-admin.js:删掉 noRecolor 这个"能否跳过核心动作"的
    参数(调用方迟早会用错);首屏与切换走同一条路径。
  - 缺 hljs / 上色失败时 console.error 出声,并给 code 打
    data-unhighlighted、给卡片打 data-hl-failed;CSS 画橙色虚线框 +
    顶栏文字,让失败在画面上也看得出来,不再是一坨单色字。
  - 配色样式加载主路径改为 setTimeout(once, 125),link.onload 只作加速。
    onload 三情形不触发:样式表已在缓存、link 移除后重插、jsdom 无网络。

Bug 2:后台设置页报 donate.png 404。
  库里 g_donate_alipay / g_donate_wechat 存的是旧主题目录名
  `.../themes/kratos-master/assets/img/donate.png` 的绝对 URL。前台走
  yang_option_image() 有 yang_image_url_is_stale() 兜底,后台
  class-options-media-uploader.php 直接 echo $value,绕过检测。
  修在渲染处:预览图与"浏览文件"href 走检测后的 $preview(带
  function_exists 兜底),输入框仍显示库里的原值,不偷偷改用户数据。

版本 3.2.44 → 3.2.45。
2026-10-26 12:20:33 +08:00

149 lines
5.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* @package Options_Framework
* @author Devin Price <devin@wptheming.com>
* @license GPL-2.0+
* @link http://wptheming.com
* @copyright 2010-2014 WP Theming
*/
class Options_Framework_Media_Uploader {
/**
* Initialize the media uploader class
*
* @since 1.7.0
*/
public function init() {
add_action( 'admin_enqueue_scripts', array( $this, 'optionsframework_media_scripts' ) );
}
/**
* Media Uploader Using the WordPress Media Library.
*
* Parameters:
*
* string $_id - A token to identify this field (the name).
* string $_value - The value of the field, if present.
* string $_desc - An optional description of the field.
*
*/
static function optionsframework_uploader( $_id, $_value, $_desc = '', $_name = '' ) {
// Gets the unique option id
$options_framework = new Options_Framework;
$option_name = $options_framework->get_option_name();
$output = '';
$id = '';
$class = '';
$int = '';
$value = '';
$name = '';
$id = strip_tags( strtolower( $_id ) );
// If a value is passed and we don't have a stored value, use the value that's passed through.
if ( $_value != '' && $value == '' ) {
$value = $_value;
}
if ($_name != '') {
$name = $_name;
}
else {
$name = $option_name.'['.$id.']';
}
if ( $value ) {
$class = ' has-file';
}
$output .= '<input id="' . $id . '" class="upload' . $class . '" type="text" name="'.$name.'" value="' . $value . '" placeholder="' . __('没有选择任何文件', 'kratos') .'" />' . "\n";
if ( function_exists( 'wp_enqueue_media' ) ) {
if ( ( $value == '' ) ) {
$output .= '<input id="upload-' . $id . '" class="upload-button button" type="button" value="' . __( '上传', 'kratos' ) . '" />' . "\n";
} else {
$output .= '<input id="remove-' . $id . '" class="remove-file button" type="button" value="' . __( '删除', 'kratos' ) . '" />' . "\n";
}
} else {
$output .= '<p><i>' . __( '升级 WordPress 版本获得完整的媒体支持', 'kratos' ) . '</i></p>';
}
if ( $_desc != '' ) {
$output .= '<span class="of-metabox-desc">' . $_desc . '</span>' . "\n";
}
$output .= '<div class="screenshot" id="' . $id . '-image">' . "\n";
if ( $value != '' ) {
$remove = '<a class="remove-image">删除</a>';
/* 🔴 预览图必须先过一遍「陈旧地址」检查,不能把库里的值直接塞进 src。
*
* 3.2.45 实测:kratos 选项里 g_donate_alipay / g_donate_wechat
* 躺着 `https://站点/wp-content/themes/kratos-master/assets/img/donate.png`
* —— 很早以前保存表单时把当时的 std(绝对 URL)写进了库,
* 后来主题目录从 kratos-master 改名成 kratos_yang。
*
* 前台没事:模板走 yang_option_image(),那个函数里有
* yang_image_url_is_stale() 拦着,会落回默认图。
* 但**后台这里不走那个函数**,直接 echo $value ⇒
* 每打开一次选项页就往控制台报一次 donate.png 404。
*
* 为什么在渲染处修而不是"直接把库改掉":库是所有站点的,
* 而这段代码要保证**任何人**的库里有陈旧地址时后台都不报 404。
* 同时前台的行为一个字都不用变(它本来就有兜底)。
*
* ⚠️ 只影响预览图与"浏览文件"链接的 href,
* **输入框里仍然显示库里的原值** —— 那是用户能看到、
* 能自己改的地方。偷偷改显示值会让人以为设置被篡改了。 */
$preview = $value;
if ( function_exists( 'yang_image_url_is_stale' ) && yang_image_url_is_stale( $preview ) ) {
$preview = function_exists( 'yang_image_default' )
? yang_image_default( $id )
: '';
}
$image = preg_match( '/(^.*\.jpg|jpeg|png|gif|svg|ico*)/i', $preview );
if ( $image && $preview !== '' ) {
$output .= '<img src="' . esc_url( $preview ) . '" alt="" />' . $remove;
} else {
$parts = explode( "/", $preview );
for( $i = 0; $i < sizeof( $parts ); ++$i ) {
$title = $parts[$i];
}
// No output preview if it's not an image.
$output .= '';
// Standard generic output if it's not an image.
$title = __( '浏览文件', 'kratos' );
$output .= '<div class="no-image"><span class="file_link"><a href="' . esc_url( $preview ) . '" target="_blank" rel="external">'.$title.'</a></span></div>';
}
}
$output .= '</div>' . "\n";
return $output;
}
/**
* Enqueue scripts for file uploader
*/
function optionsframework_media_scripts( $hook ) {
$menu = Options_Framework_Admin::menu_settings();
if ( substr( $hook, -strlen( $menu['menu_slug'] ) ) !== $menu['menu_slug'] )
return;
if ( function_exists( 'wp_enqueue_media' ) )
wp_enqueue_media();
wp_register_script( 'of-media-uploader', get_stylesheet_directory_uri() . '/inc/options-framework/js/media-uploader.js', array( 'jquery' ), Options_Framework::VERSION );
wp_enqueue_script( 'of-media-uploader' );
wp_localize_script( 'of-media-uploader', 'optionsframework_l10n', array(
'upload' => __( '上传', 'kratos' ),
'remove' => __( '删除', 'kratos' )
) );
}
}