class-wp-site-health-auto-updates.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. * Class for testing automatic updates in the WordPress code.
  4. *
  5. * @package WordPress
  6. * @subpackage Site_Health
  7. * @since 5.2.0
  8. */
  9. class WP_Site_Health_Auto_Updates {
  10. /**
  11. * WP_Site_Health_Auto_Updates constructor.
  12. * @since 5.2.0
  13. */
  14. public function __construct() {
  15. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  16. }
  17. /**
  18. * Run tests to determine if auto-updates can run.
  19. *
  20. * @since 5.2.0
  21. *
  22. * @return array The test results.
  23. */
  24. public function run_tests() {
  25. $tests = array(
  26. $this->test_constants( 'WP_AUTO_UPDATE_CORE', true ),
  27. $this->test_wp_version_check_attached(),
  28. $this->test_filters_automatic_updater_disabled(),
  29. $this->test_wp_automatic_updates_disabled(),
  30. $this->test_if_failed_update(),
  31. $this->test_vcs_abspath(),
  32. $this->test_check_wp_filesystem_method(),
  33. $this->test_all_files_writable(),
  34. $this->test_accepts_dev_updates(),
  35. $this->test_accepts_minor_updates(),
  36. );
  37. $tests = array_filter( $tests );
  38. $tests = array_map(
  39. function( $test ) {
  40. $test = (object) $test;
  41. if ( empty( $test->severity ) ) {
  42. $test->severity = 'warning';
  43. }
  44. return $test;
  45. },
  46. $tests
  47. );
  48. return $tests;
  49. }
  50. /**
  51. * Test if auto-updates related constants are set correctly.
  52. *
  53. * @since 5.2.0
  54. *
  55. * @param string $constant The name of the constant to check.
  56. * @param bool $value The value that the constant should be, if set.
  57. * @return array The test results.
  58. */
  59. public function test_constants( $constant, $value ) {
  60. if ( defined( $constant ) && constant( $constant ) != $value ) {
  61. return array(
  62. 'description' => sprintf(
  63. /* translators: %s: Name of the constant used. */
  64. __( 'The %s constant is defined and enabled.' ),
  65. "<code>$constant</code>"
  66. ),
  67. 'severity' => 'fail',
  68. );
  69. }
  70. }
  71. /**
  72. * Check if updates are intercepted by a filter.
  73. *
  74. * @since 5.2.0
  75. *
  76. * @return array The test results.
  77. */
  78. public function test_wp_version_check_attached() {
  79. if ( ! is_main_site() ) {
  80. return;
  81. }
  82. $cookies = wp_unslash( $_COOKIE );
  83. $timeout = 10;
  84. $headers = array(
  85. 'Cache-Control' => 'no-cache',
  86. );
  87. /** This filter is documented in wp-includes/class-wp-http-streams.php */
  88. $sslverify = apply_filters( 'https_local_ssl_verify', false );
  89. // Include Basic auth in loopback requests.
  90. if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
  91. $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
  92. }
  93. $url = add_query_arg(
  94. array(
  95. 'health-check-test-wp_version_check' => true,
  96. ),
  97. admin_url( 'site-health.php' )
  98. );
  99. $test = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
  100. if ( is_wp_error( $test ) ) {
  101. return array(
  102. 'description' => sprintf(
  103. /* translators: %s: Name of the filter used. */
  104. __( 'Could not confirm that the %s filter is available.' ),
  105. '<code>wp_version_check()</code>'
  106. ),
  107. 'severity' => 'warning',
  108. );
  109. }
  110. $response = wp_remote_retrieve_body( $test );
  111. if ( 'yes' !== $response ) {
  112. return array(
  113. 'description' => sprintf(
  114. /* translators: %s: Name of the filter used. */
  115. __( 'A plugin has prevented updates by disabling %s.' ),
  116. '<code>wp_version_check()</code>'
  117. ),
  118. 'severity' => 'fail',
  119. );
  120. }
  121. }
  122. /**
  123. * Check if automatic updates are disabled by a filter.
  124. *
  125. * @since 5.2.0
  126. *
  127. * @return array The test results.
  128. */
  129. public function test_filters_automatic_updater_disabled() {
  130. /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
  131. if ( apply_filters( 'automatic_updater_disabled', false ) ) {
  132. return array(
  133. 'description' => sprintf(
  134. /* translators: %s: Name of the filter used. */
  135. __( 'The %s filter is enabled.' ),
  136. '<code>automatic_updater_disabled</code>'
  137. ),
  138. 'severity' => 'fail',
  139. );
  140. }
  141. }
  142. /**
  143. * Check if automatic updates are disabled.
  144. *
  145. * @since 5.3.0
  146. *
  147. * @return array|bool The test results. False if auto updates are enabled.
  148. */
  149. public function test_wp_automatic_updates_disabled() {
  150. if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
  151. require_once( ABSPATH . 'wp-admin/includes/class-wp-automatic-updates.php' );
  152. }
  153. $auto_updates = new WP_Automatic_Updater();
  154. if ( ! $auto_updates->is_disabled() ) {
  155. return false;
  156. }
  157. return array(
  158. 'description' => __( 'All automatic updates are disabled.' ),
  159. 'severity' => 'fail',
  160. );
  161. }
  162. /**
  163. * Check if automatic updates have tried to run, but failed, previously.
  164. *
  165. * @since 5.2.0
  166. *
  167. * @return array|bool The test results. False if the auto updates failed.
  168. */
  169. function test_if_failed_update() {
  170. $failed = get_site_option( 'auto_core_update_failed' );
  171. if ( ! $failed ) {
  172. return false;
  173. }
  174. if ( ! empty( $failed['critical'] ) ) {
  175. $description = __( 'A previous automatic background update ended with a critical failure, so updates are now disabled.' );
  176. $description .= ' ' . __( 'You would have received an email because of this.' );
  177. $description .= ' ' . __( "When you've been able to update using the \"Update Now\" button on Dashboard > Updates, we'll clear this error for future update attempts." );
  178. $description .= ' ' . sprintf(
  179. /* translators: %s: Code of error shown. */
  180. __( 'The error code was %s.' ),
  181. '<code>' . $failed['error_code'] . '</code>'
  182. );
  183. return array(
  184. 'description' => $description,
  185. 'severity' => 'warning',
  186. );
  187. }
  188. $description = __( 'A previous automatic background update could not occur.' );
  189. if ( empty( $failed['retry'] ) ) {
  190. $description .= ' ' . __( 'You would have received an email because of this.' );
  191. }
  192. $description .= ' ' . __( "We'll try again with the next release." );
  193. $description .= ' ' . sprintf(
  194. /* translators: %s: Code of error shown. */
  195. __( 'The error code was %s.' ),
  196. '<code>' . $failed['error_code'] . '</code>'
  197. );
  198. return array(
  199. 'description' => $description,
  200. 'severity' => 'warning',
  201. );
  202. }
  203. /**
  204. * Check if WordPress is controlled by a VCS (Git, Subversion etc).
  205. *
  206. * @since 5.2.0
  207. *
  208. * @return array The test results.
  209. */
  210. public function test_vcs_abspath() {
  211. $context_dirs = array( ABSPATH );
  212. $vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
  213. $check_dirs = array();
  214. foreach ( $context_dirs as $context_dir ) {
  215. // Walk up from $context_dir to the root.
  216. do {
  217. $check_dirs[] = $context_dir;
  218. // Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
  219. if ( dirname( $context_dir ) == $context_dir ) {
  220. break;
  221. }
  222. // Continue one level at a time.
  223. } while ( $context_dir = dirname( $context_dir ) );
  224. }
  225. $check_dirs = array_unique( $check_dirs );
  226. // Search all directories we've found for evidence of version control.
  227. foreach ( $vcs_dirs as $vcs_dir ) {
  228. foreach ( $check_dirs as $check_dir ) {
  229. // phpcs:ignore
  230. if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
  231. break 2;
  232. }
  233. }
  234. }
  235. /** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
  236. if ( $checkout && ! apply_filters( 'automatic_updates_is_vcs_checkout', true, ABSPATH ) ) {
  237. return array(
  238. 'description' => sprintf(
  239. /* translators: 1: Folder name. 2: Version control directory. 3: Filter name. */
  240. __( 'The folder %1$s was detected as being under version control (%2$s), but the %3$s filter is allowing updates.' ),
  241. '<code>' . $check_dir . '</code>',
  242. "<code>$vcs_dir</code>",
  243. '<code>automatic_updates_is_vcs_checkout</code>'
  244. ),
  245. 'severity' => 'info',
  246. );
  247. }
  248. if ( $checkout ) {
  249. return array(
  250. 'description' => sprintf(
  251. /* translators: 1: Folder name. 2: Version control directory. */
  252. __( 'The folder %1$s was detected as being under version control (%2$s).' ),
  253. '<code>' . $check_dir . '</code>',
  254. "<code>$vcs_dir</code>"
  255. ),
  256. 'severity' => 'warning',
  257. );
  258. }
  259. return array(
  260. 'description' => __( 'No version control systems were detected.' ),
  261. 'severity' => 'pass',
  262. );
  263. }
  264. /**
  265. * Check if we can access files without providing credentials.
  266. *
  267. * @since 5.2.0
  268. *
  269. * @return array The test results.
  270. */
  271. function test_check_wp_filesystem_method() {
  272. $skin = new Automatic_Upgrader_Skin;
  273. $success = $skin->request_filesystem_credentials( false, ABSPATH );
  274. if ( ! $success ) {
  275. $description = __( 'Your installation of WordPress prompts for FTP credentials to perform updates.' );
  276. $description .= ' ' . __( '(Your site is performing updates over FTP due to file ownership. Talk to your hosting company.)' );
  277. return array(
  278. 'description' => $description,
  279. 'severity' => 'fail',
  280. );
  281. }
  282. return array(
  283. 'description' => __( "Your installation of WordPress doesn't require FTP credentials to perform updates." ),
  284. 'severity' => 'pass',
  285. );
  286. }
  287. /**
  288. * Check if core files are writable by the web user/group.
  289. *
  290. * @since 5.2.0
  291. *
  292. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  293. *
  294. * @return array|bool The test results. False if they're not writeable.
  295. */
  296. function test_all_files_writable() {
  297. global $wp_filesystem;
  298. include ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
  299. $skin = new Automatic_Upgrader_Skin;
  300. $success = $skin->request_filesystem_credentials( false, ABSPATH );
  301. if ( ! $success ) {
  302. return false;
  303. }
  304. WP_Filesystem();
  305. if ( 'direct' != $wp_filesystem->method ) {
  306. return false;
  307. }
  308. $checksums = get_core_checksums( $wp_version, 'en_US' );
  309. $dev = ( false !== strpos( $wp_version, '-' ) );
  310. // Get the last stable version's files and test against that
  311. if ( ! $checksums && $dev ) {
  312. $checksums = get_core_checksums( (float) $wp_version - 0.1, 'en_US' );
  313. }
  314. // There aren't always checksums for development releases, so just skip the test if we still can't find any
  315. if ( ! $checksums && $dev ) {
  316. return false;
  317. }
  318. if ( ! $checksums ) {
  319. $description = sprintf(
  320. /* translators: %s: WordPress version. */
  321. __( "Couldn't retrieve a list of the checksums for WordPress %s." ),
  322. $wp_version
  323. );
  324. $description .= ' ' . __( 'This could mean that connections are failing to WordPress.org.' );
  325. return array(
  326. 'description' => $description,
  327. 'severity' => 'warning',
  328. );
  329. }
  330. $unwritable_files = array();
  331. foreach ( array_keys( $checksums ) as $file ) {
  332. if ( 'wp-content' == substr( $file, 0, 10 ) ) {
  333. continue;
  334. }
  335. if ( ! file_exists( ABSPATH . $file ) ) {
  336. continue;
  337. }
  338. if ( ! is_writable( ABSPATH . $file ) ) {
  339. $unwritable_files[] = $file;
  340. }
  341. }
  342. if ( $unwritable_files ) {
  343. if ( count( $unwritable_files ) > 20 ) {
  344. $unwritable_files = array_slice( $unwritable_files, 0, 20 );
  345. $unwritable_files[] = '...';
  346. }
  347. return array(
  348. 'description' => __( 'Some files are not writable by WordPress:' ) . ' <ul><li>' . implode( '</li><li>', $unwritable_files ) . '</li></ul>',
  349. 'severity' => 'fail',
  350. );
  351. } else {
  352. return array(
  353. 'description' => __( 'All of your WordPress files are writable.' ),
  354. 'severity' => 'pass',
  355. );
  356. }
  357. }
  358. /**
  359. * Check if the install is using a development branch and can use nightly packages.
  360. *
  361. * @since 5.2.0
  362. *
  363. * @return array|bool The test results. False if it isn't a development version.
  364. */
  365. function test_accepts_dev_updates() {
  366. include ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
  367. // Only for dev versions
  368. if ( false === strpos( $wp_version, '-' ) ) {
  369. return false;
  370. }
  371. if ( defined( 'WP_AUTO_UPDATE_CORE' ) && ( 'minor' === WP_AUTO_UPDATE_CORE || false === WP_AUTO_UPDATE_CORE ) ) {
  372. return array(
  373. 'description' => sprintf(
  374. /* translators: %s: Name of the constant used. */
  375. __( 'WordPress development updates are blocked by the %s constant.' ),
  376. '<code>WP_AUTO_UPDATE_CORE</code>'
  377. ),
  378. 'severity' => 'fail',
  379. );
  380. }
  381. /** This filter is documented in wp-admin/includes/class-core-upgrader.php */
  382. if ( ! apply_filters( 'allow_dev_auto_core_updates', $wp_version ) ) {
  383. return array(
  384. 'description' => sprintf(
  385. /* translators: %s: Name of the filter used. */
  386. __( 'WordPress development updates are blocked by the %s filter.' ),
  387. '<code>allow_dev_auto_core_updates</code>'
  388. ),
  389. 'severity' => 'fail',
  390. );
  391. }
  392. }
  393. /**
  394. * Check if the site supports automatic minor updates.
  395. *
  396. * @since 5.2.0
  397. *
  398. * @return array The test results.
  399. */
  400. function test_accepts_minor_updates() {
  401. if ( defined( 'WP_AUTO_UPDATE_CORE' ) && false === WP_AUTO_UPDATE_CORE ) {
  402. return array(
  403. 'description' => sprintf(
  404. /* translators: %s: Name of the constant used. */
  405. __( 'WordPress security and maintenance releases are blocked by %s.' ),
  406. "<code>define( 'WP_AUTO_UPDATE_CORE', false );</code>"
  407. ),
  408. 'severity' => 'fail',
  409. );
  410. }
  411. /** This filter is documented in wp-admin/includes/class-core-upgrader.php */
  412. if ( ! apply_filters( 'allow_minor_auto_core_updates', true ) ) {
  413. return array(
  414. 'description' => sprintf(
  415. /* translators: %s: Name of the filter used. */
  416. __( 'WordPress security and maintenance releases are blocked by the %s filter.' ),
  417. '<code>allow_minor_auto_core_updates</code>'
  418. ),
  419. 'severity' => 'fail',
  420. );
  421. }
  422. }
  423. }