wp/wp-admin/includes/class-wp-site-health-auto-updates.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    64 	 * @since 5.5.1 The `$value` parameter can accept an array.
    64 	 * @since 5.5.1 The `$value` parameter can accept an array.
    65 	 *
    65 	 *
    66 	 * @param string $constant         The name of the constant to check.
    66 	 * @param string $constant         The name of the constant to check.
    67 	 * @param bool|string|array $value The value that the constant should be, if set,
    67 	 * @param bool|string|array $value The value that the constant should be, if set,
    68 	 *                                 or an array of acceptable values.
    68 	 *                                 or an array of acceptable values.
    69 	 * @return array The test results.
    69 	 * @return array|null The test results if there are any constants set incorrectly,
       
    70 	 *                    or null if the test passed.
    70 	 */
    71 	 */
    71 	public function test_constants( $constant, $value ) {
    72 	public function test_constants( $constant, $value ) {
    72 		$acceptable_values = (array) $value;
    73 		$acceptable_values = (array) $value;
    73 
    74 
    74 		if ( defined( $constant ) && ! in_array( constant( $constant ), $acceptable_values, true ) ) {
    75 		if ( defined( $constant ) && ! in_array( constant( $constant ), $acceptable_values, true ) ) {
    80 					'<code>' . esc_html( var_export( constant( $constant ), true ) ) . '</code>'
    81 					'<code>' . esc_html( var_export( constant( $constant ), true ) ) . '</code>'
    81 				),
    82 				),
    82 				'severity'    => 'fail',
    83 				'severity'    => 'fail',
    83 			);
    84 			);
    84 		}
    85 		}
       
    86 
       
    87 		return null;
    85 	}
    88 	}
    86 
    89 
    87 	/**
    90 	/**
    88 	 * Checks if updates are intercepted by a filter.
    91 	 * Checks if updates are intercepted by a filter.
    89 	 *
    92 	 *
    90 	 * @since 5.2.0
    93 	 * @since 5.2.0
    91 	 *
    94 	 *
    92 	 * @return array The test results.
    95 	 * @return array|null The test results if wp_version_check() is disabled,
       
    96 	 *                    or null if the test passed.
    93 	 */
    97 	 */
    94 	public function test_wp_version_check_attached() {
    98 	public function test_wp_version_check_attached() {
    95 		if ( ( ! is_multisite() || is_main_site() && is_network_admin() )
    99 		if ( ( ! is_multisite() || is_main_site() && is_network_admin() )
    96 			&& ! has_filter( 'wp_version_check', 'wp_version_check' )
   100 			&& ! has_filter( 'wp_version_check', 'wp_version_check' )
    97 		) {
   101 		) {
   102 					'<code>wp_version_check()</code>'
   106 					'<code>wp_version_check()</code>'
   103 				),
   107 				),
   104 				'severity'    => 'fail',
   108 				'severity'    => 'fail',
   105 			);
   109 			);
   106 		}
   110 		}
       
   111 
       
   112 		return null;
   107 	}
   113 	}
   108 
   114 
   109 	/**
   115 	/**
   110 	 * Checks if automatic updates are disabled by a filter.
   116 	 * Checks if automatic updates are disabled by a filter.
   111 	 *
   117 	 *
   112 	 * @since 5.2.0
   118 	 * @since 5.2.0
   113 	 *
   119 	 *
   114 	 * @return array The test results.
   120 	 * @return array|null The test results if the {@see 'automatic_updater_disabled'} filter is set,
       
   121 	 *                    or null if the test passed.
   115 	 */
   122 	 */
   116 	public function test_filters_automatic_updater_disabled() {
   123 	public function test_filters_automatic_updater_disabled() {
   117 		/** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
   124 		/** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
   118 		if ( apply_filters( 'automatic_updater_disabled', false ) ) {
   125 		if ( apply_filters( 'automatic_updater_disabled', false ) ) {
   119 			return array(
   126 			return array(
   123 					'<code>automatic_updater_disabled</code>'
   130 					'<code>automatic_updater_disabled</code>'
   124 				),
   131 				),
   125 				'severity'    => 'fail',
   132 				'severity'    => 'fail',
   126 			);
   133 			);
   127 		}
   134 		}
       
   135 
       
   136 		return null;
   128 	}
   137 	}
   129 
   138 
   130 	/**
   139 	/**
   131 	 * Checks if automatic updates are disabled.
   140 	 * Checks if automatic updates are disabled.
   132 	 *
   141 	 *
   133 	 * @since 5.3.0
   142 	 * @since 5.3.0
   134 	 *
   143 	 *
   135 	 * @return array|false The test results. False if auto-updates are enabled.
   144 	 * @return array|false The test results if auto-updates are disabled, false otherwise.
   136 	 */
   145 	 */
   137 	public function test_wp_automatic_updates_disabled() {
   146 	public function test_wp_automatic_updates_disabled() {
   138 		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
   147 		if ( ! class_exists( 'WP_Automatic_Updater' ) ) {
   139 			require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
   148 			require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';
   140 		}
   149 		}
   154 	/**
   163 	/**
   155 	 * Checks if automatic updates have tried to run, but failed, previously.
   164 	 * Checks if automatic updates have tried to run, but failed, previously.
   156 	 *
   165 	 *
   157 	 * @since 5.2.0
   166 	 * @since 5.2.0
   158 	 *
   167 	 *
   159 	 * @return array|false The test results. False if the auto-updates failed.
   168 	 * @return array|false The test results if auto-updates previously failed, false otherwise.
   160 	 */
   169 	 */
   161 	public function test_if_failed_update() {
   170 	public function test_if_failed_update() {
   162 		$failed = get_site_option( 'auto_core_update_failed' );
   171 		$failed = get_site_option( 'auto_core_update_failed' );
   163 
   172 
   164 		if ( ! $failed ) {
   173 		if ( ! $failed ) {
   222 				// Continue one level at a time.
   231 				// Continue one level at a time.
   223 			} while ( $context_dir = dirname( $context_dir ) );
   232 			} while ( $context_dir = dirname( $context_dir ) );
   224 		}
   233 		}
   225 
   234 
   226 		$check_dirs = array_unique( $check_dirs );
   235 		$check_dirs = array_unique( $check_dirs );
       
   236 		$updater    = new WP_Automatic_Updater();
       
   237 		$checkout   = false;
   227 
   238 
   228 		// Search all directories we've found for evidence of version control.
   239 		// Search all directories we've found for evidence of version control.
   229 		foreach ( $vcs_dirs as $vcs_dir ) {
   240 		foreach ( $vcs_dirs as $vcs_dir ) {
   230 			foreach ( $check_dirs as $check_dir ) {
   241 			foreach ( $check_dirs as $check_dir ) {
   231 				// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition,Squiz.PHP.DisallowMultipleAssignments
   242 				if ( ! $updater->is_allowed_dir( $check_dir ) ) {
   232 				if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
   243 					continue;
       
   244 				}
       
   245 
       
   246 				$checkout = is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" );
       
   247 				if ( $checkout ) {
   233 					break 2;
   248 					break 2;
   234 				}
   249 				}
   235 			}
   250 			}
   236 		}
   251 		}
   237 
   252 
   304 	 *
   319 	 *
   305 	 * @since 5.2.0
   320 	 * @since 5.2.0
   306 	 *
   321 	 *
   307 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   322 	 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
   308 	 *
   323 	 *
   309 	 * @return array|false The test results. False if they're not writeable.
   324 	 * @return array|false The test results if at least some of WordPress core files are writeable,
       
   325 	 *                     or if a list of the checksums could not be retrieved from WordPress.org.
       
   326 	 *                     False if the core files are not writeable.
   310 	 */
   327 	 */
   311 	public function test_all_files_writable() {
   328 	public function test_all_files_writable() {
   312 		global $wp_filesystem;
   329 		global $wp_filesystem;
   313 
   330 
   314 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   331 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   389 	/**
   406 	/**
   390 	 * Checks if the install is using a development branch and can use nightly packages.
   407 	 * Checks if the install is using a development branch and can use nightly packages.
   391 	 *
   408 	 *
   392 	 * @since 5.2.0
   409 	 * @since 5.2.0
   393 	 *
   410 	 *
   394 	 * @return array|false The test results. False if it isn't a development version.
   411 	 * @return array|false|null The test results if development updates are blocked.
       
   412 	 *                          False if it isn't a development version. Null if the test passed.
   395 	 */
   413 	 */
   396 	public function test_accepts_dev_updates() {
   414 	public function test_accepts_dev_updates() {
   397 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   415 		require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
   398 		// Only for dev versions.
   416 		// Only for dev versions.
   399 		if ( ! str_contains( $wp_version, '-' ) ) {
   417 		if ( ! str_contains( $wp_version, '-' ) ) {
   420 					'<code>allow_dev_auto_core_updates</code>'
   438 					'<code>allow_dev_auto_core_updates</code>'
   421 				),
   439 				),
   422 				'severity'    => 'fail',
   440 				'severity'    => 'fail',
   423 			);
   441 			);
   424 		}
   442 		}
       
   443 
       
   444 		return null;
   425 	}
   445 	}
   426 
   446 
   427 	/**
   447 	/**
   428 	 * Checks if the site supports automatic minor updates.
   448 	 * Checks if the site supports automatic minor updates.
   429 	 *
   449 	 *
   430 	 * @since 5.2.0
   450 	 * @since 5.2.0
   431 	 *
   451 	 *
   432 	 * @return array The test results.
   452 	 * @return array|null The test results if minor updates are blocked,
       
   453 	 *                    or null if the test passed.
   433 	 */
   454 	 */
   434 	public function test_accepts_minor_updates() {
   455 	public function test_accepts_minor_updates() {
   435 		if ( defined( 'WP_AUTO_UPDATE_CORE' ) && false === WP_AUTO_UPDATE_CORE ) {
   456 		if ( defined( 'WP_AUTO_UPDATE_CORE' ) && false === WP_AUTO_UPDATE_CORE ) {
   436 			return array(
   457 			return array(
   437 				'description' => sprintf(
   458 				'description' => sprintf(
   452 					'<code>allow_minor_auto_core_updates</code>'
   473 					'<code>allow_minor_auto_core_updates</code>'
   453 				),
   474 				),
   454 				'severity'    => 'fail',
   475 				'severity'    => 'fail',
   455 			);
   476 			);
   456 		}
   477 		}
       
   478 
       
   479 		return null;
   457 	}
   480 	}
   458 }
   481 }