wp/wp-admin/includes/class-wp-site-health-auto-updates.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    25 	 *
    25 	 *
    26 	 * @return array The test results.
    26 	 * @return array The test results.
    27 	 */
    27 	 */
    28 	public function run_tests() {
    28 	public function run_tests() {
    29 		$tests = array(
    29 		$tests = array(
    30 			$this->test_constants( 'WP_AUTO_UPDATE_CORE', array( true, 'minor' ) ),
    30 			$this->test_constants( 'WP_AUTO_UPDATE_CORE', array( true, 'beta', 'rc', 'development', 'branch-development', 'minor' ) ),
    31 			$this->test_wp_version_check_attached(),
    31 			$this->test_wp_version_check_attached(),
    32 			$this->test_filters_automatic_updater_disabled(),
    32 			$this->test_filters_automatic_updater_disabled(),
    33 			$this->test_wp_automatic_updates_disabled(),
    33 			$this->test_wp_automatic_updates_disabled(),
    34 			$this->test_if_failed_update(),
    34 			$this->test_if_failed_update(),
    35 			$this->test_vcs_abspath(),
    35 			$this->test_vcs_abspath(),
    88 	 * @since 5.2.0
    88 	 * @since 5.2.0
    89 	 *
    89 	 *
    90 	 * @return array The test results.
    90 	 * @return array The test results.
    91 	 */
    91 	 */
    92 	public function test_wp_version_check_attached() {
    92 	public function test_wp_version_check_attached() {
    93 		if ( ! is_main_site() ) {
    93 		if ( ( ! is_multisite() || is_main_site() && is_network_admin() )
    94 			return;
    94 			&& ! has_filter( 'wp_version_check', 'wp_version_check' )
    95 		}
    95 		) {
    96 
       
    97 		$cookies = wp_unslash( $_COOKIE );
       
    98 		$timeout = 10;
       
    99 		$headers = array(
       
   100 			'Cache-Control' => 'no-cache',
       
   101 		);
       
   102 		/** This filter is documented in wp-includes/class-wp-http-streams.php */
       
   103 		$sslverify = apply_filters( 'https_local_ssl_verify', false );
       
   104 
       
   105 		// Include Basic auth in loopback requests.
       
   106 		if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
       
   107 			$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
       
   108 		}
       
   109 
       
   110 		$url = add_query_arg(
       
   111 			array(
       
   112 				'health-check-test-wp_version_check' => true,
       
   113 			),
       
   114 			admin_url( 'site-health.php' )
       
   115 		);
       
   116 
       
   117 		$test = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
       
   118 
       
   119 		if ( is_wp_error( $test ) ) {
       
   120 			return array(
       
   121 				'description' => sprintf(
       
   122 					/* translators: %s: Name of the filter used. */
       
   123 					__( 'Could not confirm that the %s filter is available.' ),
       
   124 					'<code>wp_version_check()</code>'
       
   125 				),
       
   126 				'severity'    => 'warning',
       
   127 			);
       
   128 		}
       
   129 
       
   130 		$response = wp_remote_retrieve_body( $test );
       
   131 
       
   132 		if ( 'yes' !== $response ) {
       
   133 			return array(
    96 			return array(
   134 				'description' => sprintf(
    97 				'description' => sprintf(
   135 					/* translators: %s: Name of the filter used. */
    98 					/* translators: %s: Name of the filter used. */
   136 					__( 'A plugin has prevented updates by disabling %s.' ),
    99 					__( 'A plugin has prevented updates by disabling %s.' ),
   137 					'<code>wp_version_check()</code>'
   100 					'<code>wp_version_check()</code>'
   165 	/**
   128 	/**
   166 	 * Check if automatic updates are disabled.
   129 	 * Check if automatic updates are disabled.
   167 	 *
   130 	 *
   168 	 * @since 5.3.0
   131 	 * @since 5.3.0
   169 	 *
   132 	 *
   170 	 * @return array|bool The test results. False if auto-updates are enabled.
   133 	 * @return array|false The test results. False if auto-updates are enabled.
   171 	 */
   134 	 */
   172 	public function test_wp_automatic_updates_disabled() {
   135 	public function test_wp_automatic_updates_disabled() {
   173 		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
   136 		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
   174 			require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
   137 			require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
   175 		}
   138 		}
   189 	/**
   152 	/**
   190 	 * Check if automatic updates have tried to run, but failed, previously.
   153 	 * Check if automatic updates have tried to run, but failed, previously.
   191 	 *
   154 	 *
   192 	 * @since 5.2.0
   155 	 * @since 5.2.0
   193 	 *
   156 	 *
   194 	 * @return array|bool The test results. False if the auto-updates failed.
   157 	 * @return array|false The test results. False if the auto-updates failed.
   195 	 */
   158 	 */
   196 	function test_if_failed_update() {
   159 	function test_if_failed_update() {
   197 		$failed = get_site_option( 'auto_core_update_failed' );
   160 		$failed = get_site_option( 'auto_core_update_failed' );
   198 
   161 
   199 		if ( ! $failed ) {
   162 		if ( ! $failed ) {
   201 		}
   164 		}
   202 
   165 
   203 		if ( ! empty( $failed['critical'] ) ) {
   166 		if ( ! empty( $failed['critical'] ) ) {
   204 			$description  = __( 'A previous automatic background update ended with a critical failure, so updates are now disabled.' );
   167 			$description  = __( 'A previous automatic background update ended with a critical failure, so updates are now disabled.' );
   205 			$description .= ' ' . __( 'You would have received an email because of this.' );
   168 			$description .= ' ' . __( 'You would have received an email because of this.' );
   206 			$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." );
   169 			$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." );
   207 			$description .= ' ' . sprintf(
   170 			$description .= ' ' . sprintf(
   208 				/* translators: %s: Code of error shown. */
   171 				/* translators: %s: Code of error shown. */
   209 				__( 'The error code was %s.' ),
   172 				__( 'The error code was %s.' ),
   210 				'<code>' . $failed['error_code'] . '</code>'
   173 				'<code>' . $failed['error_code'] . '</code>'
   211 			);
   174 			);
   308 	 * @since 5.2.0
   271 	 * @since 5.2.0
   309 	 *
   272 	 *
   310 	 * @return array The test results.
   273 	 * @return array The test results.
   311 	 */
   274 	 */
   312 	function test_check_wp_filesystem_method() {
   275 	function test_check_wp_filesystem_method() {
       
   276 		// Make sure the `request_filesystem_credentials()` function is available during our REST API call.
       
   277 		if ( ! function_exists( 'request_filesystem_credentials' ) ) {
       
   278 			require_once ABSPATH . '/wp-admin/includes/file.php';
       
   279 		}
       
   280 
   313 		$skin    = new Automatic_Upgrader_Skin;
   281 		$skin    = new Automatic_Upgrader_Skin;
   314 		$success = $skin->request_filesystem_credentials( false, ABSPATH );
   282 		$success = $skin->request_filesystem_credentials( false, ABSPATH );
   315 
   283 
   316 		if ( ! $success ) {
   284 		if ( ! $success ) {
   317 			$description  = __( 'Your installation of WordPress prompts for FTP credentials to perform updates.' );
   285 			$description  = __( 'Your installation of WordPress prompts for FTP credentials to perform updates.' );
   334 	 *
   302 	 *
   335 	 * @since 5.2.0
   303 	 * @since 5.2.0
   336 	 *
   304 	 *
   337 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   305 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   338 	 *
   306 	 *
   339 	 * @return array|bool The test results. False if they're not writeable.
   307 	 * @return array|false The test results. False if they're not writeable.
   340 	 */
   308 	 */
   341 	function test_all_files_writable() {
   309 	function test_all_files_writable() {
   342 		global $wp_filesystem;
   310 		global $wp_filesystem;
   343 
   311 
   344 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   312 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   352 
   320 
   353 		WP_Filesystem();
   321 		WP_Filesystem();
   354 
   322 
   355 		if ( 'direct' !== $wp_filesystem->method ) {
   323 		if ( 'direct' !== $wp_filesystem->method ) {
   356 			return false;
   324 			return false;
       
   325 		}
       
   326 
       
   327 		// Make sure the `get_core_checksums()` function is available during our REST API call.
       
   328 		if ( ! function_exists( 'get_core_checksums' ) ) {
       
   329 			require_once ABSPATH . '/wp-admin/includes/update.php';
   357 		}
   330 		}
   358 
   331 
   359 		$checksums = get_core_checksums( $wp_version, 'en_US' );
   332 		$checksums = get_core_checksums( $wp_version, 'en_US' );
   360 		$dev       = ( false !== strpos( $wp_version, '-' ) );
   333 		$dev       = ( false !== strpos( $wp_version, '-' ) );
   361 		// Get the last stable version's files and test against that.
   334 		// Get the last stable version's files and test against that.
   414 	/**
   387 	/**
   415 	 * Check if the install is using a development branch and can use nightly packages.
   388 	 * Check if the install is using a development branch and can use nightly packages.
   416 	 *
   389 	 *
   417 	 * @since 5.2.0
   390 	 * @since 5.2.0
   418 	 *
   391 	 *
   419 	 * @return array|bool The test results. False if it isn't a development version.
   392 	 * @return array|false The test results. False if it isn't a development version.
   420 	 */
   393 	 */
   421 	function test_accepts_dev_updates() {
   394 	function test_accepts_dev_updates() {
   422 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   395 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   423 		// Only for dev versions.
   396 		// Only for dev versions.
   424 		if ( false === strpos( $wp_version, '-' ) ) {
   397 		if ( false === strpos( $wp_version, '-' ) ) {