* @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 .= '' . "\n"; if ( function_exists( 'wp_enqueue_media' ) ) { if ( ( $value == '' ) ) { $output .= '' . "\n"; } else { $output .= '' . "\n"; } } else { $output .= '

' . __( '升级 WordPress 版本获得完整的媒体支持', 'kratos' ) . '

'; } if ( $_desc != '' ) { $output .= '' . $_desc . '' . "\n"; } $output .= '
' . "\n"; if ( $value != '' ) { $remove = '删除'; /* 🔴 预览图必须先过一遍「陈旧地址」检查,不能把库里的值直接塞进 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 .= '' . $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 .= '
'.$title.'
'; } } $output .= '
' . "\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' ) ) ); } }