PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-admin/includes/update.php

https://gitlab.com/webkod3r/tripolis
PHP | 559 lines | 295 code | 81 blank | 183 comment | 78 complexity | ed4a24bbfdc4cea2d3a657bb4feee15a 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" aria-label="Please update WordPress now">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. /* translators: 1: plugin name, 2: details URL, 3: accessibility text, 4: version number */
  281. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>.' ),
  282. $plugin_name,
  283. esc_url( $details_url ),
  284. /* translators: 1: plugin name, 2: version number */
  285. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $r->new_version ) ),
  286. $r->new_version
  287. );
  288. } elseif ( empty( $r->package ) ) {
  289. /* translators: 1: plugin name, 2: details URL, 3: accessibility text, 4: version number */
  290. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>' ),
  291. $plugin_name,
  292. esc_url( $details_url ),
  293. /* translators: 1: plugin name, 2: version number */
  294. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $r->new_version ) ),
  295. $r->new_version
  296. );
  297. } else {
  298. /* translators: 1: plugin name, 2: details URL, 3: accessibility text, 4: version number, 5: update URL, 6: accessibility text */
  299. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a> or <a href="%5$s" class="update-link" aria-label="%6$s">update now</a>.' ),
  300. $plugin_name,
  301. esc_url( $details_url ),
  302. /* translators: 1: plugin name, 2: version number */
  303. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $r->new_version ) ),
  304. $r->new_version,
  305. wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ),
  306. /* translators: %s: plugin name */
  307. esc_attr( sprintf( __( 'Update %s now' ), $plugin_name ) )
  308. );
  309. }
  310. /**
  311. * Fires at the end of the update message container in each
  312. * row of the plugins list table.
  313. *
  314. * The dynamic portion of the hook name, `$file`, refers to the path
  315. * of the plugin's primary file relative to the plugins directory.
  316. *
  317. * @since 2.8.0
  318. *
  319. * @param array $plugin_data {
  320. * An array of plugin metadata.
  321. *
  322. * @type string $name The human-readable name of the plugin.
  323. * @type string $plugin_uri Plugin URI.
  324. * @type string $version Plugin version.
  325. * @type string $description Plugin description.
  326. * @type string $author Plugin author.
  327. * @type string $author_uri Plugin author URI.
  328. * @type string $text_domain Plugin text domain.
  329. * @type string $domain_path Relative path to the plugin's .mo file(s).
  330. * @type bool $network Whether the plugin can only be activated network wide.
  331. * @type string $title The human-readable title of the plugin.
  332. * @type string $author_name Plugin author's name.
  333. * @type bool $update Whether there's an available update. Default null.
  334. * }
  335. * @param array $r {
  336. * An array of metadata about the available plugin update.
  337. *
  338. * @type int $id Plugin ID.
  339. * @type string $slug Plugin slug.
  340. * @type string $new_version New plugin version.
  341. * @type string $url Plugin URL.
  342. * @type string $package Plugin update package URL.
  343. * }
  344. */
  345. do_action( "in_plugin_update_message-{$file}", $plugin_data, $r );
  346. echo '</div></td></tr>';
  347. }
  348. }
  349. /**
  350. *
  351. * @return array
  352. */
  353. function get_theme_updates() {
  354. $current = get_site_transient('update_themes');
  355. if ( ! isset( $current->response ) )
  356. return array();
  357. $update_themes = array();
  358. foreach ( $current->response as $stylesheet => $data ) {
  359. $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
  360. $update_themes[ $stylesheet ]->update = $data;
  361. }
  362. return $update_themes;
  363. }
  364. /**
  365. * @since 3.1.0
  366. */
  367. function wp_theme_update_rows() {
  368. if ( !current_user_can('update_themes' ) )
  369. return;
  370. $themes = get_site_transient( 'update_themes' );
  371. if ( isset($themes->response) && is_array($themes->response) ) {
  372. $themes = array_keys( $themes->response );
  373. foreach ( $themes as $theme ) {
  374. add_action( "after_theme_row_$theme", 'wp_theme_update_row', 10, 2 );
  375. }
  376. }
  377. }
  378. /**
  379. *
  380. * @param string $theme_key
  381. * @param WP_Theme $theme
  382. * @return false|void
  383. */
  384. function wp_theme_update_row( $theme_key, $theme ) {
  385. $current = get_site_transient( 'update_themes' );
  386. if ( !isset( $current->response[ $theme_key ] ) )
  387. return false;
  388. $r = $current->response[ $theme_key ];
  389. $theme_name = $theme['Name'];
  390. $details_url = add_query_arg( array( 'TB_iframe' => 'true', 'width' => 1024, 'height' => 800 ), $current->response[ $theme_key ]['url'] );
  391. $wp_list_table = _get_list_table('WP_MS_Themes_List_Table');
  392. $active = $theme->is_allowed( 'network' ) ? ' active': '';
  393. echo '<tr class="plugin-update-tr' . $active . '" id="' . esc_attr( $theme->get_stylesheet() . '-update' ) . '" data-slug="' . esc_attr( $theme->get_stylesheet() ) . '"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
  394. if ( ! current_user_can('update_themes') ) {
  395. /* translators: 1: theme name, 2: details URL, 3: accessibility text, 4: version number */
  396. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>.'),
  397. $theme_name,
  398. esc_url( $details_url ),
  399. /* translators: 1: theme name, 2: version number */
  400. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $r['new_version'] ) ),
  401. $r['new_version']
  402. );
  403. } elseif ( empty( $r['package'] ) ) {
  404. /* translators: 1: theme name, 2: details URL, 3: accessibility text, 4: version number */
  405. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ),
  406. $theme_name,
  407. esc_url( $details_url ),
  408. /* translators: 1: theme name, 2: version number */
  409. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $r['new_version'] ) ),
  410. $r['new_version']
  411. );
  412. } else {
  413. /* translators: 1: theme name, 2: details URL, 3: accessibility text, 4: version number, 5: update URL, 6: accessibility text */
  414. printf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox open-plugin-details-modal" aria-label="%3$s">View version %4$s details</a> or <a href="%5$s" class="update-link" aria-label="%6$s">update now</a>.' ),
  415. $theme_name,
  416. esc_url( $details_url ),
  417. /* translators: 1: theme name, 2: version number */
  418. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $r['new_version'] ) ),
  419. $r['new_version'],
  420. wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' ) . $theme_key, 'upgrade-theme_' . $theme_key ),
  421. /* translators: %s: theme name */
  422. esc_attr( sprintf( __( 'Update %s now' ), $theme_name ) )
  423. );
  424. }
  425. /**
  426. * Fires at the end of the update message container in each
  427. * row of the themes list table.
  428. *
  429. * The dynamic portion of the hook name, `$theme_key`, refers to
  430. * the theme slug as found in the WordPress.org themes repository.
  431. *
  432. * @since 3.1.0
  433. *
  434. * @param WP_Theme $theme The WP_Theme object.
  435. * @param array $r {
  436. * An array of metadata about the available theme update.
  437. *
  438. * @type string $new_version New theme version.
  439. * @type string $url Theme URL.
  440. * @type string $package Theme update package URL.
  441. * }
  442. */
  443. do_action( "in_theme_update_message-{$theme_key}", $theme, $r );
  444. echo '</div></td></tr>';
  445. }
  446. /**
  447. *
  448. * @global int $upgrading
  449. * @return false|void
  450. */
  451. function maintenance_nag() {
  452. include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
  453. global $upgrading;
  454. $nag = isset( $upgrading );
  455. if ( ! $nag ) {
  456. $failed = get_site_option( 'auto_core_update_failed' );
  457. /*
  458. * If an update failed critically, we may have copied over version.php but not other files.
  459. * In that case, if the install claims we're running the version we attempted, nag.
  460. * This is serious enough to err on the side of nagging.
  461. *
  462. * If we simply failed to update before we tried to copy any files, then assume things are
  463. * OK if they are now running the latest.
  464. *
  465. * This flag is cleared whenever a successful update occurs using Core_Upgrader.
  466. */
  467. $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
  468. if ( version_compare( $failed['attempted'], $wp_version, $comparison ) )
  469. $nag = true;
  470. }
  471. if ( ! $nag )
  472. return false;
  473. if ( current_user_can('update_core') )
  474. $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' );
  475. else
  476. $msg = __('An automated WordPress update has failed to complete! Please notify the site administrator.');
  477. echo "<div class='update-nag'>$msg</div>";
  478. }