函数文档

_wp_timezone_choice_usort_callback()

💡 云策文档标注

概述

_wp_timezone_choice_usort_callback() 是 WordPress 中用于时区排序的回调函数,作为 usort() 的辅助函数,处理时区数组的排序逻辑。

关键要点

  • 函数用于对时区数组进行排序,支持自定义排序规则,如将 Etc 时区置于列表底部。
  • 参数 $a 和 $b 为必需数组,包含时区信息(如 continent、city、t_continent、t_city、t_subcity)。
  • 返回值为整数,表示排序顺序(负值表示 $a 在前,正值表示 $b 在前,0 表示相等)。
  • 排序逻辑优先处理 Etc 时区,确保 UTC 和 GMT+ 时区的特定顺序,并使用 strnatcasecmp 进行自然排序。

代码示例

function _wp_timezone_choice_usort_callback( $a, $b ) {
    // Don't use translated versions of Etc.
    if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
        // Make the order of these more like the old dropdown.
        if ( str_starts_with( $a['city'], 'GMT+' ) && str_starts_with( $b['city'], 'GMT+' ) ) {
            return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
        }

        if ( 'UTC' === $a['city'] ) {
            if ( str_starts_with( $b['city'], 'GMT+' ) ) {
                return 1;
            }

            return -1;
        }

        if ( 'UTC' === $b['city'] ) {
            if ( str_starts_with( $a['city'], 'GMT+' ) ) {
                return -1;
            }

            return 1;
        }

        return strnatcasecmp( $a['city'], $b['city'] );
    }

    if ( $a['t_continent'] === $b['t_continent'] ) {
        if ( $a['t_city'] === $b['t_city'] ) {
            return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
        }

        return strnatcasecmp( $a['t_city'], $b['t_city'] );
    } else {
        // Force Etc to the bottom of the list.
        if ( 'Etc' === $a['continent'] ) {
            return 1;
        }

        if ( 'Etc' === $b['continent'] ) {
            return -1;
        }

        return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
    }
}

注意事项

  • 此函数自 WordPress 2.9.0 版本引入,主要用于内部时区选择下拉列表的排序。
  • 开发者在使用时需确保传入的数组包含正确的键(如 continent、city 等),否则可能导致排序错误。
  • 函数逻辑依赖于特定时区命名约定(如 Etc、UTC、GMT+),修改时区数据时需注意兼容性。

📄 原文内容

Sort-helper for timezones.

Parameters

$aarrayrequired
$barrayrequired

Return

int

Source

function _wp_timezone_choice_usort_callback( $a, $b ) {
	// Don't use translated versions of Etc.
	if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
		// Make the order of these more like the old dropdown.
		if ( str_starts_with( $a['city'], 'GMT+' ) && str_starts_with( $b['city'], 'GMT+' ) ) {
			return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
		}

		if ( 'UTC' === $a['city'] ) {
			if ( str_starts_with( $b['city'], 'GMT+' ) ) {
				return 1;
			}

			return -1;
		}

		if ( 'UTC' === $b['city'] ) {
			if ( str_starts_with( $a['city'], 'GMT+' ) ) {
				return -1;
			}

			return 1;
		}

		return strnatcasecmp( $a['city'], $b['city'] );
	}

	if ( $a['t_continent'] === $b['t_continent'] ) {
		if ( $a['t_city'] === $b['t_city'] ) {
			return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
		}

		return strnatcasecmp( $a['t_city'], $b['t_city'] );
	} else {
		// Force Etc to the bottom of the list.
		if ( 'Etc' === $a['continent'] ) {
			return 1;
		}

		if ( 'Etc' === $b['continent'] ) {
			return -1;
		}

		return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
	}
}

Changelog

Version Description
2.9.0 Introduced.