2022-01-07 11:15:42 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @package Options_Framework
|
|
|
|
|
|
* @author Devin Price <devin@wptheming.com>
|
|
|
|
|
|
* @license GPL-2.0+
|
|
|
|
|
|
* @link http://wptheming.com
|
|
|
|
|
|
* @copyright 2010-2014 WP Theming
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for text input
|
|
|
|
|
|
*
|
|
|
|
|
|
* @link http://developer.wordpress.org/reference/functions/sanitize_text_field/
|
|
|
|
|
|
*/
|
|
|
|
|
|
add_filter( 'of_sanitize_text', 'sanitize_text_field' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for password input
|
|
|
|
|
|
*
|
|
|
|
|
|
* @link http://developer.wordpress.org/reference/functions/sanitize_text_field/
|
|
|
|
|
|
*/
|
|
|
|
|
|
add_filter( 'of_sanitize_password', 'sanitize_text_field' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for select input
|
|
|
|
|
|
*
|
|
|
|
|
|
* Validates that the selected option is a valid option.
|
|
|
|
|
|
*/
|
|
|
|
|
|
add_filter( 'of_sanitize_select', 'of_sanitize_enum', 10, 2 );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for radio input
|
|
|
|
|
|
*
|
|
|
|
|
|
* Validates that the selected option is a valid option.
|
|
|
|
|
|
*/
|
|
|
|
|
|
add_filter( 'of_sanitize_radio', 'of_sanitize_enum', 10, 2 );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for image selector
|
|
|
|
|
|
*
|
|
|
|
|
|
* Validates that the selected option is a valid option.
|
|
|
|
|
|
*/
|
|
|
|
|
|
add_filter( 'of_sanitize_images', 'of_sanitize_enum', 10, 2 );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for textarea field
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param $input string
|
|
|
|
|
|
* @return $output sanitized string
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_textarea( $input ) {
|
|
|
|
|
|
global $allowedposttags;
|
|
|
|
|
|
$output = wp_kses( $input, $allowedposttags );
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_textarea', 'of_sanitize_textarea' );
|
|
|
|
|
|
|
2026-10-23 10:35:56 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for raw code fields (theme's own and user's CSS / JS).
|
|
|
|
|
|
*
|
|
|
|
|
|
* 为什么不能复用 textarea:textarea 走 wp_kses(),CSS 里的子选择器
|
|
|
|
|
|
* `>`、`+`、`~` 以及 JS 里的比较运算符 `<`、`>` 都会被当成 HTML 标签
|
|
|
|
|
|
* 剥掉。例:`a > b { }` 存进库会变成 `a b { }`,样式静默失效。
|
|
|
|
|
|
* 所以这里只做三件必要的事:
|
|
|
|
|
|
* 1. 去掉 PHP 标签,避免这段文本将来被 eval / include 时执行代码;
|
|
|
|
|
|
* 2. 把 `</style>`、`</script>` 打断,避免提前闭合输出标签;
|
|
|
|
|
|
* 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( '#</\s*(style|script)#i', '<\\/$1', $output );
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_code', 'of_sanitize_code' );
|
|
|
|
|
|
|
2022-01-07 11:15:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for checkbox input
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param $input string (1 or empty) checkbox state
|
|
|
|
|
|
* @return $output '1' or false
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_checkbox( $input ) {
|
|
|
|
|
|
if ( $input ) {
|
|
|
|
|
|
$output = '1';
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$output = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_checkbox', 'of_sanitize_checkbox' );
|
|
|
|
|
|
|
2026-10-23 13:55:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for multicheck input
|
|
|
|
|
|
*
|
|
|
|
|
|
* 保存成 array( 键 => 1|0 ),键表来自选项定义里的 options。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 关键点:**所有已知键都要出现在结果里**,未勾选的写 0。
|
|
|
|
|
|
* 如果只保留勾选的键,用户取消全部勾选后存进去是空数组,
|
|
|
|
|
|
* 读取时无法与「这个选项从未配置过」区分,会静默回退到默认值,
|
|
|
|
|
|
* 表现成「取消勾选不生效」。这是同类字段最常见的 bug。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 输入的键先与选项定义里的白名单求交集,防止用户 POST 伪造任意键。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param array $input 提交上来的值
|
|
|
|
|
|
* @param array $option 选项定义(含 options 白名单)
|
|
|
|
|
|
* @return array
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_multicheck( $input, $option ) {
|
|
|
|
|
|
$output = array();
|
|
|
|
|
|
if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
$input = is_array( $input ) ? $input : array();
|
|
|
|
|
|
foreach ( $option['options'] as $key => $value ) {
|
|
|
|
|
|
// 只认白名单里的键;值为真即视为勾选
|
|
|
|
|
|
$output[ $key ] = ( isset( $input[ $key ] ) && $input[ $key ] ) ? 1 : 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_multicheck', 'of_sanitize_multicheck', 10, 2 );
|
|
|
|
|
|
|
2022-01-07 11:15:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* File upload sanitization.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Returns a sanitized filepath if it has a valid extension.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $input filepath
|
|
|
|
|
|
* @returns string $output filepath
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_upload( $input ) {
|
|
|
|
|
|
$output = '';
|
|
|
|
|
|
$filetype = wp_check_filetype( $input );
|
|
|
|
|
|
if ( $filetype["ext"] ) {
|
|
|
|
|
|
$output = esc_url( $input );
|
|
|
|
|
|
}
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_upload', 'of_sanitize_upload' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization of input with allowed tags and wpautotop.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Allows allowed tags in html input and ensures tags close properly.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $input
|
|
|
|
|
|
* @returns string $output
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_allowedtags( $input ) {
|
|
|
|
|
|
global $allowedtags;
|
|
|
|
|
|
$output = wpautop( wp_kses( $input, $allowedtags ) );
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization of input with allowed post tags and wpautotop.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Allows allowed post tags in html input and ensures tags close properly.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $input
|
|
|
|
|
|
* @returns string $output
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_allowedposttags( $input ) {
|
|
|
|
|
|
global $allowedposttags;
|
|
|
|
|
|
$output = wpautop( wp_kses( $input, $allowedposttags) );
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Validates that the $input is one of the avilable choices
|
|
|
|
|
|
* for that specific option.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string $input
|
|
|
|
|
|
* @returns string $output
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_enum( $input, $option ) {
|
|
|
|
|
|
$output = '';
|
|
|
|
|
|
if ( array_key_exists( $input, $option['options'] ) ) {
|
|
|
|
|
|
$output = $input;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitization for background option.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @returns array $output
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_sanitize_background( $input ) {
|
|
|
|
|
|
|
|
|
|
|
|
$output = wp_parse_args( $input, array(
|
|
|
|
|
|
'color' => '',
|
|
|
|
|
|
'image' => '',
|
|
|
|
|
|
'repeat' => 'repeat',
|
|
|
|
|
|
'position' => 'top center',
|
|
|
|
|
|
'attachment' => 'scroll'
|
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
|
|
$output['color'] = apply_filters( 'of_sanitize_hex', $input['color'] );
|
|
|
|
|
|
$output['image'] = apply_filters( 'of_sanitize_upload', $input['image'] );
|
|
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_background', 'of_sanitize_background' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Sanitize a color represented in hexidecimal notation.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
|
|
|
|
|
|
* @param string The value that this function should return if it cannot be recognized as a color.
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
function of_sanitize_hex( $hex, $default = '' ) {
|
|
|
|
|
|
if ( of_validate_hex( $hex ) ) {
|
|
|
|
|
|
return $hex;
|
|
|
|
|
|
}
|
|
|
|
|
|
return $default;
|
|
|
|
|
|
}
|
|
|
|
|
|
add_filter( 'of_sanitize_color', 'of_sanitize_hex' );
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Is a given string a color formatted in hexidecimal notation?
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param string Color in hexidecimal notation. "#" may or may not be prepended to the string.
|
|
|
|
|
|
* @return bool
|
|
|
|
|
|
*/
|
|
|
|
|
|
function of_validate_hex( $hex ) {
|
|
|
|
|
|
$hex = trim( $hex );
|
|
|
|
|
|
/* Strip recognized prefixes. */
|
|
|
|
|
|
if ( 0 === strpos( $hex, '#' ) ) {
|
|
|
|
|
|
$hex = substr( $hex, 1 );
|
|
|
|
|
|
}
|
|
|
|
|
|
elseif ( 0 === strpos( $hex, '%23' ) ) {
|
|
|
|
|
|
$hex = substr( $hex, 3 );
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Regex match. */
|
|
|
|
|
|
if ( 0 === preg_match( '/^[0-9a-fA-F]{6}$/', $hex ) ) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|