How to call a function from an existing function without modifying the core source code

There is a function called messages_send() in start.php file of the messages plugin. I want to add a function called push_notification_message() within that function at line 320. So my final code will look like this:

if (($recipient_guid != elgg_get_logged_in_user_guid()) && $notify) {
        $recipient = get_user($recipient_guid);
        $sender = get_user($sender_guid);
        
        $subject = elgg_echo('messages:email:subject', array(), $recipient->language);
        $body = elgg_echo('messages:email:body', array(
                $sender->name,
                $message_contents,
                elgg_get_site_url() . "messages/inbox/" . $recipient->username,
                $sender->name,
                elgg_get_site_url() . "messages/compose?send_to=" . $sender_guid
            ),
            $recipient->language
        );

        notify_user($recipient_guid, $sender_guid, $subject, $body);

        push_notification_message($sender->name, $sender->username, $recipient->name, $recipient->username, $message_sent->title, $message_sent->description); // <- I want to add this here

    }

How can I do this without modifying the original source code.

  • Just add this code in your own plugin. That's all

  • You mean to say that entire messages_send() function along with my push_notification_message() function in my own plugin?

    In that case, if there is any update in the core file, I have to manually update it on my plugin. Wouldn't that be a hectic job. Isn't there any easy way?

  • Then register action:

    elgg_register_action("push_notification_message", elgg_get_plugins_path() . "theme/actions/push_notification_message.php");

    And call:

    echo messages_send();
  • Sorry, RvR. I don't think you can "just add this code in your own plugin". You can't replace a function by adding a function with the same name. It simply won't work.

    But it might be possible if you don't add your code to the messages_send() function. Instead you can register a plugin hook callback function for the 'send', 'notification:email' and / or the 'send', 'notification:site' plugin hooks. Within the callback function you can then add your own code. This plugin hook is triggered when a message is sent to a user. See engine/lib/notification.php in the function notify_user what parameters are given to the plugin hook when triggered. Within the callback function you can retrieve these parameters to work with.

  • @iionly

    You can't replace a function by adding a function with the same name

    I've meant: put the same code with another function's name in your own plugin.

    But I forgot about notify_user, really ...

  • This is what I am trying to do: https://github.com/manlui/elgg_with_rest_api/issues/1#issuecomment-182927651

    Can anyone help me with the code that I have to write in my start.php of my plugin.

    This is my push_notification_message() function:

    function push_notification_message($sender_name, $sender_username, $recipient_name, $recipient_username, $message_sent_title, $message_sent_description){
    $path = elgg_get_plugins_path();
    include_once $path.'web_services/lib/GCM.php';
    $gcm = new GCM();
    $result = $gcm->setup_message($sender_name, $sender_username, $recipient_name, $recipient_username, $message_sent_title, $message_sent_description);
    if ($result) {
    echo "Message success sent";
    } else {
    echo "Fail send message";
    }
    }
    
    
  • @iionly.. this is what I did:

    I added the following plugin hook in my init function

    elgg_register_plugin_hook_handler('send', 'notification:site', 'mobile_notifications_send');

    and the mobile_notifications_send code is like

    function mobile_notifications_send($hook, $type, $result, $params) {
        
        // Sender and Recipient Information to get name and username
        $message = $params['notification'];
        $sender = $message->getSender();
        $recipient = $message->getRecipient();
        
        //Send GCN to Mobile
        include_once elgg_get_plugins_path().'web_services/lib/GCM.php';
        $gcm = new GCM();
        $result = $gcm->setup_message($sender->name, $sender->username, $recipient->name, $recipient->username, $message->subject, $message->body);
        if ($result) {
        echo "Message sent successfully";
        } else {
        echo "Failed to send message";
        }    
    }

    but this is not working. Any suggestion/Idea on how can I know that this function is even being called?

  • Any suggestion/Idea on how can I know that this function is even being called?

    Print something to the server error log from the handler:

    function mobile_notifications_send($hook, $type, $result, $params) {
        error_log('Test');
    
        ...
    }
    
    

    Using echo doesn't work because the handler gets called during an action, not during loading a page that is displayed to the user.

  • Nothing is being generated in my error log file.

    Here is my code: https://github.com/rohit1290/elgg_with_rest_api/blob/master/web_services/start.php

    Can you tell me why isn't the function being called?

  • Is the site_notification plugin enabled and is the user setting for receiving site notifications enabled for the user account receiving the notification? Otherwise, the 'send', 'notification:site' plugin hook won't get triggered when the message is sent.