PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-admin/includes/update.php

https://gitlab.com/plusplusminus/leandrikers
PHP | 492 lines | 257 code | 76 blank | 159 comment | 78 complexity | 83480cff09bcd3a8e090854ce3faedc7 MD5 | raw file
  1. <?php
  2. /**
  3. * WordPress Administration Update API
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. */
  8. /**
  9. * Selects the first update version from the update_core option.
  10. *
  11. * @return object|array|false The response from the API on success, false on failure.
  12. */
  13. function get_preferred_from_update_core() {
  14. $updates = get_core_updates();
  15. if ( ! is_array( $updates ) )
  16. return false;
  17. if ( empty( $updates ) )
  18. return (object) array( 'response' => 'latest' );
  19. return $updates[0];
  20. }
  21. /**
  22. * Get available core updates.
  23. *
  24. * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
  25. * set $options['available'] to false to skip not-dismissed updates.
  26. * @return array|false Array of the update objects on success, false on failure.
  27. */
  28. function get_core_updates( $options = array() ) {
  29. $options = array_merge( array( 'available' => true, 'dismissed' => false ), $options );
  30. $dismissed = get_site_option( 'dismissed_update_core' );
  31. if ( ! is_array( $dismissed ) )
  32. $dismissed = array();
  33. $from_api = get_site_transient( 'update_core' );
  34. if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
  35. return false;
  36. $updates = $from_api->updates;
  37. $result = array();
  38. foreach ( $updates as $update ) {
  39. if ( $update->response == 'autoupdate' )
  40. continue;
  41. if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
  42. if ( $options['dismissed'] ) {
  43. $update->dismissed = true;
  44. $result[] = $update;
  45. }
  46. } else {
  47. if ( $options['available'] ) {
  48. $update->dismissed = false;
  49. $result[] = $update;
  50. }
  51. }
  52. }
  53. return $result;
  54. }
  55. /**
  56. * Gets the best available (and enabled) Auto-Update for WordPress Core.
  57. *
  58. * If there's 1.2.3 and 1.3 on offer, it'll choose 1.3 if the install allows it, else, 1.2.3
  59. *
  60. * @since 3.7.0
  61. *
  62. * @return array|false False on failure, otherwise the core update offering.
  63. */
  64. function find_core_auto_update() {
  65. $updates = get_site_transient( 'update_core' );
  66. if ( ! $updates || empty( $updates->updates ) )
  67. return false;
  68. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
  69. $auto_update = false;
  70. $upgrader = new WP_Automatic_Updater;
  71. foreach ( $updates->updates as $update ) {
  72. if ( 'autoupdate' != $update->response )
  73. continue;
  74. if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) )
  75. continue;
  76. if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )
  77. $auto_update = $update;
  78. }
  79. return $auto_update;
  80. }
  81. /**
  82. * Gets and caches the checksums for the given version of WordPress.
  83. *
  84. * @since 3.7.0
  85. *
  86. * @param string $version Version string to query.
  87. * @param string $locale Locale to query.
  88. * @return bool|array False on failure. An array of checksums on success.
  89. */
  90. function get_core_checksums( $version, $locale ) {
  91. $url = $http_url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), null, '&' );
  92. if ( $ssl = wp_http_supports( array( 'ssl' ) ) )
  93. $url = set_url_scheme( $url, 'https' );
  94. $options = array(
  95. 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3 ),
  96. );
  97. $response = wp_remote_get( $url, $options );
  98. if ( $ssl && is_wp_error( $response ) ) {
  99. trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;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 );
  100. $response = wp_remote_get( $http_url, $options );
  101. }
  102. if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
  103. return false;
  104. $body = trim( wp_remote_retrieve_body( $response ) );
  105. $body = json_decode( $body, true );
  106. if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) )
  107. return false;
  108. return $body['checksums'];
  109. }
  110. /**
  111. *
  112. * @param object $update
  113. * @return bool
  114. */
  115. function dismiss_core_update( $update ) {
  116. $dismissed = get_site_option( 'dismissed_update_core' );
  117. $dismissed[ $update->current . '|' . $update->locale ] = true;
  118. return update_site_option( 'dismissed_update_core', $dismissed );
  119. }
  120. /**
  121. *
  122. * @param string $version
  123. * @param string $locale
  124. * @return bool
  125. */
  126. function undismiss_core_update( $version, $locale ) {
  127. $dismissed = get_site_option( 'dismissed_update_core' );
  128. $key = $version . '|' . $locale;
  129. if ( ! isset( $dismissed[$key] ) )
  130. return false;
  131. unset( $dismissed[$key] );
  132. return update_site_option( 'dismissed_update_core', $dismissed );
  133. }
  134. /**
  135. *
  136. * @param string $version
  137. * @param string $locale
  138. * @return object|false
  139. */
  140. function find_core_update( $version, $locale ) {
  141. $from_api = get_site_transient( 'update_core' );
  142. if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) )
  143. return false;
  144. $updates = $from_api->updates;
  145. foreach ( $updates as $update ) {
  146. if ( $update->current == $version && $update->locale == $locale )
  147. return $update;
  148. }
  149. return false;
  150. }
  151. /**
  152. *
  153. * @param string $msg
  154. * @return string
  155. */
  156. function core_update_footer( $msg = '' ) {
  157. if ( !current_user_can('update_core') )
  158. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  159. $cur = get_preferred_from_update_core();
  160. if ( ! is_object( $cur ) )
  161. $cur = new stdClass;
  162. if ( ! isset( $cur->current ) )
  163. $cur->current = '';
  164. if ( ! isset( $cur->url ) )
  165. $cur->url = '';
  166. if ( ! isset( $cur->response ) )
  167. $cur->response = '';
  168. switch ( $cur->response ) {
  169. case 'development' :
  170. return sprintf( __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ), get_bloginfo( 'version', 'display' ), network_admin_url( 'update-core.php' ) );
  171. case 'upgrade' :
  172. return sprintf( '<strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', network_admin_url( 'update-core.php' ), $cur->current);
  173. case 'latest' :
  174. default :
  175. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  176. }
  177. }
  178. /**
  179. *
  180. * @global string $pagenow
  181. * @return false|void
  182. */
  183. function update_nag() {
  184. if ( is_multisite() && !current_user_can('update_core') )
  185. return false;
  186. global $pagenow;
  187. if ( 'update-core.php' == $pagenow )
  188. return;
  189. $cur = get_preferred_from_update_core();
  190. if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
  191. return false;
  192. if ( current_user_can('update_core') ) {
  193. $msg = sprintf( __('<a href="https://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! <a href="%2$s">Please update now</a>.'), $cur->current, network_admin_url( 'update-core.php' ) );
  194. } else {
  195. $msg = sprintf( __('<a href="https://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.'), $cur->current );
  196. }
  197. echo "<div class='update-nag'>$msg</div>";
  198. }
  199. // Called directly from dashboard
  200. function update_right_now_message() {
  201. $theme_name = wp_get_theme();
  202. if ( current_user_can( 'switch_themes' ) ) {
  203. $theme_name = sprintf( '<a href="themes.php">%1$s</a>', $theme_name );
  204. }
  205. $msg = '';
  206. if ( current_user_can('update_core') ) {
  207. $cur = get_preferred_from_update_core();
  208. if ( isset( $cur->response ) && $cur->response == 'upgrade' )
  209. $msg .= '<a href="' . network_admin_url( 'update-core.php' ) . '" class="button" aria-describedby="wp-version">' . sprintf( __( 'Update to %s' ), $cur->current ? $cur->current : __( 'Latest' ) ) . '</a> ';
  210. }
  211. $msg .= sprintf( '<span id="wp-version">' . __( 'WordPress %1$s running %2$s theme.' ) . '</span>', get_bloginfo( 'version', 'display' ), $theme_name );
  212. echo "<p id='wp-version-message'>$msg</p>";
  213. }
  214. /**
  215. * @since 2.9.0
  216. *
  217. * @return array
  218. */
  219. function get_plugin_updates() {
  220. $all_plugins = get_plugins();
  221. $upgrade_plugins = array();
  222. $current = get_site_transient( 'update_plugins' );
  223. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  224. if ( isset( $current->response[ $plugin_file ] ) ) {
  225. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  226. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  227. }
  228. }
  229. return $upgrade_plugins;
  230. }
  231. /**
  232. * @since 2.9.0
  233. */
  234. function wp_plugin_update_rows() {
  235. if ( !current_user_can('update_plugins' ) )
  236. return;
  237. $plugins = get_site_transient( 'update_plugins' );
  238. if ( isset($plugins->response) && is_array($plugins->response) ) {
  239. $plugins = array_keys( $plugins->response );
  240. foreach( $plugins as $plugin_file ) {
  241. add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
  242. }
  243. }
  244. }
  245. /**
  246. *
  247. * @param string $file
  248. * @param array $plugin_data
  249. * @return false|void
  250. */
  251. function wp_plugin_update_row( $file, $plugin_data ) {
  252. $current = get_site_transient( 'update_plugins' );
  253. if ( !isset( $current->response[ $file ] ) )
  254. return false;
  255. $r = $current->response[ $file ];
  256. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  257. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  258. $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
  259. $wp_list_table = _get_list_table('WP_Plugins_List_Table');
  260. if ( is_network_admin() || !is_multisite() ) {
  261. if ( is_network_admin() ) {
  262. $active_class = is_plugin_active_for_network( $file ) ? ' active': '';
  263. } else {
  264. $active_class = is_plugin_active( $file ) ? ' active' : '';
  265. }
  266. echo '<tr class="plugin-update-tr' . $active_class . '" id="' . esc_attr( $r->slug . '-update' ) . '" data-slug="' . esc_attr( $r->slug ) . '" data-plugin="' . esc_attr( $file ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message">';
  267. if ( ! current_user_can( 'update_plugins' ) ) {
  268. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
  269. } elseif ( empty($r->package) ) {
  270. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>'), $plugin_name, esc_url($details_url), esc_attr($plugin_name), $r->new_version );
  271. } else {
  272. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s" class="update-link">update now</a>.' ), $plugin_name, esc_url( $details_url ), esc_attr( $plugin_name ), $r->new_version, wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ) );
  273. }
  274. /**
  275. * Fires at the end of the update message container in each
  276. * row of the plugins list table.
  277. *
  278. * The dynamic portion of the hook name, `$file`, refers to the path
  279. * of the plugin's primary file relative to the plugins directory.
  280. *
  281. * @since 2.8.0
  282. *
  283. * @param array $plugin_data {
  284. * An array of plugin metadata.
  285. *
  286. * @type string $name The human-readable name of the plugin.
  287. * @type string $plugin_uri Plugin URI.
  288. * @type string $version Plugin version.
  289. * @type string $description Plugin description.
  290. * @type string $author Plugin author.
  291. * @type string $author_uri Plugin author URI.
  292. * @type string $text_domain Plugin text domain.
  293. * @type string $domain_path Relative path to the plugin's .mo file(s).
  294. * @type bool $network Whether the plugin can only be activated network wide.
  295. * @type string $title The human-readable title of the plugin.
  296. * @type string $author_name Plugin author's name.
  297. * @type bool $update Whether there's an available update. Default null.
  298. * }
  299. * @param array $r {
  300. * An array of metadata about the available plugin update.
  301. *
  302. * @type int $id Plugin ID.
  303. * @type string $slug Plugin slug.
  304. * @type string $new_version New plugin version.
  305. * @type string $url Plugin URL.
  306. * @type string $package Plugin update package URL.
  307. * }
  308. */
  309. do_action( "in_plugin_update_message-{$file}", $plugin_data, $r );
  310. echo '</div></td></tr>';
  311. }
  312. }
  313. /**
  314. *
  315. * @return array
  316. */
  317. function get_theme_updates() {
  318. $current = get_site_transient('update_themes');
  319. if ( ! isset( $current->response ) )
  320. return array();
  321. $update_themes = array();
  322. foreach ( $current->response as $stylesheet => $data ) {
  323. $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
  324. $update_themes[ $stylesheet ]->update = $data;
  325. }
  326. return $update_themes;
  327. }
  328. /**
  329. * @since 3.1.0
  330. */
  331. function wp_theme_update_rows() {
  332. if ( !current_user_can('update_themes' ) )
  333. return;
  334. $themes = get_site_transient( 'update_themes' );
  335. if ( isset($themes->response) && is_array($themes->response) ) {
  336. $themes = array_keys( $themes->response );
  337. foreach( $themes as $theme ) {
  338. add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
  339. }
  340. }
  341. }
  342. /**
  343. *
  344. * @param string $theme_key
  345. * @param WP_Theme $theme
  346. * @return false|void
  347. */
  348. function wp_theme_update_row( $theme_key, $theme ) {
  349. $current = get_site_transient( 'update_themes' );
  350. if ( !isset( $current->response[ $theme_key ] ) )
  351. return false;
  352. $r = $current->response[ $theme_key ];
  353. $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
  354. $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
  355. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  356. if ( ! current_user_can('update_themes') ) {
  357. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r->new_version );
  358. } elseif ( empty( $r['package'] ) ) {
  359. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r['new_version'] );
  360. } else {
  361. printf( __('There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a> or <a href="%5$s">update now</a>.'), $theme['Name'], esc_url($details_url), esc_attr($theme['Name']), $r['new_version'], wp_nonce_url( self_admin_url('update.php?action=upgrade-theme&theme=') . $theme_key, 'upgrade-theme_' . $theme_key) );
  362. }
  363. /**
  364. * Fires at the end of the update message container in each
  365. * row of the themes list table.
  366. *
  367. * The dynamic portion of the hook name, `$theme_key`, refers to
  368. * the theme slug as found in the WordPress.org themes repository.
  369. *
  370. * @since 3.1.0
  371. *
  372. * @param WP_Theme $theme The WP_Theme object.
  373. * @param array $r {
  374. * An array of metadata about the available theme update.
  375. *
  376. * @type string $new_version New theme version.
  377. * @type string $url Theme URL.
  378. * @type string $package Theme update package URL.
  379. * }
  380. */
  381. do_action( "in_theme_update_message-{$theme_key}", $theme, $r );
  382. echo '</div></td></tr>';
  383. }
  384. /**
  385. *
  386. * @global int $upgrading
  387. * @return false|void
  388. */
  389. function maintenance_nag() {
  390. include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
  391. global $upgrading;
  392. $nag = isset( $upgrading );
  393. if ( ! $nag ) {
  394. $failed = get_site_option( 'auto_core_update_failed' );
  395. /*
  396. * If an update failed critically, we may have copied over version.php but not other files.
  397. * In that case, if the install claims we're running the version we attempted, nag.
  398. * This is serious enough to err on the side of nagging.
  399. *
  400. * If we simply failed to update before we tried to copy any files, then assume things are
  401. * OK if they are now running the latest.
  402. *
  403. * This flag is cleared whenever a successful update occurs using Core_Upgrader.
  404. */
  405. $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
  406. if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
  407. $nag = true;
  408. }
  409. if ( ! $nag )
  410. return false;
  411. if ( current_user_can('update_core') )
  412. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  413. else
  414. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  415. echo "<div class='update-nag'>$msg</div>";
  416. }