Почему-то нет приходят кастомные уведомлений Buddypress из самописного плагина
Я создала небольшой плагин для wordpress+Buddypress, в котором пользователи могут подписываться на авторов, но по какой-то причине пользователи не уведомляются, когда автор публикует новый пост. Пожалуйста, помогите решить эту проблему. Проверила логи, там видно, что уведомление отправлено, но самого уведомления нет!
<?php
/*
Plugin Name: Author Subscription and Notification
Description: Allows users to subscribe to authors and receive notifications when authors publish new posts.
Version: 1.3
Author: test
*/
// Hook to create the database table upon plugin activation
register_activation_hook(__FILE__, 'create_subscriptions_table');
function create_subscriptions_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'author_subscriptions';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
author_id bigint(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user_author (user_id, author_id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Hook to add subscription button in the post content
add_filter('the_content', 'add_author_subscription_button_to_post');
function add_author_subscription_button_to_post($content) {
if (is_single() && is_user_logged_in()) {
$author_id = get_the_author_meta('ID');
$current_user_id = get_current_user_id();
global $wpdb;
$table_name = $wpdb->prefix . 'author_subscriptions';
// Check if user is already subscribed
$is_subscribed = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE user_id = %d AND author_id = %d",
$current_user_id, $author_id
));
$button_text = $is_subscribed ? 'Unsubscribe' : 'Subscribe';
$button_html = '<button id="subscribe-author" class="subscribe-button" data-author-id="' . esc_attr($author_id) . '">' . esc_html($button_text) . '</button>';
$button_html .= '<div id="subscription-message"></div>';
$content .= $button_html;
}
return $content;
}
// Enqueue the JavaScript file and localize script
add_action('wp_enqueue_scripts', 'enqueue_subscription_script');
function enqueue_subscription_script() {
if (is_single() && is_user_logged_in()) {
wp_enqueue_script('subscription-script', plugin_dir_url(__FILE__) . 'subscription.js', array('jquery'), '1.0', true);
wp_localize_script('subscription-script', 'subscriptionData', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('subscription_nonce')
));
}
}
// Handle the AJAX request for subscription
add_action('wp_ajax_handle_author_subscription', 'handle_author_subscription');
function handle_author_subscription() {
check_ajax_referer('subscription_nonce', 'nonce');
if (is_user_logged_in()) {
global $wpdb;
$table_name = $wpdb->prefix . 'author_subscriptions';
$current_user_id = get_current_user_id();
$author_id = intval($_POST['author_id']);
$operation = sanitize_text_field($_POST['operation']);
if ($operation === 'subscribe') {
$wpdb->insert($table_name, array(
'user_id' => $current_user_id,
'author_id' => $author_id
));
wp_send_json_success('Subscribed successfully.');
error_log("User ID $current_user_id subscribed to author ID $author_id");
} else if ($operation === 'unsubscribe') {
$wpdb->delete($table_name, array(
'user_id' => $current_user_id,
'author_id' => $author_id
));
wp_send_json_success('Unsubscribed successfully.');
error_log("User ID $current_user_id unsubscribed from author ID $author_id");
}
}
wp_send_json_error('Failed to update subscription.');
}
// Hook to send notifications when an author publishes a new post
add_action('publish_post', 'notify_subscribers_on_new_post');
function notify_subscribers_on_new_post($post_id) {
global $wpdb;
$table_name = $wpdb->prefix . 'author_subscriptions';
$post = get_post($post_id);
$author_id = $post->post_author;
error_log("New post published by author ID: " . $author_id);
// Get subscribers
$subscribers = $wpdb->get_results($wpdb->prepare(
"SELECT user_id FROM $table_name WHERE author_id = %d",
$author_id
));
error_log("Subscribers found: " . count($subscribers));
foreach ($subscribers as $subscriber) {
// Send BuddyPress notification
bp_notifications_add_notification(array(
'user_id' => $subscriber->user_id,
'item_id' => $post_id,
'secondary_item_id' => $author_id,
'component_name' => 'buddypress',
'component_action' => 'new_post_by_subscribed_author',
'date_notified' => bp_core_current_time(),
'is_new' => true,
));
error_log("Notification sent to user ID: " . $subscriber->user_id);
}
}
// Custom notification format
add_filter('bp_notifications_get_notifications_for_user', 'custom_bp_notification_format', 10, 5);
function custom_bp_notification_format($content, $item_id, $secondary_item_id, $action, $component_name) {
if ($component_name === 'buddypress' && $action === 'new_post_by_subscribed_author') {
$author_name = get_the_author_meta('display_name', $secondary_item_id);
$post_title = get_the_title($item_id);
$post_url = get_permalink($item_id);
$content = sprintf(__('New post by %s: <a href="%s">%s</a>', 'text-domain'), $author_name, $post_url, $post_title);
}
return $content;
}
?>
Вот сам лог!
[23-May-2024 09:42:15 UTC] a:0:{}
[23-May-2024 09:42:40 UTC] New post published by author ID: 1
[23-May-2024 09:42:40 UTC] Subscribers found: 2
[23-May-2024 09:42:40 UTC] Notification sent to user ID: 1
[23-May-2024 09:42:40 UTC] Notification sent to user ID: 27
[23-May-2024 09:42:43 UTC] a:0:{}