xfn_check()
云策文档标注
概述
函数 xfn_check() 用于为 XFN 微格式选项输出 'checked' 复选框属性,基于当前链接的 XFN 关系进行条件判断。它处理不同的 XFN 关系类别,如 'friendship'、'family' 等,并支持指定值匹配或默认逻辑。
关键要点
- 函数参数包括 $xfn_relationship(必需,指定 XFN 关系类别)、$xfn_value(可选,指定要标记为 checked 的 XFN 值)和 $deprecated(可选,已弃用)。
- 当 $xfn_value 不为空且匹配当前链接的 link_rel 时,输出 checked="checked" 属性。
- 当 $xfn_value 为空时,根据 $xfn_relationship 执行特定逻辑:例如,对于 'family' 类别,如果当前链接不包含相关值(如 'child'、'parent'),则输出 checked。
- 函数内部使用全局变量 $link 获取链接关系,并通过 preg_split 分割 link_rel 字符串。
- 包含弃用参数处理,调用 _deprecated_argument() 函数。
代码示例
function xfn_check( $xfn_relationship, $xfn_value = '', $deprecated = '' ) {
global $link;
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.5.0' ); // Never implemented.
}
$link_rel = isset( $link->link_rel ) ? $link->link_rel : '';
$link_rels = preg_split( '/s+/', $link_rel );
// Mark the specified value as checked if it matches the current link's relationship.
if ( '' !== $xfn_value && in_array( $xfn_value, $link_rels, true ) ) {
echo ' checked="checked"';
}
if ( '' === $xfn_value ) {
// Mark the 'none' value as checked if the current link does not match the specified relationship.
if ( 'family' === $xfn_relationship
&& ! array_intersect( $link_rels, array( 'child', 'parent', 'sibling', 'spouse', 'kin' ) )
) {
echo ' checked="checked"';
}
if ( 'friendship' === $xfn_relationship
&& ! array_intersect( $link_rels, array( 'friend', 'acquaintance', 'contact' ) )
) {
echo ' checked="checked"';
}
if ( 'geographical' === $xfn_relationship
&& ! array_intersect( $link_rels, array( 'co-resident', 'neighbor' ) )
) {
echo ' checked="checked"';
}
// Mark the 'me' value as checked if it matches the current link's relationship.
if ( 'identity' === $xfn_relationship
&& in_array( 'me', $link_rels, true )
) {
echo ' checked="checked"';
}
}
}
原文内容
Displays ‘checked’ checkboxes attribute for XFN microformat options.
Parameters
$xfn_relationshipstringrequired-
XFN relationship category. Possible values are:
'friendship','physical','professional','geographical','family','romantic','identity'. $xfn_valuestringoptional-
The XFN value to mark as checked if it matches the current link’s relationship.
Default empty string. $deprecatedmixedoptional-
Deprecated. Not used.
Source
function xfn_check( $xfn_relationship, $xfn_value = '', $deprecated = '' ) {
global $link;
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '2.5.0' ); // Never implemented.
}
$link_rel = isset( $link->link_rel ) ? $link->link_rel : '';
$link_rels = preg_split( '/s+/', $link_rel );
// Mark the specified value as checked if it matches the current link's relationship.
if ( '' !== $xfn_value && in_array( $xfn_value, $link_rels, true ) ) {
echo ' checked="checked"';
}
if ( '' === $xfn_value ) {
// Mark the 'none' value as checked if the current link does not match the specified relationship.
if ( 'family' === $xfn_relationship
&& ! array_intersect( $link_rels, array( 'child', 'parent', 'sibling', 'spouse', 'kin' ) )
) {
echo ' checked="checked"';
}
if ( 'friendship' === $xfn_relationship
&& ! array_intersect( $link_rels, array( 'friend', 'acquaintance', 'contact' ) )
) {
echo ' checked="checked"';
}
if ( 'geographical' === $xfn_relationship
&& ! array_intersect( $link_rels, array( 'co-resident', 'neighbor' ) )
) {
echo ' checked="checked"';
}
// Mark the 'me' value as checked if it matches the current link's relationship.
if ( 'identity' === $xfn_relationship
&& in_array( 'me', $link_rels, true )
) {
echo ' checked="checked"';
}
}
}
Changelog
| Version | Description |
|---|---|
| 1.0.1 | Introduced. |