Finally i did it, I'm using "Front-End PM" plugin, So far I created this function to send message for the post Author when he/she post is published.
But how can i create two messages one for the post author and one for all users, for notice all of them about the new published post.
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 );
}
Here is the fep_send_message function, I want to make the two message in one function..this can happen?
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;
}
Please check below code for Front End PM plugin. I updated some code in "send_message_to_contributor" as "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;
}