list_core_update()
云策文档标注
概述
list_core_update() 函数用于列出可用的 WordPress 核心更新,并生成相应的更新界面元素。它处理版本字符串、语言本地化、PHP 和 MySQL 兼容性检查,以及更新按钮的显示逻辑。
关键要点
- 函数接受一个 $update 对象参数,包含更新信息,如版本、语言包和响应类型。
- 根据当前语言环境(get_locale())和更新语言($update->locale)动态构建版本字符串,处理英语和非英语版本的显示差异。
- 检查 PHP 版本(PHP_VERSION)和 MySQL 版本(通过 $wpdb->db_version())是否满足更新要求,若不兼容则显示错误消息并隐藏更新按钮。
- 针对开发版本(如 nightly builds)和常规版本,生成不同的更新提示和提交按钮文本。
- 支持更新操作的本地化警告,例如当安装英语版本可能破坏翻译时显示提示。
- 使用 wp_nonce_field() 添加安全 nonce 字段,并通过 submit_button() 输出更新、隐藏或恢复更新的按钮。
- 函数内部调用多个辅助函数,如 get_core_updates()、wp_get_wp_version() 和 wp_get_update_php_url(),以获取相关数据。
代码示例
// 示例调用(通常在更新核心界面中自动触发)
$updates = get_core_updates();
if ( $updates ) {
foreach ( $updates as $update ) {
list_core_update( $update );
}
}注意事项
- 此函数主要用于 WordPress 管理后台的更新界面(如 update-core.php),不建议在主题或插件中直接调用,除非有特定需求。
- 更新逻辑依赖于全局变量 $wp_local_package 和 $wpdb,确保在调用前这些变量已正确初始化。
- 版本兼容性检查是关键部分,开发者应确保服务器环境满足更新要求,以避免更新失败。
- 函数输出 HTML 内容,直接 echo 到页面,因此需在适当的钩子或模板中使用。
原文内容
Lists available core updates.
Parameters
$updateobjectrequired
Source
function list_core_update( $update ) {
global $wp_local_package, $wpdb;
static $first_pass = true;
$wp_version = wp_get_wp_version();
$version_string = sprintf( '%s–%s', $update->current, get_locale() );
if ( 'en_US' === $update->locale && 'en_US' === get_locale() ) {
$version_string = $update->current;
} elseif ( 'en_US' === $update->locale && $update->packages->partial && $wp_version === $update->partial_version ) {
$updates = get_core_updates();
if ( $updates && 1 === count( $updates ) ) {
// If the only available update is a partial builds, it doesn't need a language-specific version string.
$version_string = $update->current;
}
} elseif ( 'en_US' === $update->locale && 'en_US' !== get_locale() ) {
$version_string = sprintf( '%s–%s', $update->current, $update->locale );
}
$current = false;
if ( ! isset( $update->response ) || 'latest' === $update->response ) {
$current = true;
}
$message = '';
$form_action = 'update-core.php?action=do-core-upgrade';
$php_version = PHP_VERSION;
$mysql_version = $wpdb->db_version();
$show_buttons = true;
// Nightly build versions have two hyphens and a commit number.
if ( preg_match( '/-w+-d+/', $update->current ) ) {
// Retrieve the major version number.
preg_match( '/^d+.d+/', $update->current, $update_major );
/* translators: %s: WordPress version. */
$submit = sprintf( __( 'Update to latest %s nightly' ), $update_major[0] );
} else {
/* translators: %s: WordPress version. */
$submit = sprintf( __( 'Update to version %s' ), $version_string );
}
if ( 'development' === $update->response ) {
$message = __( 'You can update to the latest nightly build manually:' );
} else {
if ( $current ) {
/* translators: %s: WordPress version. */
$submit = sprintf( __( 'Re-install version %s' ), $version_string );
$form_action = 'update-core.php?action=do-core-reinstall';
} else {
$php_compat = version_compare( $php_version, $update->php_version, '>=' );
if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
$mysql_compat = true;
} else {
$mysql_compat = version_compare( $mysql_version, $update->mysql_version, '>=' );
}
$version_url = sprintf(
/* translators: %s: WordPress version. */
esc_url( __( 'https://wordpress.org/documentation/wordpress-version/version-%s/' ) ),
sanitize_title( $update->current )
);
$php_update_message = '</p><p>' . sprintf(
/* translators: %s: URL to Update PHP page. */
__( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
);
$annotation = wp_get_update_php_annotation();
if ( $annotation ) {
$php_update_message .= '</p><p><em>' . $annotation . '</em>';
}
if ( ! $mysql_compat && ! $php_compat ) {
$message = sprintf(
/* translators: 1: URL to WordPress release notes, 2: WordPress version number, 3: Minimum required PHP version number, 4: Minimum required MySQL version number, 5: Current PHP version number, 6: Current MySQL version number. */
__( 'You cannot update because <a href="%1$s">WordPress %2$s</a> requires PHP version %3$s or higher and MySQL version %4$s or higher. You are running PHP version %5$s and MySQL version %6$s.' ),
$version_url,
$update->current,
$update->php_version,
$update->mysql_version,
$php_version,
$mysql_version
) . $php_update_message;
} elseif ( ! $php_compat ) {
$message = sprintf(
/* translators: 1: URL to WordPress release notes, 2: WordPress version number, 3: Minimum required PHP version number, 4: Current PHP version number. */
__( 'You cannot update because <a href="%1$s">WordPress %2$s</a> requires PHP version %3$s or higher. You are running version %4$s.' ),
$version_url,
$update->current,
$update->php_version,
$php_version
) . $php_update_message;
} elseif ( ! $mysql_compat ) {
$message = sprintf(
/* translators: 1: URL to WordPress release notes, 2: WordPress version number, 3: Minimum required MySQL version number, 4: Current MySQL version number. */
__( 'You cannot update because <a href="%1$s">WordPress %2$s</a> requires MySQL version %3$s or higher. You are running version %4$s.' ),
$version_url,
$update->current,
$update->mysql_version,
$mysql_version
);
} else {
$message = sprintf(
/* translators: 1: Installed WordPress version number, 2: URL to WordPress release notes, 3: New WordPress version number, including locale if necessary. */
__( 'You can update from WordPress %1$s to <a href="%2$s">WordPress %3$s</a> manually:' ),
$wp_version,
$version_url,
$version_string
);
}
if ( ! $mysql_compat || ! $php_compat ) {
$show_buttons = false;
}
}
}
echo '<p>';
echo $message;
echo '</p>';
echo '<form method="post" action="' . esc_url( $form_action ) . '" name="upgrade" class="upgrade">';
wp_nonce_field( 'upgrade-core' );
echo '<p>';
echo '<input name="version" value="' . esc_attr( $update->current ) . '" type="hidden" />';
echo '<input name="locale" value="' . esc_attr( $update->locale ) . '" type="hidden" />';
if ( $show_buttons ) {
if ( $first_pass ) {
submit_button( $submit, $current ? '' : 'primary regular', 'upgrade', false );
$first_pass = false;
} else {
submit_button( $submit, '', 'upgrade', false );
}
}
if ( 'en_US' !== $update->locale ) {
if ( ! isset( $update->dismissed ) || ! $update->dismissed ) {
submit_button( __( 'Hide this update' ), '', 'dismiss', false );
} else {
submit_button( __( 'Bring back this update' ), '', 'undismiss', false );
}
}
echo '</p>';
if ( 'en_US' !== $update->locale && ( ! isset( $wp_local_package ) || $wp_local_package !== $update->locale ) ) {
echo '<p class="hint">' . __( 'This localized version contains both the translation and various other localization fixes.' ) . '</p>';
} elseif ( 'en_US' === $update->locale && 'en_US' !== get_locale() && ( ! $update->packages->partial && $wp_version === $update->partial_version ) ) {
// Partial builds don't need language-specific warnings.
echo '<p class="hint">' . sprintf(
/* translators: %s: WordPress version. */
__( 'You are about to install WordPress %s <strong>in English (US)</strong>. There is a chance this update will break your translation. You may prefer to wait for the localized version to be released.' ),
'development' !== $update->response ? $update->current : ''
) . '</p>';
}
echo '</form>';
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |