WordPress-在发布帖子时发送两则消息,一则给帖子作者,一则给所有用户

时间:2018-09-18 08:57:55

标签: php wordpress message author

最后我做到了,我使用的是“ Front-End PM”插件,到目前为止,我已经创建了此功能,以便在发布发布者时为发布者发送消息。

但是我该如何为帖子作者创建两条消息,为所有用户创建一条消息,以通知所有人有关新发布的帖子的信息。

add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

function fep_cus_user_publish_send_messaage( $ID, $post ){

    if ( ! function_exists( 'fep_send_message' ) )
    return;
    $message = [];

    $message['message_to_id'] = $post->post_author; // Post author ID. 
    $name = get_the_author_meta( 'display_name', $post->post_author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID ); 
    $message['message_title'] = sprintf( 'Published: %s', $title );
    $message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.', $name, $title );
    $message['message_content'] .= sprintf( 'View: %s', $permalink );
    $message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
    $override = array('post_author' => 1);//change with message sender id  


    // Send message
    fep_send_message( $message, $override );      
}

这是fep_send_message函数,我想在一个函数中生成两条消息。这可能发生吗?

function fep_send_message( $message = null, $override = array() ) {
    if ( null === $message ) {
        $message = $_POST;
    }
    if ( ! empty( $message['fep_parent_id'] ) ) {
        $message['post_parent'] = absint( $message['fep_parent_id'] );
        $message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
        $message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
        $message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
    } else {
        $message['post_status'] = fep_get_option( 'parent_post_status','publish' );
        $message['post_parent'] = 0;
    }
    $message = apply_filters( 'fep_filter_message_before_send', $message );
    if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
        return false;
    }
    // Create post array
    $post = array(
        'post_title'    => $message['message_title'],
        'post_content'  => $message['message_content'],
        'post_status'   => $message['post_status'],
        'post_parent'   => $message['post_parent'],
        'post_type'     => 'message',
        'post_author'   => get_current_user_id(),
        'mgs_created'   => current_time( 'mysql', true ),
    );

    if ( $override && is_array( $override ) ) {
        $post = wp_parse_args( $override, $post );
    }
    if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
        $post['mgs_last_reply_by

    '] = $post['post_author'];
            $post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, $post['post_content'] );
            $post['mgs_last_reply_time'] = $post['mgs_created'];
        }

        $post = apply_filters( 'fep_filter_message_after_override', $post, $message );

        foreach( $post as $k => $v ){
            if( 0 === strpos( $k, 'post_') ){
                $post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
                unset( $post[ $k ] );
            }
        }
        $post = wp_unslash( $post );

        $new_message = new FEP_Message;
        $message_id = $new_message->insert( $post );
        // Insert the message into the database
        if ( ! $message_id  ) {
            return false;
        }
        /*
        $inserted_message = FEP_Message::get_instance( $message_id );
        if( ! $inserted_message ){
            return false;
        }
        */
        if( ! empty( $message['message_to_id'] ) ){
            $message['message_to_id'] = (array) $message['message_to_id'];
            $message['message_to_id'][] = $new_message->mgs_author;
            $new_message->insert_participants( $message['message_to_id'] );
        }
        do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );

        fep_status_change( 'new', $new_message );

        return $message_id;
    }

2 个答案:

答案 0 :(得分:0)

请检查以下代码。它将为您提供帮助。

add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

function fep_cus_user_publish_send_messaage( $ID, $post ){

    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( 'display_name', $author );
    $email = get_the_author_meta( 'user_email', $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $ID, '' );
    $to[] = sprintf( '%s <%s>', $name, $email );
    $subject = sprintf( 'Published: %s', $title );
    $messaged = sprintf ('Congratulations, %s! Your article “%s” has been published.' . "\n\n", $name, $title );
    $messaged .= sprintf( 'View: %s', $permalink );
    $headers[] = ''; 
    wp_mail( $to, $subject, $messaged, $headers ); 


    // Send message
    send_message_to_contributor( $name, $title );      
}


function send_message_to_contributor($author_name, $post_title) {

    $blogusers = get_users( [ 'role__in' => [ 'contributor' ] ] );

    foreach ( $blogusers as $user ) {
        $permalink = get_permalink( $ID );
        $user_name = $user->data->display_name;
        $email = $user->data->user_email;
        $to[] = sprintf( '%s <%s>', $user_name, $email );
        $subject = sprintf( 'Published: %s', $post_title );
        $message = sprintf ('Hello, %s! New post is “%s” published.' . "\n\n", $user_name, $post_title );
        $message .= sprintf( 'View: %s', $permalink );
        $headers[] = '';
        wp_mail( $to, $subject, $message, $headers );
    }
}  

如果未发送电子邮件,则安装此插件并设置SMTP Easy WP SMTP

答案 1 :(得分:0)

请检查以下Front End PM插件代码。我将“ send_message_to_contributor”中的一些代码更新为“ fep_send_message”。

add_action( 'publish_post', 'fep_cus_user_publish_send_messaage', 10, 2 );

function fep_cus_user_publish_send_messaage( $ID, $post ){

    if ( ! function_exists( 'fep_send_message' ) )
    return;
    $message = [];

    $message['message_to_id'] = $post->post_author; // Post author ID. 
    $name = get_the_author_meta( 'display_name', $post->post_author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID ); 
    $message['message_title'] = sprintf( 'Published: %s', $title );
    $message['message_content'] = sprintf ('Congratulations, %s! Your article “%s” has been published.', $name, $title );
    $message['message_content'] .= sprintf( 'View: %s', $permalink );
    $message['message_content'] .= sprintf( 'This is an automatic message, to let you know your post is published, and qualified for our quality standard!' );
    $override = array('post_author' => 1);//change with message sender id 

    // Send message
    send_message_to_contributor( $name, $title );      
}


function send_message_to_contributor( $message = null, $override = array() ) {

    $blogusers = get_users();

    if ( null === $message ) {
        $message = $_POST;
    }
    if ( ! empty( $message['fep_parent_id'] ) ) {
        $message['post_parent'] = absint( $message['fep_parent_id'] );
        $message['post_status'] = fep_get_option( 'reply_post_status', 'publish' );
        $message['message_title'] = __( 'RE:', 'front-end-pm' ). ' ' . wp_slash( fep_get_message_field( 'mgs_title', $message['post_parent'] ) );
        $message['message_to_id'] = fep_get_participants( $message['post_parent'], true );
    } else {
        $message['post_status'] = fep_get_option( 'parent_post_status','publish' );
        $message['post_parent'] = 0;
    }
    $message = apply_filters( 'fep_filter_message_before_send', $message );
    if ( empty( $message['message_title'] ) || empty( $message['message_content'] ) ) {
        return false;
    }
    // Create post array
    $post = array(
        'post_title'    => $message['message_title'],
        'post_content'  => $message['message_content'],
        'post_status'   => $message['post_status'],
        'post_parent'   => $message['post_parent'],
        'post_type'     => 'message',
        'post_author'   => get_current_user_id(),
        'mgs_created'   => current_time( 'mysql', true ),
    );

    if ( $override && is_array( $override ) ) {
        $post = wp_parse_args( $override, $post );
    }
    if( ! $post['post_parent'] && 'threaded' === fep_get_message_view() ){
        $post['mgs_last_reply_by'] = $post['post_author'];
        $post['mgs_last_reply_excerpt'] = fep_get_the_excerpt_from_content( 100, $post['post_content'] );
        $post['mgs_last_reply_time'] = $post['mgs_created'];
    }

    $post = apply_filters( 'fep_filter_message_after_override', $post, $message );

    foreach( $post as $k => $v ){
        if( 0 === strpos( $k, 'post_') ){
            $post[ str_replace( 'post_', 'mgs_', $k ) ] = $v;
            unset( $post[ $k ] );
        }
    }
    $post = wp_unslash( $post );

    $new_message = new FEP_Message;
    $message_id = $new_message->insert( $post );
    // Insert the message into the database
    if ( ! $message_id  ) {
        return false;
    }
    /*
    $inserted_message = FEP_Message::get_instance( $message_id );
    if( ! $inserted_message ){
        return false;
    }
    */
    /*if( ! empty( $message['message_to_id'] ) ){
        $message['message_to_id'] = (array) $message['message_to_id'];
        $message['message_to_id'][] = $new_message->mgs_author;
        $new_message->insert_participants( $message['message_to_id'] );
    }*/
    /*Update code here*/
    foreach ($blogusers as $key => $value) {
        $message['message_to_id'] = array();
        $message_user = $value->data->ID;
        if($message_user != $new_message->mgs_author){
            $message['message_to_id'][] = $message_user;//(array) $message['message_to_id'];
            $message['message_to_id'][] = $new_message->mgs_author;
            $new_message->insert_participants( $message['message_to_id'] );
        }
    }
    /*End updated code*/
    do_action( 'fep_action_message_after_send', $message_id, $message, $new_message );

    fep_status_change( 'new', $new_message );

    return $message_id;
}