_wp_to_kebab_case

函数
_wp_to_kebab_case ( $string )
参数
  • (string) $string The string to kebab-case.
    Required:
返回值
  • (string) kebab-cased-string.
定义位置
相关方法
_wp_sidebars_changedwp_revoke_userwp_restore_imagewp_list_cats_wp_customize_publish_changeset
引入
-
弃用
-

_wp_to_kebab_case: 这个函数将一个字符串转换为kebab大小写格式(小写的连字符而不是空格)。它用于将lug或taxonomy名称转换为URL友好格式。

这个函数试图复制lodash的kebabCase(JS库)在客户端的作用。

我们需要这个函数的原因是,我们在客户端和服务器中都做了一些处理(例如:我们从预设的slug中生成预设的类),需要创建相同的输出。

由于向后兼容,我们不能删除或更新客户端的库(lodash的kebabCase的一些输出被保存在文章内容中)。我们必须使服务器的行为与客户端一样。

对这个函数的修改应该跟随客户端的更新,其逻辑也是一样的。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _wp_to_kebab_case( $string ) {
//phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
// ignore the camelCase names for variables so the names are the same as lodash
// so comparing and porting new changes is easier.
/*
* Some notable things we've removed compared to the lodash version are:
*
* - non-alphanumeric characters: rsAstralRange, rsEmoji, etc
* - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper
*
*/
/** Used to compose unicode character classes. */
$rsLowerRange = 'a-z\xdf-\xf6\xf8-\xff';
$rsNonCharRange = '\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf';
$rsPunctuationRange = '\x{2000}-\x{206f}';
$rsSpaceRange = ' \t\x0b\f\xa0\x{feff}\n\r\x{2028}\x{2029}\x{1680}\x{180e}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200a}\x{202f}\x{205f}\x{3000}';
$rsUpperRange = 'A-Z\xc0-\xd6\xd8-\xde';
$rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;
/** Used to compose unicode capture groups. */
$rsBreak = '[' . $rsBreakRange . ']';
$rsDigits = '\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use.
$rsLower = '[' . $rsLowerRange . ']';
$rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']';
$rsUpper = '[' . $rsUpperRange . ']';
/** Used to compose unicode regexes. */
$rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')';
$rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')';
$rsOrdLower = '\d*(?:1st|2nd|3rd|(?![123])\dth)(?=\b|[A-Z_])';
$rsOrdUpper = '\d*(?:1ST|2ND|3RD|(?![123])\dTH)(?=\b|[a-z_])';
$regexp = '/' . implode(
'|',
array(
$rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')',
$rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')',
$rsUpper . '?' . $rsMiscLower . '+',
$rsUpper . '+',
$rsOrdUpper,
$rsOrdLower,
$rsDigits,
)
) . '/u';
preg_match_all( $regexp, str_replace( "'", '', $string ), $matches );
return strtolower( implode( '-', $matches[0] ) );
//phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}
function _wp_to_kebab_case( $string ) { //phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase // ignore the camelCase names for variables so the names are the same as lodash // so comparing and porting new changes is easier. /* * Some notable things we've removed compared to the lodash version are: * * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper * */ /** Used to compose unicode character classes. */ $rsLowerRange = 'a-z\xdf-\xf6\xf8-\xff'; $rsNonCharRange = '\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf'; $rsPunctuationRange = '\x{2000}-\x{206f}'; $rsSpaceRange = ' \t\x0b\f\xa0\x{feff}\n\r\x{2028}\x{2029}\x{1680}\x{180e}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200a}\x{202f}\x{205f}\x{3000}'; $rsUpperRange = 'A-Z\xc0-\xd6\xd8-\xde'; $rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange; /** Used to compose unicode capture groups. */ $rsBreak = '[' . $rsBreakRange . ']'; $rsDigits = '\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use. $rsLower = '[' . $rsLowerRange . ']'; $rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']'; $rsUpper = '[' . $rsUpperRange . ']'; /** Used to compose unicode regexes. */ $rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')'; $rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')'; $rsOrdLower = '\d*(?:1st|2nd|3rd|(?![123])\dth)(?=\b|[A-Z_])'; $rsOrdUpper = '\d*(?:1ST|2ND|3RD|(?![123])\dTH)(?=\b|[a-z_])'; $regexp = '/' . implode( '|', array( $rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')', $rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')', $rsUpper . '?' . $rsMiscLower . '+', $rsUpper . '+', $rsOrdUpper, $rsOrdLower, $rsDigits, ) ) . '/u'; preg_match_all( $regexp, str_replace( "'", '', $string ), $matches ); return strtolower( implode( '-', $matches[0] ) ); //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase }
function _wp_to_kebab_case( $string ) {
	//phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
	// ignore the camelCase names for variables so the names are the same as lodash
	// so comparing and porting new changes is easier.

	/*
	 * Some notable things we've removed compared to the lodash version are:
	 *
	 * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc
	 * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper
	 *
	 */

	/** Used to compose unicode character classes. */
	$rsLowerRange       = 'a-z\xdf-\xf6\xf8-\xff';
	$rsNonCharRange     = '\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf';
	$rsPunctuationRange = '\x{2000}-\x{206f}';
	$rsSpaceRange       = ' \t\x0b\f\xa0\x{feff}\n\r\x{2028}\x{2029}\x{1680}\x{180e}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200a}\x{202f}\x{205f}\x{3000}';
	$rsUpperRange       = 'A-Z\xc0-\xd6\xd8-\xde';
	$rsBreakRange       = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;

	/** Used to compose unicode capture groups. */
	$rsBreak  = '[' . $rsBreakRange . ']';
	$rsDigits = '\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use.
	$rsLower  = '[' . $rsLowerRange . ']';
	$rsMisc   = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']';
	$rsUpper  = '[' . $rsUpperRange . ']';

	/** Used to compose unicode regexes. */
	$rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')';
	$rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')';
	$rsOrdLower  = '\d*(?:1st|2nd|3rd|(?![123])\dth)(?=\b|[A-Z_])';
	$rsOrdUpper  = '\d*(?:1ST|2ND|3RD|(?![123])\dTH)(?=\b|[a-z_])';

	$regexp = '/' . implode(
		'|',
		array(
			$rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')',
			$rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')',
			$rsUpper . '?' . $rsMiscLower . '+',
			$rsUpper . '+',
			$rsOrdUpper,
			$rsOrdLower,
			$rsDigits,
		)
	) . '/u';

	preg_match_all( $regexp, str_replace( "'", '', $string ), $matches );
	return strtolower( implode( '-', $matches[0] ) );
	//phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
}

常见问题

FAQs
查看更多 >