update.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. * @since 2.7.0
  12. *
  13. * @return object|array|false The response from the API on success, false on failure.
  14. */
  15. function get_preferred_from_update_core() {
  16. $updates = get_core_updates();
  17. if ( ! is_array( $updates ) ) {
  18. return false;
  19. }
  20. if ( empty( $updates ) ) {
  21. return (object) array( 'response' => 'latest' );
  22. }
  23. return $updates[0];
  24. }
  25. /**
  26. * Gets available core updates.
  27. *
  28. * @since 2.7.0
  29. *
  30. * @param array $options Set $options['dismissed'] to true to show dismissed upgrades too,
  31. * set $options['available'] to false to skip not-dismissed updates.
  32. * @return array|false Array of the update objects on success, false on failure.
  33. */
  34. function get_core_updates( $options = array() ) {
  35. $options = array_merge(
  36. array(
  37. 'available' => true,
  38. 'dismissed' => false,
  39. ),
  40. $options
  41. );
  42. $dismissed = get_site_option( 'dismissed_update_core' );
  43. if ( ! is_array( $dismissed ) ) {
  44. $dismissed = array();
  45. }
  46. $from_api = get_site_transient( 'update_core' );
  47. if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) ) {
  48. return false;
  49. }
  50. $updates = $from_api->updates;
  51. $result = array();
  52. foreach ( $updates as $update ) {
  53. if ( 'autoupdate' === $update->response ) {
  54. continue;
  55. }
  56. if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
  57. if ( $options['dismissed'] ) {
  58. $update->dismissed = true;
  59. $result[] = $update;
  60. }
  61. } else {
  62. if ( $options['available'] ) {
  63. $update->dismissed = false;
  64. $result[] = $update;
  65. }
  66. }
  67. }
  68. return $result;
  69. }
  70. /**
  71. * Gets the best available (and enabled) Auto-Update for WordPress core.
  72. *
  73. * If there's 1.2.3 and 1.3 on offer, it'll choose 1.3 if the installation allows it, else, 1.2.3.
  74. *
  75. * @since 3.7.0
  76. *
  77. * @return object|false The core update offering on success, false on failure.
  78. */
  79. function find_core_auto_update() {
  80. $updates = get_site_transient( 'update_core' );
  81. if ( ! $updates || empty( $updates->updates ) ) {
  82. return false;
  83. }
  84. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  85. $auto_update = false;
  86. $upgrader = new WP_Automatic_Updater;
  87. foreach ( $updates->updates as $update ) {
  88. if ( 'autoupdate' !== $update->response ) {
  89. continue;
  90. }
  91. if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) ) {
  92. continue;
  93. }
  94. if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) ) {
  95. $auto_update = $update;
  96. }
  97. }
  98. return $auto_update;
  99. }
  100. /**
  101. * Gets and caches the checksums for the given version of WordPress.
  102. *
  103. * @since 3.7.0
  104. *
  105. * @param string $version Version string to query.
  106. * @param string $locale Locale to query.
  107. * @return array|false An array of checksums on success, false on failure.
  108. */
  109. function get_core_checksums( $version, $locale ) {
  110. $http_url = 'http://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), '', '&' );
  111. $url = $http_url;
  112. $ssl = wp_http_supports( array( 'ssl' ) );
  113. if ( $ssl ) {
  114. $url = set_url_scheme( $url, 'https' );
  115. }
  116. $options = array(
  117. 'timeout' => wp_doing_cron() ? 30 : 3,
  118. );
  119. $response = wp_remote_get( $url, $options );
  120. if ( $ssl && is_wp_error( $response ) ) {
  121. trigger_error(
  122. sprintf(
  123. /* translators: %s: Support forums URL. */
  124. __( '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="%s">support forums</a>.' ),
  125. __( 'https://wordpress.org/support/forums/' )
  126. ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
  127. headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
  128. );
  129. $response = wp_remote_get( $http_url, $options );
  130. }
  131. if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
  132. return false;
  133. }
  134. $body = trim( wp_remote_retrieve_body( $response ) );
  135. $body = json_decode( $body, true );
  136. if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) ) {
  137. return false;
  138. }
  139. return $body['checksums'];
  140. }
  141. /**
  142. * Dismisses core update.
  143. *
  144. * @since 2.7.0
  145. *
  146. * @param object $update
  147. * @return bool
  148. */
  149. function dismiss_core_update( $update ) {
  150. $dismissed = get_site_option( 'dismissed_update_core' );
  151. $dismissed[ $update->current . '|' . $update->locale ] = true;
  152. return update_site_option( 'dismissed_update_core', $dismissed );
  153. }
  154. /**
  155. * Undismisses core update.
  156. *
  157. * @since 2.7.0
  158. *
  159. * @param string $version
  160. * @param string $locale
  161. * @return bool
  162. */
  163. function undismiss_core_update( $version, $locale ) {
  164. $dismissed = get_site_option( 'dismissed_update_core' );
  165. $key = $version . '|' . $locale;
  166. if ( ! isset( $dismissed[ $key ] ) ) {
  167. return false;
  168. }
  169. unset( $dismissed[ $key ] );
  170. return update_site_option( 'dismissed_update_core', $dismissed );
  171. }
  172. /**
  173. * Finds the available update for WordPress core.
  174. *
  175. * @since 2.7.0
  176. *
  177. * @param string $version Version string to find the update for.
  178. * @param string $locale Locale to find the update for.
  179. * @return object|false The core update offering on success, false on failure.
  180. */
  181. function find_core_update( $version, $locale ) {
  182. $from_api = get_site_transient( 'update_core' );
  183. if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) ) {
  184. return false;
  185. }
  186. $updates = $from_api->updates;
  187. foreach ( $updates as $update ) {
  188. if ( $update->current == $version && $update->locale == $locale ) {
  189. return $update;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Returns core update footer message.
  196. *
  197. * @since 2.3.0
  198. *
  199. * @param string $msg
  200. * @return string
  201. */
  202. function core_update_footer( $msg = '' ) {
  203. if ( ! current_user_can( 'update_core' ) ) {
  204. /* translators: %s: WordPress version. */
  205. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  206. }
  207. $cur = get_preferred_from_update_core();
  208. if ( ! is_object( $cur ) ) {
  209. $cur = new stdClass;
  210. }
  211. if ( ! isset( $cur->current ) ) {
  212. $cur->current = '';
  213. }
  214. if ( ! isset( $cur->response ) ) {
  215. $cur->response = '';
  216. }
  217. // Include an unmodified $wp_version.
  218. require ABSPATH . WPINC . '/version.php';
  219. $is_development_version = preg_match( '/alpha|beta|RC/', $wp_version );
  220. if ( $is_development_version ) {
  221. return sprintf(
  222. /* translators: 1: WordPress version number, 2: URL to WordPress Updates screen. */
  223. __( 'You are using a development version (%1$s). Cool! Please <a href="%2$s">stay updated</a>.' ),
  224. get_bloginfo( 'version', 'display' ),
  225. network_admin_url( 'update-core.php' )
  226. );
  227. }
  228. switch ( $cur->response ) {
  229. case 'upgrade':
  230. return sprintf(
  231. '<strong><a href="%s">%s</a></strong>',
  232. network_admin_url( 'update-core.php' ),
  233. /* translators: %s: WordPress version. */
  234. sprintf( __( 'Get Version %s' ), $cur->current )
  235. );
  236. case 'latest':
  237. default:
  238. /* translators: %s: WordPress version. */
  239. return sprintf( __( 'Version %s' ), get_bloginfo( 'version', 'display' ) );
  240. }
  241. }
  242. /**
  243. * Returns core update notification message.
  244. *
  245. * @since 2.3.0
  246. *
  247. * @global string $pagenow The filename of the current screen.
  248. * @return void|false
  249. */
  250. function update_nag() {
  251. global $pagenow;
  252. if ( is_multisite() && ! current_user_can( 'update_core' ) ) {
  253. return false;
  254. }
  255. if ( 'update-core.php' === $pagenow ) {
  256. return;
  257. }
  258. $cur = get_preferred_from_update_core();
  259. if ( ! isset( $cur->response ) || 'upgrade' !== $cur->response ) {
  260. return false;
  261. }
  262. $version_url = sprintf(
  263. /* translators: %s: WordPress version. */
  264. esc_url( __( 'https://wordpress.org/support/wordpress-version/version-%s/' ) ),
  265. sanitize_title( $cur->current )
  266. );
  267. if ( current_user_can( 'update_core' ) ) {
  268. $msg = sprintf(
  269. /* translators: 1: URL to WordPress release notes, 2: New WordPress version, 3: URL to network admin, 4: Accessibility text. */
  270. __( '<a href="%1$s">WordPress %2$s</a> is available! <a href="%3$s" aria-label="%4$s">Please update now</a>.' ),
  271. $version_url,
  272. $cur->current,
  273. network_admin_url( 'update-core.php' ),
  274. esc_attr__( 'Please update WordPress now' )
  275. );
  276. } else {
  277. $msg = sprintf(
  278. /* translators: 1: URL to WordPress release notes, 2: New WordPress version. */
  279. __( '<a href="%1$s">WordPress %2$s</a> is available! Please notify the site administrator.' ),
  280. $version_url,
  281. $cur->current
  282. );
  283. }
  284. echo "<div class='update-nag notice notice-warning inline'>$msg</div>";
  285. }
  286. /**
  287. * Displays WordPress version and active theme in the 'At a Glance' dashboard widget.
  288. *
  289. * @since 2.5.0
  290. */
  291. function update_right_now_message() {
  292. $theme_name = wp_get_theme();
  293. if ( current_user_can( 'switch_themes' ) ) {
  294. $theme_name = sprintf( '<a href="themes.php">%1$s</a>', $theme_name );
  295. }
  296. $msg = '';
  297. if ( current_user_can( 'update_core' ) ) {
  298. $cur = get_preferred_from_update_core();
  299. if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
  300. $msg .= sprintf(
  301. '<a href="%s" class="button" aria-describedby="wp-version">%s</a> ',
  302. network_admin_url( 'update-core.php' ),
  303. /* translators: %s: WordPress version number, or 'Latest' string. */
  304. sprintf( __( 'Update to %s' ), $cur->current ? $cur->current : __( 'Latest' ) )
  305. );
  306. }
  307. }
  308. /* translators: 1: Version number, 2: Theme name. */
  309. $content = __( 'WordPress %1$s running %2$s theme.' );
  310. /**
  311. * Filters the text displayed in the 'At a Glance' dashboard widget.
  312. *
  313. * Prior to 3.8.0, the widget was named 'Right Now'.
  314. *
  315. * @since 4.4.0
  316. *
  317. * @param string $content Default text.
  318. */
  319. $content = apply_filters( 'update_right_now_text', $content );
  320. $msg .= sprintf( '<span id="wp-version">' . $content . '</span>', get_bloginfo( 'version', 'display' ), $theme_name );
  321. echo "<p id='wp-version-message'>$msg</p>";
  322. }
  323. /**
  324. * Retrieves plugins with updates available.
  325. *
  326. * @since 2.9.0
  327. *
  328. * @return array
  329. */
  330. function get_plugin_updates() {
  331. $all_plugins = get_plugins();
  332. $upgrade_plugins = array();
  333. $current = get_site_transient( 'update_plugins' );
  334. foreach ( (array) $all_plugins as $plugin_file => $plugin_data ) {
  335. if ( isset( $current->response[ $plugin_file ] ) ) {
  336. $upgrade_plugins[ $plugin_file ] = (object) $plugin_data;
  337. $upgrade_plugins[ $plugin_file ]->update = $current->response[ $plugin_file ];
  338. }
  339. }
  340. return $upgrade_plugins;
  341. }
  342. /**
  343. * Adds a callback to display update information for plugins with updates available.
  344. *
  345. * @since 2.9.0
  346. */
  347. function wp_plugin_update_rows() {
  348. if ( ! current_user_can( 'update_plugins' ) ) {
  349. return;
  350. }
  351. $plugins = get_site_transient( 'update_plugins' );
  352. if ( isset( $plugins->response ) && is_array( $plugins->response ) ) {
  353. $plugins = array_keys( $plugins->response );
  354. foreach ( $plugins as $plugin_file ) {
  355. add_action( "after_plugin_row_{$plugin_file}", 'wp_plugin_update_row', 10, 2 );
  356. }
  357. }
  358. }
  359. /**
  360. * Displays update information for a plugin.
  361. *
  362. * @since 2.3.0
  363. *
  364. * @param string $file Plugin basename.
  365. * @param array $plugin_data Plugin information.
  366. * @return void|false
  367. */
  368. function wp_plugin_update_row( $file, $plugin_data ) {
  369. $current = get_site_transient( 'update_plugins' );
  370. if ( ! isset( $current->response[ $file ] ) ) {
  371. return false;
  372. }
  373. $response = $current->response[ $file ];
  374. $plugins_allowedtags = array(
  375. 'a' => array(
  376. 'href' => array(),
  377. 'title' => array(),
  378. ),
  379. 'abbr' => array( 'title' => array() ),
  380. 'acronym' => array( 'title' => array() ),
  381. 'code' => array(),
  382. 'em' => array(),
  383. 'strong' => array(),
  384. );
  385. $plugin_name = wp_kses( $plugin_data['Name'], $plugins_allowedtags );
  386. $plugin_slug = isset( $response->slug ) ? $response->slug : $response->id;
  387. if ( isset( $response->slug ) ) {
  388. $details_url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . $plugin_slug . '&section=changelog' );
  389. } elseif ( isset( $response->url ) ) {
  390. $details_url = $response->url;
  391. } else {
  392. $details_url = $plugin_data['PluginURI'];
  393. }
  394. $details_url = add_query_arg(
  395. array(
  396. 'TB_iframe' => 'true',
  397. 'width' => 600,
  398. 'height' => 800,
  399. ),
  400. $details_url
  401. );
  402. /** @var WP_Plugins_List_Table $wp_list_table */
  403. $wp_list_table = _get_list_table(
  404. 'WP_Plugins_List_Table',
  405. array(
  406. 'screen' => get_current_screen(),
  407. )
  408. );
  409. if ( is_network_admin() || ! is_multisite() ) {
  410. if ( is_network_admin() ) {
  411. $active_class = is_plugin_active_for_network( $file ) ? ' active' : '';
  412. } else {
  413. $active_class = is_plugin_active( $file ) ? ' active' : '';
  414. }
  415. $requires_php = isset( $response->requires_php ) ? $response->requires_php : null;
  416. $compatible_php = is_php_version_compatible( $requires_php );
  417. $notice_type = $compatible_php ? 'notice-warning' : 'notice-error';
  418. printf(
  419. '<tr class="plugin-update-tr%s" id="%s" data-slug="%s" data-plugin="%s">' .
  420. '<td colspan="%s" class="plugin-update colspanchange">' .
  421. '<div class="update-message notice inline %s notice-alt"><p>',
  422. $active_class,
  423. esc_attr( $plugin_slug . '-update' ),
  424. esc_attr( $plugin_slug ),
  425. esc_attr( $file ),
  426. esc_attr( $wp_list_table->get_column_count() ),
  427. $notice_type
  428. );
  429. if ( ! current_user_can( 'update_plugins' ) ) {
  430. printf(
  431. /* translators: 1: Plugin name, 2: Details URL, 3: Additional link attributes, 4: Version number. */
  432. __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.' ),
  433. $plugin_name,
  434. esc_url( $details_url ),
  435. sprintf(
  436. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  437. /* translators: 1: Plugin name, 2: Version number. */
  438. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
  439. ),
  440. esc_attr( $response->new_version )
  441. );
  442. } elseif ( empty( $response->package ) ) {
  443. printf(
  444. /* translators: 1: Plugin name, 2: Details URL, 3: Additional link attributes, 4: Version number. */
  445. __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>. <em>Automatic update is unavailable for this plugin.</em>' ),
  446. $plugin_name,
  447. esc_url( $details_url ),
  448. sprintf(
  449. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  450. /* translators: 1: Plugin name, 2: Version number. */
  451. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
  452. ),
  453. esc_attr( $response->new_version )
  454. );
  455. } else {
  456. if ( $compatible_php ) {
  457. printf(
  458. /* translators: 1: Plugin name, 2: Details URL, 3: Additional link attributes, 4: Version number, 5: Update URL, 6: Additional link attributes. */
  459. __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s" %6$s>update now</a>.' ),
  460. $plugin_name,
  461. esc_url( $details_url ),
  462. sprintf(
  463. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  464. /* translators: 1: Plugin name, 2: Version number. */
  465. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
  466. ),
  467. esc_attr( $response->new_version ),
  468. wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $file, 'upgrade-plugin_' . $file ),
  469. sprintf(
  470. 'class="update-link" aria-label="%s"',
  471. /* translators: %s: Plugin name. */
  472. esc_attr( sprintf( _x( 'Update %s now', 'plugin' ), $plugin_name ) )
  473. )
  474. );
  475. } else {
  476. printf(
  477. /* translators: 1: Plugin name, 2: Details URL, 3: Additional link attributes, 4: Version number 5: URL to Update PHP page. */
  478. __( 'There is a new version of %1$s available, but it does not work with your version of PHP. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s">learn more about updating PHP</a>.' ),
  479. $plugin_name,
  480. esc_url( $details_url ),
  481. sprintf(
  482. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  483. /* translators: 1: Plugin name, 2: Version number. */
  484. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $plugin_name, $response->new_version ) )
  485. ),
  486. esc_attr( $response->new_version ),
  487. esc_url( wp_get_update_php_url() )
  488. );
  489. wp_update_php_annotation( '<br><em>', '</em>' );
  490. }
  491. }
  492. /**
  493. * Fires at the end of the update message container in each
  494. * row of the plugins list table.
  495. *
  496. * The dynamic portion of the hook name, `$file`, refers to the path
  497. * of the plugin's primary file relative to the plugins directory.
  498. *
  499. * @since 2.8.0
  500. *
  501. * @param array $plugin_data An array of plugin metadata. See get_plugin_data()
  502. * and the {@see 'plugin_row_meta'} filter for the list
  503. * of possible values.
  504. * @param object $response {
  505. * An object of metadata about the available plugin update.
  506. *
  507. * @type string $id Plugin ID, e.g. `w.org/plugins/[plugin-name]`.
  508. * @type string $slug Plugin slug.
  509. * @type string $plugin Plugin basename.
  510. * @type string $new_version New plugin version.
  511. * @type string $url Plugin URL.
  512. * @type string $package Plugin update package URL.
  513. * @type string[] $icons An array of plugin icon URLs.
  514. * @type string[] $banners An array of plugin banner URLs.
  515. * @type string[] $banners_rtl An array of plugin RTL banner URLs.
  516. * @type string $requires The version of WordPress which the plugin requires.
  517. * @type string $tested The version of WordPress the plugin is tested against.
  518. * @type string $requires_php The version of PHP which the plugin requires.
  519. * }
  520. */
  521. do_action( "in_plugin_update_message-{$file}", $plugin_data, $response ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  522. echo '</p></div></td></tr>';
  523. }
  524. }
  525. /**
  526. * Retrieves themes with updates available.
  527. *
  528. * @since 2.9.0
  529. *
  530. * @return array
  531. */
  532. function get_theme_updates() {
  533. $current = get_site_transient( 'update_themes' );
  534. if ( ! isset( $current->response ) ) {
  535. return array();
  536. }
  537. $update_themes = array();
  538. foreach ( $current->response as $stylesheet => $data ) {
  539. $update_themes[ $stylesheet ] = wp_get_theme( $stylesheet );
  540. $update_themes[ $stylesheet ]->update = $data;
  541. }
  542. return $update_themes;
  543. }
  544. /**
  545. * Adds a callback to display update information for themes with updates available.
  546. *
  547. * @since 3.1.0
  548. */
  549. function wp_theme_update_rows() {
  550. if ( ! current_user_can( 'update_themes' ) ) {
  551. return;
  552. }
  553. $themes = get_site_transient( 'update_themes' );
  554. if ( isset( $themes->response ) && is_array( $themes->response ) ) {
  555. $themes = array_keys( $themes->response );
  556. foreach ( $themes as $theme ) {
  557. add_action( "after_theme_row_{$theme}", 'wp_theme_update_row', 10, 2 );
  558. }
  559. }
  560. }
  561. /**
  562. * Displays update information for a theme.
  563. *
  564. * @since 3.1.0
  565. *
  566. * @param string $theme_key Theme stylesheet.
  567. * @param WP_Theme $theme Theme object.
  568. * @return void|false
  569. */
  570. function wp_theme_update_row( $theme_key, $theme ) {
  571. $current = get_site_transient( 'update_themes' );
  572. if ( ! isset( $current->response[ $theme_key ] ) ) {
  573. return false;
  574. }
  575. $response = $current->response[ $theme_key ];
  576. $details_url = add_query_arg(
  577. array(
  578. 'TB_iframe' => 'true',
  579. 'width' => 1024,
  580. 'height' => 800,
  581. ),
  582. $current->response[ $theme_key ]['url']
  583. );
  584. /** @var WP_MS_Themes_List_Table $wp_list_table */
  585. $wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
  586. $active = $theme->is_allowed( 'network' ) ? ' active' : '';
  587. $requires_wp = isset( $response['requires'] ) ? $response['requires'] : null;
  588. $requires_php = isset( $response['requires_php'] ) ? $response['requires_php'] : null;
  589. $compatible_wp = is_wp_version_compatible( $requires_wp );
  590. $compatible_php = is_php_version_compatible( $requires_php );
  591. printf(
  592. '<tr class="plugin-update-tr%s" id="%s" data-slug="%s">' .
  593. '<td colspan="%s" class="plugin-update colspanchange">' .
  594. '<div class="update-message notice inline notice-warning notice-alt"><p>',
  595. $active,
  596. esc_attr( $theme->get_stylesheet() . '-update' ),
  597. esc_attr( $theme->get_stylesheet() ),
  598. $wp_list_table->get_column_count()
  599. );
  600. if ( $compatible_wp && $compatible_php ) {
  601. if ( ! current_user_can( 'update_themes' ) ) {
  602. printf(
  603. /* translators: 1: Theme name, 2: Details URL, 3: Additional link attributes, 4: Version number. */
  604. __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.' ),
  605. $theme['Name'],
  606. esc_url( $details_url ),
  607. sprintf(
  608. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  609. /* translators: 1: Theme name, 2: Version number. */
  610. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme['Name'], $response['new_version'] ) )
  611. ),
  612. $response['new_version']
  613. );
  614. } elseif ( empty( $response['package'] ) ) {
  615. printf(
  616. /* translators: 1: Theme name, 2: Details URL, 3: Additional link attributes, 4: Version number. */
  617. __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>. <em>Automatic update is unavailable for this theme.</em>' ),
  618. $theme['Name'],
  619. esc_url( $details_url ),
  620. sprintf(
  621. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  622. /* translators: 1: Theme name, 2: Version number. */
  623. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme['Name'], $response['new_version'] ) )
  624. ),
  625. $response['new_version']
  626. );
  627. } else {
  628. printf(
  629. /* translators: 1: Theme name, 2: Details URL, 3: Additional link attributes, 4: Version number, 5: Update URL, 6: Additional link attributes. */
  630. __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a> or <a href="%5$s" %6$s>update now</a>.' ),
  631. $theme['Name'],
  632. esc_url( $details_url ),
  633. sprintf(
  634. 'class="thickbox open-plugin-details-modal" aria-label="%s"',
  635. /* translators: 1: Theme name, 2: Version number. */
  636. esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme['Name'], $response['new_version'] ) )
  637. ),
  638. $response['new_version'],
  639. wp_nonce_url( self_admin_url( 'update.php?action=upgrade-theme&theme=' ) . $theme_key, 'upgrade-theme_' . $theme_key ),
  640. sprintf(
  641. 'class="update-link" aria-label="%s"',
  642. /* translators: %s: Theme name. */
  643. esc_attr( sprintf( _x( 'Update %s now', 'theme' ), $theme['Name'] ) )
  644. )
  645. );
  646. }
  647. } else {
  648. if ( ! $compatible_wp && ! $compatible_php ) {
  649. printf(
  650. /* translators: %s: Theme name. */
  651. __( 'There is a new version of %s available, but it does not work with your versions of WordPress and PHP.' ),
  652. $theme['Name']
  653. );
  654. if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
  655. printf(
  656. /* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
  657. ' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
  658. self_admin_url( 'update-core.php' ),
  659. esc_url( wp_get_update_php_url() )
  660. );
  661. wp_update_php_annotation( '</p><p><em>', '</em>' );
  662. } elseif ( current_user_can( 'update_core' ) ) {
  663. printf(
  664. /* translators: %s: URL to WordPress Updates screen. */
  665. ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
  666. self_admin_url( 'update-core.php' )
  667. );
  668. } elseif ( current_user_can( 'update_php' ) ) {
  669. printf(
  670. /* translators: %s: URL to Update PHP page. */
  671. ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
  672. esc_url( wp_get_update_php_url() )
  673. );
  674. wp_update_php_annotation( '</p><p><em>', '</em>' );
  675. }
  676. } elseif ( ! $compatible_wp ) {
  677. printf(
  678. /* translators: %s: Theme name. */
  679. __( 'There is a new version of %s available, but it does not work with your version of WordPress.' ),
  680. $theme['Name']
  681. );
  682. if ( current_user_can( 'update_core' ) ) {
  683. printf(
  684. /* translators: %s: URL to WordPress Updates screen. */
  685. ' ' . __( '<a href="%s">Please update WordPress</a>.' ),
  686. self_admin_url( 'update-core.php' )
  687. );
  688. }
  689. } elseif ( ! $compatible_php ) {
  690. printf(
  691. /* translators: %s: Theme name. */
  692. __( 'There is a new version of %s available, but it does not work with your version of PHP.' ),
  693. $theme['Name']
  694. );
  695. if ( current_user_can( 'update_php' ) ) {
  696. printf(
  697. /* translators: %s: URL to Update PHP page. */
  698. ' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
  699. esc_url( wp_get_update_php_url() )
  700. );
  701. wp_update_php_annotation( '</p><p><em>', '</em>' );
  702. }
  703. }
  704. }
  705. /**
  706. * Fires at the end of the update message container in each
  707. * row of the themes list table.
  708. *
  709. * The dynamic portion of the hook name, `$theme_key`, refers to
  710. * the theme slug as found in the WordPress.org themes repository.
  711. *
  712. * @since 3.1.0
  713. *
  714. * @param WP_Theme $theme The WP_Theme object.
  715. * @param array $response {
  716. * An array of metadata about the available theme update.
  717. *
  718. * @type string $new_version New theme version.
  719. * @type string $url Theme URL.
  720. * @type string $package Theme update package URL.
  721. * }
  722. */
  723. do_action( "in_theme_update_message-{$theme_key}", $theme, $response ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
  724. echo '</p></div></td></tr>';
  725. }
  726. /**
  727. * Displays maintenance nag HTML message.
  728. *
  729. * @since 2.7.0
  730. *
  731. * @global int $upgrading
  732. * @return void|false
  733. */
  734. function maintenance_nag() {
  735. // Include an unmodified $wp_version.
  736. require ABSPATH . WPINC . '/version.php';
  737. global $upgrading;
  738. $nag = isset( $upgrading );
  739. if ( ! $nag ) {
  740. $failed = get_site_option( 'auto_core_update_failed' );
  741. /*
  742. * If an update failed critically, we may have copied over version.php but not other files.
  743. * In that case, if the installation claims we're running the version we attempted, nag.
  744. * This is serious enough to err on the side of nagging.
  745. *
  746. * If we simply failed to update before we tried to copy any files, then assume things are
  747. * OK if they are now running the latest.
  748. *
  749. * This flag is cleared whenever a successful update occurs using Core_Upgrader.
  750. */
  751. $comparison = ! empty( $failed['critical'] ) ? '>=' : '>';
  752. if ( isset( $failed['attempted'] ) && version_compare( $failed['attempted'], $wp_version, $comparison ) ) {
  753. $nag = true;
  754. }
  755. }
  756. if ( ! $nag ) {
  757. return false;
  758. }
  759. if ( current_user_can( 'update_core' ) ) {
  760. $msg = sprintf(
  761. /* translators: %s: URL to WordPress Updates screen. */
  762. __( 'An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.' ),
  763. 'update-core.php'
  764. );
  765. } else {
  766. $msg = __( 'An automated WordPress update has failed to complete! Please notify the site administrator.' );
  767. }
  768. echo "<div class='update-nag notice notice-warning inline'>$msg</div>";
  769. }
  770. /**
  771. * Prints the JavaScript templates for update admin notices.
  772. *
  773. * @since 4.6.0
  774. *
  775. * Template takes one argument with four values:
  776. *
  777. * param {object} data {
  778. * Arguments for admin notice.
  779. *
  780. * @type string id ID of the notice.
  781. * @type string className Class names for the notice.
  782. * @type string message The notice's message.
  783. * @type string type The type of update the notice is for. Either 'plugin' or 'theme'.
  784. * }
  785. */
  786. function wp_print_admin_notice_templates() {
  787. ?>
  788. <script id="tmpl-wp-updates-admin-notice" type="text/html">
  789. <div <# if ( data.id ) { #>id="{{ data.id }}"<# } #> class="notice {{ data.className }}"><p>{{{ data.message }}}</p></div>
  790. </script>
  791. <script id="tmpl-wp-bulk-updates-admin-notice" type="text/html">
  792. <div id="{{ data.id }}" class="{{ data.className }} notice <# if ( data.errors ) { #>notice-error<# } else { #>notice-success<# } #>">
  793. <p>
  794. <# if ( data.successes ) { #>
  795. <# if ( 1 === data.successes ) { #>
  796. <# if ( 'plugin' === data.type ) { #>
  797. <?php
  798. /* translators: %s: Number of plugins. */
  799. printf( __( '%s plugin successfully updated.' ), '{{ data.successes }}' );
  800. ?>
  801. <# } else { #>
  802. <?php
  803. /* translators: %s: Number of themes. */
  804. printf( __( '%s theme successfully updated.' ), '{{ data.successes }}' );
  805. ?>
  806. <# } #>
  807. <# } else { #>
  808. <# if ( 'plugin' === data.type ) { #>
  809. <?php
  810. /* translators: %s: Number of plugins. */
  811. printf( __( '%s plugins successfully updated.' ), '{{ data.successes }}' );
  812. ?>
  813. <# } else { #>
  814. <?php
  815. /* translators: %s: Number of themes. */
  816. printf( __( '%s themes successfully updated.' ), '{{ data.successes }}' );
  817. ?>
  818. <# } #>
  819. <# } #>
  820. <# } #>
  821. <# if ( data.errors ) { #>
  822. <button class="button-link bulk-action-errors-collapsed" aria-expanded="false">
  823. <# if ( 1 === data.errors ) { #>
  824. <?php
  825. /* translators: %s: Number of failed updates. */
  826. printf( __( '%s update failed.' ), '{{ data.errors }}' );
  827. ?>
  828. <# } else { #>
  829. <?php
  830. /* translators: %s: Number of failed updates. */
  831. printf( __( '%s updates failed.' ), '{{ data.errors }}' );
  832. ?>
  833. <# } #>
  834. <span class="screen-reader-text"><?php _e( 'Show more details' ); ?></span>
  835. <span class="toggle-indicator" aria-hidden="true"></span>
  836. </button>
  837. <# } #>
  838. </p>
  839. <# if ( data.errors ) { #>
  840. <ul class="bulk-action-errors hidden">
  841. <# _.each( data.errorMessages, function( errorMessage ) { #>
  842. <li>{{ errorMessage }}</li>
  843. <# } ); #>
  844. </ul>
  845. <# } #>
  846. </div>
  847. </script>
  848. <?php
  849. }
  850. /**
  851. * Prints the JavaScript templates for update and deletion rows in list tables.
  852. *
  853. * @since 4.6.0
  854. *
  855. * The update template takes one argument with four values:
  856. *
  857. * param {object} data {
  858. * Arguments for the update row
  859. *
  860. * @type string slug Plugin slug.
  861. * @type string plugin Plugin base name.
  862. * @type string colspan The number of table columns this row spans.
  863. * @type string content The row content.
  864. * }
  865. *
  866. * The delete template takes one argument with four values:
  867. *
  868. * param {object} data {
  869. * Arguments for the update row
  870. *
  871. * @type string slug Plugin slug.
  872. * @type string plugin Plugin base name.
  873. * @type string name Plugin name.
  874. * @type string colspan The number of table columns this row spans.
  875. * }
  876. */
  877. function wp_print_update_row_templates() {
  878. ?>
  879. <script id="tmpl-item-update-row" type="text/template">
  880. <tr class="plugin-update-tr update" id="{{ data.slug }}-update" data-slug="{{ data.slug }}" <# if ( data.plugin ) { #>data-plugin="{{ data.plugin }}"<# } #>>
  881. <td colspan="{{ data.colspan }}" class="plugin-update colspanchange">
  882. {{{ data.content }}}
  883. </td>
  884. </tr>
  885. </script>
  886. <script id="tmpl-item-deleted-row" type="text/template">
  887. <tr class="plugin-deleted-tr inactive deleted" id="{{ data.slug }}-deleted" data-slug="{{ data.slug }}" <# if ( data.plugin ) { #>data-plugin="{{ data.plugin }}"<# } #>>
  888. <td colspan="{{ data.colspan }}" class="plugin-update colspanchange">
  889. <# if ( data.plugin ) { #>
  890. <?php
  891. printf(
  892. /* translators: %s: Plugin name. */
  893. _x( '%s was successfully deleted.', 'plugin' ),
  894. '<strong>{{{ data.name }}}</strong>'
  895. );
  896. ?>
  897. <# } else { #>
  898. <?php
  899. printf(
  900. /* translators: %s: Theme name. */
  901. _x( '%s was successfully deleted.', 'theme' ),
  902. '<strong>{{{ data.name }}}</strong>'
  903. );
  904. ?>
  905. <# } #>
  906. </td>
  907. </tr>
  908. </script>
  909. <?php
  910. }
  911. /**
  912. * Displays a notice when the user is in recovery mode.
  913. *
  914. * @since 5.2.0
  915. */
  916. function wp_recovery_mode_nag() {
  917. if ( ! wp_is_recovery_mode() ) {
  918. return;
  919. }
  920. $url = wp_login_url();
  921. $url = add_query_arg( 'action', WP_Recovery_Mode::EXIT_ACTION, $url );
  922. $url = wp_nonce_url( $url, WP_Recovery_Mode::EXIT_ACTION );
  923. ?>
  924. <div class="notice notice-info">
  925. <p>
  926. <?php
  927. printf(
  928. /* translators: %s: Recovery Mode exit link. */
  929. __( 'You are in recovery mode. This means there may be an error with a theme or plugin. To exit recovery mode, log out or use the Exit button. <a href="%s">Exit Recovery Mode</a>' ),
  930. esc_url( $url )
  931. );
  932. ?>
  933. </p>
  934. </div>
  935. <?php
  936. }
  937. /**
  938. * Checks whether auto-updates are enabled.
  939. *
  940. * @since 5.5.0
  941. *
  942. * @param string $type The type of update being checked: 'theme' or 'plugin'.
  943. * @return bool True if auto-updates are enabled for `$type`, false otherwise.
  944. */
  945. function wp_is_auto_update_enabled_for_type( $type ) {
  946. if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
  947. require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
  948. }
  949. $updater = new WP_Automatic_Updater();
  950. $enabled = ! $updater->is_disabled();
  951. switch ( $type ) {
  952. case 'plugin':
  953. /**
  954. * Filters whether plugins auto-update is enabled.
  955. *
  956. * @since 5.5.0
  957. *
  958. * @param bool $enabled True if plugins auto-update is enabled, false otherwise.
  959. */
  960. return apply_filters( 'plugins_auto_update_enabled', $enabled );
  961. case 'theme':
  962. /**
  963. * Filters whether themes auto-update is enabled.
  964. *
  965. * @since 5.5.0
  966. *
  967. * @param bool $enabled True if themes auto-update is enabled, false otherwise.
  968. */
  969. return apply_filters( 'themes_auto_update_enabled', $enabled );
  970. }
  971. return false;
  972. }
  973. /**
  974. * Checks whether auto-updates are forced for an item.
  975. *
  976. * @since 5.6.0
  977. *
  978. * @param string $type The type of update being checked: 'theme' or 'plugin'.
  979. * @param bool|null $update Whether to update. The value of null is internally used
  980. * to detect whether nothing has hooked into this filter.
  981. * @param object $item The update offer.
  982. * @return bool True if auto-updates are forced for `$item`, false otherwise.
  983. */
  984. function wp_is_auto_update_forced_for_item( $type, $update, $item ) {
  985. /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
  986. return apply_filters( "auto_update_{$type}", $update, $item );
  987. }
  988. /**
  989. * Determines the appropriate auto-update message to be displayed.
  990. *
  991. * @since 5.5.0
  992. *
  993. * @return string The update message to be shown.
  994. */
  995. function wp_get_auto_update_message() {
  996. $next_update_time = wp_next_scheduled( 'wp_version_check' );
  997. // Check if the event exists.
  998. if ( false === $next_update_time ) {
  999. $message = __( 'Automatic update not scheduled. There may be a problem with WP-Cron.' );
  1000. } else {
  1001. $time_to_next_update = human_time_diff( (int) $next_update_time );
  1002. // See if cron is overdue.
  1003. $overdue = ( time() - $next_update_time ) > 0;
  1004. if ( $overdue ) {
  1005. $message = sprintf(
  1006. /* translators: %s: Duration that WP-Cron has been overdue. */
  1007. __( 'Automatic update overdue by %s. There may be a problem with WP-Cron.' ),
  1008. $time_to_next_update
  1009. );
  1010. } else {
  1011. $message = sprintf(
  1012. /* translators: %s: Time until the next update. */
  1013. __( 'Automatic update scheduled in %s.' ),
  1014. $time_to_next_update
  1015. );
  1016. }
  1017. }
  1018. return $message;
  1019. }