概要
woocommerce_max_webhook_delivery_failures
フィルタは、WooCommerceでのWebhook機能に関連し、Webhookの最大配信失敗回数を制御するために使用されます。このフィルタを使用することで、開発者はWebhookの配信失敗回数に基づいて動作を調整することができます。主に以下のような機能を実装する際に利用されます。
- Webhookの再試行ロジックのカスタマイズ
- Webhookの配信失敗に関する通知の制御
- 特定の条件下でのWebhookのキャンセル
- Webhookが失敗した際のバックアップ処理の実行
- プロダクション環境でのWebhookの解析とデバッグ
- Webhookの配信結果をログに保存するカスタム機能
構文
add_filter( 'woocommerce_max_webhook_delivery_failures', 'custom_max_webhook_failures' );
パラメータ
int $max_failures
: Webhookの最大配信失敗回数。デフォルトは3
。
戻り値
int
: 設定された最大配信失敗回数。
使用可能なプラグインバージョン
- WooCommerce バージョン: 3.5.0 以降
- WordPress バージョン: 4.0 以降
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプル1: 最大配信失敗回数を5に設定
add_filter( 'woocommerce_max_webhook_delivery_failures', function( $max_failures ) {
return 5;
});
このコードは、Webhookの最大配信失敗回数を5回に設定します。その結果、Webhookが5回失敗するまで再試行が行われます。
サンプル2: 条件に応じて配信失敗回数を変更
add_filter( 'woocommerce_max_webhook_delivery_failures', function( $max_failures ) {
if ( is_user_logged_in() ) {
return 10; // ログインユーザーの場合、最大失敗回数を10に
}
return $max_failures; // それ以外は元の値を維持
});
このコードは、ユーザーがログインしている場合、最大配信失敗回数を10に変更します。
サンプル3: 環境によって失敗回数を調整
add_filter( 'woocommerce_max_webhook_delivery_failures', function( $max_failures ) {
if ( defined( 'WP_ENV' ) && WP_ENV === 'production' ) {
return 3; // プロダクション環境では3回に制限
}
return 5; // 開発環境では5回まで
});
このコードは、プロダクション環境の場合に最大配信失敗回数を3に制限し、開発環境では5に設定します。
サンプル4: Webhook失敗時のログ記録
add_filter( 'woocommerce_max_webhook_delivery_failures', function( $max_failures ) {
if ( $max_failures > 3 ) {
error_log( 'Webhook delivery failures exceeded max threshold.' );
}
return $max_failures;
});
このコードは、最大配信失敗回数が3を超えた場合にエラーログにメッセージを記録します。
サンプル5: 特定のWebhookエンドポイントに対する制限
add_filter( 'woocommerce_max_webhook_delivery_failures', function( $max_failures, $webhook ) {
if ( $webhook->name === 'specific_webhook' ) {
return 2; // 'specific_webhook'については2回に制限
}
return $max_failures;
}, 10, 2);
このコードは、特定のWebhook(ここでは ‘specific_webhook’)に対してのみ最大配信失敗回数を2回に制限します。