函数文档

_wp_customize_publish_changeset()

💡 云策文档标注

概述

_wp_customize_publish_changeset() 是 WordPress 中用于发布自定义器快照更改的内部函数。它处理快照发布时的状态转换,确保自定义器设置正确应用,并根据修订功能管理快照的垃圾回收。

关键要点

  • 函数用于发布自定义器快照的更改,仅在快照状态从非发布变为发布时触发。
  • 参数包括新状态、旧状态和快照文章对象,均为必需。
  • 在发布过程中,如果 WP_Customize_Manager 未初始化,会创建实例并配置。
  • 确保 customize_register 动作触发,以注册核心、主题和插件的设置。
  • 发布后,如果未启用修订功能,会删除快照文章以避免积累。

代码示例

function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_post ) {
    global $wp_customize;

    $is_publishing_changeset = (
        'customize_changeset' === $changeset_post->post_type
        &&
        'publish' === $new_status
        &&
        'publish' !== $old_status
    );
    if ( ! $is_publishing_changeset ) {
        return;
    }

    if ( empty( $wp_customize ) ) {
        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
        $wp_customize = new WP_Customize_Manager(
            array(
                'changeset_uuid'     => $changeset_post->post_name,
                'settings_previewed' => false,
            )
        );
    }

    if ( ! did_action( 'customize_register' ) ) {
        remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) );
        $wp_customize->register_controls();
        do_action( 'customize_register', $wp_customize );
    }
    $wp_customize->_publish_changeset_values( $changeset_post->ID );

    if ( ! wp_revisions_enabled( $changeset_post ) ) {
        $wp_customize->trash_changeset_post( $changeset_post->ID );
    }
}

注意事项

  • 此函数是内部函数,通常不应直接调用,而是通过自定义器流程触发。
  • 在 CLI 或 Cron 环境中运行时,可能需要手动触发 customize_register 动作以确保设置正确注册。
  • 快照文章类型为 'customize_changeset',发布状态变化需符合特定条件。

📄 原文内容

Publishes a snapshot’s changes.

Parameters

$new_statusstringrequired
New post status.
$old_statusstringrequired
Old post status.
$changeset_postWP_Postrequired
Changeset post object.

Source

function _wp_customize_publish_changeset( $new_status, $old_status, $changeset_post ) {
	global $wp_customize;

	$is_publishing_changeset = (
		'customize_changeset' === $changeset_post->post_type
		&&
		'publish' === $new_status
		&&
		'publish' !== $old_status
	);
	if ( ! $is_publishing_changeset ) {
		return;
	}

	if ( empty( $wp_customize ) ) {
		require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
		$wp_customize = new WP_Customize_Manager(
			array(
				'changeset_uuid'     => $changeset_post->post_name,
				'settings_previewed' => false,
			)
		);
	}

	if ( ! did_action( 'customize_register' ) ) {
		/*
		 * When running from CLI or Cron, the customize_register action will need
		 * to be triggered in order for core, themes, and plugins to register their
		 * settings. Normally core will add_action( 'customize_register' ) at
		 * priority 10 to register the core settings, and if any themes/plugins
		 * also add_action( 'customize_register' ) at the same priority, they
		 * will have a $wp_customize with those settings registered since they
		 * call add_action() afterward, normally. However, when manually doing
		 * the customize_register action after the setup_theme, then the order
		 * will be reversed for two actions added at priority 10, resulting in
		 * the core settings no longer being available as expected to themes/plugins.
		 * So the following manually calls the method that registers the core
		 * settings up front before doing the action.
		 */
		remove_action( 'customize_register', array( $wp_customize, 'register_controls' ) );
		$wp_customize->register_controls();

		/** This filter is documented in wp-includes/class-wp-customize-manager.php */
		do_action( 'customize_register', $wp_customize );
	}
	$wp_customize->_publish_changeset_values( $changeset_post->ID );

	/*
	 * Trash the changeset post if revisions are not enabled. Unpublished
	 * changesets by default get garbage collected due to the auto-draft status.
	 * When a changeset post is published, however, it would no longer get cleaned
	 * out. This is a problem when the changeset posts are never displayed anywhere,
	 * since they would just be endlessly piling up. So here we use the revisions
	 * feature to indicate whether or not a published changeset should get trashed
	 * and thus garbage collected.
	 */
	if ( ! wp_revisions_enabled( $changeset_post ) ) {
		$wp_customize->trash_changeset_post( $changeset_post->ID );
	}
}

Hooks

do_action( ‘customize_register’, WP_Customize_Manager $manager )

Fires once WordPress has loaded, allowing scripts and styles to be initialized.

Changelog

Version Description
4.7.0 Introduced.