install_plugin_information()
概述
install_plugin_information() 函数用于在对话框中显示插件信息,通过调用 plugins_api() 获取插件数据,并处理 HTML 清理、标签过滤和兼容性检查。
关键要点
- 函数通过 plugins_api() 获取插件信息,使用 wp_unslash() 处理请求参数
- 使用 wp_kses() 和预定义的 $plugins_allowedtags 数组进行 HTML 清理,确保安全性
- 支持多个插件信息部分(如描述、安装、FAQ、截图等),通过 $plugins_section_titles 数组管理标题
- 检查 PHP 和 WordPress 版本兼容性,使用 is_php_version_compatible() 和 is_wp_version_compatible() 函数
- 输出插件详细信息,包括版本、作者、评分、贡献者等,并处理横幅显示和分页导航
代码示例
global $tab;
if ( empty( $_REQUEST['plugin'] ) ) {
return;
}
$api = plugins_api(
'plugin_information',
array(
'slug' => wp_unslash( $_REQUEST['plugin'] ),
)
);
if ( is_wp_error( $api ) ) {
wp_die( $api );
}注意事项
- 函数默认显示描述部分,但可通过 $_REQUEST['section'] 参数指定其他部分
- 使用 wp_admin_notice() 显示兼容性警告或错误信息,需注意用户权限检查
- 插件信息中的链接通过 links_add_base_url() 和 links_add_target() 处理,确保正确指向和打开方式
Displays plugin information in dialog box form.
Source
function install_plugin_information() {
global $tab;
if ( empty( $_REQUEST['plugin'] ) ) {
return;
}
$api = plugins_api(
'plugin_information',
array(
'slug' => wp_unslash( $_REQUEST['plugin'] ),
)
);
if ( is_wp_error( $api ) ) {
wp_die( $api );
}
$plugins_allowedtags = array(
'a' => array(
'href' => array(),
'title' => array(),
'target' => array(),
),
'abbr' => array( 'title' => array() ),
'acronym' => array( 'title' => array() ),
'code' => array(),
'pre' => array(),
'em' => array(),
'strong' => array(),
'div' => array( 'class' => array() ),
'span' => array( 'class' => array() ),
'p' => array(),
'br' => array(),
'ul' => array(),
'ol' => array(),
'li' => array(),
'h1' => array(),
'h2' => array(),
'h3' => array(),
'h4' => array(),
'h5' => array(),
'h6' => array(),
'img' => array(
'src' => array(),
'class' => array(),
'alt' => array(),
),
'blockquote' => array( 'cite' => true ),
);
$plugins_section_titles = array(
'description' => _x( 'Description', 'Plugin installer section title' ),
'installation' => _x( 'Installation', 'Plugin installer section title' ),
'faq' => _x( 'FAQ', 'Plugin installer section title' ),
'screenshots' => _x( 'Screenshots', 'Plugin installer section title' ),
'changelog' => _x( 'Changelog', 'Plugin installer section title' ),
'reviews' => _x( 'Reviews', 'Plugin installer section title' ),
'other_notes' => _x( 'Other Notes', 'Plugin installer section title' ),
);
// Sanitize HTML.
foreach ( (array) $api->sections as $section_name => $content ) {
$api->sections[ $section_name ] = wp_kses( $content, $plugins_allowedtags );
}
foreach ( array( 'version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug' ) as $key ) {
if ( isset( $api->$key ) ) {
$api->$key = wp_kses( $api->$key, $plugins_allowedtags );
}
}
$_tab = esc_attr( $tab );
// Default to the Description tab, Do not translate, API returns English.
$section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description';
if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) {
$section_titles = array_keys( (array) $api->sections );
$section = reset( $section_titles );
}
iframe_header( __( 'Plugin Installation' ) );
$_with_banner = '';
if ( ! empty( $api->banners ) && ( ! empty( $api->banners['low'] ) || ! empty( $api->banners['high'] ) ) ) {
$_with_banner = 'with-banner';
$low = empty( $api->banners['low'] ) ? $api->banners['high'] : $api->banners['low'];
$high = empty( $api->banners['high'] ) ? $api->banners['low'] : $api->banners['high'];
?>
#plugin-information-title.with-banner {
background-image: url( );
}
@media only screen and ( -webkit-min-device-pixel-ratio: 1.5 ) {
#plugin-information-title.with-banner {
background-image: url( );
}
}
';
echo "
{$api->name}
";
echo "
n";
foreach ( (array) $api->sections as $section_name => $content ) {
if ( 'reviews' === $section_name && ( empty( $api->ratings ) || 0 === array_sum( (array) $api->ratings ) ) ) {
continue;
}
if ( isset( $plugins_section_titles[ $section_name ] ) ) {
$title = $plugins_section_titles[ $section_name ];
} else {
$title = ucwords( str_replace( '_', ' ', $section_name ) );
}
$class = ( $section_name === $section ) ? ' class="current"' : '';
$href = add_query_arg(
array(
'tab' => $tab,
'section' => $section_name,
)
);
$href = esc_url( $href );
$san_section = esc_attr( $section_name );
echo "t$titlen";
}
echo "
n";
?>
<div id="-content" class=''>
version ) ) { ?>
- version; ?>
author ) ) { ?>
- author, '_blank' ); ?>
last_updated ) ) { ?>
last_updated ) ) );
?>
requires ) ) { ?>
-
requires );
?>
tested ) ) { ?>
- tested; ?>
requires_php ) ) { ?>
-
requires_php );
?>
active_installs ) ) { ?>
active_installs >= 1000000 ) {
$active_installs_millions = floor( $api->active_installs / 1000000 );
printf(
/* translators: %s: Number of millions. */
_nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ),
number_format_i18n( $active_installs_millions )
);
} elseif ( $api->active_installs active_installs ) . '+';
}
?>
slug ) && empty( $api->external ) ) { ?>
- <a target="_blank" href="slug ); ?>/">
homepage ) ) { ?>
- <a target="_blank" href="homepage ); ?>">
donate_link ) && empty( $api->contributors ) ) { ?>
- <a target="_blank" href="donate_link ); ?>">
rating ) ) { ?>
$api->rating,
'type' => 'percent',
'number' => $api->num_ratings,
)
);
?>
ratings ) && array_sum( (array) $api->ratings ) > 0 ) {
?>
ratings as $key => $ratecount ) {
// Avoid div-by-zero.
$_rating = $api->num_ratings ? ( $ratecount / $api->num_ratings ) : 0;
$aria_label = esc_attr(
sprintf(
/* translators: 1: Number of stars (used to determine singular/plural), 2: Number of reviews. */
_n(
'Reviews with %1$d star: %2$s. Opens in a new tab.',
'Reviews with %1$d stars: %2$s. Opens in a new tab.',
$key
),
$key,
number_format_i18n( $ratecount )
)
);
?>
%s',
"https://wordpress.org/support/plugin/{$api->slug}/reviews/?filter={$key}",
$aria_label,
/* translators: %s: Number of stars. */
sprintf( _n( '%d star', '%d stars', $key ), $key )
);
?>
<span class="counter-bar" style="width: px;">
contributors ) ) {
?>
contributors as $contrib_username => $contrib_details ) {
$contrib_name = $contrib_details['display_name'];
if ( ! $contrib_name ) {
$contrib_name = $contrib_username;
}
$contrib_name = esc_html( $contrib_name );
$contrib_profile = esc_url( $contrib_details['profile'] );
$contrib_avatar = esc_url( add_query_arg( 's', '36', $contrib_details['avatar'] ) );
echo "
{$contrib_name}
";
}
?>
donate_link ) ) { ?>
<a target="_blank" href="donate_link ); ?>">
requires_php ) ? $api->requires_php : null;
$requires_wp = isset( $api->requires ) ? $api->requires : null;
$compatible_php = is_php_version_compatible( $requires_php );
$compatible_wp = is_wp_version_compatible( $requires_wp );
$tested_wp = ( empty( $api->tested ) || version_compare( get_bloginfo( 'version' ), $api->tested, '<=' ) );
if ( ! $compatible_php ) {
$compatible_php_notice_message = '
';
$compatible_php_notice_message .= __( 'Error: This plugin requires a newer version of PHP.' );
if ( current_user_can( 'update_php' ) ) {
$compatible_php_notice_message .= sprintf(
/* translators: %s: URL to Update PHP page. */
' ' . __( 'Click here to learn more about updating PHP.' ),
esc_url( wp_get_update_php_url() )
) . wp_update_php_annotation( '
', '', false );
} else {
$compatible_php_notice_message .= '
';
}
wp_admin_notice(
$compatible_php_notice_message,
array(
'type' => 'error',
'additional_classes' => array( 'notice-alt' ),
'paragraph_wrap' => false,
)
);
}
if ( ! $tested_wp ) {
wp_admin_notice(
__( 'Warning: This plugin has not been tested with your current version of WordPress.' ),
array(
'type' => 'warning',
'additional_classes' => array( 'notice-alt' ),
)
);
} elseif ( ! $compatible_wp ) {
$compatible_wp_notice_message = __( 'Error: This plugin requires a newer version of WordPress.' );
if ( current_user_can( 'update_core' ) ) {
$compatible_wp_notice_message .= sprintf(
/* translators: %s: URL to WordPress Updates screen. */
' ' . __( 'Click here to update WordPress.' ),
esc_url( self_admin_url( 'update-core.php' ) )
);
}
wp_admin_notice(
$compatible_wp_notice_message,
array(
'type' => 'error',
'additional_classes' => array( 'notice-alt' ),
)
);
}
foreach ( (array) $api->sections as $section_name => $content ) {
$content = links_add_base_url( $content, 'https://wordpress.org/plugins/' . $api->slug . '/' );
$content = links_add_target( $content, '_blank' );
$san_section = esc_attr( $section_name );
$display = ( $section_name === $section ) ? 'block' : 'none';
echo "t
n";
echo $content;
echo "t
n";
}
echo "
n";
echo "
n";
echo "