install_plugin_install_status

Determine the status we can perform on a plugin.

Description

(array) install_plugin_install_status( (array|object) $api, (constant) $loop = false ); 

Returns (array)

{ Plugin installation status data. @type string $status Status of a plugin. Could be one of 'install', 'update_available', 'latest_installed' or 'newer_installed'. @type string $url Plugin installation URL. @type string $version The most recent version of the plugin. @type string $file Plugin filename relative to the plugins directory. }

Parameters (2)

0. $api (array|object)
Data about the plugin retrieved from the API.
1. $loop — Optional. (constant) => false
Disable further loops. Default false.

Usage

  1. if ( !function_exists( 'install_plugin_install_status' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin-install.php'; 
  3.  
  4. // Data about the plugin retrieved from the API. 
  5. $api = null; 
  6.  
  7. // Optional. Disable further loops. Default false. 
  8. $loop = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = install_plugin_install_status($api, $loop); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/plugin-install.php  
  1. function install_plugin_install_status($api, $loop = false) { 
  2. // This function is called recursively, $loop prevents further loops. 
  3. if ( is_array($api) ) 
  4. $api = (object) $api; 
  5.  
  6. // Default to a "new" plugin 
  7. $status = 'install'; 
  8. $url = false; 
  9. $update_file = false; 
  10.  
  11. /** 
  12. * Check to see if this plugin is known to be installed,  
  13. * and has an update awaiting it. 
  14. */ 
  15. $update_plugins = get_site_transient('update_plugins'); 
  16. if ( isset( $update_plugins->response ) ) { 
  17. foreach ( (array)$update_plugins->response as $file => $plugin ) { 
  18. if ( $plugin->slug === $api->slug ) { 
  19. $status = 'update_available'; 
  20. $update_file = $file; 
  21. $version = $plugin->new_version; 
  22. if ( current_user_can('update_plugins') ) 
  23. $url = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=' . $update_file), 'upgrade-plugin_' . $update_file); 
  24. break; 
  25.  
  26. if ( 'install' == $status ) { 
  27. if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) { 
  28. $installed_plugin = get_plugins('/' . $api->slug); 
  29. if ( empty($installed_plugin) ) { 
  30. if ( current_user_can('install_plugins') ) 
  31. $url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug); 
  32. } else { 
  33. $key = array_keys( $installed_plugin ); 
  34. $key = reset( $key ); //Use the first plugin regardless of the name, Could have issues for multiple-plugins in one directory if they share different version numbers 
  35. $update_file = $api->slug . '/' . $key; 
  36. if ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '=') ) { 
  37. $status = 'latest_installed'; 
  38. } elseif ( version_compare($api->version, $installed_plugin[ $key ]['Version'], '<') ) { 
  39. $status = 'newer_installed'; 
  40. $version = $installed_plugin[ $key ]['Version']; 
  41. } else { 
  42. //If the above update check failed, Then that probably means that the update checker has out-of-date information, force a refresh 
  43. if ( ! $loop ) { 
  44. delete_site_transient('update_plugins'); 
  45. return install_plugin_install_status($api, true); 
  46. } else { 
  47. // "install" & no directory with that slug 
  48. if ( current_user_can('install_plugins') ) 
  49. $url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $api->slug), 'install-plugin_' . $api->slug); 
  50. if ( isset($_GET['from']) ) 
  51. $url .= '&from=' . urlencode( wp_unslash( $_GET['from'] ) ); 
  52.  
  53. $file = $update_file; 
  54. return compact( 'status', 'url', 'version', 'file' );