Last active
February 9, 2017 19:19
-
-
Save mattallan/28c15b512f124c8c6127f438b3732a6f to your computer and use it in GitHub Desktop.
Immediately send `subscription.updated` webhooks for pending subscriptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WCS immediately send subscription.updated webhook | |
* Plugin URI: | |
* Description: Force immediate webhook delivery for subscription.updated webhooks on pending subscriptions | |
* Version: 1.0 | |
* Author: Matt Allan - Prospress Inc. | |
* Author URI: http://prospress.com | |
*/ | |
/** | |
* Force immediate webhook delivery if a pending subscription is updated | |
* | |
* @param bool $scheduled_delivery | |
* @param WC_Webhook $webhook | |
* @param mixed $subscription | |
* @return bool | |
*/ | |
function wcs_maybe_force_webhook_delivery( $schedule_delivery, $webhook, $subscription = null ) { | |
if ( $schedule_delivery && ! empty( $subscription ) && wcs_is_subscription( $subscription ) ) { | |
if ( ! is_object( $webhook ) ) { | |
$webhook = new WC_Webhook( $webhook ); | |
} | |
$topic = $webhook->get_topic(); | |
if ( ! is_object( $subscription ) ) { | |
$subscription = wcs_get_subscription( $subscription ); | |
} | |
if ( $subscription->has_status( 'pending' ) && 'subscription.updated' == $topic ) { | |
$schedule_delivery = false; // send webhook immediately | |
} | |
} | |
return $schedule_delivery; | |
} | |
add_filter( 'woocommerce_webhook_deliver_async', 'wcs_maybe_force_webhook_delivery', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment