wp_update_plugins

Check plugin versions against the latest versions hosted on WordPress.org.

Description

wp_update_plugins( (array) $extra_stats = array() ); 

The WordPress version, PHP version, and Locale is sent along with a list of all plugins installed. Checks against the WordPress server at api.wordpress.org. Will only check if WordPress isn't installing.

Parameters (1)

0. $extra_stats — Optional. (array) => array()
Extra statistics to report to the WordPress.org API.

Usage

  1. if ( !function_exists( 'wp_update_plugins' ) ) { 
  2. require_once ABSPATH . WPINC . '/update.php'; 
  3.  
  4. // Extra statistics to report to the WordPress.org API. 
  5. $extra_stats = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_update_plugins($extra_stats); 
  9.  

Defined (1)

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

/wp-includes/update.php  
  1. function wp_update_plugins( $extra_stats = array() ) { 
  2. if ( wp_installing() ) { 
  3. return; 
  4.  
  5. // include an unmodified $wp_version 
  6. include( ABSPATH . WPINC . '/version.php' ); 
  7.  
  8. // If running blog-side, bail unless we've not checked in the last 12 hours 
  9. if ( !function_exists( 'get_plugins' ) ) 
  10. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  11.  
  12. $plugins = get_plugins(); 
  13. $translations = wp_get_installed_translations( 'plugins' ); 
  14.  
  15. $active = get_option( 'active_plugins', array() ); 
  16. $current = get_site_transient( 'update_plugins' ); 
  17. if ( ! is_object($current) ) 
  18. $current = new stdClass; 
  19.  
  20. $new_option = new stdClass; 
  21. $new_option->last_checked = time(); 
  22.  
  23. // Check for update on a different schedule, depending on the page. 
  24. switch ( current_filter() ) { 
  25. case 'upgrader_process_complete' : 
  26. $timeout = 0; 
  27. break; 
  28. case 'load-update-core.php' : 
  29. $timeout = MINUTE_IN_SECONDS
  30. break; 
  31. case 'load-plugins.php' : 
  32. case 'load-update.php' : 
  33. $timeout = HOUR_IN_SECONDS
  34. break; 
  35. default : 
  36. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
  37. $timeout = 0; 
  38. } else { 
  39. $timeout = 12 * HOUR_IN_SECONDS
  40.  
  41. $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked ); 
  42.  
  43. if ( $time_not_changed && ! $extra_stats ) { 
  44. $plugin_changed = false; 
  45. foreach ( $plugins as $file => $p ) { 
  46. $new_option->checked[ $file ] = $p['Version']; 
  47.  
  48. if ( !isset( $current->checked[ $file ] ) || strval($current->checked[ $file ]) !== strval($p['Version']) ) 
  49. $plugin_changed = true; 
  50.  
  51. if ( isset ( $current->response ) && is_array( $current->response ) ) { 
  52. foreach ( $current->response as $plugin_file => $update_details ) { 
  53. if ( ! isset($plugins[ $plugin_file ]) ) { 
  54. $plugin_changed = true; 
  55. break; 
  56.  
  57. // Bail if we've checked recently and if nothing has changed 
  58. if ( ! $plugin_changed ) { 
  59. return; 
  60.  
  61. // Update last_checked for current to prevent multiple blockingrequestsif request hangs 
  62. $current->last_checked = time(); 
  63. set_site_transient( 'update_plugins', $current ); 
  64.  
  65. $to_send = compact( 'plugins', 'active' ); 
  66.  
  67. $locales = array_values( get_available_languages() ); 
  68.  
  69. /** 
  70. * Filters the locales requested for plugin translations. 
  71. * @since 3.7.0 
  72. * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales. 
  73. * @param array $locales Plugin locales. Default is all available locales of the site. 
  74. */ 
  75. $locales = apply_filters( 'plugins_update_check_locales', $locales ); 
  76. $locales = array_unique( $locales ); 
  77.  
  78. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
  79. $timeout = 30; 
  80. } else { 
  81. // Three seconds, plus one extra second for every 10 plugins 
  82. $timeout = 3 + (int) ( count( $plugins ) / 10 ); 
  83.  
  84. $options = array( 
  85. 'timeout' => $timeout,  
  86. 'body' => array( 
  87. 'plugins' => wp_json_encode( $to_send ),  
  88. translations => wp_json_encode( $translations ),  
  89. 'locale' => wp_json_encode( $locales ),  
  90. 'all' => wp_json_encode( true ),  
  91. ),  
  92. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) 
  93. ); 
  94.  
  95. if ( $extra_stats ) { 
  96. $options['body']['update_stats'] = wp_json_encode( $extra_stats ); 
  97.  
  98. $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/'; 
  99. if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) 
  100. $url = set_url_scheme( $url, 'https' ); 
  101.  
  102. $raw_response = wp_remote_post( $url, $options ); 
  103. if ( $ssl && is_wp_error( $raw_response ) ) { 
  104. trigger_error( __( 'An unexpectederroroccurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE ); 
  105. $raw_response = wp_remote_post( $http_url, $options ); 
  106.  
  107. if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) { 
  108. return; 
  109.  
  110. $response = json_decode( wp_remote_retrieve_body( $raw_response ), true ); 
  111. foreach ( $response['plugins'] as &$plugin ) { 
  112. $plugin = (object) $plugin; 
  113. if ( isset( $plugin->compatibility ) ) { 
  114. $plugin->compatibility = (object) $plugin->compatibility; 
  115. foreach ( $plugin->compatibility as &$data ) { 
  116. $data = (object) $data; 
  117. unset( $plugin, $data ); 
  118. foreach ( $response['no_update'] as &$plugin ) { 
  119. $plugin = (object) $plugin; 
  120. unset( $plugin ); 
  121.  
  122. if ( is_array( $response ) ) { 
  123. $new_option->response = $response['plugins']; 
  124. $new_option->translations = $response[translations]; 
  125. // TODO: Perhaps better to store no_update in a separate transient with an expiry? 
  126. $new_option->no_update = $response['no_update']; 
  127. } else { 
  128. $new_option->response = array(); 
  129. $new_option->translations = array(); 
  130. $new_option->no_update = array(); 
  131.  
  132. set_site_transient( 'update_plugins', $new_option );