函数文档

find_core_auto_update()

💡 云策文档标注

概述

find_core_auto_update() 函数用于获取 WordPress 核心的最佳可用(且已启用)自动更新。它会从可用的更新中选择版本最高的一个,前提是安装环境允许。

关键要点

  • 函数返回一个核心更新对象或 false,表示成功或失败。
  • 通过 get_site_transient('update_core') 获取更新数据,并检查 'autoupdate' 响应。
  • 使用 WP_Automatic_Updater 类的 should_update() 方法验证更新是否适用于当前安装。
  • 通过 version_compare() 选择版本最高的更新。
  • 自 WordPress 3.7.0 版本引入。

代码示例

function find_core_auto_update() {
    $updates = get_site_transient( 'update_core' );

    if ( ! $updates || empty( $updates->updates ) ) {
        return false;
    }

    require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

    $auto_update = false;
    $upgrader    = new WP_Automatic_Updater();

    foreach ( $updates->updates as $update ) {
        if ( 'autoupdate' !== $update->response ) {
            continue;
        }

        if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) ) {
            continue;
        }

        if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) ) {
            $auto_update = $update;
        }
    }

    return $auto_update;
}

📄 原文内容

Gets the best available (and enabled) Auto-Update for WordPress core.

Description

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.

Return

object|false The core update offering on success, false on failure.

Source

function find_core_auto_update() {
	$updates = get_site_transient( 'update_core' );

	if ( ! $updates || empty( $updates->updates ) ) {
		return false;
	}

	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

	$auto_update = false;
	$upgrader    = new WP_Automatic_Updater();

	foreach ( $updates->updates as $update ) {
		if ( 'autoupdate' !== $update->response ) {
			continue;
		}

		if ( ! $upgrader->should_update( 'core', $update, ABSPATH ) ) {
			continue;
		}

		if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) ) {
			$auto_update = $update;
		}
	}

	return $auto_update;
}

Changelog

Version Description
3.7.0 Introduced.