Ecommerce Shopify WordPress Discussion

Issues hooking into action for WordPress plugin updates

I am trying to put together a WordPress plugin that will send a webhook when a plugin is updated. I am hooking into upgrader_process_complete, and also tried upgrader_post_install but nothing seems to be working. There is no errors showing in the logs either. Here is the plugin code: // Webhook URL define('WEBHOOK_URL', 'https://example.com'); // Hook into the plugin update process add_filter('upgrader_post_install', 'webhook_on_plugin_update', 10, 3); function webhook_on_plugin_update($response, $hook_extra, $result) { // Check if the update process is for a plugin if ($hook_extra['type'] === 'plugin' && $hook_extra['action'] === 'update') { // Get the updated plugin information $plugin_info = $hook_extra['plugins'][0]; // Prepare data for the webhook payload $payload = array( 'event' => 'plugin_updated', 'plugin_name' => $plugin_info['Name'], 'plugin_version' => $plugin_info['Version'], 'timestamp' => current_time('timestamp'), ); // Send the webhook send_webhook_request($payload); } return $response; } function send_webhook_request($payload) { // Use wp_remote_post to send the webhook request wp_remote_post(WEBHOOK_URL, array( 'body' => json_encode($payload), 'headers' => array('Content-Type' => 'application/json'), )); } Does anyone have any insights into hooking into the upgrader_post_install or upgrader_process_complete actions that would be helpful. EDITED to add code with action define('WEBHOOK_URL', 'https://example.com'); add_action('upgrader_process_complete', 'webhook_on_plugin_update', 150); function webhook_on_plugin_update($upgrader_object, $options) { if ($options['type'] === 'plugin' && $options['action'] === 'update') { $plugin_info = get_plugin_data(WP_PLUGIN_DIR . '/' . $options['plugins'][0]); $payload = array( 'event' => 'plugin_updated', 'plugin_name' => $plugin_info['Name'], 'plugin_version' => $plugin_info['Version'], 'timestamp' => current_time('timestamp'), ); send_webhook_request($payload); } } function send_webhook_request($payload) { wp_remote_post(WEBHOOK_URL, array( 'body' => json_encode($payload), 'headers' => array('Content-Type' => 'application/json'), )); }
So, after taking a look at some other plugins and how they did it, I took a lead from the Activity Log plugin and after a few attempts I came up with this, which is working. class Webhook_On_Update_Plugin { // Define the webhook URL where the update information will be sent const WEBHOOK_URL = 'https://example.com'; public function __construct() { add_action('upgrader_process_complete', array($this, 'webhook_on_plugin_update'), 10, 2); } public function webhook_on_plugin_update($upgrader_object, $options) { error_log('webhook_on_plugin_update fired'); // Check if the update process is for a plugin if ($options['type'] === 'plugin' && $options['action'] === 'update') { // Get the updated plugin information $plugin_info = get_plugin_data(WP_PLUGIN_DIR . '/' . $options['plugins'][0]); // Prepare data for the webhook payload $payload = array( 'event' => 'plugin_updated', 'plugin_name' => $plugin_info['Name'], 'plugin_version' => $plugin_info['Version'], 'timestamp' => current_time('timestamp'), ); // Send the webhook $this->send_webhook_request($payload); error_log('send_webhook_request fired'); } } public function send_webhook_request($payload) { // Use wp_remote_post to send the webhook request wp_remote_post(self::WEBHOOK_URL, array( 'body' => json_encode($payload), 'headers' => array('Content-Type' => 'application/json'), )); } } // Instantiate the class new Webhook_On_Update_Plugin(); This is was was received by the webhook { "event": "plugin_updated", "plugin_name": "Child Theme Configurator", "plugin_version": "2.6.5", "timestamp": 1705358283 } and this is what showed in the logs [15-Jan-2024 22:38:03 UTC] webhook_on_plugin_update fired [15-Jan-2024 22:38:03 UTC] send_webhook_request fired This is the code from Activity Log which I looked at to try and figure it out. public function hooks_plugin_install_or_update( $upgrader, $extra ) { if ( ! isset( $extra['type'] ) || 'plugin' !== $extra['type'] ) return; if ( 'install' === $extra['action'] ) { $path = $upgrader->plugin_info(); if ( ! $path ) return; $data = get_plugin_data( $upgrader->skin->result['local_destination'] . '/' . $path, true, false ); aal_insert_log( array( 'action' => 'installed', 'object_type' => 'Plugins', 'object_name' => $data['Name'], 'object_subtype' => $data['Version'], ) ); } if ( 'update' === $extra['action'] ) { if ( isset( $extra['bulk'] ) && true == $extra['bulk'] ) { $slugs = $extra['plugins']; } else { $plugin_slug = isset( $upgrader->skin->plugin ) ? $upgrader->skin->plugin : $extra['plugin']; if ( empty( $plugin_slug ) ) { return; } $slugs = array( $plugin_slug ); } foreach ( $slugs as $slug ) { $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug, true, false ); aal_insert_log( array( 'action' => 'updated', 'object_type' => 'Plugins', 'object_name' => $data['Name'], 'object_subtype' => $data['Version'], ) ); } } } public function __construct() { add_action( 'upgrader_process_complete', array( $this, 'hooks_plugin_install_or_update' ), 10, 2 ); parent::__construct(); } Having looked back through the logs to see what errors my old code was spitting out, I found this which was telling me there was an issue with the number of arguments being passed to the webhook_on_plugin_update function. It would seem only one argument was being passed to the upgrader_process_complete action. [15-Jan-2024 22:32:17 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function webhook_on_plugin_update(), 1 passed in /home/******/public_html/wp-includes/class-wp-hook.php on line 326 and exactly 3 expected in /home/******/public_html/wp-content/plugins/wp-total-plugin-update-notify/wp-total-plugin-update-notify.php:15 Thank you to those who replied, I do appreciate the time you took out of your day to reply to my query.

January 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.