我们有一个脚本,可以在贡献者创建新页面或帖子时通过电子邮件通知管理员,因此“待处理”但通知重复。页面/帖子正在使用古腾堡编辑器,我认为这是导致此问题的原因(请参阅此处:https : //github.com/WordPress/gutenberg/issues/15094)但我不知道解决方案是什么。
add_action( 'transition_post_status', 'pending_submission_notifications_send_email', 10, 3 );
function pending_submission_notifications_send_email( $new_status, $old_status, $post ) {
// Notify Admin that Contributor has written a post.
if ( 'pending' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$pending_submission_email = '[email protected]';
$admins = ( empty( $pending_submission_email ) ) ? get_option( 'admin_email' ) : $pending_submission_email;
$edit_link = get_edit_post_link( $post->ID, '' );
$preview_link = get_permalink( $post->ID ) . '&preview=true';
$username = get_userdata( $post->post_author );
$username_last_edit = get_the_modified_author();
$subject = __( 'New post or page pending review', 'pending-submission-notifications' ) . ': "' . $post->post_title . '"';
$message = __( 'A new post or page is pending review.', 'pending-submission-notifications' );
$message .= "\r\n\r\n";
$message .= __( 'Author', 'pending-submission-notifications' ) . ': ' . $username->user_login . "\r\n";
$message .= __( 'Title', 'pending-submission-notifications' ) . ': ' . $post->post_title . "\r\n";
$message .= "\r\n\r\n";
$message .= __( 'Edit the submission', 'pending-submission-notifications' ) . ': ' . $edit_link . "\r\n";
$message .= __( 'Preview the submission', 'pending-submission-notifications' ) . ': ' . $preview_link;
$result = wp_mail( $admins, $subject, $message );
} // Notify Contributor that Admin has published their post.
elseif ( 'pending' === $old_status && 'publish' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$username = get_userdata( $post->post_author );
$url = get_permalink( $post->ID );
$subject = __( 'Your submission is now live! ', 'pending-submission-notifications' );
$message = '"' . $post->post_title . '" ' . __( 'was just published ', 'pending-submission-notifications' ) . "! \r\n\r\n";
$message .= $url;
$result = wp_mail( $username->user_email, $subject, $message );
}
}
你是对的,它似乎与你链接的古腾堡问题直接相关。(在该线程中回答的原因https://github.com/WordPress/gutenberg/issues/15094#issuecomment-558986406)。
本质上,如果您使用一个插件将自定义元数据添加到启用了 Gutenberg 的帖子,则挂钩会被触发两次以实现向后兼容性,以确保无法使用 Gutenberg REST 调用的插件仍然可以保存其数据。
为了让你的案例工作,你必须向你的脚本添加条件检查,以确保你在正确的传递上运行它(即非Rest 调用)
add_action( 'transition_post_status', 'pending_submission_notifications_send_email', 10, 3 );
function pending_submission_notifications_send_email( $new_status, $old_status, $post ) {
// If this is running in the Gutenberg REST call, ignore it
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return false; }
// Notify Admin that Contributor has written a post.
if ( 'pending' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$pending_submission_email = '[email protected]';
$admins = ( empty( $pending_submission_email ) ) ? get_option( 'admin_email' ) : $pending_submission_email;
$edit_link = get_edit_post_link( $post->ID, '' );
$preview_link = get_permalink( $post->ID ) . '&preview=true';
$username = get_userdata( $post->post_author );
$username_last_edit = get_the_modified_author();
$subject = __( 'New post or page pending review', 'pending-submission-notifications' ) . ': "' . $post->post_title . '"';
$message = __( 'A new post or page is pending review.', 'pending-submission-notifications' );
$message .= "\r\n\r\n";
$message .= __( 'Author', 'pending-submission-notifications' ) . ': ' . $username->user_login . "\r\n";
$message .= __( 'Title', 'pending-submission-notifications' ) . ': ' . $post->post_title . "\r\n";
$message .= "\r\n\r\n";
$message .= __( 'Edit the submission', 'pending-submission-notifications' ) . ': ' . $edit_link . "\r\n";
$message .= __( 'Preview the submission', 'pending-submission-notifications' ) . ': ' . $preview_link;
$result = wp_mail( $admins, $subject, $message );
} // Notify Contributor that Admin has published their post.
elseif ( 'pending' === $old_status && 'publish' === $new_status && user_can( $post->post_author, 'edit_posts' ) && ! user_can( $post->post_author, 'publish_posts' ) ) {
$username = get_userdata( $post->post_author );
$url = get_permalink( $post->ID );
$subject = __( 'Your submission is now live! ', 'pending-submission-notifications' );
$message = '"' . $post->post_title . '" ' . __( 'was just published ', 'pending-submission-notifications' ) . "! \r\n\r\n";
$message .= $url;
$result = wp_mail( $username->user_email, $subject, $message );
}
}
请记住,这是未经测试的,但根据上面链接的问题报告,这应该忽略通过古腾堡编辑器的第一次传递,只传递具有正确帖子数据的版本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句