函数文档

get_theme_update_available()

💡 云策文档标注

概述

get_theme_update_available() 函数用于检查主题是否有可用更新,并返回相应的更新链接 HTML。它基于用户权限和主题更新数据,生成包含详情查看或更新操作的链接。

关键要点

  • 函数返回字符串(更新链接 HTML)或 false(如果参数无效或用户无权限)。
  • 参数 $theme 必须是 WP_Theme 对象,否则返回 false。
  • 用户需具备 'update_themes' 权限才能获取更新链接。
  • 更新数据通过 get_site_transient('update_themes') 缓存获取。
  • 根据是否为多站点、用户权限和更新包状态,生成不同的链接内容。

代码示例

function get_theme_update_available( $theme ) {
    static $themes_update = null;

    if ( ! current_user_can( 'update_themes' ) ) {
        return false;
    }

    if ( ! isset( $themes_update ) ) {
        $themes_update = get_site_transient( 'update_themes' );
    }

    if ( ! ( $theme instanceof WP_Theme ) ) {
        return false;
    }

    $stylesheet = $theme->get_stylesheet();

    $html = '';

    if ( isset( $themes_update->response[ $stylesheet ] ) ) {
        $update      = $themes_update->response[ $stylesheet ];
        $theme_name  = $theme->display( 'Name' );
        $details_url = add_query_arg(
            array(
                'TB_iframe' => 'true',
                'width'     => 1024,
                'height'    => 800,
            ),
            $update['url']
        );
        $update_url  = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );

        if ( ! is_multisite() ) {
            if ( ! current_user_can( 'update_themes' ) ) {
                $html = sprintf(
                    '' . __( 'There is a new version of %1$s available. View version %4$s details.' ) . '',
                    $theme_name,
                    esc_url( $details_url ),
                    sprintf(
                        'class="thickbox open-plugin-details-modal" aria-label="%s"',
                        esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
                    ),
                    $update['new_version']
                );
            } elseif ( empty( $update['package'] ) ) {
                $html = sprintf(
                    '' . __( 'There is a new version of %1$s available. View version %4$s details. Automatic update is unavailable for this theme.' ) . '',
                    $theme_name,
                    esc_url( $details_url ),
                    sprintf(
                        'class="thickbox open-plugin-details-modal" aria-label="%s"',
                        esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
                    ),
                    $update['new_version']
                );
            } else {
                $html = sprintf(
                    '' . __( 'There is a new version of %1$s available. View version %4$s details or update now.' ) . '',
                    $theme_name,
                    esc_url( $details_url ),
                    sprintf(
                        'class="thickbox open-plugin-details-modal" aria-label="%s"',
                        esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
                    ),
                    $update['new_version'],
                    $update_url,
                    sprintf(
                        'aria-label="%s" id="update-theme" data-slug="%s"',
                        esc_attr( sprintf( _x( 'Update %s now', 'theme' ), $theme_name ) ),
                        $stylesheet
                    )
                );
            }
        }
    }

    return $html;
}

注意事项

  • 函数仅适用于非多站点环境,多站点下返回空字符串。
  • 更新链接包含 Thickbox 模态框参数,用于主题详情预览。
  • 使用 wp_nonce_url 确保更新操作的安全性。
  • 相关函数如 current_user_can()、get_site_transient() 等需正确配置。

📄 原文内容

Retrieves the update link if there is a theme update available.

Description

Will return a link if there is an update available.

Parameters

$themeWP_Themerequired
WP_Theme object.

Return

string|false HTML for the update link, or false if invalid info was passed.

Source

function get_theme_update_available( $theme ) {
	static $themes_update = null;

	if ( ! current_user_can( 'update_themes' ) ) {
		return false;
	}

	if ( ! isset( $themes_update ) ) {
		$themes_update = get_site_transient( 'update_themes' );
	}

	if ( ! ( $theme instanceof WP_Theme ) ) {
		return false;
	}

	$stylesheet = $theme->get_stylesheet();

	$html = '';

	if ( isset( $themes_update->response[ $stylesheet ] ) ) {
		$update      = $themes_update->response[ $stylesheet ];
		$theme_name  = $theme->display( 'Name' );
		$details_url = add_query_arg(
			array(
				'TB_iframe' => 'true',
				'width'     => 1024,
				'height'    => 800,
			),
			$update['url']
		); // Theme browser inside WP? Replace this. Also, theme preview JS will override this on the available list.
		$update_url  = wp_nonce_url( admin_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $stylesheet ) ), 'upgrade-theme_' . $stylesheet );

		if ( ! is_multisite() ) {
			if ( ! current_user_can( 'update_themes' ) ) {
				$html = sprintf(
					/* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
					'<p><strong>' . __( 'There is a new version of %1$s available. <a href="%2$s" %3$s>View version %4$s details</a>.' ) . '</strong></p>',
					$theme_name,
					esc_url( $details_url ),
					sprintf(
						'class="thickbox open-plugin-details-modal" aria-label="%s"',
						/* translators: 1: Theme name, 2: Version number. */
						esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
					),
					$update['new_version']
				);
			} elseif ( empty( $update['package'] ) ) {
				$html = sprintf(
					/* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number. */
					'<p><strong>' . __( '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>' ) . '</strong></p>',
					$theme_name,
					esc_url( $details_url ),
					sprintf(
						'class="thickbox open-plugin-details-modal" aria-label="%s"',
						/* translators: 1: Theme name, 2: Version number. */
						esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
					),
					$update['new_version']
				);
			} else {
				$html = sprintf(
					/* translators: 1: Theme name, 2: Theme details URL, 3: Additional link attributes, 4: Version number, 5: Update URL, 6: Additional link attributes. */
					'<p><strong>' . __( '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>.' ) . '</strong></p>',
					$theme_name,
					esc_url( $details_url ),
					sprintf(
						'class="thickbox open-plugin-details-modal" aria-label="%s"',
						/* translators: 1: Theme name, 2: Version number. */
						esc_attr( sprintf( __( 'View %1$s version %2$s details' ), $theme_name, $update['new_version'] ) )
					),
					$update['new_version'],
					$update_url,
					sprintf(
						'aria-label="%s" id="update-theme" data-slug="%s"',
						/* translators: %s: Theme name. */
						esc_attr( sprintf( _x( 'Update %s now', 'theme' ), $theme_name ) ),
						$stylesheet
					)
				);
			}
		}
	}

	return $html;
}

Changelog

Version Description
3.8.0 Introduced.