wp_update_themes

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

Description

wp_update_themes( (array) $extra_stats = array() ); 

A list of all themes installed in sent to WP. 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_themes' ) ) { 
  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_themes($extra_stats); 
  9.  

Defined (1)

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

/wp-includes/update.php  
  1. function wp_update_themes( $extra_stats = array() ) { 
  2. if ( wp_installing() ) { 
  3. return; 
  4.  
  5. // include an unmodified $wp_version 
  6. include( ABSPATH . WPINC . '/version.php' ); 
  7.  
  8. $installed_themes = wp_get_themes(); 
  9. $translations = wp_get_installed_translations( 'themes' ); 
  10.  
  11. $last_update = get_site_transient( 'update_themes' ); 
  12. if ( ! is_object($last_update) ) 
  13. $last_update = new stdClass; 
  14.  
  15. $themes = $checked = $request = array(); 
  16.  
  17. // Put slug of current theme into request. 
  18. $request['active'] = get_option( 'stylesheet' ); 
  19.  
  20. foreach ( $installed_themes as $theme ) { 
  21. $checked[ $theme->get_stylesheet() ] = $theme->get('Version'); 
  22.  
  23. $themes[ $theme->get_stylesheet() ] = array( 
  24. 'Name' => $theme->get('Name'),  
  25. 'Title' => $theme->get('Name'),  
  26. 'Version' => $theme->get('Version'),  
  27. 'Author' => $theme->get('Author'),  
  28. 'Author URI' => $theme->get('AuthorURI'),  
  29. 'Template' => $theme->get_template(),  
  30. 'Stylesheet' => $theme->get_stylesheet(),  
  31. ); 
  32.  
  33. // Check for update on a different schedule, depending on the page. 
  34. switch ( current_filter() ) { 
  35. case 'upgrader_process_complete' : 
  36. $timeout = 0; 
  37. break; 
  38. case 'load-update-core.php' : 
  39. $timeout = MINUTE_IN_SECONDS
  40. break; 
  41. case 'load-themes.php' : 
  42. case 'load-update.php' : 
  43. $timeout = HOUR_IN_SECONDS
  44. break; 
  45. default : 
  46. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
  47. $timeout = 0; 
  48. } else { 
  49. $timeout = 12 * HOUR_IN_SECONDS
  50.  
  51. $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked ); 
  52.  
  53. if ( $time_not_changed && ! $extra_stats ) { 
  54. $theme_changed = false; 
  55. foreach ( $checked as $slug => $v ) { 
  56. if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($v) ) 
  57. $theme_changed = true; 
  58.  
  59. if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) { 
  60. foreach ( $last_update->response as $slug => $update_details ) { 
  61. if ( ! isset($checked[ $slug ]) ) { 
  62. $theme_changed = true; 
  63. break; 
  64.  
  65. // Bail if we've checked recently and if nothing has changed 
  66. if ( ! $theme_changed ) { 
  67. return; 
  68.  
  69. // Update last_checked for current to prevent multiple blockingrequestsif request hangs 
  70. $last_update->last_checked = time(); 
  71. set_site_transient( 'update_themes', $last_update ); 
  72.  
  73. $request['themes'] = $themes; 
  74.  
  75. $locales = array_values( get_available_languages() ); 
  76.  
  77. /** 
  78. * Filters the locales requested for theme translations. 
  79. * @since 3.7.0 
  80. * @since 4.5.0 The default value of the `$locales` parameter changed to include all locales. 
  81. * @param array $locales Theme locales. Default is all available locales of the site. 
  82. */ 
  83. $locales = apply_filters( 'themes_update_check_locales', $locales ); 
  84. $locales = array_unique( $locales ); 
  85.  
  86. if ( defined( 'DOING_CRON' ) && DOING_CRON ) { 
  87. $timeout = 30; 
  88. } else { 
  89. // Three seconds, plus one extra second for every 10 themes 
  90. $timeout = 3 + (int) ( count( $themes ) / 10 ); 
  91.  
  92. $options = array( 
  93. 'timeout' => $timeout,  
  94. 'body' => array( 
  95. 'themes' => wp_json_encode( $request ),  
  96. translations => wp_json_encode( $translations ),  
  97. 'locale' => wp_json_encode( $locales ),  
  98. ),  
  99. 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) 
  100. ); 
  101.  
  102. if ( $extra_stats ) { 
  103. $options['body']['update_stats'] = wp_json_encode( $extra_stats ); 
  104.  
  105. $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/'; 
  106. if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) 
  107. $url = set_url_scheme( $url, 'https' ); 
  108.  
  109. $raw_response = wp_remote_post( $url, $options ); 
  110. if ( $ssl && is_wp_error( $raw_response ) ) { 
  111. trigger_error( 
  112. sprintf( 
  113. /** translators: %s: support forums URL */ 
  114. __( '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="%s">support forums</a>.' ),  
  115. __( 'https://wordpress.org/support/' ) 
  116. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),  
  117. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE 
  118. ); 
  119. $raw_response = wp_remote_post( $http_url, $options ); 
  120.  
  121. if ( is_wp_error( $raw_response ) || 200 != wp_remote_retrieve_response_code( $raw_response ) ) { 
  122. return; 
  123.  
  124. $new_update = new stdClass; 
  125. $new_update->last_checked = time(); 
  126. $new_update->checked = $checked; 
  127.  
  128. $response = json_decode( wp_remote_retrieve_body( $raw_response ), true ); 
  129.  
  130. if ( is_array( $response ) ) { 
  131. $new_update->response = $response['themes']; 
  132. $new_update->translations = $response[translations]; 
  133.  
  134. set_site_transient( 'update_themes', $new_update );