diff --git a/README.md b/README.md index a7fd6ae..3573279 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ theme-update/readme.html # 「详情」页,可省 **先改 `style.css` 里的 `Version`,再打包**,否则 WordPress 认为没有新版本,不会提示升级。 -1. 改 `style.css` 的 `Version`(例如 3.2.0 → 3.3.0) +1. 改 `style.css` 的 `Version`(例如 3.2.2 → 3.3.0) 2. 打包:`zip -r kratos_yang.zip kratos_yang -x "*.DS_Store" "*/.git/*"` 3. 传 `kratos_yang.zip` 到 `theme-update/` 4. **最后**才改 `update.json` 里的 `version` @@ -96,14 +96,27 @@ theme-update/readme.html # 「详情」页,可省 ## ⚠️ 自动升级会覆盖主题目录 -WordPress 升级主题的做法是**删掉整个 `kratos_yang/` 再解压新包**。 -所以任何直接改在服务器主题目录里的东西都会丢,包括: +WordPress 升级主题的做法是**删掉整个 `kratos_yang/` 再解压新包** +(`wp-admin/includes/class-theme-upgrader.php` 里 `clear_destination => true`, +随后 `delete_old_theme()` 递归删除)。所以任何直接改在服务器主题目录里的东西都会丢, +包括手工打的补丁(例如曾经直接在 `footer.php` 里加的 Prism 脚本)。 -- `custom/custom.css`、`custom/custom.js`(放本地自定义样式的常用位置) -- 手工打的补丁(例如曾经直接在 `footer.php` 里加的 Prism 脚本) +**自定义样式不再放主题目录。** 早期版本有一个 `custom/` 目录(`custom.css` / +`custom.js` / `custom.php`),是给用户随手加样式的入口,但它位于主题目录内, +每次升级都会被清空 —— 而且 `custom.php` 会被 `require`,等于允许在主题里执行任意 +PHP 代码。该模块已在 3.2.2 移除,改为后台选项: -把自定义内容放主题目录外,或者在升级后重新部署。本仓库的 `custom/` 已经纳入版本管理, -修改请改仓库再发布新版本,不要在服务器上直接编辑。 +> 后台 → 主题选项 → **自定义代码** → 自定义 CSS / 自定义 JS + +这两段内容存在数据库的 `kratos` 选项里,**升级、重装、改主题目录名都不受影响**。 +它们分别在主题样式表和主题脚本之后加载,优先级最高。 + +几个要点: + +- 不需要写 ``、`` 打断,避免提前闭合输出标签; + * 3. 其余内容原样返回(含反斜杠转义,如 CSS 的 `\f101`)。 + * + * @param $input string + * @return $output sanitized string + */ +function of_sanitize_code( $input ) { + if ( ! is_string( $input ) ) { + return ''; + } + $output = str_replace( array( '' ), '', $input ); + $output = preg_replace( '# home_url(), diff --git a/inc/theme-custom-code.php b/inc/theme-custom-code.php new file mode 100644 index 0000000..ef3b65a --- /dev/null +++ b/inc/theme-custom-code.php @@ -0,0 +1,68 @@ + + * @license MIT License + */ + +/** + * 默认自定义 CSS。 + * + * 历史沿革:这些规则原本放在主题目录下的一个 custom 子目录里,是该主题早期的 + * 「自定义样式」入口。改成配置项后,把它作为默认值,保证切换瞬间站点外观 + * 完全不变;同时 `恢复默认` 也能拿回这套样式。 + * + * 想彻底清掉:在后台把「自定义 CSS」清空保存即可(空值会覆盖这里的默认值)。 + */ +function yang_default_custom_css() +{ + return <<<'CSS' +.article .content p{ text-indent:2em } +.k-main .details .article .content h2 { + margin: 16px -16px; + padding: 0 10px; + border-left: 5px solid #00a2ff; + background-color: rgba(243, 243, 243, 0.7); + font-size: 20px; + line-height: 41px; +} +ul{ + margin-left:2em; +} +ol{ + margin-left:2em; +} +ol > li{ + line-height: 1.8; +} +ul > li{ + line-height: 1.8; +} + +CSS; +} + +/** + * 取自定义 CSS(未设置时回退到默认值) + */ +function yang_custom_css() +{ + $css = kratos_option('g_custom_css', yang_default_custom_css()); + return is_string($css) ? $css : ''; +} + +/** + * 取自定义 JS(未设置时为空) + */ +function yang_custom_js() +{ + $js = kratos_option('g_custom_js', ''); + return is_string($js) ? $js : ''; +} \ No newline at end of file diff --git a/inc/theme-options.php b/inc/theme-options.php index f0819ab..4e248ef 100644 --- a/inc/theme-options.php +++ b/inc/theme-options.php @@ -515,6 +515,28 @@ $options[] = array( 'type' => 'text', ); + $options[] = array( + 'name' => __('自定义代码', 'kratos'), + 'type' => 'heading', + ); + + $options[] = array( + 'name' => __('自定义 CSS', 'kratos'), + 'desc' => __('附加样式,在所有主题样式之后加载。保存后立即生效,随主题升级保留。', 'kratos'), + 'id' => 'g_custom_css', + 'std' => yang_default_custom_css(), + 'type' => 'code', + 'settings' => array('rows' => 14), + ); + + $options[] = array( + 'name' => __('自定义 JS', 'kratos'), + 'desc' => __('附加脚本,在页面底部加载,不需要写 <script> 标签。', 'kratos'), + 'id' => 'g_custom_js', + 'std' => '', + 'type' => 'code', + 'settings' => array('rows' => 14), + ); return $options; } diff --git a/style.css b/style.css index 5e892ee..35c90bb 100644 --- a/style.css +++ b/style.css @@ -1,7 +1,7 @@ /* Theme Name: Kratos_Yang Text Domain: kratos_Yang -Version: 3.2.1 +Version: 3.2.2 Requires PHP: 7.0 Description: 这是一个Kratos主题的分支版本,用来增加一些定制化功能。 Tags: 博客, 双栏, 响应式, 自定义背景, 自定义颜色, 自定义图标, 自定义菜单, 特色图像, 收录优化, 邮件中心, 主题选项, 小工具, 文章置顶, 无障碍友好