php - Wordpress - 在发布帖子时发送两条消息,一条给帖子作者,一条给所有用户
问题描述
最后我做到了,我正在使用“前端 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;
}
解决方案
请检查前端 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;
}
推荐阅读
- ios - App Store Connect API/fastlane:审核开始后如何“开发者拒绝”提交
- ios - 面向儿童的 Google Ads 类别 Ios
- python - 在新目录中下载文件 - 套接字编程
- reactjs - 如何约束基于 JSX 的函数的返回类型?
- function - Kotlin - 如何仅在接收器存在时才使用接收器调用可为空的方法?
- python - 在工作人员上存储对象并执行方法
- python - 我怎样才能得到这些点的坐标?
- reactjs - react-table - 如何为数据中的嵌套值实现 useGlobalFilter?
- javascript - 嗨,我怎样才能让 OPTION 在里面有一个值?并使其更改消息为选择另一个选项?
- python-3.x - 带有反应的翻译在 python 上不起作用