install_plugin_install_status()
云策文档标注
概述
install_plugin_install_status() 函数用于确定对插件可执行的操作状态,如安装、更新或已安装。它基于插件 API 数据和本地插件信息,返回包含状态、URL、版本和文件路径的数组。
关键要点
- 函数接受两个参数:$api(必需,插件 API 数据数组或对象)和 $loop(可选,布尔值,默认为 false,用于防止递归循环)。
- 返回一个数组,包含 status(状态字符串,可能为 'install'、'update_available'、'latest_installed' 或 'newer_installed')、url(插件安装或更新 URL)、version(插件最新版本)和 file(插件文件路径)。
- 函数首先检查插件是否有可用更新,通过 get_site_transient('update_plugins') 获取更新信息;如果没有更新,则检查插件是否已安装,并比较版本号确定状态。
- 状态逻辑包括:如果插件未安装,状态为 'install';如果有更新,状态为 'update_available';如果已安装且版本匹配,状态为 'latest_installed';如果已安装但版本较新,状态为 'newer_installed'。
- 函数使用 current_user_can() 检查用户权限,以生成相应的操作 URL,并处理递归调用和 URL 参数。
代码示例
function install_plugin_install_status( $api, $loop = false ) {
// This function is called recursively, $loop prevents further loops.
if ( is_array( $api ) ) {
$api = (object) $api;
}
// Default to a "new" plugin.
$status = 'install';
$url = false;
$update_file = false;
$version = '';
/*
* Check to see if this plugin is known to be installed,
* and has an update awaiting it.
*/
$update_plugins = get_site_transient( 'update_plugins' );
if ( isset( $update_plugins->response ) ) {
foreach ( (array) $update_plugins->response as $file => $plugin ) {
if ( $plugin->slug === $api->slug ) {
$status = 'update_available';
$update_file = $file;
$version = $plugin->new_version;
if ( current_user_can( 'update_plugins' ) ) {
$url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin;=' . $update_file ), 'upgrade-plugin_' . $update_file );
}
break;
}
}
}
if ( 'install' === $status ) {
if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
$installed_plugin = get_plugins( '/' . $api->slug );
if ( empty( $installed_plugin ) ) {
if ( current_user_can( 'install_plugins' ) ) {
$url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin;=' . $api->slug ), 'install-plugin_' . $api->slug );
}
} else {
$key = array_keys( $installed_plugin );
/*
* Use the first plugin regardless of the name.
* Could have issues for multiple plugins in one directory if they share different version numbers.
*/
$key = reset( $key );
$update_file = $api->slug . '/' . $key;
if ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '=' ) ) {
$status = 'latest_installed';
} elseif ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], 'slug ), 'install-plugin_' . $api->slug );
}
}
}
if ( isset( $_GET['from'] ) ) {
$url .= '&from=' . urlencode( wp_unslash( $_GET['from'] ) );
}
$file = $update_file;
return compact( 'status', 'url', 'version', 'file' );
}注意事项
- 函数可能被递归调用,使用 $loop 参数防止无限循环;默认 $loop 为 false。
- 状态判断依赖于插件 slug 匹配和版本比较,需确保 API 数据准确。
- URL 生成涉及用户权限检查(如 current_user_can('update_plugins') 或 current_user_can('install_plugins')),需在适当上下文中调用。
- 函数处理来自 $_GET['from'] 的参数,以附加到 URL,增强用户体验。
原文内容
Determines the status we can perform on a plugin.
Parameters
$apiarray|objectrequired-
Data about the plugin retrieved from the API.
$loopbooloptional-
Disable further loops.
Default:
false
Source
function install_plugin_install_status( $api, $loop = false ) {
// This function is called recursively, $loop prevents further loops.
if ( is_array( $api ) ) {
$api = (object) $api;
}
// Default to a "new" plugin.
$status = 'install';
$url = false;
$update_file = false;
$version = '';
/*
* Check to see if this plugin is known to be installed,
* and has an update awaiting it.
*/
$update_plugins = get_site_transient( 'update_plugins' );
if ( isset( $update_plugins->response ) ) {
foreach ( (array) $update_plugins->response as $file => $plugin ) {
if ( $plugin->slug === $api->slug ) {
$status = 'update_available';
$update_file = $file;
$version = $plugin->new_version;
if ( current_user_can( 'update_plugins' ) ) {
$url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin;=' . $update_file ), 'upgrade-plugin_' . $update_file );
}
break;
}
}
}
if ( 'install' === $status ) {
if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
$installed_plugin = get_plugins( '/' . $api->slug );
if ( empty( $installed_plugin ) ) {
if ( current_user_can( 'install_plugins' ) ) {
$url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin;=' . $api->slug ), 'install-plugin_' . $api->slug );
}
} else {
$key = array_keys( $installed_plugin );
/*
* Use the first plugin regardless of the name.
* Could have issues for multiple plugins in one directory if they share different version numbers.
*/
$key = reset( $key );
$update_file = $api->slug . '/' . $key;
if ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '=' ) ) {
$status = 'latest_installed';
} elseif ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '<' ) ) {
$status = 'newer_installed';
$version = $installed_plugin[ $key ]['Version'];
} else {
// If the above update check failed, then that probably means that the update checker has out-of-date information, force a refresh.
if ( ! $loop ) {
delete_site_transient( 'update_plugins' );
wp_update_plugins();
return install_plugin_install_status( $api, true );
}
}
}
} else {
// "install" & no directory with that slug.
if ( current_user_can( 'install_plugins' ) ) {
$url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin;=' . $api->slug ), 'install-plugin_' . $api->slug );
}
}
}
if ( isset( $_GET['from'] ) ) {
$url .= '&from=' . urlencode( wp_unslash( $_GET['from'] ) );
}
$file = $update_file;
return compact( 'status', 'url', 'version', 'file' );
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |