新 Posts/Pages 待定时向管理员发送电子邮件通知

Sending Email Notifications To Admin When New Posts/Pages Are Pending

我们有一个脚本可以在贡献者创建新页面或 post 时通过电子邮件通知管理员,因此 'pending' 但通知是重复的。 pages/posts 正在使用 gutenberg 编辑器,我认为这是造成这种情况的原因(请参阅此处: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 = 'example@email.com';
        $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 post,该挂钩会被触发两次以实现向后兼容性,以确保无法使用 Gutenberg REST 调用的插件仍然可以保存他们的数据。

为了使您的案例正常运行,您必须向脚本添加条件检查以确保您 运行 它在正确的传递中(即 non休息电话)

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 = 'example@email.com';
        $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 );
    }
}

请记住,这是未经测试的,但根据上面链接的问题报告,这应该忽略第一次通过 Gutenberg 编辑器,只传递具有正确 post 数据的版本。