wp_maybe_transition_site_statuses_on_update()
云策文档标注
概述
wp_maybe_transition_site_statuses_on_update() 是一个 WordPress 函数,用于在站点状态更新时触发相应的动作钩子。它比较新旧站点对象的属性变化,并根据变化执行特定的 do_action 调用。
关键要点
- 函数接受两个参数:$new_site(必需,更新后的 WP_Site 对象)和 $old_site(可选,更新前的 WP_Site 对象,默认为 null)。
- 当 $old_site 未提供时,会使用默认值创建一个新的 WP_Site 对象。
- 函数检测 spam、mature、archived、deleted 和 public 属性的变化,并触发对应的动作钩子,如 make_spam_blog、mature_blog、archive_blog、make_delete_blog 和 update_blog_public。
- 每个钩子都传递站点 ID 作为参数,部分钩子(如 update_blog_public)还传递额外参数。
- 此函数自 WordPress 5.1.0 版本引入。
代码示例
wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
$site_id = $new_site->id;
if ( ! $old_site ) {
$old_site = new WP_Site( new stdClass() );
}
if ( $new_site->spam !== $old_site->spam ) {
if ( '1' === $new_site->spam ) {
do_action( 'make_spam_blog', $site_id );
} else {
do_action( 'make_ham_blog', $site_id );
}
}
// 其他属性检测类似...
}注意事项
- 确保 $new_site 是一个有效的 WP_Site 对象,否则可能导致错误。
- 钩子触发依赖于属性值的精确比较,属性值应为字符串类型(如 '1' 或 '0')。
- 开发者可以通过这些钩子添加自定义功能,响应站点状态变化。
原文内容
Triggers actions on site status updates.
Parameters
Source
function wp_maybe_transition_site_statuses_on_update( $new_site, $old_site = null ) {
$site_id = $new_site->id;
// Use the default values for a site if no previous state is given.
if ( ! $old_site ) {
$old_site = new WP_Site( new stdClass() );
}
if ( $new_site->spam !== $old_site->spam ) {
if ( '1' === $new_site->spam ) {
/**
* Fires when the 'spam' status is added to a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'make_spam_blog', $site_id );
} else {
/**
* Fires when the 'spam' status is removed from a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'make_ham_blog', $site_id );
}
}
if ( $new_site->mature !== $old_site->mature ) {
if ( '1' === $new_site->mature ) {
/**
* Fires when the 'mature' status is added to a site.
*
* @since 3.1.0
*
* @param int $site_id Site ID.
*/
do_action( 'mature_blog', $site_id );
} else {
/**
* Fires when the 'mature' status is removed from a site.
*
* @since 3.1.0
*
* @param int $site_id Site ID.
*/
do_action( 'unmature_blog', $site_id );
}
}
if ( $new_site->archived !== $old_site->archived ) {
if ( '1' === $new_site->archived ) {
/**
* Fires when the 'archived' status is added to a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'archive_blog', $site_id );
} else {
/**
* Fires when the 'archived' status is removed from a site.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
*/
do_action( 'unarchive_blog', $site_id );
}
}
if ( $new_site->deleted !== $old_site->deleted ) {
if ( '1' === $new_site->deleted ) {
/**
* Fires when the 'flagged for deletion' status is added to a site.
*
* @since 3.5.0
*
* @param int $site_id Site ID.
*/
do_action( 'make_delete_blog', $site_id );
} else {
/**
* Fires when the 'flagged for deletion' status is removed from a site.
*
* @since 3.5.0
*
* @param int $site_id Site ID.
*/
do_action( 'make_undelete_blog', $site_id );
}
}
if ( $new_site->public !== $old_site->public ) {
/**
* Fires after the current blog's 'public' setting is updated.
*
* @since MU (3.0.0)
*
* @param int $site_id Site ID.
* @param string $is_public Whether the site is public. A numeric string,
* for compatibility reasons. Accepts '1' or '0'.
*/
do_action( 'update_blog_public', $site_id, $new_site->public );
}
}
Hooks
- do_action( ‘archive_blog’, int $site_id )
-
Fires when the ‘archived’ status is added to a site.
- do_action( ‘make_delete_blog’, int $site_id )
-
Fires when the ‘flagged for deletion’ status is added to a site.
- do_action( ‘make_ham_blog’, int $site_id )
-
Fires when the ‘spam’ status is removed from a site.
- do_action( ‘make_spam_blog’, int $site_id )
-
Fires when the ‘spam’ status is added to a site.
- do_action( ‘make_undelete_blog’, int $site_id )
-
Fires when the ‘flagged for deletion’ status is removed from a site.
- do_action( ‘mature_blog’, int $site_id )
-
Fires when the ‘mature’ status is added to a site.
- do_action( ‘unarchive_blog’, int $site_id )
-
Fires when the ‘archived’ status is removed from a site.
- do_action( ‘unmature_blog’, int $site_id )
-
Fires when the ‘mature’ status is removed from a site.
- do_action( ‘update_blog_public’, int $site_id, string $is_public )
-
Fires after the current blog’s ‘public’ setting is updated.
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Introduced. |