Skip to:
Content
Pages
Categories
Search
Top
Bottom

Buddypress custom notification block does not display notifications themselves


  • Koka boka
    Participant

    @koka777

    I’m trying to create a block with notifications that should be displayed using a shortcode, but for some reason I get one message (Error: Unable to retrieve notification data.) in the place of the notifications themselves. Tell me where did I mess up?
    The code itself:

    <?php
    /*
    Plugin Name: BuddyPress Real-Time Notifications
    Description: 
    Version: 1.0
    Author: 
    */
    
    // JavaScript
    function bprtn_enqueue_scripts() {
        wp_enqueue_script('bprtn-scripts', plugin_dir_url(__FILE__) . 'scripts.js', array('jquery'), null, true);
    }
    
    add_action('wp_enqueue_scripts', 'bprtn_enqueue_scripts');
    
    // CSS
    function bprtn_enqueue_styles() {
        wp_enqueue_style('bprtn-styles', plugin_dir_url(__FILE__) . 'styles.css');
    }
    
    add_action('wp_enqueue_scripts', 'bprtn_enqueue_styles');
    
    // function
    function bprtn_display_notification($notification) {
        if (isset($notification->component_name) && isset($notification->component_action) && isset($notification->item_id)) {
            echo '<div class="bprtn-notification">';
            echo '<p class="bprtn-description">' . bp_the_notification_description($notification->component_action, $notification->item_id) . '</p>';
            echo '<p class="bprtn-date">' . bp_the_notification_time_since($notification->date_notified) . '</p>';
            echo '<div class="bprtn-actions">';
            // buttons
            bp_the_notification_action_links(array('before' => '<div class="bprtn-action">', 'after' => '</div>', 'notification' => $notification));
            echo '</div>';
            echo '</div>';
        } else {
            echo '<p class="bprtn-notification-error">Error: Unable to retrieve notification data.</p>';
        }
    }
    
    // function load notifications
    function bprtn_load_notifications() {
        $user_id = bp_loggedin_user_id();
        $notifications = bp_notifications_get_notifications_for_user($user_id);
    
        if (!empty($notifications)) {
            foreach ($notifications as $notification) {
                bprtn_display_notification($notification);
            }
        } else {
            echo '<p class="bprtn-no-notifications">No new notifications.</p>';
        }
    }
    
    // shortcode
    function bprtn_notifications_shortcode() {
        ob_start();
        bprtn_load_notifications();
        return ob_get_clean();
    }
    
    add_shortcode('bprtn_notifications', 'bprtn_notifications_shortcode');
    
    // AJAX
    function bprtn_mark_as_read() {
        if (isset($_POST['notification_id'])) {
            $notification_id = $_POST['notification_id'];
            bp_notifications_mark_notifications_by_id(array($notification_id), 'read');
        }
        wp_die();
    }
    
    add_action('wp_ajax_bprtn_mark_as_read', 'bprtn_mark_as_read');
    ?>
  • You must be logged in to reply to this topic.
Skip to toolbar