我正在尝试组装一个 WordPress 插件,该插件会在插件更新时发送 webhook。
我正在连接upgrader_process_complete,也尝试了upgrader_post_install,但似乎没有任何效果。日志中也没有显示任何错误。
这是插件代码:
// 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'),
));
}
是否有人对挂钩upgrader_post_install或upgrader_process_complete操作有任何见解,这会有所帮助。
编辑添加带有操作的代码
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'),
));
}
因此,在查看了其他一些插件以及它们是如何做到的之后,我从活动日志插件中获得了指导,经过几次尝试后,我想出了这个,它正在工作。
class Webhook_On_Update_Plugin {
// Define the webhook URL where the update information will be sent
const WEBHOOK_URL = 'https://webhook.site/76fc8563-c2a7-485f-90b5-a908dd3f7c93';
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();
这是由 webhook 收到的
{
"event": "plugin_updated",
"plugin_name": "Child Theme Configurator",
"plugin_version": "2.6.5",
"timestamp": 1705358283
}
这就是日志中显示的内容
[15-Jan-2024 22:38:03 UTC] webhook_on_plugin_update fired
[15-Jan-2024 22:38:03 UTC] send_webhook_request fired
这是活动日志中的代码,我查看了它并尝试找出答案。
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();
}
回顾日志以查看我的旧代码吐出了哪些错误,我发现这告诉我传递给 webhook_on_plugin_update 函数的参数数量存在问题。似乎只有一个参数被传递给 Upgrader_process_complete 操作。
[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
感谢那些回复我的人,我非常感谢您抽出宝贵的时间来回复我的问题。