PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/update.php

https://gitlab.com/rio.munas/gddc
PHP | 506 lines | 259 code | 78 blank | 169 comment | 78 complexity | b93bcc52aae6fb805f634031199d0bf1 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 '<strong><a href="' . network_admin_url( 'update-core.php' ) . '">' . sprintf( __( 'Get Version %s' ), $cur->current ) . '</a></strong>';
  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. /* translators: 1: version number, 2: theme name */
  212. $content = __( 'WordPress %1$s running %2$s theme.' );
  213. /**
  214. * Filter the text displayed in the 'At a Glance' dashboard widget.
  215. *
  216. * Prior to 3.8.0, the widget was named 'Right Now'.
  217. *
  218. * @since 4.4.0
  219. *
  220. * @param string $content Default text.
  221. */
  222. $content = apply_filters( 'update_right_now_text', $content );
  223. $msg .= sprintf( '<span id="wp-version">' . $content . '</span>', get_bloginfo( 'version', 'display' ), $theme_name );
  224. echo "<p id='wp-version-message'>$msg</p>";
  225. }
  226. /**
  227. * @since 2.9.0
  228. *
  229. * @return array
  230. */
  231. function get_plugin_updates() {
  232. $all_plugins = get_plugins();
  233. $upgrade_plugins = array();
  234. $current = get_site_transient( 'update_plugins' );
  235. foreach ( (array)$all_plugins as $plugin_file => $plugin_data) {
  236. if ( isset( $current->response[ $plugin_file ] ) ) {
  237. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  238. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  239. }
  240. }
  241. return $upgrade_plugins;
  242. }
  243. /**
  244. * @since 2.9.0
  245. */
  246. function wp_plugin_update_rows() {
  247. if ( !current_user_can('update_plugins' ) )
  248. return;
  249. $plugins = get_site_transient( 'update_plugins' );
  250. if ( isset($plugins->response) && is_array($plugins->response) ) {
  251. $plugins = array_keys( $plugins->response );
  252. foreach ( $plugins as $plugin_file ) {
  253. add_action( "after_plugin_row_$plugin_file", 'wp_plugin_update_row', 10, 2 );
  254. }
  255. }
  256. }
  257. /**
  258. *
  259. * @param string $file
  260. * @param array $plugin_data
  261. * @return false|void
  262. */
  263. function wp_plugin_update_row( $file, $plugin_data ) {
  264. $current = get_site_transient( 'update_plugins' );
  265. if ( !isset( $current->response[ $file ] ) )
  266. return false;
  267. $r = $current->response[ $file ];
  268. $plugins_allowedtags = array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array());
  269. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  270. $details_url = self_admin_url('plugin-install.php?tab=plugin-information&plugin=' . $r->slug . '&section=changelog&TB_iframe=true&width=600&height=800');
  271. $wp_list_table = _get_list_table('WP_Plugins_List_Table');
  272. if ( is_network_admin() || !is_multisite() ) {
  273. if ( is_network_admin() ) {
  274. $active_class = is_plugin_active_for_network( $file ) ? ' active': '';
  275. } else {
  276. $active_class = is_plugin_active( $file ) ? ' active' : '';
  277. }
  278. 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">';
  279. if ( ! current_user_can( 'update_plugins' ) ) {
  280. 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 );
  281. } elseif ( empty($r->package) ) {
  282. 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 );
  283. } else {
  284. 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 ) );
  285. }
  286. /**
  287. * Fires at the end of the update message container in each
  288. * row of the plugins list table.
  289. *
  290. * The dynamic portion of the hook name, `$file`, refers to the path
  291. * of the plugin's primary file relative to the plugins directory.
  292. *
  293. * @since 2.8.0
  294. *
  295. * @param array $plugin_data {
  296. * An array of plugin metadata.
  297. *
  298. * @type string $name The human-readable name of the plugin.
  299. * @type string $plugin_uri Plugin URI.
  300. * @type string $version Plugin version.
  301. * @type string $description Plugin description.
  302. * @type string $author Plugin author.
  303. * @type string $author_uri Plugin author URI.
  304. * @type string $text_domain Plugin text domain.
  305. * @type string $domain_path Relative path to the plugin's .mo file(s).
  306. * @type bool $network Whether the plugin can only be activated network wide.
  307. * @type string $title The human-readable title of the plugin.
  308. * @type string $author_name Plugin author's name.
  309. * @type bool $update Whether there's an available update. Default null.
  310. * }
  311. * @param array $r {
  312. * An array of metadata about the available plugin update.
  313. *
  314. * @type int $id Plugin ID.
  315. * @type string $slug Plugin slug.
  316. * @type string $new_version New plugin version.
  317. * @type string $url Plugin URL.
  318. * @type string $package Plugin update package URL.
  319. * }
  320. */
  321. do_action( "in_plugin_update_message-{$file}", $plugin_data, $r );
  322. echo '</div></td></tr>';
  323. }
  324. }
  325. /**
  326. *
  327. * @return array
  328. */
  329. function get_theme_updates() {
  330. $current = get_site_transient('update_themes');
  331. if ( ! isset( $current->response ) )
  332. return array();
  333. $update_themes = array();
  334. foreach ( $current->response as $stylesheet => $data ) {
  335. $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
  336. $update_themes[ $stylesheet ]->update = $data;
  337. }
  338. return $update_themes;
  339. }
  340. /**
  341. * @since 3.1.0
  342. */
  343. function wp_theme_update_rows() {
  344. if ( !current_user_can('update_themes' ) )
  345. return;
  346. $themes = get_site_transient( 'update_themes' );
  347. if ( isset($themes->response) && is_array($themes->response) ) {
  348. $themes = array_keys( $themes->response );
  349. foreach ( $themes as $theme ) {
  350. add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
  351. }
  352. }
  353. }
  354. /**
  355. *
  356. * @param string $theme_key
  357. * @param WP_Theme $theme
  358. * @return false|void
  359. */
  360. function wp_theme_update_row( $theme_key, $theme ) {
  361. $current = get_site_transient( 'update_themes' );
  362. if ( !isset( $current->response[ $theme_key ] ) )
  363. return false;
  364. $r = $current->response[ $theme_key ];
  365. $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
  366. $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
  367. echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  368. if ( ! current_user_can('update_themes') ) {
  369. 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 );
  370. } elseif ( empty( $r['package'] ) ) {
  371. 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'] );
  372. } else {
  373. 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) );
  374. }
  375. /**
  376. * Fires at the end of the update message container in each
  377. * row of the themes list table.
  378. *
  379. * The dynamic portion of the hook name, `$theme_key`, refers to
  380. * the theme slug as found in the WordPress.org themes repository.
  381. *
  382. * @since 3.1.0
  383. *
  384. * @param WP_Theme $theme The WP_Theme object.
  385. * @param array $r {
  386. * An array of metadata about the available theme update.
  387. *
  388. * @type string $new_version New theme version.
  389. * @type string $url Theme URL.
  390. * @type string $package Theme update package URL.
  391. * }
  392. */
  393. do_action( "in_theme_update_message-{$theme_key}", $theme, $r );
  394. echo '</div></td></tr>';
  395. }
  396. /**
  397. *
  398. * @global int $upgrading
  399. * @return false|void
  400. */
  401. function maintenance_nag() {
  402. include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
  403. global $upgrading;
  404. $nag = isset( $upgrading );
  405. if ( ! $nag ) {
  406. $failed = get_site_option( 'auto_core_update_failed' );
  407. /*
  408. * If an update failed critically, we may have copied over version.php but not other files.
  409. * In that case, if the install claims we're running the version we attempted, nag.
  410. * This is serious enough to err on the side of nagging.
  411. *
  412. * If we simply failed to update before we tried to copy any files, then assume things are
  413. * OK if they are now running the latest.
  414. *
  415. * This flag is cleared whenever a successful update occurs using Core_Upgrader.
  416. */
  417. $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
  418. if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
  419. $nag = true;
  420. }
  421. if ( ! $nag )
  422. return false;
  423. if ( current_user_can('update_core') )
  424. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  425. else
  426. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  427. echo "<div class='update-nag'>$msg</div>";
  428. }