wp_get_plugin_action_button()
云策文档标注
概述
wp_get_plugin_action_button() 函数用于生成插件安装操作按钮的 HTML 标记。它根据插件状态、兼容性和依赖关系动态输出安装、更新或激活按钮。
关键要点
- 函数接受插件名称、数据对象、PHP 和 WordPress 兼容性检查结果作为参数。
- 通过 install_plugin_install_status() 确定插件状态(如 install、update_available、latest_installed)。
- 检查插件依赖关系,包括已安装和激活的依赖插件数量。
- 根据用户权限(如 install_plugins、update_plugins)和状态生成相应按钮标记。
- 返回按钮的 HTML 字符串,若无权限则返回空字符串。
代码示例
function wp_get_plugin_action_button( $name, $data, $compatible_php, $compatible_wp ) {
$button = '';
$data = (object) $data;
$status = install_plugin_install_status( $data );
$requires_plugins = $data->requires_plugins ?? array();
// 确定插件依赖状态
$installed_plugins = get_plugins();
$active_plugins = get_option( 'active_plugins', array() );
$plugin_dependencies_count = count( $requires_plugins );
$installed_plugin_dependencies_count = 0;
$active_plugin_dependencies_count = 0;
foreach ( $requires_plugins as $dependency ) {
foreach ( array_keys( $installed_plugins ) as $installed_plugin_file ) {
if ( str_contains( $installed_plugin_file, '/' ) && explode( '/', $installed_plugin_file )[0] === $dependency ) {
++$installed_plugin_dependencies_count;
}
}
foreach ( $active_plugins as $active_plugin_file ) {
if ( str_contains( $active_plugin_file, '/' ) && explode( '/', $active_plugin_file )[0] === $dependency ) {
++$active_plugin_dependencies_count;
}
}
}
$all_plugin_dependencies_installed = $installed_plugin_dependencies_count === $plugin_dependencies_count;
$all_plugin_dependencies_active = $active_plugin_dependencies_count === $plugin_dependencies_count;
if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
switch ( $status['status'] ) {
case 'install':
if ( $status['url'] ) {
if ( $compatible_php && $compatible_wp && $all_plugin_dependencies_installed && ! empty( $data->download_link ) ) {
$button = sprintf(
'<a class="install-now button" data-slug="%s" href="%s" aria-label="%s" data-name="%s">%s</a>',
esc_attr( $data->slug ),
esc_url( $status['url'] ),
esc_attr( sprintf( _x( 'Install %s now', 'plugin' ), $name ) ),
esc_attr( $name ),
_x( 'Install Now', 'plugin' )
);
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Install Now', 'plugin' )
);
}
}
break;
case 'update_available':
if ( $status['url'] ) {
if ( $compatible_php && $compatible_wp ) {
$button = sprintf(
'<a class="update-now button aria-button-if-js" data-plugin="%s" data-slug="%s" href="%s" aria-label="%s" data-name="%s">%s</a>',
esc_attr( $status['file'] ),
esc_attr( $data->slug ),
esc_url( $status['url'] ),
esc_attr( sprintf( _x( 'Update %s now', 'plugin' ), $name ) ),
esc_attr( $name ),
_x( 'Update Now', 'plugin' )
);
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Update Now', 'plugin' )
);
}
}
break;
case 'latest_installed':
case 'newer_installed':
if ( is_plugin_active( $status['file'] ) ) {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Active', 'plugin' )
);
} elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) {
if ( $compatible_php && $compatible_wp && $all_plugin_dependencies_active ) {
$button_text = _x( 'Activate', 'plugin' );
$button_label = _x( 'Activate %s', 'plugin' );
$activate_url = add_query_arg(
array(
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
'action' => 'activate',
'plugin' => $status['file'],
),
network_admin_url( 'plugins.php' )
);
if ( is_network_admin() ) {
$button_text = _x( 'Network Activate', 'plugin' );
$button_label = _x( 'Network Activate %s', 'plugin' );
$activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
}
$button = sprintf(
'<a href="%1$s" class="button button-primary" aria-label="%5$s" data-name="%2$s" data-slug="%3$s" data-plugin="%4$s">%6$s</a>',
esc_url( $activate_url ),
esc_attr( $name ),
esc_attr( $data->slug ),
esc_attr( $status['file'] ),
esc_attr( sprintf( $button_label, $name ) ),
$button_text
);
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
is_network_admin() ? _x( 'Network Activate', 'plugin' ) : _x( 'Activate', 'plugin' )
);
}
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Installed', 'plugin' )
);
}
break;
}
}
return $button;
}注意事项
- 函数依赖于 install_plugin_install_status() 获取插件状态,需确保数据对象正确。
- 用户权限检查是关键,无 install_plugins 或 update_plugins 权限时返回空字符串。
- 依赖插件检查基于 slug 匹配,需确保 requires_plugins 数组格式正确。
- 兼容性参数($compatible_php 和 $compatible_wp)影响按钮可用性,应预先验证。
- 在 WordPress 6.5.0 版本中引入,使用时需注意版本兼容性。
原文内容
Gets the markup for the plugin install action button.
Parameters
$namestringrequired-
Plugin name.
$dataarray|objectrequired-
An array or object of plugin data. Can be retrieved from the API.
slugstringThe plugin slug.requires_pluginsstring[]An array of plugin dependency slugs.versionstringThe plugin’s version string. Used when getting the install status.
$compatible_phpboolrequired-
The result of a PHP compatibility check.
$compatible_wpboolrequired-
The result of a WP compatibility check.
Source
function wp_get_plugin_action_button( $name, $data, $compatible_php, $compatible_wp ) {
$button = '';
$data = (object) $data;
$status = install_plugin_install_status( $data );
$requires_plugins = $data->requires_plugins ?? array();
// Determine the status of plugin dependencies.
$installed_plugins = get_plugins();
$active_plugins = get_option( 'active_plugins', array() );
$plugin_dependencies_count = count( $requires_plugins );
$installed_plugin_dependencies_count = 0;
$active_plugin_dependencies_count = 0;
foreach ( $requires_plugins as $dependency ) {
foreach ( array_keys( $installed_plugins ) as $installed_plugin_file ) {
if ( str_contains( $installed_plugin_file, '/' ) && explode( '/', $installed_plugin_file )[0] === $dependency ) {
++$installed_plugin_dependencies_count;
}
}
foreach ( $active_plugins as $active_plugin_file ) {
if ( str_contains( $active_plugin_file, '/' ) && explode( '/', $active_plugin_file )[0] === $dependency ) {
++$active_plugin_dependencies_count;
}
}
}
$all_plugin_dependencies_installed = $installed_plugin_dependencies_count === $plugin_dependencies_count;
$all_plugin_dependencies_active = $active_plugin_dependencies_count === $plugin_dependencies_count;
if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
switch ( $status['status'] ) {
case 'install':
if ( $status['url'] ) {
if ( $compatible_php && $compatible_wp && $all_plugin_dependencies_installed && ! empty( $data->download_link ) ) {
$button = sprintf(
'<a class="install-now button" data-slug="%s" href="%s" aria-label="%s" data-name="%s" role="button">%s</a>',
esc_attr( $data->slug ),
esc_url( $status['url'] ),
/* translators: %s: Plugin name and version. */
esc_attr( sprintf( _x( 'Install %s now', 'plugin' ), $name ) ),
esc_attr( $name ),
_x( 'Install Now', 'plugin' )
);
} else {
$button = sprintf(
'<button type="button" class="install-now button button-disabled" disabled="disabled">%s</button>',
_x( 'Install Now', 'plugin' )
);
}
}
break;
case 'update_available':
if ( $status['url'] ) {
if ( $compatible_php && $compatible_wp ) {
$button = sprintf(
'<a class="update-now button aria-button-if-js" data-plugin="%s" data-slug="%s" href="%s" aria-label="%s" data-name="%s" role="button">%s</a>',
esc_attr( $status['file'] ),
esc_attr( $data->slug ),
esc_url( $status['url'] ),
/* translators: %s: Plugin name and version. */
esc_attr( sprintf( _x( 'Update %s now', 'plugin' ), $name ) ),
esc_attr( $name ),
_x( 'Update Now', 'plugin' )
);
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Update Now', 'plugin' )
);
}
}
break;
case 'latest_installed':
case 'newer_installed':
if ( is_plugin_active( $status['file'] ) ) {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Active', 'plugin' )
);
} elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) {
if ( $compatible_php && $compatible_wp && $all_plugin_dependencies_active ) {
$button_text = _x( 'Activate', 'plugin' );
/* translators: %s: Plugin name. */
$button_label = _x( 'Activate %s', 'plugin' );
$activate_url = add_query_arg(
array(
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
'action' => 'activate',
'plugin' => $status['file'],
),
network_admin_url( 'plugins.php' )
);
if ( is_network_admin() ) {
$button_text = _x( 'Network Activate', 'plugin' );
/* translators: %s: Plugin name. */
$button_label = _x( 'Network Activate %s', 'plugin' );
$activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
}
$button = sprintf(
'<a href="%1$s" data-name="%2$s" data-slug="%3$s" data-plugin="%4$s" class="button button-primary activate-now" aria-label="%5$s" role="button">%6$s</a>',
esc_url( $activate_url ),
esc_attr( $name ),
esc_attr( $data->slug ),
esc_attr( $status['file'] ),
esc_attr( sprintf( $button_label, $name ) ),
$button_text
);
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
is_network_admin() ? _x( 'Network Activate', 'plugin' ) : _x( 'Activate', 'plugin' )
);
}
} else {
$button = sprintf(
'<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
_x( 'Installed', 'plugin' )
);
}
break;
}
}
return $button;
}
Changelog
| Version | Description |
|---|---|
| 6.5.0 | Introduced. |