list_plugin_updates()
概述
list_plugin_updates() 函数用于在 WordPress 后台显示插件升级表单,检查并列出所有可更新的插件,同时处理兼容性信息和用户界面。
关键要点
- 函数通过 get_plugin_updates() 获取有更新的插件列表,若无更新则显示提示信息。
- 检查 WordPress 核心更新状态,以确定插件与当前及更新后版本的兼容性。
- 为每个插件生成详细信息,包括图标、兼容性(针对当前和更新后的 WordPress 版本)、PHP 版本要求、升级通知和详情链接。
- 输出包含复选框的表单,允许用户选择要升级的插件,并包含安全性和本地化处理。
代码示例
// 示例调用:在后台页面中直接使用 list_plugin_updates() 函数
list_plugin_updates();注意事项
- 函数依赖于多个 WordPress 核心函数,如 get_plugin_updates() 和 get_core_updates(),确保这些函数可用。
- 兼容性检查包括 WordPress 版本和 PHP 版本,需注意 is_php_version_compatible() 的使用。
- 输出内容包含 HTML 表单和国际化字符串,适合在管理界面使用。
Display the upgrade plugins form.
Source
function list_plugin_updates() {
$wp_version = wp_get_wp_version();
$cur_wp_version = preg_replace( '/-.*$/', '', $wp_version );
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
$plugins = get_plugin_updates();
if ( empty( $plugins ) ) {
echo '<h2>' . __( 'Plugins' ) . '</h2>';
echo '<p>' . __( 'Your plugins are all up to date.' ) . '</p>';
return;
}
$form_action = 'update-core.php?action=do-plugin-upgrade';
$core_updates = get_core_updates();
if ( ! isset( $core_updates[0]->response ) || 'latest' === $core_updates[0]->response || 'development' === $core_updates[0]->response || version_compare( $core_updates[0]->current, $cur_wp_version, '=' ) ) {
$core_update_version = false;
} else {
$core_update_version = $core_updates[0]->current;
}
$plugins_count = count( $plugins );
?>
<h2>
(%d)</span>',
__( 'Plugins' ),
number_format_i18n( $plugins_count )
);
?>
</h2>
<p></p>
<form method="post" action="<?php echo esc_url( $form_action ); ?>" name="upgrade-plugins" class="upgrade">
<p><input id="upgrade-plugins" class="button" type="submit" value="<?php esc_attr_e( 'Update Plugins' ); ?>" name="upgrade" /></p>
<table class="widefat updates-table" id="update-plugins-table">
<thead>
<tr>
<td class="manage-column check-column"><input type="checkbox" id="plugins-select-all" /></td>
<td class="manage-column"><label for="plugins-select-all"></label></td>
</tr>
</thead>
<tbody class="plugins">
$plugin_data ) {
$plugin_data = (object) _get_plugin_data_markup_translate( $plugin_file, (array) $plugin_data, false, true );
$icon = '<span class="dashicons dashicons-admin-plugins"></span>';
$preferred_icons = array( 'svg', '2x', '1x', 'default' );
foreach ( $preferred_icons as $preferred_icon ) {
if ( ! empty( $plugin_data->update->icons[ $preferred_icon ] ) ) {
$icon = '<img src="' . esc_url( $plugin_data->update->icons[ $preferred_icon ] ) . '" alt="" class="plugin-icon" />';
break;
}
}
// Get plugin compat for running version of WordPress.
if ( isset( $plugin_data->update->tested ) && version_compare( $plugin_data->update->tested, $cur_wp_version, '>=' ) ) {
/* translators: %s: WordPress version. */
$compat = '<br />' . sprintf( __( 'Compatibility with WordPress %s: 100%% (according to its author)' ), $cur_wp_version );
} else {
/* translators: %s: WordPress version. */
$compat = '<br />' . sprintf( __( 'Compatibility with WordPress %s: Unknown' ), $cur_wp_version );
}
// Get plugin compat for updated version of WordPress.
if ( $core_update_version ) {
if ( isset( $plugin_data->update->tested ) && version_compare( $plugin_data->update->tested, $core_update_version, '>=' ) ) {
/* translators: %s: WordPress version. */
$compat .= '<br />' . sprintf( __( 'Compatibility with WordPress %s: 100%% (according to its author)' ), $core_update_version );
} else {
/* translators: %s: WordPress version. */
$compat .= '<br />' . sprintf( __( 'Compatibility with WordPress %s: Unknown' ), $core_update_version );
}
}
$requires_php = isset( $plugin_data->update->requires_php ) ? $plugin_data->update->requires_php : null;
$compatible_php = is_php_version_compatible( $requires_php );
if ( ! $compatible_php && current_user_can( 'update_php' ) ) {
$compat .= '<br />' . __( 'This update does not work with your version of PHP.' ) . ' ';
$compat .= 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 ) {
$compat .= '</p><p><em>' . $annotation . '</em>';
}
}
// Get the upgrade notice for the new plugin version.
if ( isset( $plugin_data->update->upgrade_notice ) ) {
$upgrade_notice = '<br />' . strip_tags( $plugin_data->update->upgrade_notice );
} else {
$upgrade_notice = '';
}
$details_url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin;=' . $plugin_data->update->slug . '§ion;=changelog&TB;_iframe=true&width;=640&height;=662' );
$details = sprintf(
'<a href="%1$s" class="thickbox open-plugin-details-modal" aria-label="%2$s">%3$s</a>',
esc_url( $details_url ),
/* translators: 1: Plugin name, 2: Version number. */
esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_data->Name, $plugin_data->update->new_version ) ),
/* translators: %s: Plugin version. */
sprintf( __( 'View version %s details.' ), $plugin_data->update->new_version )
);
$checkbox_id = 'checkbox_' . md5( $plugin_file );
?>
<tr>
<td class="check-column">
<input type="checkbox" name="checked[]" id="<?php echo $checkbox_id; ?>" value="<?php echo esc_attr( $plugin_file ); ?>" />
<label for="<?php echo $checkbox_id; ?>">
<span class="screen-reader-text">
Name );
?>
</span>
</label>
</td>
<td class="plugin-title"><p>
<strong>Name; ?></strong>
Version,
$plugin_data->update->new_version
);
echo ' ' . $details . $compat;
if ( in_array( $plugin_file, $auto_updates, true ) ) {
echo $auto_update_notice;
}
echo $upgrade_notice;
?>
</p></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="manage-column check-column"><input type="checkbox" id="plugins-select-all-2" /></td>
<td class="manage-column"><label for="plugins-select-all-2"></label></td>
</tr>
</tfoot>
</table>
<p><input id="upgrade-plugins-2" class="button" type="submit" value="<?php esc_attr_e( 'Update Plugins' ); ?>" name="upgrade" /></p>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/update-core.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/update-core.php#L462">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/update-core.php#L462-L632">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_get_wp_version/">wp_get_wp_version()</a><code>wp-includes/functions.php
Returns the current WordPress version.
wp_is_auto_update_enabled_for_type()wp-admin/includes/update.php
Checks whether auto-updates are enabled.
wp_get_auto_update_message()wp-admin/includes/update.php
Determines the appropriate auto-update message to be displayed.
is_php_version_compatible()wp-includes/functions.php
Checks compatibility with the current PHP version.
wp_get_update_php_annotation()wp-includes/functions.php
Returns the default annotation for the web hosting altering the “Update PHP” page URL.
wp_get_update_php_url()wp-includes/functions.php
Gets the URL to learn more about updating the PHP version the site is running on.
get_plugin_updates()wp-admin/includes/update.php
Retrieves plugins with updates available.
get_core_updates()wp-admin/includes/update.php
Gets available core updates.
_get_plugin_data_markup_translate()wp-admin/includes/plugin.php
Sanitizes plugin data, optionally adds markup, optionally translates.
esc_attr_e()wp-includes/l10n.php
Displays translated text that has been escaped for safe use in an attribute.
wp_nonce_field()wp-includes/functions.php
Retrieves or display nonce hidden field for forms.
self_admin_url()wp-includes/link-template.php
Retrieves the URL to the admin area for either the current site or the network depending on context.
current_user_can()wp-includes/capabilities.php
Returns whether the current user has the specified capability.
__()wp-includes/l10n.php
Retrieves the translation of $text.
_e()wp-includes/l10n.php
Displays translated text.
esc_url()wp-includes/formatting.php
Checks and cleans a URL.
esc_attr()wp-includes/formatting.php
Escaping for HTML attributes.
number_format_i18n()wp-includes/functions.php
Converts float number to format based on the locale.
get_site_option()wp-includes/option.php
Retrieve an option value for the current network based on name of option.
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |