Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix membership auto-enroll webhooks #329

Merged
30 changes: 1 addition & 29 deletions includes/models/class-llms-rest-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@
*/
class LLMS_REST_Webhook extends LLMS_REST_Webhook_Data {

/**
* Store which object IDs this webhook has processed (ie scheduled to be delivered)
* within the current page request.
*
* @var array
*/
protected $processed = array();

/**
* Delivers the webhook
*
Expand Down Expand Up @@ -187,20 +179,6 @@ public function enqueue() {

}

/**
* Checks if the specified resource has already been queued for delivery within the current request
*
* Helps avoid duplication of data being sent for topics that have more than one hook defined.
*
* @since [version] Verify the processed flag with all arguments of `$args`.
*
* @param array $args Numeric array of arguments from the originating hook.
* @return bool
*/
protected function is_already_processed( $args ) {
return false !== array_search( $args, $this->processed, true );
}

/**
* Determine if the current action is valid for the webhook
*
Expand Down Expand Up @@ -362,10 +340,6 @@ public function process_hook( ...$args ) {
return false;
}

// Mark this hook's first argument as processed to ensure it doesn't get processed again within the current request,
// as it might happen with webhooks with multiple hookes defined in `LLMS_REST_Webhooks::get_hooks()`.
$this->processed[] = $args;
ideadude marked this conversation as resolved.
Show resolved Hide resolved

/**
* Disable background processing of webhooks by returning a falsy
*
Expand Down Expand Up @@ -425,7 +399,6 @@ public function ping() {
* Determines if an originating action qualifies for webhook delivery
*
* @since 1.0.0-beta.1
* @since [verison] Drop checking whether the webhook is pending in favor of a check on if is already processed within the current request.
*
* @param array $args Numeric array of arguments from the originating hook.
* @return bool
Expand All @@ -434,8 +407,7 @@ protected function should_deliver( $args ) {

$deliver = ( 'active' === $this->get( 'status' ) ) // Must be active.
&& $this->is_valid_action( $args ) // Valid action.
&& $this->is_valid_resource( $args ) // Valid resource.
&& ! $this->is_already_processed( $args ); // Not already processed.
ideadude marked this conversation as resolved.
Show resolved Hide resolved
&& $this->is_valid_resource( $args ); // Valid resource.

/**
* Skip or hijack webhook delivery scheduling
Expand Down
28 changes: 0 additions & 28 deletions tests/unit-tests/class-llms-rest-test-webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,32 +966,4 @@ public function test_should_deliver_status() {

}

/**
* Test should_deliver() method with already processed hooks().
*
* @since 1.0.0-beta.17
*
* @return void
*/
public function test_should_deliver_already_processed() {

$webhook = LLMS_REST_API()->webhooks()->create( array(
'delivery_url' => 'https://mock.tld',
'topic' => 'student.created',
'status' => 'active',
) );

$student_id = $this->factory->student->create();

// Not processed.
$this->assertTrue( LLMS_Unit_Test_Util::call_method( $webhook, 'should_deliver', array( array( $student_id ) ) ) );

// Process the hook.
$webhook->process_hook( $student_id );

// Processed.
$this->assertFalse( LLMS_Unit_Test_Util::call_method( $webhook, 'should_deliver', array( array( $student_id ) ) ) );

}

}